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.

CommitCommandTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (C) 2011-2012, GitHub Inc.
  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 static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNotEquals;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. import static org.junit.Assert.fail;
  50. import java.io.File;
  51. import java.util.Date;
  52. import java.util.List;
  53. import java.util.TimeZone;
  54. import org.eclipse.jgit.api.errors.EmtpyCommitException;
  55. import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
  56. import org.eclipse.jgit.diff.DiffEntry;
  57. import org.eclipse.jgit.dircache.DirCache;
  58. import org.eclipse.jgit.dircache.DirCacheBuilder;
  59. import org.eclipse.jgit.dircache.DirCacheEntry;
  60. import org.eclipse.jgit.junit.RepositoryTestCase;
  61. import org.eclipse.jgit.lib.ConfigConstants;
  62. import org.eclipse.jgit.lib.Constants;
  63. import org.eclipse.jgit.lib.FileMode;
  64. import org.eclipse.jgit.lib.ObjectId;
  65. import org.eclipse.jgit.lib.PersonIdent;
  66. import org.eclipse.jgit.lib.RefUpdate;
  67. import org.eclipse.jgit.lib.RefUpdate.Result;
  68. import org.eclipse.jgit.lib.ReflogEntry;
  69. import org.eclipse.jgit.lib.Repository;
  70. import org.eclipse.jgit.lib.StoredConfig;
  71. import org.eclipse.jgit.revwalk.RevCommit;
  72. import org.eclipse.jgit.submodule.SubmoduleWalk;
  73. import org.eclipse.jgit.treewalk.TreeWalk;
  74. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  75. import org.eclipse.jgit.util.FS;
  76. import org.junit.Ignore;
  77. import org.junit.Test;
  78. /**
  79. * Unit tests of {@link CommitCommand}.
  80. */
  81. public class CommitCommandTest extends RepositoryTestCase {
  82. @Test
  83. public void testExecutableRetention() throws Exception {
  84. StoredConfig config = db.getConfig();
  85. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  86. ConfigConstants.CONFIG_KEY_FILEMODE, true);
  87. config.save();
  88. FS executableFs = new FS() {
  89. @Override
  90. public boolean supportsExecute() {
  91. return true;
  92. }
  93. @Override
  94. public boolean setExecute(File f, boolean canExec) {
  95. return true;
  96. }
  97. @Override
  98. public ProcessBuilder runInShell(String cmd, String[] args) {
  99. return null;
  100. }
  101. @Override
  102. public boolean retryFailedLockFileCommit() {
  103. return false;
  104. }
  105. @Override
  106. public FS newInstance() {
  107. return this;
  108. }
  109. @Override
  110. protected File discoverGitExe() {
  111. return null;
  112. }
  113. @Override
  114. public boolean canExecute(File f) {
  115. return true;
  116. }
  117. @Override
  118. public boolean isCaseSensitive() {
  119. return true;
  120. }
  121. };
  122. Git git = Git.open(db.getDirectory(), executableFs);
  123. String path = "a.txt";
  124. writeTrashFile(path, "content");
  125. git.add().addFilepattern(path).call();
  126. RevCommit commit1 = git.commit().setMessage("commit").call();
  127. TreeWalk walk = TreeWalk.forPath(db, path, commit1.getTree());
  128. assertNotNull(walk);
  129. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  130. FS nonExecutableFs = new FS() {
  131. @Override
  132. public boolean supportsExecute() {
  133. return false;
  134. }
  135. @Override
  136. public boolean setExecute(File f, boolean canExec) {
  137. return false;
  138. }
  139. @Override
  140. public ProcessBuilder runInShell(String cmd, String[] args) {
  141. return null;
  142. }
  143. @Override
  144. public boolean retryFailedLockFileCommit() {
  145. return false;
  146. }
  147. @Override
  148. public FS newInstance() {
  149. return this;
  150. }
  151. @Override
  152. protected File discoverGitExe() {
  153. return null;
  154. }
  155. @Override
  156. public boolean canExecute(File f) {
  157. return false;
  158. }
  159. @Override
  160. public boolean isCaseSensitive() {
  161. return true;
  162. }
  163. };
  164. config = db.getConfig();
  165. config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null,
  166. ConfigConstants.CONFIG_KEY_FILEMODE, false);
  167. config.save();
  168. Git git2 = Git.open(db.getDirectory(), nonExecutableFs);
  169. writeTrashFile(path, "content2");
  170. RevCommit commit2 = git2.commit().setOnly(path).setMessage("commit2")
  171. .call();
  172. walk = TreeWalk.forPath(db, path, commit2.getTree());
  173. assertNotNull(walk);
  174. assertEquals(FileMode.EXECUTABLE_FILE, walk.getFileMode(0));
  175. }
  176. @Test
  177. public void commitNewSubmodule() throws Exception {
  178. try (Git git = new Git(db)) {
  179. writeTrashFile("file.txt", "content");
  180. git.add().addFilepattern("file.txt").call();
  181. RevCommit commit = git.commit().setMessage("create file").call();
  182. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  183. String path = "sub";
  184. command.setPath(path);
  185. String uri = db.getDirectory().toURI().toString();
  186. command.setURI(uri);
  187. Repository repo = command.call();
  188. assertNotNull(repo);
  189. addRepoToClose(repo);
  190. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  191. assertTrue(generator.next());
  192. assertEquals(path, generator.getPath());
  193. assertEquals(commit, generator.getObjectId());
  194. assertEquals(uri, generator.getModulesUrl());
  195. assertEquals(path, generator.getModulesPath());
  196. assertEquals(uri, generator.getConfigUrl());
  197. Repository subModRepo = generator.getRepository();
  198. assertNotNull(subModRepo);
  199. subModRepo.close();
  200. assertEquals(commit, repo.resolve(Constants.HEAD));
  201. RevCommit submoduleCommit = git.commit().setMessage("submodule add")
  202. .setOnly(path).call();
  203. assertNotNull(submoduleCommit);
  204. try (TreeWalk walk = new TreeWalk(db)) {
  205. walk.addTree(commit.getTree());
  206. walk.addTree(submoduleCommit.getTree());
  207. walk.setFilter(TreeFilter.ANY_DIFF);
  208. List<DiffEntry> diffs = DiffEntry.scan(walk);
  209. assertEquals(1, diffs.size());
  210. DiffEntry subDiff = diffs.get(0);
  211. assertEquals(FileMode.MISSING, subDiff.getOldMode());
  212. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  213. assertEquals(ObjectId.zeroId(), subDiff.getOldId().toObjectId());
  214. assertEquals(commit, subDiff.getNewId().toObjectId());
  215. assertEquals(path, subDiff.getNewPath());
  216. }
  217. }
  218. }
  219. @Test
  220. public void commitSubmoduleUpdate() throws Exception {
  221. try (Git git = new Git(db)) {
  222. writeTrashFile("file.txt", "content");
  223. git.add().addFilepattern("file.txt").call();
  224. RevCommit commit = git.commit().setMessage("create file").call();
  225. writeTrashFile("file.txt", "content2");
  226. git.add().addFilepattern("file.txt").call();
  227. RevCommit commit2 = git.commit().setMessage("edit file").call();
  228. SubmoduleAddCommand command = new SubmoduleAddCommand(db);
  229. String path = "sub";
  230. command.setPath(path);
  231. String uri = db.getDirectory().toURI().toString();
  232. command.setURI(uri);
  233. Repository repo = command.call();
  234. assertNotNull(repo);
  235. addRepoToClose(repo);
  236. SubmoduleWalk generator = SubmoduleWalk.forIndex(db);
  237. assertTrue(generator.next());
  238. assertEquals(path, generator.getPath());
  239. assertEquals(commit2, generator.getObjectId());
  240. assertEquals(uri, generator.getModulesUrl());
  241. assertEquals(path, generator.getModulesPath());
  242. assertEquals(uri, generator.getConfigUrl());
  243. Repository subModRepo = generator.getRepository();
  244. assertNotNull(subModRepo);
  245. subModRepo.close();
  246. assertEquals(commit2, repo.resolve(Constants.HEAD));
  247. RevCommit submoduleAddCommit = git.commit().setMessage("submodule add")
  248. .setOnly(path).call();
  249. assertNotNull(submoduleAddCommit);
  250. RefUpdate update = repo.updateRef(Constants.HEAD);
  251. update.setNewObjectId(commit);
  252. assertEquals(Result.FORCED, update.forceUpdate());
  253. RevCommit submoduleEditCommit = git.commit()
  254. .setMessage("submodule add").setOnly(path).call();
  255. assertNotNull(submoduleEditCommit);
  256. try (TreeWalk walk = new TreeWalk(db)) {
  257. walk.addTree(submoduleAddCommit.getTree());
  258. walk.addTree(submoduleEditCommit.getTree());
  259. walk.setFilter(TreeFilter.ANY_DIFF);
  260. List<DiffEntry> diffs = DiffEntry.scan(walk);
  261. assertEquals(1, diffs.size());
  262. DiffEntry subDiff = diffs.get(0);
  263. assertEquals(FileMode.GITLINK, subDiff.getOldMode());
  264. assertEquals(FileMode.GITLINK, subDiff.getNewMode());
  265. assertEquals(commit2, subDiff.getOldId().toObjectId());
  266. assertEquals(commit, subDiff.getNewId().toObjectId());
  267. assertEquals(path, subDiff.getNewPath());
  268. assertEquals(path, subDiff.getOldPath());
  269. }
  270. }
  271. }
  272. @Ignore("very flaky when run with Hudson")
  273. @Test
  274. public void commitUpdatesSmudgedEntries() throws Exception {
  275. try (Git git = new Git(db)) {
  276. File file1 = writeTrashFile("file1.txt", "content1");
  277. assertTrue(file1.setLastModified(file1.lastModified() - 5000));
  278. File file2 = writeTrashFile("file2.txt", "content2");
  279. assertTrue(file2.setLastModified(file2.lastModified() - 5000));
  280. File file3 = writeTrashFile("file3.txt", "content3");
  281. assertTrue(file3.setLastModified(file3.lastModified() - 5000));
  282. assertNotNull(git.add().addFilepattern("file1.txt")
  283. .addFilepattern("file2.txt").addFilepattern("file3.txt").call());
  284. RevCommit commit = git.commit().setMessage("add files").call();
  285. assertNotNull(commit);
  286. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  287. int file1Size = cache.getEntry("file1.txt").getLength();
  288. int file2Size = cache.getEntry("file2.txt").getLength();
  289. int file3Size = cache.getEntry("file3.txt").getLength();
  290. ObjectId file2Id = cache.getEntry("file2.txt").getObjectId();
  291. ObjectId file3Id = cache.getEntry("file3.txt").getObjectId();
  292. assertTrue(file1Size > 0);
  293. assertTrue(file2Size > 0);
  294. assertTrue(file3Size > 0);
  295. // Smudge entries
  296. cache = DirCache.lock(db.getIndexFile(), db.getFS());
  297. cache.getEntry("file1.txt").setLength(0);
  298. cache.getEntry("file2.txt").setLength(0);
  299. cache.getEntry("file3.txt").setLength(0);
  300. cache.write();
  301. assertTrue(cache.commit());
  302. // Verify entries smudged
  303. cache = DirCache.read(db.getIndexFile(), db.getFS());
  304. assertEquals(0, cache.getEntry("file1.txt").getLength());
  305. assertEquals(0, cache.getEntry("file2.txt").getLength());
  306. assertEquals(0, cache.getEntry("file3.txt").getLength());
  307. long indexTime = db.getIndexFile().lastModified();
  308. db.getIndexFile().setLastModified(indexTime - 5000);
  309. write(file1, "content4");
  310. assertTrue(file1.setLastModified(file1.lastModified() + 2500));
  311. assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
  312. .call());
  313. cache = db.readDirCache();
  314. assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
  315. assertEquals(file2Size, cache.getEntry("file2.txt").getLength());
  316. assertEquals(file3Size, cache.getEntry("file3.txt").getLength());
  317. assertEquals(file2Id, cache.getEntry("file2.txt").getObjectId());
  318. assertEquals(file3Id, cache.getEntry("file3.txt").getObjectId());
  319. }
  320. }
  321. @Ignore("very flaky when run with Hudson")
  322. @Test
  323. public void commitIgnoresSmudgedEntryWithDifferentId() throws Exception {
  324. try (Git git = new Git(db)) {
  325. File file1 = writeTrashFile("file1.txt", "content1");
  326. assertTrue(file1.setLastModified(file1.lastModified() - 5000));
  327. File file2 = writeTrashFile("file2.txt", "content2");
  328. assertTrue(file2.setLastModified(file2.lastModified() - 5000));
  329. assertNotNull(git.add().addFilepattern("file1.txt")
  330. .addFilepattern("file2.txt").call());
  331. RevCommit commit = git.commit().setMessage("add files").call();
  332. assertNotNull(commit);
  333. DirCache cache = DirCache.read(db.getIndexFile(), db.getFS());
  334. int file1Size = cache.getEntry("file1.txt").getLength();
  335. int file2Size = cache.getEntry("file2.txt").getLength();
  336. assertTrue(file1Size > 0);
  337. assertTrue(file2Size > 0);
  338. writeTrashFile("file2.txt", "content3");
  339. assertNotNull(git.add().addFilepattern("file2.txt").call());
  340. writeTrashFile("file2.txt", "content4");
  341. // Smudge entries
  342. cache = DirCache.lock(db.getIndexFile(), db.getFS());
  343. cache.getEntry("file1.txt").setLength(0);
  344. cache.getEntry("file2.txt").setLength(0);
  345. cache.write();
  346. assertTrue(cache.commit());
  347. // Verify entries smudged
  348. cache = db.readDirCache();
  349. assertEquals(0, cache.getEntry("file1.txt").getLength());
  350. assertEquals(0, cache.getEntry("file2.txt").getLength());
  351. long indexTime = db.getIndexFile().lastModified();
  352. db.getIndexFile().setLastModified(indexTime - 5000);
  353. write(file1, "content5");
  354. assertTrue(file1.setLastModified(file1.lastModified() + 1000));
  355. assertNotNull(git.commit().setMessage("edit file").setOnly("file1.txt")
  356. .call());
  357. cache = db.readDirCache();
  358. assertEquals(file1Size, cache.getEntry("file1.txt").getLength());
  359. assertEquals(0, cache.getEntry("file2.txt").getLength());
  360. }
  361. }
  362. @Test
  363. public void commitAfterSquashMerge() throws Exception {
  364. try (Git git = new Git(db)) {
  365. writeTrashFile("file1", "file1");
  366. git.add().addFilepattern("file1").call();
  367. RevCommit first = git.commit().setMessage("initial commit").call();
  368. assertTrue(new File(db.getWorkTree(), "file1").exists());
  369. createBranch(first, "refs/heads/branch1");
  370. checkoutBranch("refs/heads/branch1");
  371. writeTrashFile("file2", "file2");
  372. git.add().addFilepattern("file2").call();
  373. git.commit().setMessage("second commit").call();
  374. assertTrue(new File(db.getWorkTree(), "file2").exists());
  375. checkoutBranch("refs/heads/master");
  376. MergeResult result = git.merge()
  377. .include(db.exactRef("refs/heads/branch1"))
  378. .setSquash(true)
  379. .call();
  380. assertTrue(new File(db.getWorkTree(), "file1").exists());
  381. assertTrue(new File(db.getWorkTree(), "file2").exists());
  382. assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
  383. result.getMergeStatus());
  384. // comment not set, should be inferred from SQUASH_MSG
  385. RevCommit squashedCommit = git.commit().call();
  386. assertEquals(1, squashedCommit.getParentCount());
  387. assertNull(db.readSquashCommitMsg());
  388. assertEquals("commit: Squashed commit of the following:", db
  389. .getReflogReader(Constants.HEAD).getLastEntry().getComment());
  390. assertEquals("commit: Squashed commit of the following:", db
  391. .getReflogReader(db.getBranch()).getLastEntry().getComment());
  392. }
  393. }
  394. @Test
  395. public void testReflogs() throws Exception {
  396. try (Git git = new Git(db)) {
  397. writeTrashFile("f", "1");
  398. git.add().addFilepattern("f").call();
  399. git.commit().setMessage("c1").call();
  400. writeTrashFile("f", "2");
  401. git.commit().setMessage("c2").setAll(true).setReflogComment(null)
  402. .call();
  403. writeTrashFile("f", "3");
  404. git.commit().setMessage("c3").setAll(true)
  405. .setReflogComment("testRl").call();
  406. db.getReflogReader(Constants.HEAD).getReverseEntries();
  407. assertEquals("testRl;commit (initial): c1;", reflogComments(
  408. db.getReflogReader(Constants.HEAD).getReverseEntries()));
  409. assertEquals("testRl;commit (initial): c1;", reflogComments(
  410. db.getReflogReader(db.getBranch()).getReverseEntries()));
  411. }
  412. }
  413. private static String reflogComments(List<ReflogEntry> entries) {
  414. StringBuffer b = new StringBuffer();
  415. for (ReflogEntry e : entries) {
  416. b.append(e.getComment()).append(";");
  417. }
  418. return b.toString();
  419. }
  420. @Test(expected = WrongRepositoryStateException.class)
  421. public void commitAmendOnInitialShouldFail() throws Exception {
  422. try (Git git = new Git(db)) {
  423. git.commit().setAmend(true).setMessage("initial commit").call();
  424. }
  425. }
  426. @Test
  427. public void commitAmendWithoutAuthorShouldSetOriginalAuthorAndAuthorTime()
  428. throws Exception {
  429. try (Git git = new Git(db)) {
  430. writeTrashFile("file1", "file1");
  431. git.add().addFilepattern("file1").call();
  432. final String authorName = "First Author";
  433. final String authorEmail = "author@example.org";
  434. final Date authorDate = new Date(1349621117000L);
  435. PersonIdent firstAuthor = new PersonIdent(authorName, authorEmail,
  436. authorDate, TimeZone.getTimeZone("UTC"));
  437. git.commit().setMessage("initial commit").setAuthor(firstAuthor).call();
  438. RevCommit amended = git.commit().setAmend(true)
  439. .setMessage("amend commit").call();
  440. PersonIdent amendedAuthor = amended.getAuthorIdent();
  441. assertEquals(authorName, amendedAuthor.getName());
  442. assertEquals(authorEmail, amendedAuthor.getEmailAddress());
  443. assertEquals(authorDate.getTime(), amendedAuthor.getWhen().getTime());
  444. }
  445. }
  446. @Test
  447. public void commitAmendWithAuthorShouldUseIt() throws Exception {
  448. try (Git git = new Git(db)) {
  449. writeTrashFile("file1", "file1");
  450. git.add().addFilepattern("file1").call();
  451. git.commit().setMessage("initial commit").call();
  452. RevCommit amended = git.commit().setAmend(true)
  453. .setAuthor("New Author", "newauthor@example.org")
  454. .setMessage("amend commit").call();
  455. PersonIdent amendedAuthor = amended.getAuthorIdent();
  456. assertEquals("New Author", amendedAuthor.getName());
  457. assertEquals("newauthor@example.org", amendedAuthor.getEmailAddress());
  458. }
  459. }
  460. @Test
  461. public void commitEmptyCommits() throws Exception {
  462. try (Git git = new Git(db)) {
  463. writeTrashFile("file1", "file1");
  464. git.add().addFilepattern("file1").call();
  465. RevCommit initial = git.commit().setMessage("initial commit")
  466. .call();
  467. RevCommit emptyFollowUp = git.commit()
  468. .setAuthor("New Author", "newauthor@example.org")
  469. .setMessage("no change").call();
  470. assertNotEquals(initial.getId(), emptyFollowUp.getId());
  471. assertEquals(initial.getTree().getId(),
  472. emptyFollowUp.getTree().getId());
  473. try {
  474. git.commit().setAuthor("New Author", "newauthor@example.org")
  475. .setMessage("again no change").setAllowEmpty(false)
  476. .call();
  477. fail("Didn't get the expected EmtpyCommitException");
  478. } catch (EmtpyCommitException e) {
  479. // expect this exception
  480. }
  481. // Allow empty commits also when setOnly was set
  482. git.commit().setAuthor("New Author", "newauthor@example.org")
  483. .setMessage("again no change").setOnly("file1")
  484. .setAllowEmpty(true).call();
  485. }
  486. }
  487. @Test
  488. public void commitOnlyShouldCommitUnmergedPathAndNotAffectOthers()
  489. throws Exception {
  490. DirCache index = db.lockDirCache();
  491. DirCacheBuilder builder = index.builder();
  492. addUnmergedEntry("unmerged1", builder);
  493. addUnmergedEntry("unmerged2", builder);
  494. DirCacheEntry other = new DirCacheEntry("other");
  495. other.setFileMode(FileMode.REGULAR_FILE);
  496. builder.add(other);
  497. builder.commit();
  498. writeTrashFile("unmerged1", "unmerged1 data");
  499. writeTrashFile("unmerged2", "unmerged2 data");
  500. writeTrashFile("other", "other data");
  501. assertEquals("[other, mode:100644]"
  502. + "[unmerged1, mode:100644, stage:1]"
  503. + "[unmerged1, mode:100644, stage:2]"
  504. + "[unmerged1, mode:100644, stage:3]"
  505. + "[unmerged2, mode:100644, stage:1]"
  506. + "[unmerged2, mode:100644, stage:2]"
  507. + "[unmerged2, mode:100644, stage:3]",
  508. indexState(0));
  509. try (Git git = new Git(db)) {
  510. RevCommit commit = git.commit().setOnly("unmerged1")
  511. .setMessage("Only one file").call();
  512. assertEquals("[other, mode:100644]" + "[unmerged1, mode:100644]"
  513. + "[unmerged2, mode:100644, stage:1]"
  514. + "[unmerged2, mode:100644, stage:2]"
  515. + "[unmerged2, mode:100644, stage:3]",
  516. indexState(0));
  517. try (TreeWalk walk = TreeWalk.forPath(db, "unmerged1", commit.getTree())) {
  518. assertEquals(FileMode.REGULAR_FILE, walk.getFileMode(0));
  519. }
  520. }
  521. }
  522. @Test
  523. public void commitOnlyShouldHandleIgnored() throws Exception {
  524. try (Git git = new Git(db)) {
  525. writeTrashFile("subdir/foo", "Hello World");
  526. writeTrashFile("subdir/bar", "Hello World");
  527. writeTrashFile(".gitignore", "bar");
  528. git.add().addFilepattern("subdir").call();
  529. git.commit().setOnly("subdir").setMessage("first commit").call();
  530. }
  531. }
  532. private static void addUnmergedEntry(String file, DirCacheBuilder builder) {
  533. DirCacheEntry stage1 = new DirCacheEntry(file, DirCacheEntry.STAGE_1);
  534. DirCacheEntry stage2 = new DirCacheEntry(file, DirCacheEntry.STAGE_2);
  535. DirCacheEntry stage3 = new DirCacheEntry(file, DirCacheEntry.STAGE_3);
  536. stage1.setFileMode(FileMode.REGULAR_FILE);
  537. stage2.setFileMode(FileMode.REGULAR_FILE);
  538. stage3.setFileMode(FileMode.REGULAR_FILE);
  539. builder.add(stage1);
  540. builder.add(stage2);
  541. builder.add(stage3);
  542. }
  543. }