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

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