Skip to content

Commit c8c9465

Browse files
author
Bengt Brodersen
committed
feat: add option describeTagFirstParent option to disable follow first parent only
1 parent 73f88a1 commit c8c9465

File tree

9 files changed

+40
-10
lines changed

9 files changed

+40
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## 6.4.0
4+
5+
##### Features
6+
- add option `describeTagFirstParent` option to disable follow first parent only
7+
38
## 6.3.8
49

510
##### Fixes

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ gitVersioning.apply {
103103
- `disable` global disable(`true`)/enable(`false`) extension, default is `false`.
104104
- Can be overridden by command option, see (Parameters & Environment Variables)[#parameters-&-environment-variables]
105105

106-
- `describeTagPattern` An arbitrary regex to match tag names for git describe command (has to be a **full match pattern** e.g. `v.+`), default is `.*`
106+
- `describeTagPattern` An arbitrary regex to match tag names for git describe command
107+
- has to be a **full match pattern** e.g. `v(.+)`, default is `.*`
108+
- `describeTagFirstParent` Enable(`true`) or disable(`false`) following only the first parent in a merge commit
109+
- default is `true`
110+
107111
- `updateGradleProperties` Enable(`true`)/disable(`false`) version and properties update in `gradle.properties` file, default is `false`
108112
- Can be overridden by command option, see (Parameters & Environment Variables)[#parameters-&-environment-variables]
109113

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

128134
- `version` The new version format, see [Format Placeholders](#format-placeholders)

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ plugins {
1212
}
1313

1414
group 'me.qoomon'
15-
version '6.3.8'
15+
version '6.4.0'
1616
sourceCompatibility = JavaVersion.VERSION_11
1717
targetCompatibility = JavaVersion.VERSION_11
1818

src/main/java/me/qoomon/gitversioning/commons/GitSituation.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.List;
1414
import java.util.function.Supplier;
1515
import java.util.regex.Pattern;
16-
import java.util.stream.Collectors;
1716

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

4039
private Pattern describeTagPattern = Pattern.compile(".*");
40+
41+
private boolean firstParent = true;
42+
4143
private Supplier<GitDescription> description = Lazy.by(this::describe);
4244

4345
public GitSituation(Repository repository) throws IOException {
@@ -153,6 +155,14 @@ public Pattern getDescribeTagPattern() {
153155
return describeTagPattern;
154156
}
155157

158+
public boolean isFirstParent() {
159+
return firstParent;
160+
}
161+
162+
public void setFirstParent(boolean firstParent) {
163+
this.firstParent = firstParent;
164+
}
165+
156166
public GitDescription getDescription() {
157167
return description.get();
158168
}
@@ -178,6 +188,6 @@ private boolean clean() throws GitAPIException {
178188
}
179189

180190
private GitDescription describe() throws IOException {
181-
return GitUtil.describe(head, describeTagPattern, repository);
191+
return GitUtil.describe(head, describeTagPattern, repository, firstParent);
182192
}
183193
}

src/main/java/me/qoomon/gitversioning/commons/GitUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static List<String> tagsPointAt(ObjectId revObjectId, Repository reposito
4747
return reverseTagRefMap(repository).getOrDefault(revObjectId, emptyList());
4848
}
4949

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

src/main/java/me/qoomon/gitversioning/commons/TagComparator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
44
import org.eclipse.jgit.lib.Ref;
55
import org.eclipse.jgit.lib.Repository;
6-
import org.eclipse.jgit.revwalk.*;
6+
import org.eclipse.jgit.revwalk.RevObject;
7+
import org.eclipse.jgit.revwalk.RevTag;
8+
import org.eclipse.jgit.revwalk.RevWalk;
79

810
import java.util.Comparator;
911
import java.util.Date;

src/main/java/me/qoomon/gradle/gitversioning/GitVersioningPluginConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public Pattern projectVersionPattern() {
3434
}
3535

3636
public String describeTagPattern = null;
37+
public Boolean describeTagFirstParent = true;
3738

3839
public Boolean updateGradleProperties;
3940

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

6061
public String describeTagPattern;
62+
public Boolean describeTagFirstParent = null;
6163

6264
public Pattern getDescribeTagPattern() {
6365
return Pattern.compile(describeTagPattern);
@@ -93,6 +95,7 @@ public RefPatchDescription(GitRefType type, Pattern pattern) {
9395
public RefPatchDescription(GitRefType type, Pattern pattern, PatchDescription patch) {
9496
this(type, pattern);
9597
this.describeTagPattern = patch.describeTagPattern;
98+
this.describeTagFirstParent = patch.describeTagFirstParent;
9699
this.updateGradleProperties = patch.updateGradleProperties;
97100
this.version = patch.version;
98101
this.properties = patch.properties;

src/main/java/me/qoomon/gradle/gitversioning/GitVersioningPluginExtension.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ private void apply() throws IOException {
132132
project.getLogger().lifecycle(" describeTagPattern: " + patchDescription.describeTagPattern);
133133
gitSituation.setDescribeTagPattern(patchDescription.getDescribeTagPattern());
134134
}
135+
if (patchDescription.describeTagFirstParent != null) {
136+
project.getLogger().info(" describeTagFirstParent: " + patchDescription.describeTagFirstParent);
137+
gitSituation.setFirstParent(patchDescription.describeTagFirstParent);
138+
}
135139
if (patchDescription.version != null) {
136140
project.getLogger().lifecycle(" version: " + patchDescription.version);
137141
}

src/test/java/me/qoomon/gitversioning/commons/GitUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void describe() throws Exception {
187187
git.tag().setName(givenTagName).setAnnotated(true).setObjectId(givenCommit).setMessage(".").call();
188188

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

192192
// then
193193
assertThat(description).satisfies(it -> {

0 commit comments

Comments
 (0)