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

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