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 create annotated tags #22

Merged
merged 13 commits into from
May 11, 2021
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ spotlessChangelog { // all defaults
// tag and push
tagPrefix 'release/'
commitMessage 'Published release/{{version}}' // {{version}} will be replaced
annotateMessage '' // default is empty string (creates lightweight tag); {{version}} will be replaced
rzabini marked this conversation as resolved.
Show resolved Hide resolved
remote 'origin'
branch 'main'
// default value is `yes`, but if you set it to `no`, then it will
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.function.Consumer;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.PushCommand;
import org.eclipse.jgit.api.TagCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
Expand Down Expand Up @@ -87,23 +88,29 @@ public void assertNoTag() throws IOException {

/** Adds and commits the changelog. */
public void addAndCommit() throws GitAPIException {
String commitMsg = cfg.commitMessage.replace(GitCfg.COMMIT_MESSAGE_VERSION, model.versions().next());
String path = repository.getWorkTree().toPath().relativize(changelogFile.toPath()).toString();
git.add()
.addFilepattern(path)
.call();
git.commit()
.setMessage(commitMsg)
.setMessage(formatCommitMessage(cfg.commitMessage))
.call();
}

/** Tags and pushes the tag and the branch. */
/** Tags and pushes the tag and the branch. */
public void tagBranchPush() throws GitAPIException {
nedtwigg marked this conversation as resolved.
Show resolved Hide resolved
Ref tagRef = git.tag().setName(tagName()).setAnnotated(false).call();
push(tagRef, RemoteRefUpdate.Status.OK);
final TagCommand tagCommand = git.tag().setName(tagName());
if (!cfg.annotateMessage().isEmpty()) {
tagCommand.setAnnotated(true).setMessage(formatCommitMessage(cfg.annotateMessage()));
}
push(tagCommand.call(), RemoteRefUpdate.Status.OK);
push(cfg.branch, RemoteRefUpdate.Status.OK);
}

private String formatCommitMessage(final String commitMessage) {
return commitMessage.replace(GitCfg.COMMIT_MESSAGE_VERSION, model.versions().next());
}

private String tagName() {
return cfg.tagPrefix + model.versions().next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class GitCfg {
public String tagPrefix = "release/";
/** Message used for release commits, default is `Published release/{{version}}`. */
public String commitMessage = "Published release/" + COMMIT_MESSAGE_VERSION;
private String annotateMessage = "";
public String remote = "origin";
public String branch = "main";
public String sshStrictHostKeyChecking = "yes";
Expand All @@ -43,4 +44,14 @@ public static String validateCommitMessage(String commitMessage) {
}
return commitMessage;
}

/** Sets the essage to annotate release tag: if empty (the default), then a lightweight tag is created`. */
public void annotateMessage(String annotateMessage) {
this.annotateMessage = annotateMessage;
}

/** Gets the essage to annotate release tag: if empty (the default), then a lightweight tag is created`. */
public String annotateMessage() {
return annotateMessage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public void enforceCheck(boolean enforceCheck) {
this.enforceCheck = enforceCheck;
}

/** Determines whether tags are lightweight (false) or annotated (true). Default is false. */
public void annotateMessage(String annotateMessage) {
gitCfg.annotateMessage(GitCfg.validateCommitMessage(annotateMessage));
}

/**
* Sets a custom {@link NextVersionFunction} by calling the public no-arg constructor of the given class.
* Default value is {@link com.diffplug.spotless.changelog.NextVersionFunction.Semver Semver}.
Expand Down