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 21KB

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