This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-scm.git
The following commit(s) were added to refs/heads/master by this push:
new 558ff8cc2 [SCM-848] Add an option to create lightweight git tags
558ff8cc2 is described below
commit 558ff8cc2b92b0000fd7b6ab666e0deb8e43c449
Author: Sylwester Lachiewicz <[email protected]>
AuthorDate: Tue Nov 17 02:09:08 2026 +0100
[SCM-848] Add an option to create lightweight git tags
ScmTagParameters.setLightweight(true) makes the gitexe and jgit providers
create a lightweight tag, and the scm:tag goal exposes it as the
lightweight parameter. A lightweight tag has neither message nor
signature, so both are ignored, and gitexe rejects a forced signature.
gitexe always passes --no-sign: with tag.gpgSign=true a bare git tag
demands a message ("fatal: no tag message?"). The default stays an
annotated tag.
Fixes #1073
---
.../org/apache/maven/scm/ScmTagParameters.java | 29 +++++++-
.../java/org/apache/maven/scm/plugin/TagMojo.java | 11 +++
.../git/gitexe/command/tag/GitTagCommand.java | 79 +++++++++++++++-------
.../git/gitexe/command/tag/GitTagCommandTest.java | 25 ++++++-
.../git/command/tag/GitTagCommandTckTest.java | 47 +++++++++++++
.../git/jgit/command/tag/JGitTagCommand.java | 15 ++--
6 files changed, 172 insertions(+), 34 deletions(-)
diff --git
a/maven-scm-api/src/main/java/org/apache/maven/scm/ScmTagParameters.java
b/maven-scm-api/src/main/java/org/apache/maven/scm/ScmTagParameters.java
index f9a101ef9..d800075c6 100644
--- a/maven-scm-api/src/main/java/org/apache/maven/scm/ScmTagParameters.java
+++ b/maven-scm-api/src/main/java/org/apache/maven/scm/ScmTagParameters.java
@@ -37,6 +37,8 @@ public class ScmTagParameters implements Serializable {
private String scmRevision;
+ private boolean lightweight = false;
+
public ScmTagParameters() {
this.remoteTagging = false;
this.pinExternals = false;
@@ -120,13 +122,38 @@ public void setSignOption(CommandParameters.SignOption
signOption) {
this.signOption = signOption;
}
+ /**
+ * Tells whether a lightweight tag (a plain reference without tag object,
message or signature) is requested.
+ * Only applicable to SCMs that distinguish lightweight and annotated tags
(Git).
+ *
+ * @return {@code true} for a lightweight tag, {@code false} (the default)
for an annotated one
+ * @since 2.3.0
+ */
+ public boolean isLightweight() {
+ return lightweight;
+ }
+
+ /**
+ * Sets whether a lightweight tag should be created instead of an
annotated one. A lightweight tag carries
+ * neither a message nor a signature, so both are ignored. A lightweight
tag is never signed, even if the Git
+ * configuration asks for signed tags: the gitexe provider rejects {@code
FORCE_SIGN} and the jgit provider
+ * ignores the sign option.
+ *
+ * @param lightweight {@code true} for a lightweight tag
+ * @since 2.3.0
+ */
+ public void setLightweight(boolean lightweight) {
+ this.lightweight = lightweight;
+ }
+
@Override
public String toString() {
return "ScmTagParameters{" + "message='"
+ message + '\'' + ", remoteTagging="
+ remoteTagging + ", pinExternals="
+ pinExternals + ", signOption="
- + signOption + ", scmRevision='"
+ + signOption + ", lightweight="
+ + lightweight + ", scmRevision='"
+ scmRevision + '\'' + '}';
}
}
diff --git
a/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/TagMojo.java
b/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/TagMojo.java
index 988aa1cbf..066dc5e94 100644
--- a/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/TagMojo.java
+++ b/maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/TagMojo.java
@@ -117,6 +117,16 @@ public class TagMojo extends AbstractScmMojo {
@Parameter(property = "signOption")
private SignOption signOption = SignOption.DEFAULT;
+ /**
+ * Creates a lightweight tag instead of an annotated one (only applicable
to Git). A lightweight tag has neither
+ * a message nor a signature, so {@link #message} and {@link #signOption}
are ignored; combining it with
+ * {@link SignOption#FORCE_SIGN} fails.
+ *
+ * @since 2.3.0
+ */
+ @Parameter(property = "lightweight", defaultValue = "false")
+ private boolean lightweight;
+
@Inject
public TagMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
super(manager, settingsDecrypter);
@@ -160,6 +170,7 @@ public void execute() throws MojoExecutionException {
ScmTagParameters scmTagParameters = new ScmTagParameters(message);
scmTagParameters.setRemoteTagging(remoteTagging);
scmTagParameters.setPinExternals(pinExternals);
+ scmTagParameters.setLightweight(lightweight);
if (signOption != null) {
scmTagParameters.setSignOption(signOption);
} else if (sign) {
diff --git
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommand.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommand.java
index bec5fb17a..689e364c0 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommand.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommand.java
@@ -72,16 +72,24 @@ public ScmResult executeTagCommand(
GitScmProviderRepository repository = (GitScmProviderRepository) repo;
- File messageFile = FileUtils.createTempFile("maven-scm-", ".commit",
null);
+ boolean lightweight = scmTagParameters.isLightweight();
+ if (lightweight && scmTagParameters.getSignOption() ==
SignOption.FORCE_SIGN) {
+ throw new ScmException("A lightweight tag cannot be signed");
+ }
- try {
- FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8",
scmTagParameters.getMessage());
- } catch (IOException ex) {
- return new TagScmResult(
- null,
- "Error while making a temporary file for the commit
message: " + ex.getMessage(),
- null,
- false);
+ File messageFile = null;
+ if (!lightweight) {
+ messageFile = FileUtils.createTempFile("maven-scm-", ".commit",
null);
+
+ try {
+ FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8",
scmTagParameters.getMessage());
+ } catch (IOException ex) {
+ return new TagScmResult(
+ null,
+ "Error while making a temporary file for the commit
message: " + ex.getMessage(),
+ null,
+ false);
+ }
}
try {
@@ -91,7 +99,7 @@ public ScmResult executeTagCommand(
int exitCode;
Commandline clTag = createCommandLine(
- repository, fileSet.getBasedir(), tag, messageFile,
scmTagParameters.getSignOption());
+ repository, fileSet.getBasedir(), tag, messageFile,
scmTagParameters.getSignOption(), lightweight);
exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr);
if (exitCode != 0) {
@@ -122,10 +130,12 @@ public ScmResult executeTagCommand(
return new TagScmResult(clTag.toString(),
listConsumer.getListedFiles());
} finally {
- try {
- FileUtils.forceDelete(messageFile);
- } catch (IOException ex) {
- // ignore
+ if (messageFile != null) {
+ try {
+ FileUtils.forceDelete(messageFile);
+ } catch (IOException ex) {
+ // ignore
+ }
}
}
}
@@ -140,22 +150,39 @@ static Commandline createCommandLine(
String tag,
File messageFile,
SignOption signOption) {
+ return createCommandLine(repository, workingDirectory, tag,
messageFile, signOption, false);
+ }
+
+ static Commandline createCommandLine(
+ GitScmProviderRepository repository,
+ File workingDirectory,
+ String tag,
+ File messageFile,
+ SignOption signOption,
+ boolean lightweight) {
Commandline cl =
GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag");
- switch (signOption) {
- case FORCE_SIGN:
- cl.createArg().setValue("-s");
- break;
- case FORCE_NO_SIGN:
- cl.createArg().setValue("--no-sign");
- break;
- default:
- // no CLI argument needed when default option from git config
should be used
- break;
+ if (lightweight) {
+ // with tag.gpgSign=true git would otherwise ask for a tag message
+ cl.createArg().setValue("--no-sign");
+ } else {
+ switch (signOption) {
+ case FORCE_SIGN:
+ cl.createArg().setValue("-s");
+ break;
+ case FORCE_NO_SIGN:
+ cl.createArg().setValue("--no-sign");
+ break;
+ default:
+ // no CLI argument needed when default option from git
config should be used
+ break;
+ }
}
- cl.createArg().setValue("-F");
- cl.createArg().setValue(messageFile.getAbsolutePath());
+ if (!lightweight) {
+ cl.createArg().setValue("-F");
+ cl.createArg().setValue(messageFile.getAbsolutePath());
+ }
// Note: this currently assumes you have the tag base checked out too
cl.createArg().setValue(tag);
diff --git
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommandTest.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommandTest.java
index fa3bd26ab..eceef249f 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommandTest.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/test/java/org/apache/maven/scm/provider/git/gitexe/command/tag/GitTagCommandTest.java
@@ -83,19 +83,42 @@ void testCommandLineWithUsernameAndTagAndSign() throws
Exception {
SignOption.FORCE_SIGN);
}
+ @Test
+ void testCommandLineLightweightTag() throws Exception {
+ testCommandLine(
+ "scm:git:http://foo.com/git/trunk", "my-tag-1", "git tag
--no-sign my-tag-1", SignOption.DEFAULT, true);
+ }
+
+ @Test
+ void testCommandLineLightweightTagForceNoSign() throws Exception {
+ testCommandLine(
+ "scm:git:http://foo.com/git/trunk",
+ "my-tag-1",
+ "git tag --no-sign my-tag-1",
+ SignOption.FORCE_NO_SIGN,
+ true);
+ }
+
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
private void testCommandLine(String scmUrl, String tag, String
commandLine, SignOption signOption)
throws Exception {
+ testCommandLine(scmUrl, tag, commandLine, signOption, false);
+ }
+
+ private void testCommandLine(
+ String scmUrl, String tag, String commandLine, SignOption
signOption, boolean lightweight)
+ throws Exception {
File workingDirectory = getTestFile("target/git-checkin-command-test");
ScmRepository repository = getScmManager().makeScmRepository(scmUrl);
GitScmProviderRepository gitRepository = (GitScmProviderRepository)
repository.getProviderRepository();
- Commandline cl = GitTagCommand.createCommandLine(gitRepository,
workingDirectory, tag, messageFile, signOption);
+ Commandline cl = GitTagCommand.createCommandLine(
+ gitRepository, workingDirectory, tag, messageFile, signOption,
lightweight);
assertCommandLine(commandLine, workingDirectory, cl);
}
diff --git
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/tag/GitTagCommandTckTest.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/tag/GitTagCommandTckTest.java
index 65491a1d3..dcd38cc4b 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/tag/GitTagCommandTckTest.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gittest/src/main/java/org/apache/maven/scm/provider/git/command/tag/GitTagCommandTckTest.java
@@ -18,9 +18,13 @@
*/
package org.apache.maven.scm.provider.git.command.tag;
+import java.io.BufferedReader;
import java.io.File;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
import org.apache.maven.scm.ScmFileSet;
+import org.apache.maven.scm.ScmTagParameters;
import org.apache.maven.scm.command.checkout.CheckOutScmResult;
import org.apache.maven.scm.command.tag.TagScmResult;
import org.apache.maven.scm.provider.git.GitScmTestUtils;
@@ -28,6 +32,7 @@
import org.apache.maven.scm.tck.command.tag.TagCommandTckTest;
import org.junit.jupiter.api.Test;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
@@ -62,4 +67,46 @@ void testPushTagRejected() throws Exception {
assertFalse(tagResult.isSuccess(), "Tag should not have been pushed");
}
+
+ @Test
+ void testAnnotatedTagIsTheDefault() throws Exception {
+ ScmTagParameters parameters = new ScmTagParameters();
+ parameters.setMessage("annotated message");
+
+ tag("annotated-tag", parameters);
+
+ assertEquals("tag", gitObjectType("annotated-tag"));
+ }
+
+ @Test
+ void testLightweightTag() throws Exception {
+ ScmTagParameters parameters = new ScmTagParameters();
+ parameters.setMessage("ignored message");
+ parameters.setLightweight(true);
+
+ tag("lightweight-tag", parameters);
+
+ assertEquals("commit", gitObjectType("lightweight-tag"));
+ }
+
+ private void tag(String tag, ScmTagParameters parameters) throws Exception
{
+ TagScmResult tagResult = getScmManager()
+ .getProviderByUrl(getScmUrl())
+ .tag(getScmRepository(), new ScmFileSet(getWorkingCopy()),
tag, parameters);
+
+ assertResultIsSuccess(tagResult);
+ }
+
+ private String gitObjectType(String tag) throws Exception {
+ Process process = new ProcessBuilder("git", "cat-file", "-t",
"refs/tags/" + tag)
+ .directory(getWorkingCopy())
+ .redirectErrorStream(true)
+ .start();
+ try (BufferedReader reader =
+ new BufferedReader(new
InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
+ String type = reader.readLine();
+ process.waitFor();
+ return type;
+ }
+ }
}
diff --git
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/tag/JGitTagCommand.java
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/tag/JGitTagCommand.java
index a839f461b..679019b9e 100644
---
a/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/tag/JGitTagCommand.java
+++
b/maven-scm-providers/maven-scm-providers-git/maven-scm-provider-jgit/src/main/java/org/apache/maven/scm/provider/git/jgit/command/tag/JGitTagCommand.java
@@ -42,6 +42,7 @@
import
org.apache.maven.scm.provider.git.jgit.command.ScmProviderAwareSshdSessionFactory;
import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.TagCommand;
import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants;
@@ -99,12 +100,14 @@ public ScmResult executeTagCommand(
git = JGitUtils.openRepo(fileSet.getBasedir());
// tag the revision
- String tagMessage = scmTagParameters.getMessage();
- Ref tagRef = git.tag()
- .setName(escapedTagName)
- .setMessage(tagMessage)
- .setForceUpdate(false)
- .call();
+ TagCommand tagCommand =
git.tag().setName(escapedTagName).setForceUpdate(false);
+ if (scmTagParameters.isLightweight()) {
+ // JGit rejects a message on an unannotated tag
+ tagCommand.setAnnotated(false);
+ } else {
+ tagCommand.setMessage(scmTagParameters.getMessage());
+ }
+ Ref tagRef = tagCommand.call();
if (repo.isPushChanges()) {
TransportConfigCallback transportConfigCallback = new
JGitTransportConfigCallback(