aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java')
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java101
1 files changed, 65 insertions, 36 deletions
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
index 14c449a6b3..8df028d2e2 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Commit.java
@@ -37,18 +37,18 @@
*/
package org.eclipse.jgit.pgm;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.api.CommitCommand;
import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
+import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
-import org.eclipse.jgit.api.errors.NoHeadException;
-import org.eclipse.jgit.api.errors.NoMessageException;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.pgm.internal.CLIText;
+import org.eclipse.jgit.pgm.opt.GpgSignHandler;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.util.RawParseUtils;
import org.kohsuke.args4j.Argument;
@@ -74,43 +74,72 @@ class Commit extends TextBuiltin {
@Option(name = "--amend", usage = "usage_CommitAmend")
private boolean amend;
+ @Option(name = "--gpg-sign", aliases = { "-S" }, forbids = {
+ "--no-gpg-sign" }, handler = GpgSignHandler.class)
+ private String gpgSigningKey;
+
+ @Option(name = "--no-gpg-sign", forbids = { "--gpg-sign" })
+ private boolean noGpgSign;
+
@Argument(metaVar = "metaVar_commitPaths", usage = "usage_CommitPaths")
- private List<String> paths = new ArrayList<String>();
+ private List<String> paths = new ArrayList<>();
@Override
- protected void run() throws NoHeadException, NoMessageException,
- ConcurrentRefUpdateException, JGitInternalException, Exception {
- CommitCommand commitCmd = new Git(db).commit();
- if (author != null)
- commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
- if (message != null)
- commitCmd.setMessage(message);
- if (only && paths.isEmpty())
- throw die(CLIText.get().pathsRequired);
- if (only && all)
- throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed);
- if (!paths.isEmpty())
- for (String p : paths)
- commitCmd.setOnly(p);
- commitCmd.setAmend(amend);
- commitCmd.setAll(all);
- Ref head = db.getRef(Constants.HEAD);
- RevCommit commit;
- try {
- commit = commitCmd.call();
- } catch (JGitInternalException e) {
- throw die(e.getMessage());
- }
+ protected void run() {
+ try (Git git = new Git(db)) {
+ CommitCommand commitCmd = git.commit();
+ if (author != null) {
+ commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
+ }
+ if (message != null) {
+ commitCmd.setMessage(message);
+ }
+ if (noGpgSign) {
+ commitCmd.setSign(Boolean.FALSE);
+ } else if (gpgSigningKey != null) {
+ commitCmd.setSign(Boolean.TRUE);
+ if (!gpgSigningKey.equals(GpgSignHandler.DEFAULT)) {
+ commitCmd.setSigningKey(gpgSigningKey);
+ }
+ }
+ if (only && paths.isEmpty()) {
+ throw die(CLIText.get().pathsRequired);
+ }
+ if (only && all) {
+ throw die(CLIText.get().onlyOneCommitOptionAllowed);
+ }
+ if (!paths.isEmpty()) {
+ for (String p : paths) {
+ commitCmd.setOnly(p);
+ }
+ }
+ commitCmd.setAmend(amend);
+ commitCmd.setAll(all);
+ Ref head = db.exactRef(Constants.HEAD);
+ if (head == null) {
+ throw die(CLIText.get().onBranchToBeBorn);
+ }
+ RevCommit commit;
+ try {
+ commit = commitCmd.call();
+ } catch (JGitInternalException | GitAPIException e) {
+ throw die(e.getMessage(), e);
+ }
- String branchName;
- if (!head.isSymbolic())
- branchName = CLIText.get().branchDetachedHEAD;
- else {
- branchName = head.getTarget().getName();
- if (branchName.startsWith(Constants.R_HEADS))
- branchName = branchName.substring(Constants.R_HEADS.length());
+ String branchName;
+ if (!head.isSymbolic()) {
+ branchName = CLIText.get().branchDetachedHEAD;
+ } else {
+ branchName = head.getTarget().getName();
+ if (branchName.startsWith(Constants.R_HEADS)) {
+ branchName = branchName
+ .substring(Constants.R_HEADS.length());
+ }
+ }
+ outw.println('[' + branchName + ' ' + commit.name() + "] " //$NON-NLS-1$
+ + commit.getShortMessage());
+ } catch (IOException e) {
+ throw die(e.getMessage(), e);
}
- outw.println("[" + branchName + " " + commit.name() + "] " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
- + commit.getShortMessage());
}
}