You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommitCommand.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at 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
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.api;
  44. import java.io.IOException;
  45. import java.io.InputStream;
  46. import java.text.MessageFormat;
  47. import java.util.ArrayList;
  48. import java.util.LinkedList;
  49. import java.util.List;
  50. import org.eclipse.jgit.JGitText;
  51. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  52. import org.eclipse.jgit.api.errors.JGitInternalException;
  53. import org.eclipse.jgit.api.errors.NoFilepatternException;
  54. import org.eclipse.jgit.api.errors.NoHeadException;
  55. import org.eclipse.jgit.api.errors.NoMessageException;
  56. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  57. import org.eclipse.jgit.dircache.DirCache;
  58. import org.eclipse.jgit.dircache.DirCacheBuilder;
  59. import org.eclipse.jgit.dircache.DirCacheEditor;
  60. import org.eclipse.jgit.dircache.DirCacheEditor.DeletePath;
  61. import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
  62. import org.eclipse.jgit.dircache.DirCacheEntry;
  63. import org.eclipse.jgit.dircache.DirCacheIterator;
  64. import org.eclipse.jgit.errors.UnmergedPathException;
  65. import org.eclipse.jgit.lib.CommitBuilder;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.FileMode;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectInserter;
  70. import org.eclipse.jgit.lib.PersonIdent;
  71. import org.eclipse.jgit.lib.Ref;
  72. import org.eclipse.jgit.lib.RefUpdate;
  73. import org.eclipse.jgit.lib.RefUpdate.Result;
  74. import org.eclipse.jgit.lib.Repository;
  75. import org.eclipse.jgit.lib.RepositoryState;
  76. import org.eclipse.jgit.revwalk.RevCommit;
  77. import org.eclipse.jgit.revwalk.RevWalk;
  78. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  79. import org.eclipse.jgit.treewalk.FileTreeIterator;
  80. import org.eclipse.jgit.treewalk.TreeWalk;
  81. import org.eclipse.jgit.util.ChangeIdUtil;
  82. /**
  83. * A class used to execute a {@code Commit} command. It has setters for all
  84. * supported options and arguments of this command and a {@link #call()} method
  85. * to finally execute the command.
  86. *
  87. * @see <a
  88. * href="http://www.kernel.org/pub/software/scm/git/docs/git-commit.html"
  89. * >Git documentation about Commit</a>
  90. */
  91. public class CommitCommand extends GitCommand<RevCommit> {
  92. private PersonIdent author;
  93. private PersonIdent committer;
  94. private String message;
  95. private boolean all;
  96. private List<String> only = new ArrayList<String>();
  97. private boolean[] onlyProcessed;
  98. private boolean amend;
  99. private boolean insertChangeId;
  100. /**
  101. * parents this commit should have. The current HEAD will be in this list
  102. * and also all commits mentioned in .git/MERGE_HEAD
  103. */
  104. private List<ObjectId> parents = new LinkedList<ObjectId>();
  105. private String reflogComment;
  106. /**
  107. * @param repo
  108. */
  109. protected CommitCommand(Repository repo) {
  110. super(repo);
  111. }
  112. /**
  113. * Executes the {@code commit} command with all the options and parameters
  114. * collected by the setter methods of this class. Each instance of this
  115. * class should only be used for one invocation of the command (means: one
  116. * call to {@link #call()})
  117. *
  118. * @return a {@link RevCommit} object representing the successful commit.
  119. * @throws NoHeadException
  120. * when called on a git repo without a HEAD reference
  121. * @throws NoMessageException
  122. * when called without specifying a commit message
  123. * @throws UnmergedPathException
  124. * when the current index contained unmerged paths (conflicts)
  125. * @throws WrongRepositoryStateException
  126. * when repository is not in the right state for committing
  127. * @throws JGitInternalException
  128. * a low-level exception of JGit has occurred. The original
  129. * exception can be retrieved by calling
  130. * {@link Exception#getCause()}. Expect only
  131. * {@code IOException's} to be wrapped. Subclasses of
  132. * {@link IOException} (e.g. {@link UnmergedPathException}) are
  133. * typically not wrapped here but thrown as original exception
  134. */
  135. public RevCommit call() throws NoHeadException, NoMessageException,
  136. UnmergedPathException, ConcurrentRefUpdateException,
  137. JGitInternalException, WrongRepositoryStateException {
  138. checkCallable();
  139. RepositoryState state = repo.getRepositoryState();
  140. if (!state.canCommit())
  141. throw new WrongRepositoryStateException(MessageFormat.format(
  142. JGitText.get().cannotCommitOnARepoWithState, state.name()));
  143. processOptions(state);
  144. try {
  145. if (all && !repo.isBare() && repo.getWorkTree() != null) {
  146. Git git = new Git(repo);
  147. try {
  148. git.add()
  149. .addFilepattern(".")
  150. .setUpdate(true).call();
  151. } catch (NoFilepatternException e) {
  152. // should really not happen
  153. throw new JGitInternalException(e.getMessage(), e);
  154. }
  155. }
  156. Ref head = repo.getRef(Constants.HEAD);
  157. if (head == null)
  158. throw new NoHeadException(
  159. JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported);
  160. // determine the current HEAD and the commit it is referring to
  161. ObjectId headId = repo.resolve(Constants.HEAD + "^{commit}");
  162. if (headId != null)
  163. if (amend) {
  164. RevCommit previousCommit = new RevWalk(repo)
  165. .parseCommit(headId);
  166. RevCommit[] p = previousCommit.getParents();
  167. for (int i = 0; i < p.length; i++)
  168. parents.add(0, p[i].getId());
  169. } else {
  170. parents.add(0, headId);
  171. }
  172. // lock the index
  173. DirCache index = repo.lockDirCache();
  174. try {
  175. if (!only.isEmpty())
  176. index = createTemporaryIndex(headId, index);
  177. ObjectInserter odi = repo.newObjectInserter();
  178. try {
  179. // Write the index as tree to the object database. This may
  180. // fail for example when the index contains unmerged paths
  181. // (unresolved conflicts)
  182. ObjectId indexTreeId = index.writeTree(odi);
  183. if (insertChangeId)
  184. insertChangeId(indexTreeId);
  185. // Create a Commit object, populate it and write it
  186. CommitBuilder commit = new CommitBuilder();
  187. commit.setCommitter(committer);
  188. commit.setAuthor(author);
  189. commit.setMessage(message);
  190. commit.setParentIds(parents);
  191. commit.setTreeId(indexTreeId);
  192. ObjectId commitId = odi.insert(commit);
  193. odi.flush();
  194. RevWalk revWalk = new RevWalk(repo);
  195. try {
  196. RevCommit revCommit = revWalk.parseCommit(commitId);
  197. RefUpdate ru = repo.updateRef(Constants.HEAD);
  198. ru.setNewObjectId(commitId);
  199. if (reflogComment != null) {
  200. ru.setRefLogMessage(reflogComment, false);
  201. } else {
  202. String prefix = amend ? "commit (amend): "
  203. : "commit: ";
  204. ru.setRefLogMessage(
  205. prefix + revCommit.getShortMessage(), false);
  206. }
  207. if (headId != null)
  208. ru.setExpectedOldObjectId(headId);
  209. else
  210. ru.setExpectedOldObjectId(ObjectId.zeroId());
  211. Result rc = ru.forceUpdate();
  212. switch (rc) {
  213. case NEW:
  214. case FORCED:
  215. case FAST_FORWARD: {
  216. setCallable(false);
  217. if (state == RepositoryState.MERGING_RESOLVED) {
  218. // Commit was successful. Now delete the files
  219. // used for merge commits
  220. repo.writeMergeCommitMsg(null);
  221. repo.writeMergeHeads(null);
  222. } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
  223. repo.writeMergeCommitMsg(null);
  224. repo.writeCherryPickHead(null);
  225. }
  226. return revCommit;
  227. }
  228. case REJECTED:
  229. case LOCK_FAILURE:
  230. throw new ConcurrentRefUpdateException(JGitText
  231. .get().couldNotLockHEAD, ru.getRef(), rc);
  232. default:
  233. throw new JGitInternalException(MessageFormat
  234. .format(JGitText.get().updatingRefFailed,
  235. Constants.HEAD,
  236. commitId.toString(), rc));
  237. }
  238. } finally {
  239. revWalk.release();
  240. }
  241. } finally {
  242. odi.release();
  243. }
  244. } finally {
  245. index.unlock();
  246. }
  247. } catch (UnmergedPathException e) {
  248. // since UnmergedPathException is a subclass of IOException
  249. // which should not be wrapped by a JGitInternalException we
  250. // have to catch and re-throw it here
  251. throw e;
  252. } catch (IOException e) {
  253. throw new JGitInternalException(
  254. JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
  255. }
  256. }
  257. private void insertChangeId(ObjectId treeId) throws IOException {
  258. ObjectId firstParentId = null;
  259. if (!parents.isEmpty())
  260. firstParentId = parents.get(0);
  261. ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId,
  262. author, committer, message);
  263. message = ChangeIdUtil.insertId(message, changeId);
  264. if (changeId != null)
  265. message = message.replaceAll("\nChange-Id: I"
  266. + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I"
  267. + changeId.getName() + "\n");
  268. }
  269. private DirCache createTemporaryIndex(ObjectId headId, DirCache index)
  270. throws IOException {
  271. ObjectInserter inserter = null;
  272. // get DirCacheEditor to modify the index if required
  273. DirCacheEditor dcEditor = index.editor();
  274. // get DirCacheBuilder for newly created in-core index to build a
  275. // temporary index for this commit
  276. DirCache inCoreIndex = DirCache.newInCore();
  277. DirCacheBuilder dcBuilder = inCoreIndex.builder();
  278. onlyProcessed = new boolean[only.size()];
  279. boolean emptyCommit = true;
  280. TreeWalk treeWalk = new TreeWalk(repo);
  281. int dcIdx = treeWalk.addTree(new DirCacheIterator(index));
  282. int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
  283. int hIdx = -1;
  284. if (headId != null)
  285. hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
  286. treeWalk.setRecursive(true);
  287. while (treeWalk.next()) {
  288. String path = treeWalk.getPathString();
  289. // check if current entry's path matches a specified path
  290. int pos = lookupOnly(path);
  291. CanonicalTreeParser hTree = null;
  292. if (hIdx != -1)
  293. hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
  294. if (pos >= 0) {
  295. // include entry in commit
  296. DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
  297. DirCacheIterator.class);
  298. FileTreeIterator fTree = treeWalk.getTree(fIdx,
  299. FileTreeIterator.class);
  300. // check if entry refers to a tracked file
  301. boolean tracked = dcTree != null || hTree != null;
  302. if (!tracked)
  303. break;
  304. if (fTree != null) {
  305. // create a new DirCacheEntry with data retrieved from disk
  306. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  307. long entryLength = fTree.getEntryLength();
  308. dcEntry.setLength(entryLength);
  309. dcEntry.setLastModified(fTree.getEntryLastModified());
  310. dcEntry.setFileMode(fTree.getIndexFileMode(dcTree));
  311. boolean objectExists = (dcTree != null && fTree
  312. .idEqual(dcTree))
  313. || (hTree != null && fTree.idEqual(hTree));
  314. if (objectExists) {
  315. dcEntry.setObjectId(fTree.getEntryObjectId());
  316. } else {
  317. if (FileMode.GITLINK.equals(dcEntry.getFileMode())) {
  318. // Do not check the content of submodule entries
  319. // Use the old entry information instead.
  320. dcEntry.copyMetaData(index.getEntry(dcEntry
  321. .getPathString()));
  322. } else {
  323. // insert object
  324. if (inserter == null)
  325. inserter = repo.newObjectInserter();
  326. InputStream inputStream = fTree.openEntryStream();
  327. try {
  328. dcEntry.setObjectId(inserter.insert(
  329. Constants.OBJ_BLOB, entryLength,
  330. inputStream));
  331. } finally {
  332. inputStream.close();
  333. }
  334. }
  335. }
  336. // update index
  337. dcEditor.add(new PathEdit(path) {
  338. @Override
  339. public void apply(DirCacheEntry ent) {
  340. ent.copyMetaData(dcEntry);
  341. }
  342. });
  343. // add to temporary in-core index
  344. dcBuilder.add(dcEntry);
  345. if (emptyCommit
  346. && (hTree == null || !hTree.idEqual(fTree) || hTree
  347. .getEntryRawMode() != fTree
  348. .getEntryRawMode()))
  349. // this is a change
  350. emptyCommit = false;
  351. } else {
  352. // if no file exists on disk, remove entry from index and
  353. // don't add it to temporary in-core index
  354. dcEditor.add(new DeletePath(path));
  355. if (emptyCommit && hTree != null)
  356. // this is a change
  357. emptyCommit = false;
  358. }
  359. // keep track of processed path
  360. onlyProcessed[pos] = true;
  361. } else {
  362. // add entries from HEAD for all other paths
  363. if (hTree != null) {
  364. // create a new DirCacheEntry with data retrieved from HEAD
  365. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  366. dcEntry.setObjectId(hTree.getEntryObjectId());
  367. dcEntry.setFileMode(hTree.getEntryFileMode());
  368. // add to temporary in-core index
  369. dcBuilder.add(dcEntry);
  370. }
  371. }
  372. }
  373. // there must be no unprocessed paths left at this point; otherwise an
  374. // untracked or unknown path has been specified
  375. for (int i = 0; i < onlyProcessed.length; i++)
  376. if (!onlyProcessed[i])
  377. throw new JGitInternalException(MessageFormat.format(
  378. JGitText.get().entryNotFoundByPath, only.get(i)));
  379. // there must be at least one change
  380. if (emptyCommit)
  381. throw new JGitInternalException(JGitText.get().emptyCommit);
  382. // update index
  383. dcEditor.commit();
  384. // finish temporary in-core index used for this commit
  385. dcBuilder.finish();
  386. return inCoreIndex;
  387. }
  388. /**
  389. * Look an entry's path up in the list of paths specified by the --only/ -o
  390. * option
  391. *
  392. * In case the complete (file) path (e.g. "d1/d2/f1") cannot be found in
  393. * <code>only</code>, lookup is also tried with (parent) directory paths
  394. * (e.g. "d1/d2" and "d1").
  395. *
  396. * @param pathString
  397. * entry's path
  398. * @return the item's index in <code>only</code>; -1 if no item matches
  399. */
  400. private int lookupOnly(String pathString) {
  401. int i = 0;
  402. for (String o : only) {
  403. String p = pathString;
  404. while (true) {
  405. if (p.equals(o))
  406. return i;
  407. int l = p.lastIndexOf("/");
  408. if (l < 1)
  409. break;
  410. p = p.substring(0, l);
  411. }
  412. i++;
  413. }
  414. return -1;
  415. }
  416. /**
  417. * Sets default values for not explicitly specified options. Then validates
  418. * that all required data has been provided.
  419. *
  420. * @param state
  421. * the state of the repository we are working on
  422. *
  423. * @throws NoMessageException
  424. * if the commit message has not been specified
  425. */
  426. private void processOptions(RepositoryState state) throws NoMessageException {
  427. if (committer == null)
  428. committer = new PersonIdent(repo);
  429. if (author == null)
  430. author = committer;
  431. // when doing a merge commit parse MERGE_HEAD and MERGE_MSG files
  432. if (state == RepositoryState.MERGING_RESOLVED) {
  433. try {
  434. parents = repo.readMergeHeads();
  435. } catch (IOException e) {
  436. throw new JGitInternalException(MessageFormat.format(
  437. JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR,
  438. Constants.MERGE_HEAD, e), e);
  439. }
  440. if (message == null) {
  441. try {
  442. message = repo.readMergeCommitMsg();
  443. } catch (IOException e) {
  444. throw new JGitInternalException(MessageFormat.format(
  445. JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR,
  446. Constants.MERGE_MSG, e), e);
  447. }
  448. }
  449. }
  450. if (message == null)
  451. // as long as we don't suppport -C option we have to have
  452. // an explicit message
  453. throw new NoMessageException(JGitText.get().commitMessageNotSpecified);
  454. }
  455. /**
  456. * @param message
  457. * the commit message used for the {@code commit}
  458. * @return {@code this}
  459. */
  460. public CommitCommand setMessage(String message) {
  461. checkCallable();
  462. this.message = message;
  463. return this;
  464. }
  465. /**
  466. * @return the commit message used for the <code>commit</code>
  467. */
  468. public String getMessage() {
  469. return message;
  470. }
  471. /**
  472. * Sets the committer for this {@code commit}. If no committer is explicitly
  473. * specified because this method is never called or called with {@code null}
  474. * value then the committer will be deduced from config info in repository,
  475. * with current time.
  476. *
  477. * @param committer
  478. * the committer used for the {@code commit}
  479. * @return {@code this}
  480. */
  481. public CommitCommand setCommitter(PersonIdent committer) {
  482. checkCallable();
  483. this.committer = committer;
  484. return this;
  485. }
  486. /**
  487. * Sets the committer for this {@code commit}. If no committer is explicitly
  488. * specified because this method is never called or called with {@code null}
  489. * value then the committer will be deduced from config info in repository,
  490. * with current time.
  491. *
  492. * @param name
  493. * the name of the committer used for the {@code commit}
  494. * @param email
  495. * the email of the committer used for the {@code commit}
  496. * @return {@code this}
  497. */
  498. public CommitCommand setCommitter(String name, String email) {
  499. checkCallable();
  500. return setCommitter(new PersonIdent(name, email));
  501. }
  502. /**
  503. * @return the committer used for the {@code commit}. If no committer was
  504. * specified {@code null} is returned and the default
  505. * {@link PersonIdent} of this repo is used during execution of the
  506. * command
  507. */
  508. public PersonIdent getCommitter() {
  509. return committer;
  510. }
  511. /**
  512. * Sets the author for this {@code commit}. If no author is explicitly
  513. * specified because this method is never called or called with {@code null}
  514. * value then the author will be set to the committer.
  515. *
  516. * @param author
  517. * the author used for the {@code commit}
  518. * @return {@code this}
  519. */
  520. public CommitCommand setAuthor(PersonIdent author) {
  521. checkCallable();
  522. this.author = author;
  523. return this;
  524. }
  525. /**
  526. * Sets the author for this {@code commit}. If no author is explicitly
  527. * specified because this method is never called or called with {@code null}
  528. * value then the author will be set to the committer.
  529. *
  530. * @param name
  531. * the name of the author used for the {@code commit}
  532. * @param email
  533. * the email of the author used for the {@code commit}
  534. * @return {@code this}
  535. */
  536. public CommitCommand setAuthor(String name, String email) {
  537. checkCallable();
  538. return setAuthor(new PersonIdent(name, email));
  539. }
  540. /**
  541. * @return the author used for the {@code commit}. If no author was
  542. * specified {@code null} is returned and the default
  543. * {@link PersonIdent} of this repo is used during execution of the
  544. * command
  545. */
  546. public PersonIdent getAuthor() {
  547. return author;
  548. }
  549. /**
  550. * If set to true the Commit command automatically stages files that have
  551. * been modified and deleted, but new files not known by the repository are
  552. * not affected. This corresponds to the parameter -a on the command line.
  553. *
  554. * @param all
  555. * @return {@code this}
  556. * @throws JGitInternalException
  557. * in case of an illegal combination of arguments/ options
  558. */
  559. public CommitCommand setAll(boolean all) {
  560. checkCallable();
  561. if (!only.isEmpty())
  562. throw new JGitInternalException(MessageFormat.format(
  563. JGitText.get().illegalCombinationOfArguments, "--all",
  564. "--only"));
  565. this.all = all;
  566. return this;
  567. }
  568. /**
  569. * Used to amend the tip of the current branch. If set to true, the previous
  570. * commit will be amended. This is equivalent to --amend on the command
  571. * line.
  572. *
  573. * @param amend
  574. * @return {@code this}
  575. */
  576. public CommitCommand setAmend(boolean amend) {
  577. checkCallable();
  578. this.amend = amend;
  579. return this;
  580. }
  581. /**
  582. * Commit dedicated path only
  583. *
  584. * This method can be called several times to add multiple paths. Full file
  585. * paths are supported as well as directory paths; in the latter case this
  586. * commits all files/ directories below the specified path.
  587. *
  588. * @param only
  589. * path to commit
  590. * @return {@code this}
  591. */
  592. public CommitCommand setOnly(String only) {
  593. checkCallable();
  594. if (all)
  595. throw new JGitInternalException(MessageFormat.format(
  596. JGitText.get().illegalCombinationOfArguments, "--only",
  597. "--all"));
  598. String o = only.endsWith("/") ? only.substring(0, only.length() - 1)
  599. : only;
  600. // ignore duplicates
  601. if (!this.only.contains(o))
  602. this.only.add(o);
  603. return this;
  604. }
  605. /**
  606. * If set to true a change id will be inserted into the commit message
  607. *
  608. * An existing change id is not replaced. An initial change id (I000...)
  609. * will be replaced by the change id.
  610. *
  611. * @param insertChangeId
  612. *
  613. * @return {@code this}
  614. */
  615. public CommitCommand setInsertChangeId(boolean insertChangeId) {
  616. checkCallable();
  617. this.insertChangeId = insertChangeId;
  618. return this;
  619. }
  620. /**
  621. * Override the message written to the reflog
  622. *
  623. * @param reflogComment
  624. * @return {@code this}
  625. */
  626. public CommitCommand setReflogComment(String reflogComment) {
  627. this.reflogComment = reflogComment;
  628. return this;
  629. }
  630. }