Skip to content

Commit 88378e0

Browse files
author
TheSnoozer
authored
Merge pull request #332 from TheSnoozer/feature/221
Feature/221 (the closest tag and clostest.tag.count will be determined based on properties configured inside the git-describe command) plus Testcleanup (in particular remove the Whitebox class that was borrowed from mockito)
2 parents b3da4a4 + 3d12d1d commit 88378e0

File tree

14 files changed

+363
-279
lines changed

14 files changed

+363
-279
lines changed

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ public DescribeResult call() throws GitAPIException {
255255
ObjectReader objectReader = repo.newObjectReader();
256256

257257
// get tags
258-
Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, tagsFlag);
258+
String matchPattern = createMatchPattern();
259+
Map<ObjectId, List<String>> tagObjectIdToName = jGitCommon.findTagObjectIds(repo, tagsFlag, matchPattern);
259260

260261
// get current commit
261262
RevCommit headCommit = findHeadObjectId(repo);
@@ -344,37 +345,14 @@ static boolean hasTags(ObjectId headCommit, @NotNull Map<ObjectId, List<String>>
344345
}
345346

346347
RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
347-
try {
348-
ObjectId headId = repo.resolve("HEAD");
349-
350-
RevWalk walk = new RevWalk(repo);
351-
RevCommit headCommit = walk.lookupCommit(headId);
352-
walk.dispose();
353-
354-
log.info("HEAD is [{}]", headCommit.getName());
355-
return headCommit;
356-
} catch (IOException ex) {
357-
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
358-
}
359-
}
360-
361-
// git commit id -> its tag (or tags)
362-
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
363-
String matchPattern = createMatchPattern();
364-
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(repo, tagsFlag, matchPattern);
365-
Map<ObjectId, List<String>> commitIdsToTagNames = jGitCommon.transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
366-
log.info("Created map: [{}]", commitIdsToTagNames);
367-
368-
return commitIdsToTagNames;
348+
return jGitCommon.findHeadObjectId(repo);
369349
}
370350

371351
private String createMatchPattern() {
372352
if (!matchOption.isPresent()) {
373353
return ".*";
374354
}
375355

376-
return "^refs/tags/\\Q" +
377-
matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
378-
"\\E$";
356+
return jGitCommon.createMatchPattern(matchOption.get());
379357
}
380358
}

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

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.eclipse.jgit.api.Status;
2626
import org.eclipse.jgit.api.errors.GitAPIException;
2727
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
28+
import org.eclipse.jgit.lib.Constants;
2829
import org.eclipse.jgit.lib.ObjectId;
2930
import org.eclipse.jgit.lib.Ref;
3031
import org.eclipse.jgit.lib.Repository;
@@ -40,7 +41,9 @@
4041
import com.google.common.base.Predicate;
4142
import com.google.common.collect.Collections2;
4243
import com.google.common.collect.Lists;
44+
import pl.project13.maven.git.GitDescribeConfig;
4345
import pl.project13.maven.git.log.LoggerBridge;
46+
import pl.project13.maven.git.util.Pair;
4447

4548
public class JGitCommon {
4649

@@ -81,61 +84,71 @@ private Collection<String> getTags(final Git git, final ObjectId headId, final R
8184
return tags;
8285
}
8386

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;
9092
}
9193

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+
}
99102

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);
102116
}
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);
103122
}
104123

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+
}
110129

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);
115134

116-
return mapWithClosestTagOnly;
135+
return commitIdsToTagNames;
117136
}
118137

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);
121141

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();
127145

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;
131148
}
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);
137151
}
138-
return result;
139152
}
140153

141154
protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) {

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -708,51 +708,83 @@ static class CannotReadFileException extends Exception {
708708

709709
// SETTERS FOR TESTS ----------------------------------------------------
710710

711-
public void setFormat(String format) {
711+
@VisibleForTesting void setFormat(String format) {
712712
this.format = format;
713713
}
714714

715-
public void setVerbose(boolean verbose) {
715+
@VisibleForTesting void setVerbose(boolean verbose) {
716716
this.verbose = verbose;
717717
}
718718

719-
public void setDotGitDirectory(File dotGitDirectory) {
719+
@VisibleForTesting void setProject(MavenProject project) {
720+
this.project = project;
721+
}
722+
723+
@VisibleForTesting void setDotGitDirectory(File dotGitDirectory) {
720724
this.dotGitDirectory = dotGitDirectory;
721725
}
722726

723-
public void setPrefix(String prefix) {
727+
@VisibleForTesting void setPrefix(String prefix) {
724728
this.prefix = prefix;
725729
}
726730

727-
public void setDateFormat(String dateFormat) {
731+
@VisibleForTesting void setDateFormat(String dateFormat) {
728732
this.dateFormat = dateFormat;
729733
}
730734

731-
public Properties getProperties() {
735+
@VisibleForTesting Properties getProperties() {
732736
return properties;
733737
}
734738

735-
public void setGitDescribe(GitDescribeConfig gitDescribe) {
739+
@VisibleForTesting void setGitDescribe(GitDescribeConfig gitDescribe) {
736740
this.gitDescribe = gitDescribe;
737741
}
738742

739-
public void setAbbrevLength(int abbrevLength) {
743+
@VisibleForTesting void setAbbrevLength(int abbrevLength) {
740744
this.abbrevLength = abbrevLength;
741745
}
742746

743-
public void setExcludeProperties(List<String> excludeProperties) {
747+
@VisibleForTesting void setExcludeProperties(List<String> excludeProperties) {
744748
this.excludeProperties = excludeProperties;
745749
}
746750

747-
public void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
751+
@VisibleForTesting void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
748752
this.includeOnlyProperties = includeOnlyProperties;
749753
}
750754

751-
public void useNativeGit(boolean useNativeGit) {
755+
@VisibleForTesting void setUseNativeGit(boolean useNativeGit) {
752756
this.useNativeGit = useNativeGit;
753757
}
754758

755-
public void setCommitIdGenerationMode(String commitIdGenerationMode) {
759+
@VisibleForTesting void setCommitIdGenerationMode(String commitIdGenerationMode) {
756760
this.commitIdGenerationMode = commitIdGenerationMode;
757761
}
762+
763+
@VisibleForTesting void setSkip(boolean skip) {
764+
this.skip = skip;
765+
}
766+
767+
@VisibleForTesting void setSkipPoms(boolean skipPoms) {
768+
this.skipPoms = skipPoms;
769+
}
770+
771+
@VisibleForTesting void setGenerateGitPropertiesFile(boolean generateGitPropertiesFile) {
772+
this.generateGitPropertiesFile = generateGitPropertiesFile;
773+
}
774+
775+
@VisibleForTesting void setGenerateGitPropertiesFilename(String generateGitPropertiesFilename) {
776+
this.generateGitPropertiesFilename = generateGitPropertiesFilename;
777+
}
778+
779+
@VisibleForTesting void setDateFormatTimeZone(String dateFormatTimeZone) {
780+
this.dateFormatTimeZone = dateFormatTimeZone;
781+
}
782+
783+
@VisibleForTesting void setFailOnNoGitDirectory(boolean failOnNoGitDirectory) {
784+
this.failOnNoGitDirectory = failOnNoGitDirectory;
785+
}
786+
787+
@VisibleForTesting void setFailOnUnableToExtractRepoInfo(boolean failOnUnableToExtractRepoInfo) {
788+
this.failOnUnableToExtractRepoInfo = failOnUnableToExtractRepoInfo;
789+
}
758790
}

src/main/java/pl/project13/maven/git/JGitProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public String getTags() throws GitCommitIdExecutionException {
187187
public String getClosestTagName() throws GitCommitIdExecutionException {
188188
Repository repo = getGitRepository();
189189
try {
190-
return jGitCommon.getClosestTagName(repo);
190+
return jGitCommon.getClosestTagName(repo, gitDescribe);
191191
} catch (Throwable t) {
192192
// could not find any tags to describe
193193
}
@@ -198,7 +198,7 @@ public String getClosestTagName() throws GitCommitIdExecutionException {
198198
public String getClosestTagCommitCount() throws GitCommitIdExecutionException {
199199
Repository repo = getGitRepository();
200200
try {
201-
return jGitCommon.getClosestTagCommitCount(repo, headCommit);
201+
return jGitCommon.getClosestTagCommitCount(repo, gitDescribe);
202202
} catch (Throwable t) {
203203
// could not find any tags to describe
204204
}

src/main/java/pl/project13/maven/git/NativeGitProvider.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,19 @@ public String getRemoteOriginUrl() throws GitCommitIdExecutionException {
212212
@Override
213213
public String getClosestTagName() throws GitCommitIdExecutionException {
214214
try {
215-
return runGitCommand(canonical, "describe --abbrev=0 --tags");
215+
StringBuilder argumentsForGitDescribe = new StringBuilder();
216+
argumentsForGitDescribe.append("describe --abbrev=0");
217+
if (gitDescribe != null) {
218+
if (gitDescribe.getTags()) {
219+
argumentsForGitDescribe.append(" --tags");
220+
}
221+
222+
final String matchOption = gitDescribe.getMatch();
223+
if (matchOption != null && !matchOption.isEmpty()) {
224+
argumentsForGitDescribe.append(" --match=").append(matchOption);
225+
}
226+
}
227+
return runGitCommand(canonical, argumentsForGitDescribe.toString());
216228
} catch (NativeCommandException ignore) {
217229
// could not find any tags to describe
218230
}

src/test/java/org/mockito/internal/util/reflection/Whitebox.java

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)