|
| 1 | +// This file is part of libraries-repository-engine. |
| 2 | +// |
| 3 | +// Copyright 2021 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as published |
| 7 | +// by the Free Software Foundation, either version 3 of the License, or |
| 8 | +// (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | +// You can be released from the requirements of the above licenses by purchasing |
| 19 | +// a commercial license. Buying such a license is mandatory if you want to |
| 20 | +// modify or otherwise use the software for commercial activities involving the |
| 21 | +// Arduino software without disclosing the source code of your own applications. |
| 22 | +// To purchase a commercial license, send an email to [email protected]. |
| 23 | + |
| 24 | +package gitutils |
| 25 | + |
| 26 | +import ( |
| 27 | + "sort" |
| 28 | + |
| 29 | + "github.com/go-git/go-git/v5" |
| 30 | + "github.com/go-git/go-git/v5/plumbing" |
| 31 | + "github.com/go-git/go-git/v5/plumbing/object" |
| 32 | +) |
| 33 | + |
| 34 | +// ResolveTag returns the commit hash associated with a tag. |
| 35 | +func ResolveTag(tag *plumbing.Reference, repository *git.Repository) (*plumbing.Hash, error) { |
| 36 | + // Annotated tags have their own hash, different from the commit hash, so the tag must be resolved to get the has for |
| 37 | + // the associated commit. |
| 38 | + // Tags may point to any Git object. Although not common, this can include tree and blob objects in addition to commits. |
| 39 | + // Resolving non-commit objects results in an error. |
| 40 | + return repository.ResolveRevision(plumbing.Revision(tag.Hash().String())) |
| 41 | +} |
| 42 | + |
| 43 | +// SortedCommitTags returns the repository's commit object tags sorted by their chronological order in the current branch's history. |
| 44 | +// Tags for commits not in the branch's history are returned in lexicographical order relative to their adjacent tags. |
| 45 | +func SortedCommitTags(repository *git.Repository) ([]*plumbing.Reference, error) { |
| 46 | + /* |
| 47 | + Given a repository tag structure like so (I've omitted 1.0.3-1.0.9 as irrelevant): |
| 48 | +
|
| 49 | + * HEAD -> main, tag: 1.0.11 |
| 50 | + * tag: 1.0.10 |
| 51 | + * tag: 1.0.2 |
| 52 | + | * tag: 1.0.2-rc2, development-branch |
| 53 | + | * tag: 1.0.2-rc1 |
| 54 | + |/ |
| 55 | + * tag: 1.0.1 |
| 56 | + * tag: 1.0.0 |
| 57 | +
|
| 58 | + The raw tags order is lexicographical: |
| 59 | + 1.0.0 |
| 60 | + 1.0.1 |
| 61 | + 1.0.10 |
| 62 | + 1.0.2 |
| 63 | + 1.0.2-rc1 |
| 64 | + 1.0.2-rc2 |
| 65 | +
|
| 66 | + This order is not meaningful. More meaningful would be to order the tags according to the chronology of the |
| 67 | + branch's commit history: |
| 68 | + 1.0.0 |
| 69 | + 1.0.1 |
| 70 | + 1.0.2 |
| 71 | + 1.0.10 |
| 72 | +
|
| 73 | + This leaves the question of how to handle tags from other branches, which is likely why a sensible sorting |
| 74 | + capability was not provided. However, even if the sorting of those tags is not optimal, a meaningful sort of the |
| 75 | + current branch's tags will be a significant improvement over the default behavior. |
| 76 | + */ |
| 77 | + |
| 78 | + headRef, err := repository.Head() |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + |
| 83 | + headCommit, err := repository.CommitObject(headRef.Hash()) |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + commits := object.NewCommitIterCTime(headCommit, nil, nil) // Iterator for the head commit and parents in reverse chronological commit time order. |
| 89 | + commitMap := make(map[plumbing.Hash]int) // commitMap associates each hash with its chronological position in the branch history. |
| 90 | + var commitIndex int |
| 91 | + for { // Iterate over all commits. |
| 92 | + commit, err := commits.Next() |
| 93 | + if err != nil { |
| 94 | + // Reached end of commits |
| 95 | + break |
| 96 | + } |
| 97 | + commitMap[commit.Hash] = commitIndex |
| 98 | + |
| 99 | + commitIndex-- // Decrement to reflect reverse chronological order. |
| 100 | + } |
| 101 | + |
| 102 | + tags, err := repository.Tags() // Get an iterator of the refs of the repository's tags. These are returned in a useless lexicographical order (e.g, 1.0.10 < 1.0.2), so it's necessary to cross-reference them against the commits, which are in a meaningful order. |
| 103 | + |
| 104 | + type tagDataType struct { |
| 105 | + tag *plumbing.Reference |
| 106 | + position int |
| 107 | + } |
| 108 | + var tagData []tagDataType |
| 109 | + associatedCommitIndex := commitIndex // Initialize to index of oldest commit in case the first tags aren't in the branch. |
| 110 | + var tagIndex int |
| 111 | + for { // Iterate over all tag refs. |
| 112 | + tag, err := tags.Next() |
| 113 | + if err != nil { |
| 114 | + // Reached end of tags |
| 115 | + break |
| 116 | + } |
| 117 | + |
| 118 | + // Annotated tags have their own hash, different from the commit hash, so tags must be resolved before |
| 119 | + // cross-referencing against the commit hashes. |
| 120 | + resolvedTag, err := ResolveTag(tag, repository) |
| 121 | + if err != nil { |
| 122 | + // Non-commit object tags are not included in the sorted list. |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + commitIndex, ok := commitMap[*resolvedTag] |
| 127 | + if ok { |
| 128 | + // There is a commit in the branch associated with the tag. |
| 129 | + associatedCommitIndex = commitIndex |
| 130 | + } |
| 131 | + |
| 132 | + tagData = append( |
| 133 | + tagData, |
| 134 | + tagDataType{ |
| 135 | + tag: tag, |
| 136 | + position: associatedCommitIndex*10000 + tagIndex, // Leave intervals between positions to allow the insertion of unassociated tags in the existing lexicographical order relative to the last associated tag. |
| 137 | + }, |
| 138 | + ) |
| 139 | + |
| 140 | + tagIndex++ |
| 141 | + } |
| 142 | + |
| 143 | + // Sort the tags according to the branch's history where possible. |
| 144 | + sort.SliceStable( |
| 145 | + tagData, |
| 146 | + // "less" function |
| 147 | + func(thisIndex, otherIndex int) bool { |
| 148 | + return tagData[thisIndex].position < tagData[otherIndex].position |
| 149 | + }, |
| 150 | + ) |
| 151 | + |
| 152 | + var sortedTags []*plumbing.Reference |
| 153 | + for _, tagDatum := range tagData { |
| 154 | + sortedTags = append(sortedTags, tagDatum.tag) |
| 155 | + } |
| 156 | + |
| 157 | + return sortedTags, nil |
| 158 | +} |
0 commit comments