Skip to content

Commit

Permalink
Fallback to recent committer if identity not available
Browse files Browse the repository at this point in the history
  • Loading branch information
ajoberstar committed Apr 22, 2023
1 parent 8c580fe commit b911921
Showing 1 changed file with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@ public void create() {
return;
}

tag(null, null, true);
}

@Inject
protected abstract ExecOperations getExecOperations();

@Internal
public abstract DirectoryProperty getRepoDirectory();

@Input
@Optional
public abstract Property<String> getTagName();

@Input
public abstract Property<String> getTagMessage();

private void tag(String userEmail, String userName, boolean allowRetry) {
var input = new ByteArrayInputStream(getTagMessage().get().getBytes(StandardCharsets.UTF_8));
var output = new ByteArrayOutputStream();
var error = new ByteArrayOutputStream();
Expand All @@ -30,11 +47,11 @@ public void create() {
cmd.add("git");

// provide a fallback for CI-like environments that may not have an identity set
if (!(hasConfig("user.email") && hasConfig("user.name"))) {
if (userEmail != null && userName != null) {
cmd.add("-c");
cmd.add("user-email=reckon@ajoberstar.org");
cmd.add("user-email=" + userEmail);
cmd.add("-c");
cmd.add("user.name=reckon");
cmd.add("user.name=" + userName);
}

cmd.add("tag");
Expand All @@ -59,37 +76,35 @@ public void create() {
var errorStr = error.toString(StandardCharsets.UTF_8);
if (errorStr.contains(String.format("fatal: tag '%s' already exists", getTagName().get()))) {
setDidWork(false);
} else if (allowRetry && errorStr.contains(String.format("Committer identity unknown"))) {
var email = getRecentUserEmail();
var name = getRecentUserName();
System.err.println(String.format("Tagging as recent committer %s <%s>, as this machine has no git identity set.", name, email));
tag(email, name, false);
} else {
System.err.println(errorStr);
result.assertNormalExitValue();
}
}
}

@Inject
protected abstract ExecOperations getExecOperations();

@Internal
public abstract DirectoryProperty getRepoDirectory();

@Input
@Optional
public abstract Property<String> getTagName();

@Input
public abstract Property<String> getTagMessage();

private boolean hasConfig(String key) {
private String getRecentUserEmail() {
var output = new ByteArrayOutputStream();
var error = new ByteArrayOutputStream();
var result = getExecOperations().exec(spec -> {
spec.setWorkingDir(getRepoDirectory());
spec.setCommandLine("git", "config", "--get", key);
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%ae");
spec.setStandardOutput(output);
spec.setErrorOutput(error);
spec.setIgnoreExitValue(true);
});
return output.toString();
}

return result.getExitValue() == 0;
private String getRecentUserName() {
var output = new ByteArrayOutputStream();
var result = getExecOperations().exec(spec -> {
spec.setWorkingDir(getRepoDirectory());
spec.setCommandLine("git", "log", "-n", "1", "--pretty=format:%an");
spec.setStandardOutput(output);
});
return output.toString();
}
}

0 comments on commit b911921

Please sign in to comment.