|
17 | 17 |
|
18 | 18 | package pl.project13.jgit; |
19 | 19 |
|
| 20 | +import static com.google.common.collect.Lists.newArrayList; |
| 21 | +import static com.google.common.collect.Maps.newHashMap; |
| 22 | + |
20 | 23 | import java.io.IOException; |
21 | 24 | import java.util.Collection; |
| 25 | +import java.util.Collections; |
| 26 | +import java.util.Comparator; |
| 27 | +import java.util.HashMap; |
22 | 28 | import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.regex.Pattern; |
23 | 31 |
|
24 | 32 | import org.eclipse.jgit.api.Git; |
25 | 33 | import org.eclipse.jgit.api.errors.GitAPIException; |
| 34 | +import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
26 | 35 | import org.eclipse.jgit.lib.ObjectId; |
27 | 36 | import org.eclipse.jgit.lib.Ref; |
28 | 37 | import org.eclipse.jgit.lib.Repository; |
| 38 | +import org.eclipse.jgit.revwalk.RevTag; |
29 | 39 | import org.eclipse.jgit.revwalk.RevWalk; |
| 40 | +import org.jetbrains.annotations.NotNull; |
| 41 | + |
| 42 | +import pl.project13.jgit.dummy.DatedRevTag; |
| 43 | +import pl.project13.maven.git.log.LoggerBridge; |
30 | 44 |
|
| 45 | +import com.google.common.annotations.VisibleForTesting; |
31 | 46 | import com.google.common.base.Function; |
32 | 47 | import com.google.common.base.Predicate; |
| 48 | +import com.google.common.base.Throwables; |
33 | 49 | import com.google.common.collect.Collections2; |
| 50 | +import com.google.common.collect.Lists; |
34 | 51 |
|
35 | 52 | /** |
36 | 53 | * @author <a href="mailto:[email protected]">Konrad 'ktoso' Malawski</a> |
@@ -72,5 +89,108 @@ public Collection<String> getTags(Repository repo, final ObjectId headId) throws |
72 | 89 | } |
73 | 90 | } |
74 | 91 | } |
| 92 | + |
| 93 | + protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull LoggerBridge loggerBridge,@NotNull Repository repo, boolean tagsFlag, String matchPattern){ |
| 94 | + Map<ObjectId, List<DatedRevTag>> commitIdsToTags = newHashMap(); |
| 95 | + |
| 96 | + RevWalk walk = new RevWalk(repo); |
| 97 | + try { |
| 98 | + walk.markStart(walk.parseCommit(repo.resolve("HEAD"))); |
| 99 | + |
| 100 | + List<Ref> tagRefs = Git.wrap(repo).tagList().call(); |
| 101 | + Pattern regex = Pattern.compile(matchPattern); |
| 102 | + loggerBridge.log("Tag refs [", tagRefs, "]"); |
| 103 | + |
| 104 | + for (Ref tagRef : tagRefs) { |
| 105 | + walk.reset(); |
| 106 | + String name = tagRef.getName(); |
| 107 | + if (!regex.matcher(name).matches()) { |
| 108 | + loggerBridge.log("Skipping tagRef with name [", name, "] as it doesn't match [", matchPattern, "]"); |
| 109 | + continue; |
| 110 | + } |
| 111 | + ObjectId resolvedCommitId = repo.resolve(name); |
| 112 | + |
| 113 | + // TODO that's a bit of a hack... |
| 114 | + try { |
| 115 | + final RevTag revTag = walk.parseTag(resolvedCommitId); |
| 116 | + ObjectId taggedCommitId = revTag.getObject().getId(); |
| 117 | + loggerBridge.log("Resolved tag [",revTag.getTagName(),"] [",revTag.getTaggerIdent(),"], points at [",taggedCommitId,"] "); |
| 118 | + |
| 119 | + // sometimes a tag, may point to another tag, so we need to unpack it |
| 120 | + while (isTagId(taggedCommitId)) { |
| 121 | + taggedCommitId = walk.parseTag(taggedCommitId).getObject().getId(); |
| 122 | + } |
| 123 | + |
| 124 | + if (commitIdsToTags.containsKey(taggedCommitId)) { |
| 125 | + commitIdsToTags.get(taggedCommitId).add(new DatedRevTag(revTag)); |
| 126 | + } else { |
| 127 | + commitIdsToTags.put(taggedCommitId, newArrayList(new DatedRevTag(revTag))); |
| 128 | + } |
| 129 | + |
| 130 | + } catch (IncorrectObjectTypeException ex) { |
| 131 | + // it's an lightweight tag! (yeah, really) |
| 132 | + if (tagsFlag) { |
| 133 | + // --tags means "include lightweight tags" |
| 134 | + loggerBridge.log("Including lightweight tag [", name, "]"); |
| 135 | + |
| 136 | + DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name); |
| 137 | + |
| 138 | + if (commitIdsToTags.containsKey(resolvedCommitId)) { |
| 139 | + commitIdsToTags.get(resolvedCommitId).add(datedRevTag); |
| 140 | + } else { |
| 141 | + commitIdsToTags.put(resolvedCommitId, newArrayList(datedRevTag)); |
| 142 | + } |
| 143 | + } |
| 144 | + } catch (Exception ignored) { |
| 145 | + loggerBridge.error("Failed while parsing [",tagRef,"] -- ", Throwables.getStackTraceAsString(ignored)); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) { |
| 150 | + loggerBridge.log("key [",entry.getKey(),"], tags => [",entry.getValue(),"] "); |
| 151 | + } |
| 152 | + return commitIdsToTags; |
| 153 | + } catch (Exception e) { |
| 154 | + loggerBridge.log("Unable to locate tags\n[",Throwables.getStackTraceAsString(e),"]"); |
| 155 | + } finally { |
| 156 | + walk.release(); |
| 157 | + } |
| 158 | + return Collections.emptyMap(); |
| 159 | + } |
| 160 | + |
| 161 | + /** Checks if the given object id resolved to a tag object */ |
| 162 | + private boolean isTagId(ObjectId objectId) { |
| 163 | + return objectId.toString().startsWith("tag "); |
| 164 | + } |
| 165 | + |
| 166 | + protected HashMap<ObjectId, List<String>> transformRevTagsMapToDateSortedTagNames(Map<ObjectId, List<DatedRevTag>> commitIdsToTags) { |
| 167 | + HashMap<ObjectId, List<String>> commitIdsToTagNames = newHashMap(); |
| 168 | + for (Map.Entry<ObjectId, List<DatedRevTag>> objectIdListEntry : commitIdsToTags.entrySet()) { |
| 169 | + List<DatedRevTag> tags = objectIdListEntry.getValue(); |
| 170 | + |
| 171 | + List<DatedRevTag> newTags = newArrayList(tags); |
| 172 | + Collections.sort(newTags, new Comparator<DatedRevTag>() { |
| 173 | + @Override |
| 174 | + public int compare(DatedRevTag revTag, DatedRevTag revTag2) { |
| 175 | + return revTag2.date.compareTo(revTag.date); |
| 176 | + } |
| 177 | + }); |
| 178 | + |
| 179 | + List<String> tagNames = Lists.transform(newTags, new Function<DatedRevTag, String>() { |
| 180 | + @Override |
| 181 | + public String apply(DatedRevTag input) { |
| 182 | + return trimFullTagName(input.tagName); |
| 183 | + } |
| 184 | + }); |
| 185 | + |
| 186 | + commitIdsToTagNames.put(objectIdListEntry.getKey(), tagNames); |
| 187 | + } |
| 188 | + return commitIdsToTagNames; |
| 189 | + } |
| 190 | + |
| 191 | + @VisibleForTesting |
| 192 | + protected String trimFullTagName(@NotNull String tagName) { |
| 193 | + return tagName.replaceFirst("refs/tags/", ""); |
| 194 | + } |
75 | 195 |
|
76 | 196 | } |
0 commit comments