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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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. ru.setExpectedOldObjectId(headId);
  208. Result rc = ru.forceUpdate();
  209. switch (rc) {
  210. case NEW:
  211. case FORCED:
  212. case FAST_FORWARD: {
  213. setCallable(false);
  214. if (state == RepositoryState.MERGING_RESOLVED) {
  215. // Commit was successful. Now delete the files
  216. // used for merge commits
  217. repo.writeMergeCommitMsg(null);
  218. repo.writeMergeHeads(null);
  219. } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
  220. repo.writeMergeCommitMsg(null);
  221. repo.writeCherryPickHead(null);
  222. }
  223. return revCommit;
  224. }
  225. case REJECTED:
  226. case LOCK_FAILURE:
  227. throw new ConcurrentRefUpdateException(JGitText
  228. .get().couldNotLockHEAD, ru.getRef(), rc);
  229. default:
  230. throw new JGitInternalException(MessageFormat
  231. .format(JGitText.get().updatingRefFailed,
  232. Constants.HEAD,
  233. commitId.toString(), rc));
  234. }
  235. } finally {
  236. revWalk.release();
  237. }
  238. } finally {
  239. odi.release();
  240. }
  241. } finally {
  242. index.unlock();
  243. }
  244. } catch (UnmergedPathException e) {
  245. // since UnmergedPathException is a subclass of IOException
  246. // which should not be wrapped by a JGitInternalException we
  247. // have to catch and re-throw it here
  248. throw e;
  249. } catch (IOException e) {
  250. throw new JGitInternalException(
  251. JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
  252. }
  253. }
  254. private void insertChangeId(ObjectId treeId) throws IOException {
  255. ObjectId firstParentId = null;
  256. if (!parents.isEmpty())
  257. firstParentId = parents.get(0);
  258. ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId,
  259. author, committer, message);
  260. message = ChangeIdUtil.insertId(message, changeId);
  261. if (changeId != null)
  262. message = message.replaceAll("\nChange-Id: I"
  263. + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I"
  264. + changeId.getName() + "\n");
  265. }
  266. private DirCache createTemporaryIndex(ObjectId headId, DirCache index)
  267. throws IOException {
  268. ObjectInserter inserter = null;
  269. // get DirCacheEditor to modify the index if required
  270. DirCacheEditor dcEditor = index.editor();
  271. // get DirCacheBuilder for newly created in-core index to build a
  272. // temporary index for this commit
  273. DirCache inCoreIndex = DirCache.newInCore();
  274. DirCacheBuilder dcBuilder = inCoreIndex.builder();
  275. onlyProcessed = new boolean[only.size()];
  276. boolean emptyCommit = true;
  277. TreeWalk treeWalk = new TreeWalk(repo);
  278. int dcIdx = treeWalk.addTree(new DirCacheIterator(index));
  279. int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
  280. int hIdx = -1;
  281. if (headId != null)
  282. hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
  283. treeWalk.setRecursive(true);
  284. while (treeWalk.next()) {
  285. String path = treeWalk.getPathString();
  286. // check if current entry's path matches a specified path
  287. int pos = lookupOnly(path);
  288. CanonicalTreeParser hTree = null;
  289. if (hIdx != -1)
  290. hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
  291. if (pos >= 0) {
  292. // include entry in commit
  293. DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
  294. DirCacheIterator.class);
  295. FileTreeIterator fTree = treeWalk.getTree(fIdx,
  296. FileTreeIterator.class);
  297. // check if entry refers to a tracked file
  298. boolean tracked = dcTree != null || hTree != null;
  299. if (!tracked)
  300. break;
  301. if (fTree != null) {
  302. // create a new DirCacheEntry with data retrieved from disk
  303. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  304. long entryLength = fTree.getEntryLength();
  305. dcEntry.setLength(entryLength);
  306. dcEntry.setLastModified(fTree.getEntryLastModified());
  307. dcEntry.setFileMode(fTree.getEntryFileMode());
  308. boolean objectExists = (dcTree != null && fTree
  309. .idEqual(dcTree))
  310. || (hTree != null && fTree.idEqual(hTree));
  311. if (objectExists) {
  312. dcEntry.setObjectId(fTree.getEntryObjectId());
  313. } else {
  314. if (FileMode.GITLINK.equals(dcEntry.getFileMode())) {
  315. // Do not check the content of submodule entries
  316. // Use the old entry information instead.
  317. dcEntry.copyMetaData(index.getEntry(dcEntry
  318. .getPathString()));
  319. } else {
  320. // insert object
  321. if (inserter == null)
  322. inserter = repo.newObjectInserter();
  323. InputStream inputStream = fTree.openEntryStream();
  324. try {
  325. dcEntry.setObjectId(inserter.insert(
  326. Constants.OBJ_BLOB, entryLength,
  327. inputStream));
  328. } finally {
  329. inputStream.close();
  330. }
  331. }
  332. }
  333. // update index
  334. dcEditor.add(new PathEdit(path) {
  335. @Override
  336. public void apply(DirCacheEntry ent) {
  337. ent.copyMetaData(dcEntry);
  338. }
  339. });
  340. // add to temporary in-core index
  341. dcBuilder.add(dcEntry);
  342. if (emptyCommit && (hTree == null || !hTree.idEqual(fTree)))
  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. }