Skip to content

Commit 7030ff2

Browse files
committed
Rework sign option for Git tag/commit
Use tri-state for signing/not-signing/default (from config) Don't support signing with JGit yet (requires additional dependencies) Overwrite global sign settings during test execution This closes #1292
1 parent 566e43a commit 7030ff2

File tree

22 files changed

+191
-93
lines changed

22 files changed

+191
-93
lines changed

maven-scm-api/src/main/java/org/apache/maven/scm/CommandParameter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,12 @@ public class CommandParameter implements Serializable {
9494
public static final CommandParameter IGNORE_WHITESPACE = new CommandParameter("ignoreWhitespace");
9595

9696
/**
97-
* Parameter to indicate whether the commit should be signed or not.
98-
* if false (default true) this will be used by the Git provider to pass the --no-signoff option to the commit command.
99-
* @since 2.1.1
97+
* Parameter to indicate whether the commit/tag should be signed or not.
98+
* This is only applicable to Git for now.
99+
* Possible values are outlined in {@link CommandParameters.SignOption}
100+
* @since 2.2.1
100101
*/
101-
public static final CommandParameter SCM_COMMIT_SIGN = new CommandParameter("gitCommitSign");
102+
public static final CommandParameter SIGN_OPTION = new CommandParameter("sign");
102103

103104
/**
104105
* Parameter name

maven-scm-api/src/main/java/org/apache/maven/scm/CommandParameters.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,48 @@ public ScmBranchParameters getScmBranchParameters(CommandParameter parameter) th
268268
return (ScmBranchParameters) getObject(ScmBranchParameters.class, parameter, new ScmBranchParameters());
269269
}
270270

271+
// ----------------------------------------------------------------------
272+
// SigningOption (Git specific)
273+
// ----------------------------------------------------------------------
274+
/**
275+
* The sign option for a commit or tag.
276+
* <p>
277+
* This is only relevant for SCM providers that support signing commits/tags, such as Git.
278+
* </p>
279+
* @see <a href="https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work">Git Tools - Signing Your Work</a>
280+
*/
281+
public enum SignOption {
282+
/**
283+
* Signs the commit/tag irrespective of the Git configuration setting {@code commit.gpgSign} or {@code tag.gpgSign}.
284+
* Only has an effect for supported SCM providers. Others may be silently ignoring this setting.
285+
*/
286+
FORCE_SIGN,
287+
/**
288+
* Just uses the default value in the Git configuration for setting {@code commit.gpgSign} or {@code tag.gpgSign}.
289+
* Only has an effect for supported SCM providers. Others may be silently ignoring this setting.
290+
*/
291+
DEFAULT,
292+
/**
293+
* Does not sign the commit/tag irrespective of the Git configuration setting {@code commit.gpgSign} or {@code tag.gpgSign}.
294+
*/
295+
FORCE_NO_SIGN
296+
}
297+
298+
public void setSignOption(CommandParameter parameter, SignOption signOption) throws ScmException {
299+
setObject(parameter, signOption);
300+
}
301+
302+
/**
303+
* Return the sign option.
304+
*
305+
* @param parameter The parameter
306+
* @return The sign option or null if not set
307+
* @throws ScmException if the parameter has the wrong type.
308+
*/
309+
public SignOption getSignOption(CommandParameter parameter) throws ScmException {
310+
return getObject(SignOption.class, parameter, null);
311+
}
312+
271313
// ----------------------------------------------------------------------
272314
//
273315
// ----------------------------------------------------------------------
@@ -280,8 +322,8 @@ public ScmBranchParameters getScmBranchParameters(CommandParameter parameter) th
280322
* @return The parameter value
281323
* @throws ScmException if the parameter doesn't exist
282324
*/
283-
private Object getObject(Class<?> clazz, CommandParameter parameter) throws ScmException {
284-
Object object = getObject(clazz, parameter, null);
325+
private <T> T getObject(Class<T> clazz, CommandParameter parameter) throws ScmException {
326+
T object = getObject(clazz, parameter, null);
285327

286328
if (object == null) {
287329
throw new ScmException("Missing parameter: '" + parameter.getName() + "'.");
@@ -299,7 +341,8 @@ private Object getObject(Class<?> clazz, CommandParameter parameter) throws ScmE
299341
* @return The parameter value
300342
* @throws ScmException if the defaultValue is in the wrong type
301343
*/
302-
private Object getObject(Class<?> clazz, CommandParameter parameter, Object defaultValue) throws ScmException {
344+
@SuppressWarnings("unchecked")
345+
private <T> T getObject(Class<T> clazz, CommandParameter parameter, T defaultValue) throws ScmException {
303346
Object object = parameters.get(parameter.getName());
304347

305348
if (object == null) {
@@ -311,7 +354,7 @@ private Object getObject(Class<?> clazz, CommandParameter parameter, Object defa
311354
+ clazz.getName() + ", got: " + object.getClass().getName());
312355
}
313356

314-
return object;
357+
return (T) object;
315358
}
316359

317360
/**

maven-scm-api/src/main/java/org/apache/maven/scm/ScmTagParameters.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,14 @@ public class ScmTagParameters implements Serializable {
3333

3434
private boolean pinExternals = false;
3535

36-
private boolean sign = false;
37-
38-
private boolean forceNoSign = false;
36+
private CommandParameters.SignOption signOption;
3937

4038
private String scmRevision;
4139

4240
public ScmTagParameters() {
4341
this.remoteTagging = false;
4442
this.pinExternals = false;
45-
this.sign = false;
43+
this.signOption = CommandParameters.SignOption.DEFAULT;
4644
}
4745

4846
public ScmTagParameters(String message) {
@@ -73,12 +71,24 @@ public void setPinExternals(boolean pinExternals) {
7371
this.pinExternals = pinExternals;
7472
}
7573

74+
/**
75+
*
76+
* @return true if the tag operation should be signed, false otherwise.
77+
* @deprecated use {@link #getSignOption()} instead.
78+
*/
79+
@Deprecated
7680
public boolean isSign() {
77-
return sign;
81+
return signOption == CommandParameters.SignOption.FORCE_SIGN;
7882
}
7983

84+
/**
85+
* Set the signing option for the tag operation.
86+
* @param sign
87+
* @deprecated use {@link #setSignOption(org.apache.maven.scm.CommandParameters.SignOption)} instead.
88+
*/
89+
@Deprecated
8090
public void setSign(boolean sign) {
81-
this.sign = sign;
91+
signOption = sign ? CommandParameters.SignOption.FORCE_SIGN : CommandParameters.SignOption.DEFAULT;
8292
}
8393

8494
public String getScmRevision() {
@@ -89,22 +99,31 @@ public void setScmRevision(String scmRevision) {
8999
this.scmRevision = scmRevision;
90100
}
91101

92-
public boolean isForceNoSign() {
93-
return forceNoSign;
102+
/**
103+
* Get the signing option for the tag operation.
104+
* @return the signing option
105+
* @since 2.2.1
106+
*/
107+
public CommandParameters.SignOption getSignOption() {
108+
return signOption;
94109
}
95110

96-
public void setForceNoSign(boolean forceNoSign) {
97-
this.forceNoSign = forceNoSign;
111+
/**
112+
* Set the signing option for the tag operation.
113+
* @param signOption
114+
* @since 2.2.1
115+
*/
116+
public void setSignOption(CommandParameters.SignOption signOption) {
117+
this.signOption = signOption;
98118
}
99119

100120
@Override
101121
public String toString() {
102122
return "ScmTagParameters{" + "message='"
103123
+ message + '\'' + ", remoteTagging="
104124
+ remoteTagging + ", pinExternals="
105-
+ pinExternals + ", sign="
106-
+ sign + ", forceNoSign="
107-
+ forceNoSign + ", scmRevision='"
125+
+ pinExternals + ", signOption="
126+
+ signOption + ", scmRevision='"
108127
+ scmRevision + '\'' + '}';
109128
}
110129
}

maven-scm-api/src/main/java/org/apache/maven/scm/command/tag/AbstractTagCommand.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.apache.maven.scm.CommandParameter;
2222
import org.apache.maven.scm.CommandParameters;
23+
import org.apache.maven.scm.CommandParameters.SignOption;
2324
import org.apache.maven.scm.ScmException;
2425
import org.apache.maven.scm.ScmFileSet;
2526
import org.apache.maven.scm.ScmResult;
@@ -70,6 +71,10 @@ public ScmResult executeCommand(ScmProviderRepository repository, ScmFileSet fil
7071
scmTagParameters.setMessage("[maven-scm] copy for tag " + tagName);
7172
}
7273

74+
SignOption signOption = parameters.getSignOption(CommandParameter.SIGN_OPTION);
75+
if (signOption != null) {
76+
scmTagParameters.setSignOption(signOption);
77+
}
7378
return executeTagCommand(repository, fileSet, tagName, scmTagParameters);
7479
}
7580
}

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/CheckinMojo.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@
2525
import org.apache.maven.plugin.MojoExecutionException;
2626
import org.apache.maven.plugins.annotations.Mojo;
2727
import org.apache.maven.plugins.annotations.Parameter;
28+
import org.apache.maven.scm.CommandParameter;
29+
import org.apache.maven.scm.CommandParameters;
30+
import org.apache.maven.scm.CommandParameters.SignOption;
2831
import org.apache.maven.scm.ScmException;
32+
import org.apache.maven.scm.ScmVersion;
2933
import org.apache.maven.scm.command.checkin.CheckInScmResult;
3034
import org.apache.maven.scm.manager.ScmManager;
3135
import org.apache.maven.scm.repository.ScmRepository;
@@ -62,6 +66,14 @@ public class CheckinMojo extends AbstractScmMojo {
6266
@Parameter(property = "scmVersion")
6367
private String scmVersion;
6468

69+
/**
70+
* Toggles the signing for the commit used during checkin (only applicable to SCMs that support signing).
71+
*
72+
* @since 2.2.1
73+
*/
74+
@Parameter(property = "signOption")
75+
private SignOption signOption;
76+
6577
@Inject
6678
public CheckinMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
6779
super(manager, settingsDecrypter);
@@ -76,8 +88,19 @@ public void execute() throws MojoExecutionException {
7688
try {
7789
ScmRepository repository = getScmRepository();
7890

79-
CheckInScmResult result = getScmManager()
80-
.checkIn(repository, getFileSet(), getScmVersion(scmVersionType, scmVersion), message);
91+
CommandParameters parameters = new CommandParameters();
92+
93+
ScmVersion version = getScmVersion(scmVersionType, scmVersion);
94+
if (version != null) {
95+
parameters.setScmVersion(CommandParameter.SCM_VERSION, version);
96+
}
97+
if (message != null) {
98+
parameters.setString(CommandParameter.MESSAGE, message);
99+
}
100+
if (signOption != null) {
101+
parameters.setSignOption(CommandParameter.SIGN_OPTION, signOption);
102+
}
103+
CheckInScmResult result = getScmManager().checkIn(repository, getFileSet(), parameters);
81104

82105
checkResult(result);
83106
} catch (IOException | ScmException e) {

maven-scm-plugin/src/main/java/org/apache/maven/scm/plugin/TagMojo.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.maven.plugin.MojoExecutionException;
2828
import org.apache.maven.plugins.annotations.Mojo;
2929
import org.apache.maven.plugins.annotations.Parameter;
30+
import org.apache.maven.scm.CommandParameters.SignOption;
3031
import org.apache.maven.scm.ScmException;
3132
import org.apache.maven.scm.ScmTagParameters;
3233
import org.apache.maven.scm.command.tag.TagScmResult;
@@ -100,20 +101,22 @@ public class TagMojo extends AbstractScmMojo {
100101
private boolean pinExternals;
101102

102103
/**
103-
* Enable the "--sign" in Git
104+
* Enable the "--sign" in Git.
105+
* Same as {@link #signOption} set to either {@link SignOption#FORCE_SIGN} or {@link SignOption#DEFAULT}.
104106
*
105107
* @since 1.11.0
108+
* @deprecated since 2.1.1, use {@link #signOption} instead
106109
*/
107110
@Parameter(property = "sign", defaultValue = "false")
108111
private boolean sign;
109112

110113
/**
111-
* Enable the "--no-sign" in Git
114+
* Toggles the signing for the tag command (only applicable to SCMs that support signing).
112115
*
113-
* @since 2.1.1
116+
* @since 2.2.1
114117
*/
115-
@Parameter(property = "forceNoSign", defaultValue = "false")
116-
private boolean forceNoSign;
118+
@Parameter(property = "signOption")
119+
private SignOption signOption = SignOption.DEFAULT;
117120

118121
@Inject
119122
public TagMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
@@ -157,8 +160,12 @@ public void execute() throws MojoExecutionException {
157160
ScmTagParameters scmTagParameters = new ScmTagParameters(message);
158161
scmTagParameters.setRemoteTagging(remoteTagging);
159162
scmTagParameters.setPinExternals(pinExternals);
160-
scmTagParameters.setSign(sign);
161-
scmTagParameters.setForceNoSign(forceNoSign);
163+
if (signOption != null) {
164+
scmTagParameters.setSignOption(signOption);
165+
} else if (sign) {
166+
getLog().warn("The 'sign' parameter is deprecated, use 'signOption' instead.");
167+
scmTagParameters.setSign(sign);
168+
}
162169

163170
TagScmResult result = provider.tag(repository, getFileSet(), finalTag, scmTagParameters);
164171

@@ -167,20 +174,4 @@ public void execute() throws MojoExecutionException {
167174
throw new MojoExecutionException("Cannot run tag command : ", e);
168175
}
169176
}
170-
171-
public boolean isSign() {
172-
return sign;
173-
}
174-
175-
public void setSign(boolean sign) {
176-
this.sign = sign;
177-
}
178-
179-
public boolean isForceNoSign() {
180-
return forceNoSign;
181-
}
182-
183-
public void setForceNoSign(boolean forceNoSign) {
184-
this.forceNoSign = forceNoSign;
185-
}
186177
}

maven-scm-plugin/src/test/java/org/apache/maven/scm/plugin/UntagMojoTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public void testUntag() throws Exception {
7272

7373
TagMojo tagMojo = (TagMojo) lookupMojo("tag", getTestFile("src/test/resources/mojos/untag/tag.xml"));
7474
tagMojo.setWorkingDirectory(checkoutDir);
75-
tagMojo.setSign(false);
76-
tagMojo.setForceNoSign(true);
7775
tagMojo.setConnectionUrl(getConnectionLocalAddress(tagMojo));
7876
tagMojo.execute();
7977

maven-scm-providers/maven-scm-providers-git/maven-scm-provider-gitexe/src/main/java/org/apache/maven/scm/provider/git/gitexe/command/checkin/GitCheckInCommand.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Map;
2828

2929
import org.apache.commons.io.FilenameUtils;
30+
import org.apache.maven.scm.CommandParameters.SignOption;
3031
import org.apache.maven.scm.ScmException;
3132
import org.apache.maven.scm.ScmFile;
3233
import org.apache.maven.scm.ScmFileSet;
@@ -213,6 +214,16 @@ public static Commandline createCommitCommandLine(
213214
File messageFile,
214215
Map<String, String> environmentVariables)
215216
throws ScmException {
217+
return createCommitCommandLine(repository, fileSet, messageFile, environmentVariables, SignOption.DEFAULT);
218+
}
219+
220+
public static Commandline createCommitCommandLine(
221+
GitScmProviderRepository repository,
222+
ScmFileSet fileSet,
223+
File messageFile,
224+
Map<String, String> environmentVariables,
225+
SignOption signOption)
226+
throws ScmException {
216227
Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "commit");
217228

218229
cl.createArg().setValue("--verbose");
@@ -230,6 +241,17 @@ public static Commandline createCommitCommandLine(
230241
cl.createArg().setValue("--no-verify");
231242
}
232243

244+
switch (signOption) {
245+
case FORCE_SIGN:
246+
cl.createArg().setValue("--gpg-sign");
247+
break;
248+
case FORCE_NO_SIGN:
249+
cl.createArg().setValue("--no-gpg-sign");
250+
break;
251+
default:
252+
// do nothing, this is the default
253+
break;
254+
}
233255
return cl;
234256
}
235257
}

0 commit comments

Comments
 (0)