Skip to content

Commit 256cd6f

Browse files
committed
fix: ignore ci/cd environment if head SHA hash is not matching environment variable commit hash
1 parent 29569ef commit 256cd6f

File tree

4 files changed

+169
-116
lines changed

4 files changed

+169
-116
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
##### Features
66
- add visible logging for relevant output
77

8+
##### Fixes
9+
- ignore ci/cd environment if head SHA hash is not matching environment variable commit hash
10+
811

912
## 6.3.6
1013

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
294294
- **Command Line Parameters**
295295
- `gradle … -Dversioning.disable`
296296
297-
- Provide **branch** or **tag** name
297+
- Set **branch** or Add **tag** by environment variable
298298
- **Environment Variables**
299299
- `export VERSIONING_GIT_REF=$PROVIDED_REF` e.g. `refs/heads/main`, `refs/tags/v1.0.0` or `refs/pull/1000/head`
300300
- `export VERSIONING_GIT_BRANCH=$PROVIDED_BRANCH_NAME` e.g. `main` or `refs/heads/main`

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@
99
import java.io.IOException;
1010
import java.nio.file.Files;
1111
import java.time.ZonedDateTime;
12+
import java.util.ArrayList;
1213
import java.util.List;
1314
import java.util.function.Supplier;
1415
import java.util.regex.Pattern;
16+
import java.util.stream.Collectors;
1517

1618
import static java.time.Instant.EPOCH;
1719
import static java.time.ZoneOffset.UTC;
1820
import static java.util.Collections.emptyList;
1921
import static java.util.Objects.requireNonNull;
22+
import static java.util.stream.Collectors.toList;
2023
import static me.qoomon.gitversioning.commons.GitUtil.NO_COMMIT;
2124
import static org.eclipse.jgit.lib.Constants.HEAD;
2225

@@ -80,8 +83,19 @@ public String getBranch() {
8083
return branch.get();
8184
}
8285

83-
public void setBranch(String branch) {
84-
this.branch = () -> branch;
86+
protected void setBranch(String branch) {
87+
if (branch != null) {
88+
if (branch.startsWith("refs/tags/")) {
89+
throw new IllegalArgumentException("invalid branch ref" + branch);
90+
}
91+
branch = branch
92+
// support default branches (heads)
93+
.replaceFirst("^refs/heads/", "")
94+
// support other refs e.g. GitHub pull requests refs/pull/1000/head
95+
.replaceFirst("^refs/", "");
96+
}
97+
final String finalBranch = branch;
98+
this.branch = () -> finalBranch;
8599
}
86100

87101
public boolean isDetached() {
@@ -92,8 +106,38 @@ public List<String> getTags() {
92106
return tags.get();
93107
}
94108

95-
public void setTags(List<String> tags) {
96-
this.tags = () -> requireNonNull(tags);
109+
protected void addTag(String tag) {
110+
requireNonNull(tag);
111+
112+
if (tag.startsWith("refs/") && !tag.startsWith("refs/tags/")) {
113+
throw new IllegalArgumentException("invalid tag ref" + tag);
114+
}
115+
116+
final String finalTag = tag.replaceFirst("^refs/tags/", "");
117+
118+
final Supplier<List<String>> currentTags = this.tags;
119+
this.tags = Lazy.by(() -> {
120+
List<String> tags = new ArrayList<>(currentTags.get());
121+
tags.add(finalTag);
122+
return tags;
123+
});
124+
}
125+
126+
protected void setTags(List<String> tags) {
127+
requireNonNull(tags);
128+
tags.forEach(tag -> {
129+
requireNonNull(tag);
130+
if (tag.startsWith("refs/") && !tag.startsWith("refs/tags/")) {
131+
throw new IllegalArgumentException("invalid tag ref" + tag);
132+
}
133+
});
134+
135+
tags = tags.stream()
136+
.map(tag -> tag.replaceFirst("^refs/tags/", ""))
137+
.collect(toList());
138+
139+
final List<String> finalTags = tags;
140+
this.tags = () -> finalTags;
97141
}
98142

99143
public boolean isClean() {

0 commit comments

Comments
 (0)