Skip to content

Commit

Permalink
fix: ignore ci/cd environment if head SHA hash is not matching enviro…
Browse files Browse the repository at this point in the history
…nment variable commit hash
  • Loading branch information
qoomon committed Dec 5, 2022
1 parent 29569ef commit 256cd6f
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 116 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
##### Features
- add visible logging for relevant output

##### Fixes
- ignore ci/cd environment if head SHA hash is not matching environment variable commit hash


## 6.3.6

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ e.g `${dirty:-SNAPSHOT}` resolves to `-SNAPSHOT` instead of `-DIRTY`
- **Command Line Parameters**
- `gradle … -Dversioning.disable`
- Provide **branch** or **tag** name
- Set **branch** or Add **tag** by environment variable
- **Environment Variables**
- `export VERSIONING_GIT_REF=$PROVIDED_REF` e.g. `refs/heads/main`, `refs/tags/v1.0.0` or `refs/pull/1000/head`
- `export VERSIONING_GIT_BRANCH=$PROVIDED_BRANCH_NAME` e.g. `main` or `refs/heads/main`
Expand Down
52 changes: 48 additions & 4 deletions src/main/java/me/qoomon/gitversioning/commons/GitSituation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
import java.io.IOException;
import java.nio.file.Files;
import java.time.ZonedDateTime;
import java.util.ArrayList;
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;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static me.qoomon.gitversioning.commons.GitUtil.NO_COMMIT;
import static org.eclipse.jgit.lib.Constants.HEAD;

Expand Down Expand Up @@ -80,8 +83,19 @@ public String getBranch() {
return branch.get();
}

public void setBranch(String branch) {
this.branch = () -> branch;
protected void setBranch(String branch) {
if (branch != null) {
if (branch.startsWith("refs/tags/")) {
throw new IllegalArgumentException("invalid branch ref" + branch);
}
branch = branch
// support default branches (heads)
.replaceFirst("^refs/heads/", "")
// support other refs e.g. GitHub pull requests refs/pull/1000/head
.replaceFirst("^refs/", "");
}
final String finalBranch = branch;
this.branch = () -> finalBranch;
}

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

public void setTags(List<String> tags) {
this.tags = () -> requireNonNull(tags);
protected void addTag(String tag) {
requireNonNull(tag);

if (tag.startsWith("refs/") && !tag.startsWith("refs/tags/")) {
throw new IllegalArgumentException("invalid tag ref" + tag);
}

final String finalTag = tag.replaceFirst("^refs/tags/", "");

final Supplier<List<String>> currentTags = this.tags;
this.tags = Lazy.by(() -> {
List<String> tags = new ArrayList<>(currentTags.get());
tags.add(finalTag);
return tags;
});
}

protected void setTags(List<String> tags) {
requireNonNull(tags);
tags.forEach(tag -> {
requireNonNull(tag);
if (tag.startsWith("refs/") && !tag.startsWith("refs/tags/")) {
throw new IllegalArgumentException("invalid tag ref" + tag);
}
});

tags = tags.stream()
.map(tag -> tag.replaceFirst("^refs/tags/", ""))
.collect(toList());

final List<String> finalTags = tags;
this.tags = () -> finalTags;
}

public boolean isClean() {
Expand Down
Loading

0 comments on commit 256cd6f

Please sign in to comment.