Skip to content

Commit

Permalink
feat: add option describeTagFirstParent option to disable follow firs…
Browse files Browse the repository at this point in the history
…t parent only
  • Loading branch information
Bengt Brodersen committed Jan 24, 2023
1 parent 73f88a1 commit c8c9465
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 6.4.0

##### Features
- add option `describeTagFirstParent` option to disable follow first parent only

## 6.3.8

##### Fixes
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ gitVersioning.apply {
- `disable` global disable(`true`)/enable(`false`) extension, default is `false`.
- Can be overridden by command option, see (Parameters & Environment Variables)[#parameters-&-environment-variables]

- `describeTagPattern` An arbitrary regex to match tag names for git describe command (has to be a **full match pattern** e.g. `v.+`), default is `.*`
- `describeTagPattern` An arbitrary regex to match tag names for git describe command
- has to be a **full match pattern** e.g. `v(.+)`, default is `.*`
- `describeTagFirstParent` Enable(`true`) or disable(`false`) following only the first parent in a merge commit
- default is `true`

- `updateGradleProperties` Enable(`true`)/disable(`false`) version and properties update in `gradle.properties` file, default is `false`
- Can be overridden by command option, see (Parameters & Environment Variables)[#parameters-&-environment-variables]

Expand All @@ -123,6 +127,8 @@ gitVersioning.apply {
- `describeTagPattern` An arbitrary regex to match tag names for git describe command
- has to be a **full match pattern** e.g. `v.+`)
- will override global `describeTagPattern` value
- `describeTagFirstParent` Enable(`true`) or disable(`false`) following only the first parent in a merge commit
- default is `true`
<br><br>

- `version` The new version format, see [Format Placeholders](#format-placeholders)
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group 'me.qoomon'
version '6.3.8'
version '6.4.0'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

Expand Down
14 changes: 12 additions & 2 deletions src/main/java/me/qoomon/gitversioning/commons/GitSituation.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.List;
import java.util.function.Supplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.time.Instant.EPOCH;
import static java.time.ZoneOffset.UTC;
Expand All @@ -38,6 +37,9 @@ public class GitSituation {
private final Supplier<Boolean> clean = Lazy.by(this::clean);

private Pattern describeTagPattern = Pattern.compile(".*");

private boolean firstParent = true;

private Supplier<GitDescription> description = Lazy.by(this::describe);

public GitSituation(Repository repository) throws IOException {
Expand Down Expand Up @@ -153,6 +155,14 @@ public Pattern getDescribeTagPattern() {
return describeTagPattern;
}

public boolean isFirstParent() {
return firstParent;
}

public void setFirstParent(boolean firstParent) {
this.firstParent = firstParent;
}

public GitDescription getDescription() {
return description.get();
}
Expand All @@ -178,6 +188,6 @@ private boolean clean() throws GitAPIException {
}

private GitDescription describe() throws IOException {
return GitUtil.describe(head, describeTagPattern, repository);
return GitUtil.describe(head, describeTagPattern, repository, firstParent);
}
}
8 changes: 4 additions & 4 deletions src/main/java/me/qoomon/gitversioning/commons/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static List<String> tagsPointAt(ObjectId revObjectId, Repository reposito
return reverseTagRefMap(repository).getOrDefault(revObjectId, emptyList());
}

public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, Repository repository) throws IOException {
public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, Repository repository, boolean firstParent) throws IOException {
if (revObjectId == null) {
return new GitDescription(NO_COMMIT, "root", 0);
}
Expand All @@ -57,13 +57,13 @@ public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern,
// Walk back commit ancestors looking for tagged one
try (RevWalk walk = new RevWalk(repository)) {
walk.setRetainBody(false);
walk.setFirstParent(true);
walk.setFirstParent(firstParent);
walk.markStart(walk.parseCommit(revObjectId));
Iterator<RevCommit> walkIterator = walk.iterator();
int depth = 0;
while (walkIterator.hasNext()) {
RevCommit rev = walkIterator.next();
Optional<String> matchingTag = objectIdListMap.getOrDefault(rev, emptyList()).stream()
RevCommit rev = walkIterator.next();
Optional<String> matchingTag = objectIdListMap.getOrDefault(rev, emptyList()).stream()
.filter(tag -> tagPattern.matcher(tag).matches())
.findFirst();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.*;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevTag;
import org.eclipse.jgit.revwalk.RevWalk;

import java.util.Comparator;
import java.util.Date;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Pattern projectVersionPattern() {
}

public String describeTagPattern = null;
public Boolean describeTagFirstParent = true;

public Boolean updateGradleProperties;

Expand All @@ -58,6 +59,7 @@ public void rev(Action<PatchDescription> action) {
public static class PatchDescription {

public String describeTagPattern;
public Boolean describeTagFirstParent = null;

public Pattern getDescribeTagPattern() {
return Pattern.compile(describeTagPattern);
Expand Down Expand Up @@ -93,6 +95,7 @@ public RefPatchDescription(GitRefType type, Pattern pattern) {
public RefPatchDescription(GitRefType type, Pattern pattern, PatchDescription patch) {
this(type, pattern);
this.describeTagPattern = patch.describeTagPattern;
this.describeTagFirstParent = patch.describeTagFirstParent;
this.updateGradleProperties = patch.updateGradleProperties;
this.version = patch.version;
this.properties = patch.properties;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ private void apply() throws IOException {
project.getLogger().lifecycle(" describeTagPattern: " + patchDescription.describeTagPattern);
gitSituation.setDescribeTagPattern(patchDescription.getDescribeTagPattern());
}
if (patchDescription.describeTagFirstParent != null) {
project.getLogger().info(" describeTagFirstParent: " + patchDescription.describeTagFirstParent);
gitSituation.setFirstParent(patchDescription.describeTagFirstParent);
}
if (patchDescription.version != null) {
project.getLogger().lifecycle(" version: " + patchDescription.version);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ void describe() throws Exception {
git.tag().setName(givenTagName).setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();

// when
GitDescription description = GitUtil.describe(head(git), Pattern.compile("v.+"), git.getRepository());
GitDescription description = GitUtil.describe(head(git), Pattern.compile("v.+"), git.getRepository(), true);

// then
assertThat(description).satisfies(it -> {
Expand Down

0 comments on commit c8c9465

Please sign in to comment.