Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions src/main/java/pl/project13/jgit/DescribeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ public DescribeResult call() throws GitAPIException {
ObjectReader objectReader = repo.newObjectReader();

// get tags
Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, tagsFlag);
String matchPattern = createMatchPattern();
Map<ObjectId, List<String>> tagObjectIdToName = jGitCommon.findTagObjectIds(repo, tagsFlag, matchPattern);

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

RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
try {
ObjectId headId = repo.resolve("HEAD");

RevWalk walk = new RevWalk(repo);
RevCommit headCommit = walk.lookupCommit(headId);
walk.dispose();

log.info("HEAD is [{}]", headCommit.getName());
return headCommit;
} catch (IOException ex) {
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
}
}

// git commit id -> its tag (or tags)
private Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean tagsFlag) {
String matchPattern = createMatchPattern();
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = jGitCommon.getCommitIdsToTags(repo, tagsFlag, matchPattern);
Map<ObjectId, List<String>> commitIdsToTagNames = jGitCommon.transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
log.info("Created map: [{}]", commitIdsToTagNames);

return commitIdsToTagNames;
return jGitCommon.findHeadObjectId(repo);
}

private String createMatchPattern() {
if (!matchOption.isPresent()) {
return ".*";
}

return "^refs/tags/\\Q" +
matchOption.get().replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
"\\E$";
return jGitCommon.createMatchPattern(matchOption.get());
}
}
95 changes: 54 additions & 41 deletions src/main/java/pl/project13/jgit/JGitCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
Expand All @@ -40,7 +41,9 @@
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import pl.project13.maven.git.GitDescribeConfig;
import pl.project13.maven.git.log.LoggerBridge;
import pl.project13.maven.git.util.Pair;

public class JGitCommon {

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

public String getClosestTagName(@NotNull Repository repo) {
Map<ObjectId, List<DatedRevTag>> map = getClosestTagAsMap(repo);
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : map.entrySet()) {
return trimFullTagName(entry.getValue().get(0).tagName);
}
return "";
public String getClosestTagName(@NotNull Repository repo, GitDescribeConfig gitDescribe) {
// TODO: Why does some tests fail when it gets headCommit from JGitprovider?
RevCommit headCommit = findHeadObjectId(repo);
Pair<RevCommit, String> pair = getClosestRevCommit(repo, headCommit, gitDescribe);
return pair.second;
}

public String getClosestTagCommitCount(@NotNull Repository repo, RevCommit headCommit) {
HashMap<ObjectId, List<String>> map = transformRevTagsMapToDateSortedTagNames(getClosestTagAsMap(repo));
ObjectId obj = (ObjectId) map.keySet().toArray()[0];

try (RevWalk walk = new RevWalk(repo)) {
RevCommit commit = walk.lookupCommit(obj);
walk.dispose();
public String getClosestTagCommitCount(@NotNull Repository repo, GitDescribeConfig gitDescribe) {
// TODO: Why does some tests fail when it gets headCommit from JGitprovider?
RevCommit headCommit = findHeadObjectId(repo);
Pair<RevCommit, String> pair = getClosestRevCommit(repo, headCommit, gitDescribe);
RevCommit revCommit = pair.first;
int distance = distanceBetween(repo, headCommit, revCommit);
return String.valueOf(distance);
}

int distance = distanceBetween(repo, headCommit, commit);
return String.valueOf(distance);
private Pair<RevCommit, String> getClosestRevCommit(@NotNull Repository repo, RevCommit headCommit, GitDescribeConfig gitDescribe) {
boolean includeLightweightTags = false;
String matchPattern = ".*";
if (gitDescribe != null) {
includeLightweightTags = gitDescribe.getTags();
if (!"*".equals(gitDescribe.getMatch())) {
matchPattern = createMatchPattern(gitDescribe.getMatch());
}
}
Map<ObjectId, List<String>> tagObjectIdToName = findTagObjectIds(repo, includeLightweightTags, matchPattern);
if (tagObjectIdToName.containsKey(headCommit)) {
String tagName = tagObjectIdToName.get(headCommit).iterator().next();
return Pair.of(headCommit, tagName);
}
List<RevCommit> commits = findCommitsUntilSomeTag(repo, headCommit, tagObjectIdToName);
RevCommit revCommit = commits.get(0);
String tagName = tagObjectIdToName.get(revCommit).iterator().next();

return Pair.of(revCommit, tagName);
}

private Map<ObjectId, List<DatedRevTag>> getClosestTagAsMap(@NotNull Repository repo) {
Map<ObjectId, List<DatedRevTag>> mapWithClosestTagOnly = new HashMap<>();
String matchPattern = ".*";
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, true, matchPattern);
LinkedHashMap<ObjectId, List<DatedRevTag>> sortedCommitIdsToTags = sortByDatedRevTag(commitIdsToTags);
protected String createMatchPattern(String pattern) {
return "^refs/tags/\\Q" +
pattern.replace("*", "\\E.*\\Q").replace("?", "\\E.\\Q") +
"\\E$";
}

for (Map.Entry<ObjectId, List<DatedRevTag>> entry: sortedCommitIdsToTags.entrySet()) {
mapWithClosestTagOnly.put(entry.getKey(), entry.getValue());
break;
}
protected Map<ObjectId, List<String>> findTagObjectIds(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) {
Map<ObjectId, List<DatedRevTag>> commitIdsToTags = getCommitIdsToTags(repo, includeLightweightTags, matchPattern);
Map<ObjectId, List<String>> commitIdsToTagNames = transformRevTagsMapToDateSortedTagNames(commitIdsToTags);
log.info("Created map: [{}]", commitIdsToTagNames);

return mapWithClosestTagOnly;
return commitIdsToTagNames;
}

private LinkedHashMap<ObjectId, List<DatedRevTag>> sortByDatedRevTag(Map<ObjectId, List<DatedRevTag>> map) {
List<Map.Entry<ObjectId, List<DatedRevTag>>> list = new ArrayList<>(map.entrySet());
protected RevCommit findHeadObjectId(@NotNull Repository repo) throws RuntimeException {
try {
ObjectId headId = repo.resolve(Constants.HEAD);

Collections.sort(list, new Comparator<Map.Entry<ObjectId, List<DatedRevTag>>>() {
public int compare(Map.Entry<ObjectId, List<DatedRevTag>> m1, Map.Entry<ObjectId, List<DatedRevTag>> m2) {
// we need to sort the DatedRevTags to a commit first, otherwise we may get problems when we have two tags for the same commit
Collections.sort(m1.getValue(), datedRevTagComparator());
Collections.sort(m2.getValue(), datedRevTagComparator());
try (RevWalk walk = new RevWalk(repo)) {
RevCommit headCommit = walk.lookupCommit(headId);
walk.dispose();

DatedRevTag datedRevTag1 = m1.getValue().get(0);
DatedRevTag datedRevTag2 = m2.getValue().get(0);
return datedRevTagComparator().compare(datedRevTag1,datedRevTag2);
log.info("HEAD is [{}]", headCommit.getName());
return headCommit;
}
});

LinkedHashMap<ObjectId, List<DatedRevTag>> result = new LinkedHashMap<>();
for (Map.Entry<ObjectId, List<DatedRevTag>> entry : list) {
result.put(entry.getKey(), entry.getValue());
} catch (IOException ex) {
throw new RuntimeException("Unable to obtain HEAD commit!", ex);
}
return result;
}

protected Map<ObjectId, List<DatedRevTag>> getCommitIdsToTags(@NotNull Repository repo, boolean includeLightweightTags, String matchPattern) {
Expand Down
56 changes: 44 additions & 12 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,51 +708,83 @@ static class CannotReadFileException extends Exception {

// SETTERS FOR TESTS ----------------------------------------------------

public void setFormat(String format) {
@VisibleForTesting void setFormat(String format) {
this.format = format;
}

public void setVerbose(boolean verbose) {
@VisibleForTesting void setVerbose(boolean verbose) {
this.verbose = verbose;
}

public void setDotGitDirectory(File dotGitDirectory) {
@VisibleForTesting void setProject(MavenProject project) {
this.project = project;
}

@VisibleForTesting void setDotGitDirectory(File dotGitDirectory) {
this.dotGitDirectory = dotGitDirectory;
}

public void setPrefix(String prefix) {
@VisibleForTesting void setPrefix(String prefix) {
this.prefix = prefix;
}

public void setDateFormat(String dateFormat) {
@VisibleForTesting void setDateFormat(String dateFormat) {
this.dateFormat = dateFormat;
}

public Properties getProperties() {
@VisibleForTesting Properties getProperties() {
return properties;
}

public void setGitDescribe(GitDescribeConfig gitDescribe) {
@VisibleForTesting void setGitDescribe(GitDescribeConfig gitDescribe) {
this.gitDescribe = gitDescribe;
}

public void setAbbrevLength(int abbrevLength) {
@VisibleForTesting void setAbbrevLength(int abbrevLength) {
this.abbrevLength = abbrevLength;
}

public void setExcludeProperties(List<String> excludeProperties) {
@VisibleForTesting void setExcludeProperties(List<String> excludeProperties) {
this.excludeProperties = excludeProperties;
}

public void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
@VisibleForTesting void setIncludeOnlyProperties(List<String> includeOnlyProperties) {
this.includeOnlyProperties = includeOnlyProperties;
}

public void useNativeGit(boolean useNativeGit) {
@VisibleForTesting void setUseNativeGit(boolean useNativeGit) {
this.useNativeGit = useNativeGit;
}

public void setCommitIdGenerationMode(String commitIdGenerationMode) {
@VisibleForTesting void setCommitIdGenerationMode(String commitIdGenerationMode) {
this.commitIdGenerationMode = commitIdGenerationMode;
}

@VisibleForTesting void setSkip(boolean skip) {
this.skip = skip;
}

@VisibleForTesting void setSkipPoms(boolean skipPoms) {
this.skipPoms = skipPoms;
}

@VisibleForTesting void setGenerateGitPropertiesFile(boolean generateGitPropertiesFile) {
this.generateGitPropertiesFile = generateGitPropertiesFile;
}

@VisibleForTesting void setGenerateGitPropertiesFilename(String generateGitPropertiesFilename) {
this.generateGitPropertiesFilename = generateGitPropertiesFilename;
}

@VisibleForTesting void setDateFormatTimeZone(String dateFormatTimeZone) {
this.dateFormatTimeZone = dateFormatTimeZone;
}

@VisibleForTesting void setFailOnNoGitDirectory(boolean failOnNoGitDirectory) {
this.failOnNoGitDirectory = failOnNoGitDirectory;
}

@VisibleForTesting void setFailOnUnableToExtractRepoInfo(boolean failOnUnableToExtractRepoInfo) {
this.failOnUnableToExtractRepoInfo = failOnUnableToExtractRepoInfo;
}
}
4 changes: 2 additions & 2 deletions src/main/java/pl/project13/maven/git/JGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public String getTags() throws GitCommitIdExecutionException {
public String getClosestTagName() throws GitCommitIdExecutionException {
Repository repo = getGitRepository();
try {
return jGitCommon.getClosestTagName(repo);
return jGitCommon.getClosestTagName(repo, gitDescribe);
} catch (Throwable t) {
// could not find any tags to describe
}
Expand All @@ -198,7 +198,7 @@ public String getClosestTagName() throws GitCommitIdExecutionException {
public String getClosestTagCommitCount() throws GitCommitIdExecutionException {
Repository repo = getGitRepository();
try {
return jGitCommon.getClosestTagCommitCount(repo, headCommit);
return jGitCommon.getClosestTagCommitCount(repo, gitDescribe);
} catch (Throwable t) {
// could not find any tags to describe
}
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/pl/project13/maven/git/NativeGitProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,19 @@ public String getRemoteOriginUrl() throws GitCommitIdExecutionException {
@Override
public String getClosestTagName() throws GitCommitIdExecutionException {
try {
return runGitCommand(canonical, "describe --abbrev=0 --tags");
StringBuilder argumentsForGitDescribe = new StringBuilder();
argumentsForGitDescribe.append("describe --abbrev=0");
if (gitDescribe != null) {
if (gitDescribe.getTags()) {
argumentsForGitDescribe.append(" --tags");
}

final String matchOption = gitDescribe.getMatch();
if (matchOption != null && !matchOption.isEmpty()) {
argumentsForGitDescribe.append(" --match=").append(matchOption);
}
}
return runGitCommand(canonical, argumentsForGitDescribe.toString());
} catch (NativeCommandException ignore) {
// could not find any tags to describe
}
Expand Down
40 changes: 0 additions & 40 deletions src/test/java/org/mockito/internal/util/reflection/Whitebox.java

This file was deleted.

Loading