Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to not have full depth when no tag matches the expression and config to limit the depth #336

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You can configure the version and properties adjustments for specific branches a
```xml
<configuration xmlns="https://github.com/qoomon/maven-git-versioning-extension"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/qoomon/maven-git-versioning-extension https://qoomon.github.io/maven-git-versioning-extension/configuration-9.4.0.xsd">
xsi:schemaLocation="https://github.com/qoomon/maven-git-versioning-extension https://qoomon.github.io/maven-git-versioning-extension/configuration-9.8.2.xsd">

<refs>
<ref type="branch">
Expand Down Expand Up @@ -93,6 +93,12 @@ You can configure the version and properties adjustments for specific branches a
- 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`
- `<describeTagMaxDepth>` An integer that describes the maximum distance in number of commits when searching a tag
matching `<describeTagPattern>` from the ref commit. This value is applied on all `<ref>` elements unless specified
otherwise in the `<ref>` config.
`<describeTagPattern>`.
- By default, it is set to `Integer.MAX_VALUE`.
- For convenience, any strictly negative value will also remove the limitation.

- `<updatePom>` Enable(`true`)/disable(`false`) version and properties update in original pom file, default is `false`
- Can be overridden by command option, see [Parameters & Environment Variables](#parameters--environment-variables).
Expand All @@ -114,6 +120,11 @@ You can configure the version and properties adjustments for specific branches a
- will override global `<describeTagPattern>` value
- `<describeTagFirstParent>` Enable(`true`) or disable(`false`) following only the first parent in a merge commit
- default is `true`
- `<describeTagMaxDepth>` An integer that describes the maximum distance in number of commits when searching a tag
matching `<describeTagPattern>` from the ref commit. If no tag is found, `${describe.distance}`'s value is
`min(<distance from ref to root commit>, <describeTagMaxDepth>)`.
- By default, it is set to `Integer.MAX_VALUE`.
- For convenience, any strictly negative value will also remove the limitation.
<br><br>

- `<version>` The new version format, see [Format Placeholders](#format-placeholders)
Expand Down Expand Up @@ -236,7 +247,9 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`

- `${describe}` Will resolve to `git describe` output
- `${describe.distance}` The distance count to last matching tag
- `${describe.distanceOrZero}` The distance count to last matching tag or 0 if no tag is found
- `${describe.distance.snapshot}` Empty string on matching tag, `-SNAPSHOT` if `describe.distance > 0`
- `${describe.distanceOrZero.snapshot}` Empty string on matching tag, `-SNAPSHOT` if `describe.distanceOrZero > 0`
- `${describe.tag}` The matching tag of `git describe`
- `${describe.tag.version}` the tag version determined by regex `(?<version>(?<core>(?<major>\d+)(?:\.(?<minor>\d+)(?:\.(?<patch>\d+))?)?)(?:-(?<label>.*))?)`
- `${describe.tag.version.core}` the core version component of `${describe.tag.version}` e.g. '1.2.3'
Expand All @@ -247,11 +260,15 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
- `${describe.tag.version.patch}` the patch version component of `${describe.tag.version}` e.g. '3'
- `${describe.tag.version.patch.next}` the `${describe.tag.version.patch}` increased by 1 e.g. '4'
- `${describe.tag.version.patch.plus.describe.distance}` the `${describe.tag.version.patch}` increased by `${describe.distance}` e.g. '2'
- `${describe.tag.version.patch.plus.describe.distanceOrZero}` the `${describe.tag.version.patch}` increased by `${describe.distanceOrZero}` e.g. '2'
- `${describe.tag.version.patch.next.plus.describe.distance}` the `${describe.tag.version.patch.next}` increased by `${describe.distance}` e.g. '3'
- `${describe.tag.version.patch.next.plus.describe.distanceOrZero}` the `${describe.tag.version.patch.next}` increased by `${describe.distanceOrZero}` e.g. '3'
- `${describe.tag.version.label}` the label version component of `${describe.tag.version}` e.g. 'SNAPSHOT'
- `${describe.tag.version.label.next}` the `${describe.tag.version.label}` converted to an integer and increased by 1 e.g. '6'
- `${describe.tag.version.label.plus.describe.distance}` the `${describe.tag.version.label}` increased by `${describe.distance}` e.g. '2'
- `${describe.tag.version.label.plus.describe.distanceOrZero}` the `${describe.tag.version.label}` increased by `${describe.distanceOrZero}` e.g. '2'
- `${describe.tag.version.label.next.plus.describe.distance}` the `${describe.tag.version.label.next}` increased by `${describe.distance}` e.g. '3'
- `${describe.tag.version.label.next.plus.describe.distanceOrZero}` the `${describe.tag.version.label.next}` increased by `${describe.distanceOrZero}` e.g. '3'
- Describe Tag Pattern Groups
- Content of regex groups in `<describeTagPattern>` can be addressed like this:
- `${describe.tag.GROUP_NAME}` `${describe.tag.GROUP_NAME.slug}`
Expand Down
95 changes: 95 additions & 0 deletions docs/configuration-9.8.2.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="UTF-8" ?>

<xs:schema xmlns="https://github.com/qoomon/maven-git-versioning-extension"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://github.com/qoomon/maven-git-versioning-extension"
elementFormDefault="qualified">

<xs:element name="configuration">
<xs:complexType>
<xs:all>
<xs:element name="disable" type="xs:boolean" minOccurs="0"/>

<xs:element name="projectVersionPattern" type="xs:string" minOccurs="0"/>

<xs:element name="describeTagPattern" type="xs:string" minOccurs="0"/>
<xs:element name="describeTagFirstParent" type="xs:boolean" minOccurs="0"/>
<xs:element name="describeTagMaxDepth" type="xs:int" minOccurs="0"/>
<xs:element name="updatePom" type="xs:boolean" minOccurs="0"/>

<xs:element name="refs" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="ref" type="RefPatchDescription" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>

<xs:attribute name="considerTagsOnBranches" type="xs:boolean"/>
</xs:complexType>
</xs:element>

<xs:element name="rev" type="PatchDescription" minOccurs="0"/>

<xs:element name="relatedProjects" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="project" type="RelatedProject" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>
</xs:element>

<xs:complexType name="PatchDescription">
<xs:all>
<xs:element name="describeTagPattern" type="xs:string" minOccurs="0"/>
<xs:element name="describeTagFirstParent" type="xs:boolean" minOccurs="0"/>
<xs:element name="updatePom" type="xs:boolean" minOccurs="0"/>

<xs:element name="version" type="xs:string" minOccurs="0"/>
<xs:element name="properties" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:complexType>

<xs:complexType name="RefPatchDescription">
<xs:all>
<xs:element name="pattern" type="xs:string" minOccurs="0"/>
<xs:element name="describeTagPattern" type="xs:string" minOccurs="0"/>
<xs:element name="describeTagMaxDepth" type="xs:int" minOccurs="0"/>
<xs:element name="describeTagFirstParent" type="xs:boolean" minOccurs="0"/>
<xs:element name="updatePom" type="xs:boolean" minOccurs="0"/>

<xs:element name="version" type="xs:string" minOccurs="0"/>
<xs:element name="properties" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>

<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="tag"/>
<xs:enumeration value="branch"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>

<xs:complexType name="RelatedProject">
<xs:all>
<xs:element name="groupId" type="xs:string"/>
<xs:element name="artifactId" type="xs:string"/>
</xs:all>
</xs:complexType>

</xs:schema>
39 changes: 18 additions & 21 deletions docs/example-configuration.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>

<configuration xmlns="https://github.com/qoomon/maven-git-versioning-extension" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/qoomon/maven-git-versioning-extension https://qoomon.github.io/maven-git-versioning-extension/configuration-6.4.0.xsd">
xsi:schemaLocation="https://github.com/qoomon/maven-git-versioning-extension https://qoomon.github.io/maven-git-versioning-extension/configuration-9.8.2.xsd">

<disable>false</disable>
<updatePom>false</updatePom>
<preferTags>false</preferTags>
<describeTagMaxDepth>100</describeTagMaxDepth>

<branch>
<pattern>.*</pattern>
<versionFormat>${branch}-SNAPSHOT</versionFormat>
<property>
<name>name</name>
<valueFormat>value</valueFormat>
</property>
<property>
<name>name</name>
<valueFormat>value</valueFormat>
</property>
</branch>
<refs considerTagsOnBranches="true">
<ref type="tag">
<pattern>v(.*)</pattern>
<version>${ref.1}</version>
</ref>

<tag>
<pattern>v(.*)</pattern>
<versionFormat>${1}</versionFormat>
</tag>
<ref type="branch">
<pattern>.*</pattern>
<version>${ref}</version>
<properties>
<name>value</name>
</properties>
</ref>
</refs>

<commit>
<versionFormat>${commit}</versionFormat>
</commit>
<rev>
<version>${commit}</version>
</rev>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ public class GitDescription {
private final String commit;
private final String tag;
private final int distance;
private final boolean tagFound;

public GitDescription(String commit, String tag, int distance) {
public GitDescription(String commit, String tag, int distance, boolean tagFound) {
this.commit = commit;
this.tag = tag;
this.distance = distance;
this.tagFound = tagFound;
}

public String getCommit() {
Expand All @@ -19,10 +21,18 @@ public String getTag() {
return tag;
}

public boolean isTagFound() {
return tagFound;
}

public int getDistance() {
return distance;
}

public int getDistanceOrZero() {
return tagFound ? distance : 0;
}

@Override
public String toString() {
return tag + "-" + distance + "-g" + commit.substring(0,7);
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/me/qoomon/gitversioning/commons/GitSituation.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class GitSituation {

private boolean firstParent = true;

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

public GitSituation(Repository repository) throws IOException {
this.repository = repository;
Expand Down Expand Up @@ -121,9 +121,9 @@ public boolean isClean() {
return clean.get();
}

public void setDescribeTagPattern(Pattern describeTagPattern) {
public void setDescribeTagPattern(Pattern describeTagPattern, Integer describeTagMaxDepth) {
this.describeTagPattern = requireNonNull(describeTagPattern);
this.description = Lazy.by(this::describe);
this.description = Lazy.by(() -> this.describe(describeTagMaxDepth));
}

public Pattern getDescribeTagPattern() {
Expand Down Expand Up @@ -162,7 +162,7 @@ private boolean clean() throws GitAPIException {
return GitUtil.status(repository).isClean();
}

private GitDescription describe() throws IOException {
return GitUtil.describe(head, describeTagPattern, repository, firstParent);
private GitDescription describe(Integer maxDepth) throws IOException {
return GitUtil.describe(head, describeTagPattern, repository, firstParent, maxDepth);
}
}
29 changes: 19 additions & 10 deletions src/main/java/me/qoomon/gitversioning/commons/GitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.DepthWalk;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;

import java.io.File;
Expand Down Expand Up @@ -50,38 +53,44 @@ 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, boolean firstParent) throws IOException {
public static GitDescription describe(ObjectId revObjectId, Pattern tagPattern, Repository repository, boolean firstParent, Integer maxDepth) throws IOException {
Repository commonRepository = worktreesFix_getCommonRepository(repository);
if (revObjectId == null) {
return new GitDescription(NO_COMMIT, "root", 0);
return new GitDescription(NO_COMMIT, "root", 0, false);
}

Map<ObjectId, List<String>> objectIdListMap = reverseTagRefMap(repository);

final int positiveMaxDepth = maxDepth == null || maxDepth < 0 ? Integer.MAX_VALUE : maxDepth;
// Walk back commit ancestors looking for tagged one
try (RevWalk walk = new RevWalk(commonRepository)) {
try (DepthWalk.RevWalk walk = new DepthWalk.RevWalk(commonRepository, positiveMaxDepth)) {
walk.setRetainBody(false);
walk.setFirstParent(firstParent);
walk.markStart(walk.parseCommit(revObjectId));
final RevCommit startCommit = walk.parseCommit(revObjectId);
walk.markRoot(startCommit);
Iterator<RevCommit> walkIterator = walk.iterator();
int depth = 0;
int maxVisitedDepth = 0;
while (walkIterator.hasNext()) {
RevCommit rev = walkIterator.next();
int depth = 0;
if (rev instanceof DepthWalk.Commit) {
depth = ((DepthWalk.Commit) rev).getDepth();
}
maxVisitedDepth = Math.max(maxVisitedDepth, depth);
Optional<String> matchingTag = objectIdListMap.getOrDefault(rev, emptyList()).stream()
.filter(tag -> tagPattern.matcher(tag).matches())
.findFirst();

if (matchingTag.isPresent()) {
return new GitDescription(revObjectId.getName(), matchingTag.get(), depth);
return new GitDescription(revObjectId.getName(), matchingTag.get(), depth, true);
}
depth++;
}

if (isShallowRepository(repository)) {
throw new IllegalStateException("couldn't find matching tag in shallow git repository");
}

return new GitDescription(revObjectId.getName(), "root", depth);
return new GitDescription(revObjectId.getName(), "root", maxVisitedDepth, false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public Pattern describeTagPattern() {
return Pattern.compile(describeTagPattern);
}

public Integer describeTagMaxDepth = Integer.MAX_VALUE;

public Boolean updatePom = false;

public RefPatchDescriptionList refs = new RefPatchDescriptionList();
Expand All @@ -79,6 +81,8 @@ public Pattern describeTagPattern() {
return Pattern.compile(describeTagPattern);
}

public Integer describeTagMaxDepth;

@JsonDeserialize(using = IgnoreWhitespaceDeserializer.class)
public String version;

Expand Down Expand Up @@ -120,6 +124,7 @@ public RefPatchDescription(GitRefType type, Pattern pattern, PatchDescription de
this.type = type;
this.pattern = pattern != null ? pattern.pattern() : null;
this.describeTagPattern = description.describeTagPattern;
this.describeTagMaxDepth = description.describeTagMaxDepth;
this.updatePom = description.updatePom;
this.describeTagFirstParent = description.describeTagFirstParent;
this.version = description.version;
Expand Down
Loading