Skip to content

Commit a1a992d

Browse files
author
S L
committed
refactoring: moving some common functionality to JGitCommon
1 parent ea4a1b6 commit a1a992d

File tree

4 files changed

+162
-116
lines changed

4 files changed

+162
-116
lines changed

src/main/java/pl/project13/jgit/DescribeCommand.java

Lines changed: 5 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.common.base.Preconditions;
2424
import com.google.common.base.Throwables;
2525
import com.google.common.collect.Lists;
26+
2627
import org.eclipse.jgit.api.Git;
2728
import org.eclipse.jgit.api.GitCommand;
2829
import org.eclipse.jgit.api.Status;
@@ -37,6 +38,7 @@
3738
import org.eclipse.jgit.revwalk.RevWalk;
3839
import org.jetbrains.annotations.NotNull;
3940
import org.jetbrains.annotations.Nullable;
41+
4042
import pl.project13.jgit.dummy.DatedRevTag;
4143
import pl.project13.maven.git.GitDescribeConfig;
4244
import pl.project13.maven.git.log.LoggerBridge;
@@ -494,108 +496,12 @@ private void seeAllParents(@NotNull RevWalk revWalk, RevCommit child, @NotNull S
494496

495497
// git commit id -> its tag (or tags)
496498
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
497-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = newHashMap();
498-
499-
RevWalk walk = new RevWalk(repo);
500-
try {
501-
walk.markStart(walk.parseCommit(repo.resolve("HEAD")));
502-
503-
List<Ref> tagRefs = Git.wrap(repo).tagList().call();
504-
String matchPattern = createMatchPattern();
505-
Pattern regex = Pattern.compile(matchPattern);
506-
log("Tag refs [", tagRefs, "]");
507-
508-
for (Ref tagRef : tagRefs) {
509-
walk.reset();
510-
String name = tagRef.getName();
511-
if (!regex.matcher(name).matches()) {
512-
log("Skipping tagRef with name [", name, "] as it doesn't match [", matchPattern, "]");
513-
continue;
514-
}
515-
ObjectId resolvedCommitId = repo.resolve(name);
516-
517-
// todo that's a bit of a hack...
518-
try {
519-
final RevTag revTag = walk.parseTag(resolvedCommitId);
520-
ObjectId taggedCommitId = revTag.getObject().getId();
521-
log("Resolved tag [",revTag.getTagName(),"] [",revTag.getTaggerIdent(),"], points at [",taggedCommitId,"] ");
522-
523-
// sometimes a tag, may point to another tag, so we need to unpack it
524-
while (isTagId(taggedCommitId)) {
525-
taggedCommitId = walk.parseTag(taggedCommitId).getObject().getId();
526-
}
527-
528-
if (commitIdsToTags.containsKey(taggedCommitId)) {
529-
commitIdsToTags.get(taggedCommitId).add(new DatedRevTag(revTag));
530-
} else {
531-
commitIdsToTags.put(taggedCommitId, newArrayList(new DatedRevTag(revTag)));
532-
}
533-
534-
} catch (IncorrectObjectTypeException ex) {
535-
// it's an lightweight tag! (yeah, really)
536-
if (tagsFlag) {
537-
// --tags means "include lightweight tags"
538-
log("Including lightweight tag [", name, "]");
539-
540-
DatedRevTag datedRevTag = new DatedRevTag(resolvedCommitId, name);
541-
542-
if (commitIdsToTags.containsKey(resolvedCommitId)) {
543-
commitIdsToTags.get(resolvedCommitId).add(datedRevTag);
544-
} else {
545-
commitIdsToTags.put(resolvedCommitId, newArrayList(datedRevTag));
546-
}
547-
}
548-
} catch (Exception ignored) {
549-
error("Failed while parsing [",tagRef,"] -- ", Throwables.getStackTraceAsString(ignored));
550-
}
551-
}
552-
553-
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : commitIdsToTags.entrySet()) {
554-
log("key [",entry.getKey(),"], tags => [",entry.getValue(),"] ");
555-
}
556-
557-
Map<ObjectId, List<String>> commitIdsToTagNames = transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
558-
499+
String matchPattern = createMatchPattern();
500+
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = new JGitCommon().getCommitIdsToTags(loggerBridge, repo, tagsFlag, matchPattern);
501+
Map<ObjectId, List<String>> commitIdsToTagNames = new JGitCommon().transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
559502
log("Created map: [",commitIdsToTagNames,"] ");
560503

561504
return commitIdsToTagNames;
562-
} catch (Exception e) {
563-
log("Unable to locate tags\n[",Throwables.getStackTraceAsString(e),"]");
564-
} finally {
565-
walk.release();
566-
}
567-
568-
return Collections.emptyMap();
569-
}
570-
571-
/** Checks if the given object id resolved to a tag object */
572-
private boolean isTagId(ObjectId objectId) {
573-
return objectId.toString().startsWith("tag ");
574-
}
575-
576-
private HashMap<ObjectId, List<String>> transformRevTagsMapToDateSortedTagNames(Map<ObjectId, List<DatedRevTag>> commitIdsToTags) {
577-
HashMap<ObjectId, List<String>> commitIdsToTagNames = newHashMap();
578-
for (Map.Entry<ObjectId, List<DatedRevTag>> objectIdListEntry : commitIdsToTags.entrySet()) {
579-
List<DatedRevTag> tags = objectIdListEntry.getValue();
580-
581-
List<DatedRevTag> newTags = newArrayList(tags);
582-
Collections.sort(newTags, new Comparator<DatedRevTag>() {
583-
@Override
584-
public int compare(DatedRevTag revTag, DatedRevTag revTag2) {
585-
return revTag2.date.compareTo(revTag.date);
586-
}
587-
});
588-
589-
List<String> tagNames = Lists.transform(newTags, new Function<DatedRevTag, String>() {
590-
@Override
591-
public String apply(DatedRevTag input) {
592-
return trimFullTagName(input.tagName);
593-
}
594-
});
595-
596-
commitIdsToTagNames.put(objectIdListEntry.getKey(), tagNames);
597-
}
598-
return commitIdsToTagNames;
599505
}
600506

601507
private String createMatchPattern() {
@@ -610,11 +516,6 @@ private String createMatchPattern() {
610516
return buf.toString();
611517
}
612518

613-
@VisibleForTesting
614-
static String trimFullTagName(@NotNull String tagName) {
615-
return tagName.replaceFirst("refs/tags/", "");
616-
}
617-
618519
private void log(Object... parts) {
619520
loggerBridge.log(parts);
620521
}

src/main/java/pl/project13/jgit/JGitCommon.java

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,37 @@
1717

1818
package pl.project13.jgit;
1919

20+
import static com.google.common.collect.Lists.newArrayList;
21+
import static com.google.common.collect.Maps.newHashMap;
22+
2023
import java.io.IOException;
2124
import java.util.Collection;
25+
import java.util.Collections;
26+
import java.util.Comparator;
27+
import java.util.HashMap;
2228
import java.util.List;
29+
import java.util.Map;
30+
import java.util.regex.Pattern;
2331

2432
import org.eclipse.jgit.api.Git;
2533
import org.eclipse.jgit.api.errors.GitAPIException;
34+
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
2635
import org.eclipse.jgit.lib.ObjectId;
2736
import org.eclipse.jgit.lib.Ref;
2837
import org.eclipse.jgit.lib.Repository;
38+
import org.eclipse.jgit.revwalk.RevTag;
2939
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;
3044

45+
import com.google.common.annotations.VisibleForTesting;
3146
import com.google.common.base.Function;
3247
import com.google.common.base.Predicate;
48+
import com.google.common.base.Throwables;
3349
import com.google.common.collect.Collections2;
50+
import com.google.common.collect.Lists;
3451

3552
/**
3653
* @author <a href="mailto:[email protected]">Konrad 'ktoso' Malawski</a>
@@ -72,5 +89,108 @@ public Collection<String> getTags(Repository repo, final ObjectId headId) throws
7289
}
7390
}
7491
}
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+
}
75195

76196
}

src/test/java/pl/project13/jgit/DescribeCommandIntegrationTest.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -432,18 +432,6 @@ public void shouldReturnJustTheNearestTagWhenAbbrevIsZero() throws Exception {
432432
assertThat(objectId.getName()).isNotEmpty();
433433
}
434434

435-
@Test
436-
public void trimFullTagName_shouldTrimFullTagNamePrefix() throws Exception {
437-
// given
438-
String fullName = "refs/tags/v1.0.0";
439-
440-
// when
441-
String simpleName = DescribeCommand.trimFullTagName(fullName);
442-
443-
// then
444-
assertThat(simpleName).isEqualTo("v1.0.0");
445-
}
446-
447435
String abbrev(@NotNull String id) {
448436
return abbrev(id, DEFAULT_ABBREV_LEN);
449437
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad Malawski <[email protected]>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.jgit;
19+
20+
import org.junit.Test;
21+
import pl.project13.maven.git.GitIntegrationTest;
22+
import static org.fest.assertions.Assertions.assertThat;
23+
24+
public class JGitCommonIntegrationTest extends GitIntegrationTest {
25+
26+
@Test
27+
public void trimFullTagName_shouldTrimFullTagNamePrefix() throws Exception {
28+
// given
29+
String fullName = "refs/tags/v1.0.0";
30+
31+
// when
32+
String simpleName = new JGitCommon().trimFullTagName(fullName);
33+
34+
// then
35+
assertThat(simpleName).isEqualTo("v1.0.0");
36+
}
37+
}

0 commit comments

Comments
 (0)