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

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