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.util.ArrayList;
41 import java.util.List;
42
43 import org.eclipse.jgit.api.CommitCommand;
44 import org.eclipse.jgit.api.Git;
45 import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
46 import org.eclipse.jgit.api.errors.JGitInternalException;
47 import org.eclipse.jgit.api.errors.NoHeadException;
48 import org.eclipse.jgit.api.errors.NoMessageException;
49 import org.eclipse.jgit.lib.Constants;
50 import org.eclipse.jgit.lib.Ref;
51 import org.eclipse.jgit.pgm.internal.CLIText;
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 @Argument(metaVar = "metaVar_commitPaths", usage = "usage_CommitPaths")
78 private List<String> paths = new ArrayList<String>();
79
80 @Override
81 protected void run() throws NoHeadException, NoMessageException,
82 ConcurrentRefUpdateException, JGitInternalException, Exception {
83 try (Git git = new Git(db)) {
84 CommitCommand commitCmd = git.commit();
85 if (author != null)
86 commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
87 if (message != null)
88 commitCmd.setMessage(message);
89 if (only && paths.isEmpty())
90 throw die(CLIText.get().pathsRequired);
91 if (only && all)
92 throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed);
93 if (!paths.isEmpty())
94 for (String p : paths)
95 commitCmd.setOnly(p);
96 commitCmd.setAmend(amend);
97 commitCmd.setAll(all);
98 Ref head = db.getRef(Constants.HEAD);
99 if (head == null) {
100 throw die(CLIText.get().onBranchToBeBorn);
101 }
102 RevCommit commit;
103 try {
104 commit = commitCmd.call();
105 } catch (JGitInternalException e) {
106 throw die(e.getMessage());
107 }
108
109 String branchName;
110 if (!head.isSymbolic())
111 branchName = CLIText.get().branchDetachedHEAD;
112 else {
113 branchName = head.getTarget().getName();
114 if (branchName.startsWith(Constants.R_HEADS))
115 branchName = branchName.substring(Constants.R_HEADS.length());
116 }
117 outw.println("[" + branchName + " " + commit.name() + "] "
118 + commit.getShortMessage());
119 }
120 }
121 }