|
25 | 25 | import org.eclipse.jgit.api.Status; |
26 | 26 | import org.eclipse.jgit.api.errors.GitAPIException; |
27 | 27 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 28 | +import org.eclipse.jgit.lib.Constants; |
28 | 29 | import org.eclipse.jgit.lib.ObjectId; |
29 | 30 | import org.eclipse.jgit.lib.Ref; |
30 | 31 | import org.eclipse.jgit.lib.Repository; |
|
40 | 41 | import com.google.common.base.Predicate; |
41 | 42 | import com.google.common.collect.Collections2; |
42 | 43 | import com.google.common.collect.Lists; |
| 44 | +import pl.project13.maven.git.GitDescribeConfig; |
43 | 45 | import pl.project13.maven.git.log.LoggerBridge; |
| 46 | +import pl.project13.maven.git.util.Pair; |
44 | 47 |
|
45 | 48 | public class JGitCommon { |
46 | 49 |
|
@@ -81,61 +84,71 @@ private Collection<String> getTags(final Git git, final ObjectId headId, final R |
81 | 84 | return tags; |
82 | 85 | } |
83 | 86 |
|
84 | | - public String getClosestTagName(@NotNull Repository repo) { |
85 | | - Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo); |
86 | | - for (Map.Entry<ObjectId, List<DatedRevTag>> entry : map.entrySet()) { |
87 | | - return trimFullTagName(entry.getValue().get(0).tagName); |
88 | | - } |
89 | | - return ""; |
| 87 | + public String getClosestTagName(@NotNull Repository repo, GitDescribeConfig gitDescribe) { |
| 88 | + // TODO: Why does some tests fail when it gets headCommit from JGitprovider? |
| 89 | + RevCommit headCommit = findHeadObjectId(repo); |
| 90 | + Pair<RevCommit, String> pair = getClosestRevCommit(repo, headCommit, gitDescribe); |
| 91 | + return pair.second; |
90 | 92 | } |
91 | 93 |
|
92 | | - public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headCommit) { |
93 | | - HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(repo)); |
94 | | - ObjectId obj = (ObjectId) map.keySet().toArray()[0]; |
95 | | - |
96 | | - try (RevWalk walk = new RevWalk(repo)) { |
97 | | - RevCommit commit = walk.lookupCommit(obj); |
98 | | - walk.dispose(); |
| 94 | + public String getClosestTagCommitCount(@NotNull Repository repo, GitDescribeConfig gitDescribe) { |
| 95 | + // TODO: Why does some tests fail when it gets headCommit from JGitprovider? |
| 96 | + RevCommit headCommit = findHeadObjectId(repo); |
| 97 | + Pair<RevCommit, String> pair = getClosestRevCommit(repo, headCommit, gitDescribe); |
| 98 | + RevCommit revCommit = pair.first; |
| 99 | + int distance = distanceBetween(repo, headCommit, revCommit); |
| 100 | + return String.valueOf(distance); |
| 101 | + } |
99 | 102 |
|
100 | | - int distance = distanceBetween(repo, headCommit, commit); |
101 | | - return String.valueOf(distance); |
| 103 | + private Pair<RevCommit, String> getClosestRevCommit(@NotNull Repository repo, RevCommit headCommit, GitDescribeConfig gitDescribe) { |
| 104 | + boolean includeLightweightTags = false; |
| 105 | + String matchPattern = ".*"; |
| 106 | + if (gitDescribe != null) { |
| 107 | + includeLightweightTags = gitDescribe.getTags(); |
| 108 | + if (!"*".equals(gitDescribe.getMatch())) { |
| 109 | + matchPattern = createMatchPattern(gitDescribe.getMatch()); |
| 110 | + } |
| 111 | + } |
| 112 | + Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, includeLightweightTags, matchPattern); |
| 113 | + if (tagObjectIdToName.containsKey(headCommit)) { |
| 114 | + String tagName = tagObjectIdToName.get(headCommit).iterator().next(); |
| 115 | + return Pair.of(headCommit, tagName); |
102 | 116 | } |
| 117 | + List<RevCommit> commits = findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName); |
| 118 | + RevCommit revCommit = commits.get(0); |
| 119 | + String tagName = tagObjectIdToName.get(revCommit).iterator().next(); |
| 120 | + |
| 121 | + return Pair.of(revCommit, tagName); |
103 | 122 | } |
104 | 123 |
|
105 | | - private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo) { |
106 | | - Map<ObjectId, List<DatedRevTag>> mapWithClosestTagOnly = new HashMap<>(); |
107 | | - String matchPattern = ".*"; |
108 | | - Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, true, matchPattern); |
109 | | - LinkedHashMap<ObjectId, List<DatedRevTag>> sortedCommitIdsToTags = sortByDatedRevTag(commitIdsToTags); |
| 124 | + protected String createMatchPattern(String pattern) { |
| 125 | + return "^refs/tags/\\Q" + |
| 126 | + pattern.replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") + |
| 127 | + "\\E$"; |
| 128 | + } |
110 | 129 |
|
111 | | - for (Map.Entry<ObjectId, List<DatedRevTag>> entry: sortedCommitIdsToTags.entrySet()) { |
112 | | - mapWithClosestTagOnly.put(entry.getKey(), entry.getValue()); |
113 | | - break; |
114 | | - } |
| 130 | + protected Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) { |
| 131 | + Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern); |
| 132 | + Map<ObjectId, List<String>> commitIdsToTagNames = transformRevTagsMapToDateSortedTagNames(commitIdsToTags); |
| 133 | + log.info("Created map: [{}]", commitIdsToTagNames); |
115 | 134 |
|
116 | | - return mapWithClosestTagOnly; |
| 135 | + return commitIdsToTagNames; |
117 | 136 | } |
118 | 137 |
|
119 | | - private LinkedHashMap<ObjectId, List<DatedRevTag>> sortByDatedRevTag(Map<ObjectId, List<DatedRevTag>> map) { |
120 | | - List<Map.Entry<ObjectId, List<DatedRevTag>>> list = new ArrayList<>(map.entrySet()); |
| 138 | + protected RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException { |
| 139 | + try { |
| 140 | + ObjectId headId = repo.resolve(Constants.HEAD); |
121 | 141 |
|
122 | | - Collections.sort(list, new Comparator<Map.Entry<ObjectId, List<DatedRevTag>>>() { |
123 | | - public int compare(Map.Entry<ObjectId, List<DatedRevTag>> m1, Map.Entry<ObjectId, List<DatedRevTag>> m2) { |
124 | | - // we need to sort the DatedRevTags to a commit first, otherwise we may get problems when we have two tags for the same commit |
125 | | - Collections.sort(m1.getValue(), datedRevTagComparator()); |
126 | | - Collections.sort(m2.getValue(), datedRevTagComparator()); |
| 142 | + try (RevWalk walk = new RevWalk(repo)) { |
| 143 | + RevCommit headCommit = walk.lookupCommit(headId); |
| 144 | + walk.dispose(); |
127 | 145 |
|
128 | | - DatedRevTag datedRevTag1 = m1.getValue().get(0); |
129 | | - DatedRevTag datedRevTag2 = m2.getValue().get(0); |
130 | | - return datedRevTagComparator().compare(datedRevTag1,datedRevTag2); |
| 146 | + log.info("HEAD is [{}]", headCommit.getName()); |
| 147 | + return headCommit; |
131 | 148 | } |
132 | | - }); |
133 | | - |
134 | | - LinkedHashMap<ObjectId, List<DatedRevTag>> result = new LinkedHashMap<>(); |
135 | | - for (Map.Entry<ObjectId, List<DatedRevTag>> entry : list) { |
136 | | - result.put(entry.getKey(), entry.getValue()); |
| 149 | + } catch (IOException ex) { |
| 150 | + throw new RuntimeException("Unable to obtain HEAD commit!", ex); |
137 | 151 | } |
138 | | - return result; |
139 | 152 | } |
140 | 153 |
|
141 | 154 | protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) { |
|
0 commit comments