View Javadoc
1   /*
2    * Copyright (C) 2010, 2012 Christian Halstrick <christian.halstrick@sap.com> and
3    * other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v1.0 which accompanies this
7    * distribution, is reproduced below, and is available at
8    * http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or without
13   * modification, are permitted provided that the following conditions are met:
14   *
15   * - Redistributions of source code must retain the above copyright notice, this
16   * list of conditions and the following disclaimer.
17   *
18   * - Redistributions in binary form must reproduce the above copyright notice,
19   * this list of conditions and the following disclaimer in the documentation
20   * and/or other materials provided with the distribution.
21   *
22   * - Neither the name of the Eclipse Foundation, Inc. nor the names of its
23   * contributors may be used to endorse or promote products derived from this
24   * software without specific prior written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36   * POSSIBILITY OF SUCH DAMAGE.
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  	// I don't support setting the committer, because also the native git
60  	// command doesn't allow this.
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<>();
79  
80  	/** {@inheritDoc} */
81  	@Override
82  	protected void run() throws NoHeadException, NoMessageException,
83  			ConcurrentRefUpdateException, JGitInternalException, Exception {
84  		try (Git git = new Git(db)) {
85  			CommitCommand commitCmd = git.commit();
86  			if (author != null)
87  				commitCmd.setAuthor(RawParseUtils.parsePersonIdent(author));
88  			if (message != null)
89  				commitCmd.setMessage(message);
90  			if (only && paths.isEmpty())
91  				throw die(CLIText.get().pathsRequired);
92  			if (only && all)
93  				throw die(CLIText.get().onlyOneOfIncludeOnlyAllInteractiveCanBeUsed);
94  			if (!paths.isEmpty())
95  				for (String p : paths)
96  					commitCmd.setOnly(p);
97  			commitCmd.setAmend(amend);
98  			commitCmd.setAll(all);
99  			Ref head = db.exactRef(Constants.HEAD);
100 			if (head == null) {
101 				throw die(CLIText.get().onBranchToBeBorn);
102 			}
103 			RevCommit commit;
104 			try {
105 				commit = commitCmd.call();
106 			} catch (JGitInternalException e) {
107 				throw die(e.getMessage());
108 			}
109 
110 			String branchName;
111 			if (!head.isSymbolic())
112 				branchName = CLIText.get().branchDetachedHEAD;
113 			else {
114 				branchName = head.getTarget().getName();
115 				if (branchName.startsWith(Constants.R_HEADS))
116 					branchName = branchName.substring(Constants.R_HEADS.length());
117 			}
118 			outw.println("[" + branchName + " " + commit.name() + "] " //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
119 					+ commit.getShortMessage());
120 		}
121 	}
122 }