Skip to content

Commit af3c6a6

Browse files
committed
feat: add git describe placeholers
1 parent 0487d61 commit af3c6a6

File tree

13 files changed

+601
-410
lines changed

13 files changed

+601
-410
lines changed

CHANGELOG.md

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

3+
## 6.3.0 (unreleased)
4+
* **Features**
5+
* add git describe version placeholders
6+
enable describe feature by set config `describeTagPattern` at root level or at branch/tag config level
7+
* new placeholders
8+
* `${describe}`
9+
* `${describe.tag}`
10+
* `${describe.<TAG_PATTERN_GROUP_NAME or TAG_PATTERN_GROUP_INDEX>}`
11+
* `${describe.distance}`
12+
13+
314
## 4.1.0
415
* **Features**
516
* add ability to define default or overwrite values for version and property format.

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 '4.2.1'
15+
version '4.3.0'
1616

1717
sourceCompatibility = JavaVersion.VERSION_1_8
1818
targetCompatibility = JavaVersion.VERSION_1_8
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package me.qoomon.gitversioning.commons;
2+
3+
public class GitDescription {
4+
private final String commit;
5+
private final String tag;
6+
private final int distance;
7+
8+
public GitDescription(String commit, String tag, int distance) {
9+
this.commit = commit;
10+
this.tag = tag;
11+
this.distance = distance;
12+
}
13+
14+
public String getCommit() {
15+
return commit;
16+
}
17+
18+
public String getTag() {
19+
return tag;
20+
}
21+
22+
public int getDistance() {
23+
return distance;
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return tag + "-" + distance + "-g" + commit.substring(0,7);
29+
}
30+
}
Lines changed: 83 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,118 @@
11
package me.qoomon.gitversioning.commons;
22

3+
import org.eclipse.jgit.lib.ObjectId;
4+
import org.eclipse.jgit.lib.Ref;
5+
import org.eclipse.jgit.lib.Repository;
36

47
import java.io.File;
5-
import java.time.Instant;
8+
import java.io.IOException;
69
import java.time.ZonedDateTime;
710
import java.util.List;
11+
import java.util.Map;
12+
import java.util.regex.Pattern;
813

14+
import static java.time.Instant.EPOCH;
915
import static java.time.ZoneOffset.UTC;
1016
import static java.util.Collections.emptyList;
11-
import static java.util.Objects.requireNonNull;
17+
import static me.qoomon.gitversioning.commons.GitUtil.NO_COMMIT;
18+
import static org.eclipse.jgit.lib.Constants.HEAD;
1219

20+
//TODO rename to GitSituation
1321
public class GitSituation {
1422

15-
public static String NO_COMMIT = "0000000000000000000000000000000000000000";
16-
23+
private final Repository repository;
1724
private final File rootDirectory;
18-
private final String headCommit;
19-
private final long headCommitTimestamp;
20-
private final String headBranch;
21-
private final List<String> headTags;
22-
private final boolean clean;
2325

24-
public GitSituation(File rootDirectory, String headCommit, long headCommitTimestamp, String headBranch, List<String> headTags, boolean clean) {
25-
this.rootDirectory = requireNonNull(rootDirectory);
26-
this.headCommit = requireNonNull(headCommit);
27-
this.headCommitTimestamp = headCommitTimestamp;
28-
this.headBranch = headBranch;
29-
this.headTags = requireNonNull(headTags);
30-
this.clean = clean;
26+
private final ObjectId head;
27+
private final String hash;
28+
private final Lazy<ZonedDateTime> timestamp = Lazy.of(this::timestamp);
29+
private Lazy<String> branch = Lazy.of(this::branch);
30+
31+
private final Lazy<Map<ObjectId, List<Ref>>> reverseTagRefMap = Lazy.of(this::reverseTagRefMap);
32+
private Lazy<List<String>> tags = Lazy.of(this::tags);
33+
34+
private final Lazy<Boolean> clean = Lazy.of(this::clean);
35+
36+
private final Pattern describeTagPattern;
37+
private final Lazy<GitDescription> description = Lazy.of(this::describe);
38+
39+
public GitSituation(Repository repository, Pattern describeTagPattern) throws IOException {
40+
this.repository = repository;
41+
this.rootDirectory = repository.getWorkTree();
42+
this.head = repository.resolve(HEAD);
43+
this.hash = head != null ? head.getName() : NO_COMMIT;
44+
this.describeTagPattern = describeTagPattern != null ? describeTagPattern : Pattern.compile(".*");
45+
}
46+
47+
public GitSituation(Repository repository) throws IOException {
48+
this(repository, null);
3149
}
3250

3351
public File getRootDirectory() {
3452
return rootDirectory;
3553
}
3654

37-
public String getHeadCommit() {
38-
return headCommit;
55+
public String getHash() {
56+
return hash;
3957
}
4058

41-
public long getHeadCommitTimestamp() {
42-
return headCommitTimestamp;
59+
public ZonedDateTime getTimestamp() {
60+
return timestamp.get();
4361
}
4462

45-
public ZonedDateTime getHeadCommitDateTime() {
46-
Instant headCommitTimestamp = Instant.ofEpochSecond(getHeadCommitTimestamp());
47-
return ZonedDateTime.ofInstant(headCommitTimestamp, UTC);
63+
public String getBranch() {
64+
return branch.get();
4865
}
4966

50-
public String getHeadBranch() {
51-
return headBranch;
67+
public void setBranch(String branch) {
68+
this.branch = Lazy.of(branch);
5269
}
5370

54-
public List<String> getHeadTags() {
55-
return headTags;
71+
public boolean isDetached() {
72+
return branch.get() == null;
73+
}
74+
75+
public List<String> getTags() {
76+
return tags.get();
77+
}
78+
79+
public void setTags(List<String> tags) {
80+
this.tags = Lazy.of(tags);
5681
}
5782

5883
public boolean isClean() {
59-
return clean;
84+
return clean.get();
6085
}
6186

62-
public boolean isDetached() {
63-
return headBranch == null;
64-
}
65-
66-
public static class Builder {
67-
private File rootDirectory;
68-
private String headCommit = NO_COMMIT;
69-
private long headCommitTimestamp = 0;
70-
private String headBranch = null;
71-
private List<String> headTags = emptyList();
72-
private boolean clean = true;
73-
74-
75-
public Builder setRootDirectory(File rootDirectory) {
76-
this.rootDirectory = rootDirectory;
77-
return this;
78-
}
79-
80-
public Builder setHeadCommit(String headCommit) {
81-
this.headCommit = headCommit;
82-
return this;
83-
}
84-
85-
public Builder setHeadCommitTimestamp(long headCommitTimestamp) {
86-
this.headCommitTimestamp = headCommitTimestamp;
87-
return this;
88-
}
89-
90-
public Builder setHeadBranch(String headBranch) {
91-
this.headBranch = headBranch;
92-
return this;
93-
}
94-
95-
public Builder setHeadTags(List<String> headTags) {
96-
this.headTags = headTags;
97-
return this;
98-
}
99-
100-
public Builder setClean(boolean clean) {
101-
this.clean = clean;
102-
return this;
103-
}
104-
105-
public GitSituation build() {
106-
return new GitSituation(rootDirectory, headCommit, headCommitTimestamp, headBranch, headTags, clean);
107-
}
108-
109-
public static Builder of(GitSituation gitSituation) {
110-
return new Builder()
111-
.setRootDirectory(gitSituation.rootDirectory)
112-
.setClean(gitSituation.clean)
113-
.setHeadCommit(gitSituation.headCommit)
114-
.setHeadCommitTimestamp(gitSituation.headCommitTimestamp)
115-
.setHeadBranch(gitSituation.headBranch)
116-
.setHeadTags(gitSituation.headTags);
117-
}
87+
public GitDescription getDescription() {
88+
return description.get();
89+
}
90+
91+
// ----- initialization methods ------------------------------------------------------------------------------------
92+
93+
private ZonedDateTime timestamp() throws IOException {
94+
return head != null
95+
? GitUtil.revTimestamp(repository, head)
96+
: ZonedDateTime.ofInstant(EPOCH, UTC);
97+
}
98+
99+
private String branch() throws IOException {
100+
return GitUtil.branch(repository);
101+
}
102+
103+
private List<String> tags() {
104+
return head != null ? GitUtil.tagsPointAt(repository, head, reverseTagRefMap.get()) : emptyList();
105+
}
106+
107+
private boolean clean() {
108+
return GitUtil.status(repository).isClean();
109+
}
110+
111+
private GitDescription describe() throws IOException {
112+
return GitUtil.describe(repository, head, describeTagPattern, reverseTagRefMap.get());
113+
}
114+
115+
private Map<ObjectId, List<Ref>> reverseTagRefMap() throws IOException {
116+
return GitUtil.reverseTagRefMap(repository);
118117
}
119118
}

0 commit comments

Comments
 (0)