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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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. /**
  106. * @param repo
  107. */
  108. protected CommitCommand(Repository repo) {
  109. super(repo);
  110. }
  111. /**
  112. * Executes the {@code commit} command with all the options and parameters
  113. * collected by the setter methods of this class. Each instance of this
  114. * class should only be used for one invocation of the command (means: one
  115. * call to {@link #call()})
  116. *
  117. * @return a {@link RevCommit} object representing the successful commit.
  118. * @throws NoHeadException
  119. * when called on a git repo without a HEAD reference
  120. * @throws NoMessageException
  121. * when called without specifying a commit message
  122. * @throws UnmergedPathException
  123. * when the current index contained unmerged paths (conflicts)
  124. * @throws WrongRepositoryStateException
  125. * when repository is not in the right state for committing
  126. * @throws JGitInternalException
  127. * a low-level exception of JGit has occurred. The original
  128. * exception can be retrieved by calling
  129. * {@link Exception#getCause()}. Expect only
  130. * {@code IOException's} to be wrapped. Subclasses of
  131. * {@link IOException} (e.g. {@link UnmergedPathException}) are
  132. * typically not wrapped here but thrown as original exception
  133. */
  134. public RevCommit call() throws NoHeadException, NoMessageException,
  135. UnmergedPathException, ConcurrentRefUpdateException,
  136. JGitInternalException, 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. String prefix = amend ? "commit (amend): " : "commit: ";
  199. ru.setRefLogMessage(
  200. prefix + revCommit.getShortMessage(), false);
  201. ru.setExpectedOldObjectId(headId);
  202. Result rc = ru.forceUpdate();
  203. switch (rc) {
  204. case NEW:
  205. case FORCED:
  206. case FAST_FORWARD: {
  207. setCallable(false);
  208. if (state == RepositoryState.MERGING_RESOLVED) {
  209. // Commit was successful. Now delete the files
  210. // used for merge commits
  211. repo.writeMergeCommitMsg(null);
  212. repo.writeMergeHeads(null);
  213. } else if (state == RepositoryState.CHERRY_PICKING_RESOLVED) {
  214. repo.writeMergeCommitMsg(null);
  215. repo.writeCherryPickHead(null);
  216. }
  217. return revCommit;
  218. }
  219. case REJECTED:
  220. case LOCK_FAILURE:
  221. throw new ConcurrentRefUpdateException(JGitText
  222. .get().couldNotLockHEAD, ru.getRef(), rc);
  223. default:
  224. throw new JGitInternalException(MessageFormat
  225. .format(JGitText.get().updatingRefFailed,
  226. Constants.HEAD,
  227. commitId.toString(), rc));
  228. }
  229. } finally {
  230. revWalk.release();
  231. }
  232. } finally {
  233. odi.release();
  234. }
  235. } finally {
  236. index.unlock();
  237. }
  238. } catch (UnmergedPathException e) {
  239. // since UnmergedPathException is a subclass of IOException
  240. // which should not be wrapped by a JGitInternalException we
  241. // have to catch and re-throw it here
  242. throw e;
  243. } catch (IOException e) {
  244. throw new JGitInternalException(
  245. JGitText.get().exceptionCaughtDuringExecutionOfCommitCommand, e);
  246. }
  247. }
  248. private void insertChangeId(ObjectId treeId) throws IOException {
  249. ObjectId firstParentId = null;
  250. if (!parents.isEmpty())
  251. firstParentId = parents.get(0);
  252. ObjectId changeId = ChangeIdUtil.computeChangeId(treeId, firstParentId,
  253. author, committer, message);
  254. message = ChangeIdUtil.insertId(message, changeId);
  255. if (changeId != null)
  256. message = message.replaceAll("\nChange-Id: I"
  257. + ObjectId.zeroId().getName() + "\n", "\nChange-Id: I"
  258. + changeId.getName() + "\n");
  259. }
  260. private DirCache createTemporaryIndex(ObjectId headId, DirCache index)
  261. throws IOException {
  262. ObjectInserter inserter = null;
  263. // get DirCacheEditor to modify the index if required
  264. DirCacheEditor dcEditor = index.editor();
  265. // get DirCacheBuilder for newly created in-core index to build a
  266. // temporary index for this commit
  267. DirCache inCoreIndex = DirCache.newInCore();
  268. DirCacheBuilder dcBuilder = inCoreIndex.builder();
  269. onlyProcessed = new boolean[only.size()];
  270. boolean emptyCommit = true;
  271. TreeWalk treeWalk = new TreeWalk(repo);
  272. int dcIdx = treeWalk.addTree(new DirCacheIterator(index));
  273. int fIdx = treeWalk.addTree(new FileTreeIterator(repo));
  274. int hIdx = -1;
  275. if (headId != null)
  276. hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
  277. treeWalk.setRecursive(true);
  278. while (treeWalk.next()) {
  279. String path = treeWalk.getPathString();
  280. // check if current entry's path matches a specified path
  281. int pos = lookupOnly(path);
  282. CanonicalTreeParser hTree = null;
  283. if (hIdx != -1)
  284. hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
  285. if (pos >= 0) {
  286. // include entry in commit
  287. DirCacheIterator dcTree = treeWalk.getTree(dcIdx,
  288. DirCacheIterator.class);
  289. FileTreeIterator fTree = treeWalk.getTree(fIdx,
  290. FileTreeIterator.class);
  291. // check if entry refers to a tracked file
  292. boolean tracked = dcTree != null || hTree != null;
  293. if (!tracked)
  294. break;
  295. if (fTree != null) {
  296. // create a new DirCacheEntry with data retrieved from disk
  297. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  298. long entryLength = fTree.getEntryLength();
  299. dcEntry.setLength(entryLength);
  300. dcEntry.setLastModified(fTree.getEntryLastModified());
  301. dcEntry.setFileMode(fTree.getEntryFileMode());
  302. boolean objectExists = (dcTree != null && fTree
  303. .idEqual(dcTree))
  304. || (hTree != null && fTree.idEqual(hTree));
  305. if (objectExists) {
  306. dcEntry.setObjectId(fTree.getEntryObjectId());
  307. } else {
  308. if (FileMode.GITLINK.equals(dcEntry.getFileMode())) {
  309. // Do not check the content of submodule entries
  310. // Use the old entry information instead.
  311. dcEntry.copyMetaData(index.getEntry(dcEntry
  312. .getPathString()));
  313. } else {
  314. // insert object
  315. if (inserter == null)
  316. inserter = repo.newObjectInserter();
  317. InputStream inputStream = fTree.openEntryStream();
  318. try {
  319. dcEntry.setObjectId(inserter.insert(
  320. Constants.OBJ_BLOB, entryLength,
  321. inputStream));
  322. } finally {
  323. inputStream.close();
  324. }
  325. }
  326. }
  327. // update index
  328. dcEditor.add(new PathEdit(path) {
  329. @Override
  330. public void apply(DirCacheEntry ent) {
  331. ent.copyMetaData(dcEntry);
  332. }
  333. });
  334. // add to temporary in-core index
  335. dcBuilder.add(dcEntry);
  336. if (emptyCommit && (hTree == null || !hTree.idEqual(fTree)))
  337. // this is a change
  338. emptyCommit = false;
  339. } else {
  340. // if no file exists on disk, remove entry from index and
  341. // don't add it to temporary in-core index
  342. dcEditor.add(new DeletePath(path));
  343. if (emptyCommit && hTree != null)
  344. // this is a change
  345. emptyCommit = false;
  346. }
  347. // keep track of processed path
  348. onlyProcessed[pos] = true;
  349. } else {
  350. // add entries from HEAD for all other paths
  351. if (hTree != null) {
  352. // create a new DirCacheEntry with data retrieved from HEAD
  353. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  354. dcEntry.setObjectId(hTree.getEntryObjectId());
  355. dcEntry.setFileMode(hTree.getEntryFileMode());
  356. // add to temporary in-core index
  357. dcBuilder.add(dcEntry);
  358. }
  359. }
  360. }
  361. // there must be no unprocessed paths left at this point; otherwise an
  362. // untracked or unknown path has been specified
  363. for (int i = 0; i < onlyProcessed.length; i++)
  364. if (!onlyProcessed[i])
  365. throw new JGitInternalException(MessageFormat.format(
  366. JGitText.get().entryNotFoundByPath, only.get(i)));
  367. // there must be at least one change
  368. if (emptyCommit)
  369. throw new JGitInternalException(JGitText.get().emptyCommit);
  370. // update index
  371. dcEditor.commit();
  372. // finish temporary in-core index used for this commit
  373. dcBuilder.finish();
  374. return inCoreIndex;
  375. }
  376. /**
  377. * Look an entry's path up in the list of paths specified by the --only/ -o
  378. * option
  379. *
  380. * In case the complete (file) path (e.g. "d1/d2/f1") cannot be found in
  381. * <code>only</code>, lookup is also tried with (parent) directory paths
  382. * (e.g. "d1/d2" and "d1").
  383. *
  384. * @param pathString
  385. * entry's path
  386. * @return the item's index in <code>only</code>; -1 if no item matches
  387. */
  388. private int lookupOnly(String pathString) {
  389. int i = 0;
  390. for (String o : only) {
  391. String p = pathString;
  392. while (true) {
  393. if (p.equals(o))
  394. return i;
  395. int l = p.lastIndexOf("/");
  396. if (l < 1)
  397. break;
  398. p = p.substring(0, l);
  399. }
  400. i++;
  401. }
  402. return -1;
  403. }
  404. /**
  405. * Sets default values for not explicitly specified options. Then validates
  406. * that all required data has been provided.
  407. *
  408. * @param state
  409. * the state of the repository we are working on
  410. *
  411. * @throws NoMessageException
  412. * if the commit message has not been specified
  413. */
  414. private void processOptions(RepositoryState state) throws NoMessageException {
  415. if (committer == null)
  416. committer = new PersonIdent(repo);
  417. if (author == null)
  418. author = committer;
  419. // when doing a merge commit parse MERGE_HEAD and MERGE_MSG files
  420. if (state == RepositoryState.MERGING_RESOLVED) {
  421. try {
  422. parents = repo.readMergeHeads();
  423. } catch (IOException e) {
  424. throw new JGitInternalException(MessageFormat.format(
  425. JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR,
  426. Constants.MERGE_HEAD, e), e);
  427. }
  428. if (message == null) {
  429. try {
  430. message = repo.readMergeCommitMsg();
  431. } catch (IOException e) {
  432. throw new JGitInternalException(MessageFormat.format(
  433. JGitText.get().exceptionOccurredDuringReadingOfGIT_DIR,
  434. Constants.MERGE_MSG, e), e);
  435. }
  436. }
  437. }
  438. if (message == null)
  439. // as long as we don't suppport -C option we have to have
  440. // an explicit message
  441. throw new NoMessageException(JGitText.get().commitMessageNotSpecified);
  442. }
  443. /**
  444. * @param message
  445. * the commit message used for the {@code commit}
  446. * @return {@code this}
  447. */
  448. public CommitCommand setMessage(String message) {
  449. checkCallable();
  450. this.message = message;
  451. return this;
  452. }
  453. /**
  454. * @return the commit message used for the <code>commit</code>
  455. */
  456. public String getMessage() {
  457. return message;
  458. }
  459. /**
  460. * Sets the committer for this {@code commit}. If no committer is explicitly
  461. * specified because this method is never called or called with {@code null}
  462. * value then the committer will be deduced from config info in repository,
  463. * with current time.
  464. *
  465. * @param committer
  466. * the committer used for the {@code commit}
  467. * @return {@code this}
  468. */
  469. public CommitCommand setCommitter(PersonIdent committer) {
  470. checkCallable();
  471. this.committer = committer;
  472. return this;
  473. }
  474. /**
  475. * Sets the committer for this {@code commit}. If no committer is explicitly
  476. * specified because this method is never called or called with {@code null}
  477. * value then the committer will be deduced from config info in repository,
  478. * with current time.
  479. *
  480. * @param name
  481. * the name of the committer used for the {@code commit}
  482. * @param email
  483. * the email of the committer used for the {@code commit}
  484. * @return {@code this}
  485. */
  486. public CommitCommand setCommitter(String name, String email) {
  487. checkCallable();
  488. return setCommitter(new PersonIdent(name, email));
  489. }
  490. /**
  491. * @return the committer used for the {@code commit}. If no committer was
  492. * specified {@code null} is returned and the default
  493. * {@link PersonIdent} of this repo is used during execution of the
  494. * command
  495. */
  496. public PersonIdent getCommitter() {
  497. return committer;
  498. }
  499. /**
  500. * Sets the author for this {@code commit}. If no author is explicitly
  501. * specified because this method is never called or called with {@code null}
  502. * value then the author will be set to the committer.
  503. *
  504. * @param author
  505. * the author used for the {@code commit}
  506. * @return {@code this}
  507. */
  508. public CommitCommand setAuthor(PersonIdent author) {
  509. checkCallable();
  510. this.author = author;
  511. return this;
  512. }
  513. /**
  514. * Sets the author for this {@code commit}. If no author is explicitly
  515. * specified because this method is never called or called with {@code null}
  516. * value then the author will be set to the committer.
  517. *
  518. * @param name
  519. * the name of the author used for the {@code commit}
  520. * @param email
  521. * the email of the author used for the {@code commit}
  522. * @return {@code this}
  523. */
  524. public CommitCommand setAuthor(String name, String email) {
  525. checkCallable();
  526. return setAuthor(new PersonIdent(name, email));
  527. }
  528. /**
  529. * @return the author used for the {@code commit}. If no author was
  530. * specified {@code null} is returned and the default
  531. * {@link PersonIdent} of this repo is used during execution of the
  532. * command
  533. */
  534. public PersonIdent getAuthor() {
  535. return author;
  536. }
  537. /**
  538. * If set to true the Commit command automatically stages files that have
  539. * been modified and deleted, but new files not known by the repository are
  540. * not affected. This corresponds to the parameter -a on the command line.
  541. *
  542. * @param all
  543. * @return {@code this}
  544. * @throws JGitInternalException
  545. * in case of an illegal combination of arguments/ options
  546. */
  547. public CommitCommand setAll(boolean all) {
  548. checkCallable();
  549. if (!only.isEmpty())
  550. throw new JGitInternalException(MessageFormat.format(
  551. JGitText.get().illegalCombinationOfArguments, "--all",
  552. "--only"));
  553. this.all = all;
  554. return this;
  555. }
  556. /**
  557. * Used to amend the tip of the current branch. If set to true, the previous
  558. * commit will be amended. This is equivalent to --amend on the command
  559. * line.
  560. *
  561. * @param amend
  562. * @return {@code this}
  563. */
  564. public CommitCommand setAmend(boolean amend) {
  565. checkCallable();
  566. this.amend = amend;
  567. return this;
  568. }
  569. /**
  570. * Commit dedicated path only
  571. *
  572. * This method can be called several times to add multiple paths. Full file
  573. * paths are supported as well as directory paths; in the latter case this
  574. * commits all files/ directories below the specified path.
  575. *
  576. * @param only
  577. * path to commit
  578. * @return {@code this}
  579. */
  580. public CommitCommand setOnly(String only) {
  581. checkCallable();
  582. if (all)
  583. throw new JGitInternalException(MessageFormat.format(
  584. JGitText.get().illegalCombinationOfArguments, "--only",
  585. "--all"));
  586. String o = only.endsWith("/") ? only.substring(0, only.length() - 1)
  587. : only;
  588. // ignore duplicates
  589. if (!this.only.contains(o))
  590. this.only.add(o);
  591. return this;
  592. }
  593. /**
  594. * If set to true a change id will be inserted into the commit message
  595. *
  596. * An existing change id is not replaced. An initial change id (I000...)
  597. * will be replaced by the change id.
  598. *
  599. * @param insertChangeId
  600. *
  601. * @return {@code this}
  602. */
  603. public CommitCommand setInsertChangeId(boolean insertChangeId) {
  604. checkCallable();
  605. this.insertChangeId = insertChangeId;
  606. return this;
  607. }
  608. }