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 19KB

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