1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package org.eclipse.jgit.pgm;
39
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.List;
43
44 import org.eclipse.jgit.api.CommitCommand;
45 import org.eclipse.jgit.api.Git;
46 import org.eclipse.jgit.api.errors.GitAPIException;
47 import org.eclipse.jgit.api.errors.JGitInternalException;
48 import org.eclipse.jgit.lib.Constants;
49 import org.eclipse.jgit.lib.Ref;
50 import org.eclipse.jgit.pgm.internal.CLIText;
51 import org.eclipse.jgit.pgm.opt.GpgSignHandler;
52 import org.eclipse.jgit.revwalk.RevCommit;
53 import org.eclipse.jgit.util.RawParseUtils;
54 import org.kohsuke.args4j.Argument;
55 import org.kohsuke.args4j.Option;
56
57 @Command(common = true, usage = "usage_recordChangesToRepository")
58 class Commit extends TextBuiltin {
59
60
61
62 @Option(name = "--author", metaVar = "metaVar_author", usage = "usage_CommitAuthor")
63 private String author;
64
65 @Option(name = "--message", aliases = { "-m" }, metaVar = "metaVar_message", usage = "usage_CommitMessage", required = true)
66 private String message;
67
68 @Option(name = "--only", aliases = { "-o" }, usage = "usage_CommitOnly")
69 private boolean only;
70
71 @Option(name = "--all", aliases = { "-a" }, usage = "usage_CommitAll")
72 private boolean all;
73
74 @Option(name = "--amend", usage = "usage_CommitAmend")
75 private boolean amend;
76
77 @Option(name = "--gpg-sign", aliases = { "-S" }, forbids = {
78 "--no-gpg-sign" }, handler = GpgSignHandler.class)
79 private String gpgSigningKey;
80
81 @Option(name = "--no-gpg-sign", forbids = { "--gpg-sign" })
82 private boolean noGpgSign;
83
84 @Argument(metaVar = "metaVar_commitPaths", usage = "usage_CommitPaths")
85 private List<String> paths = new ArrayList<>();
86
87
88 @Override
89 protected void run() {
90 try (Gitit.html#Git">Git git = new Git(db)) {
91 CommitCommand commitCmd = git.commit();
92 if (author != null) {
93 commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
94 }
95 if (message != null) {
96 commitCmd.setMessage(message);
97 }
98 if (noGpgSign) {
99 commitCmd.setSign(Boolean.FALSE);
100 } else if (gpgSigningKey != null) {
101 commitCmd.setSign(Boolean.TRUE);
102 if (!gpgSigningKey.equals(GpgSignHandler.DEFAULT)) {
103 commitCmd.setSigningKey(gpgSigningKey);
104 }
105 }
106 if (only && paths.isEmpty()) {
107 throw die(CLIText.get().pathsRequired);
108 }
109 if (only && all) {
110 throw die(CLIText.get().onlyOneCommitOptionAllowed);
111 }
112 if (!paths.isEmpty()) {
113 for (String p : paths) {
114 commitCmd.setOnly(p);
115 }
116 }
117 commitCmd.setAmend(amend);
118 commitCmd.setAll(all);
119 Ref head = db.exactRef(Constants.HEAD);
120 if (head == null) {
121 throw die(CLIText.get().onBranchToBeBorn);
122 }
123 RevCommit commit;
124 try {
125 commit = commitCmd.call();
126 } catch (JGitInternalException | GitAPIException e) {
127 throw die(e.getMessage(), e);
128 }
129
130 String branchName;
131 if (!head.isSymbolic()) {
132 branchName = CLIText.get().branchDetachedHEAD;
133 } else {
134 branchName = head.getTarget().getName();
135 if (branchName.startsWith(Constants.R_HEADS)) {
136 branchName = branchName
137 .substring(Constants.R_HEADS.length());
138 }
139 }
140 outw.println('[' + branchName + ' ' + commit.name() + "] "
141 + commit.getShortMessage());
142 } catch (IOException e) {
143 throw die(e.getMessage(), e);
144 }
145 }
146 }