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.

ResetCommandTest.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (C) 2011-2018, Chris Aniszczyk <caniszczyk@gmail.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.api;
  11. import static java.time.Instant.EPOCH;
  12. import static org.eclipse.jgit.api.ResetCommand.ResetType.HARD;
  13. import static org.junit.Assert.assertEquals;
  14. import static org.junit.Assert.assertFalse;
  15. import static org.junit.Assert.assertNotNull;
  16. import static org.junit.Assert.assertNull;
  17. import static org.junit.Assert.assertTrue;
  18. import static org.junit.Assert.fail;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import java.nio.file.Files;
  22. import java.nio.file.attribute.FileTime;
  23. import java.time.Instant;
  24. import org.eclipse.jgit.api.ResetCommand.ResetType;
  25. import org.eclipse.jgit.api.errors.GitAPIException;
  26. import org.eclipse.jgit.api.errors.JGitInternalException;
  27. import org.eclipse.jgit.dircache.DirCache;
  28. import org.eclipse.jgit.dircache.DirCacheBuilder;
  29. import org.eclipse.jgit.dircache.DirCacheEntry;
  30. import org.eclipse.jgit.errors.AmbiguousObjectException;
  31. import org.eclipse.jgit.junit.RepositoryTestCase;
  32. import org.eclipse.jgit.lib.Constants;
  33. import org.eclipse.jgit.lib.FileMode;
  34. import org.eclipse.jgit.lib.ObjectId;
  35. import org.eclipse.jgit.lib.Ref;
  36. import org.eclipse.jgit.revwalk.RevCommit;
  37. import org.eclipse.jgit.revwalk.RevWalk;
  38. import org.eclipse.jgit.treewalk.TreeWalk;
  39. import org.eclipse.jgit.util.FileUtils;
  40. import org.junit.Assert;
  41. import org.junit.Test;
  42. public class ResetCommandTest extends RepositoryTestCase {
  43. private Git git;
  44. private RevCommit initialCommit;
  45. private RevCommit secondCommit;
  46. private File indexFile;
  47. private File indexNestedFile;
  48. private File untrackedFile;
  49. private DirCacheEntry prestage;
  50. public void setupRepository() throws IOException, JGitInternalException,
  51. GitAPIException {
  52. // create initial commit
  53. git = new Git(db);
  54. initialCommit = git.commit().setMessage("initial commit").call();
  55. // create file
  56. indexFile = writeTrashFile("a.txt", "content");
  57. // create nested file
  58. indexNestedFile = writeTrashFile("dir/b.txt", "content");
  59. // add files and commit them
  60. git.add().addFilepattern("a.txt").addFilepattern("dir/b.txt").call();
  61. secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt").call();
  62. prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName());
  63. // modify files and add to index
  64. writeTrashFile("a.txt", "new content");
  65. writeTrashFile("dir/b.txt", "new content");
  66. git.add().addFilepattern("a.txt").addFilepattern("dir/b.txt").call();
  67. // create a file not added to the index
  68. untrackedFile = writeTrashFile("notAddedToIndex.txt", "content");
  69. }
  70. @Test
  71. public void testHardReset() throws JGitInternalException,
  72. AmbiguousObjectException, IOException, GitAPIException {
  73. setupRepository();
  74. ObjectId prevHead = db.resolve(Constants.HEAD);
  75. ResetCommand reset = git.reset();
  76. assertSameAsHead(reset.setMode(ResetType.HARD)
  77. .setRef(initialCommit.getName()).call());
  78. assertFalse("reflog should be enabled", reset.isReflogDisabled());
  79. // check if HEAD points to initial commit now
  80. ObjectId head = db.resolve(Constants.HEAD);
  81. assertEquals(initialCommit, head);
  82. // check if files were removed
  83. assertFalse(indexFile.exists());
  84. assertFalse(indexNestedFile.exists());
  85. assertTrue(untrackedFile.exists());
  86. // fileInIndex must no longer be in HEAD and in the index
  87. String fileInIndexPath = indexFile.getAbsolutePath();
  88. assertFalse(inHead(fileInIndexPath));
  89. assertFalse(inIndex(indexFile.getName()));
  90. assertReflog(prevHead, head);
  91. assertEquals(prevHead, db.readOrigHead());
  92. }
  93. @Test
  94. public void testHardResetReflogDisabled() throws Exception {
  95. setupRepository();
  96. ObjectId prevHead = db.resolve(Constants.HEAD);
  97. ResetCommand reset = git.reset();
  98. assertSameAsHead(reset.setMode(ResetType.HARD)
  99. .setRef(initialCommit.getName()).disableRefLog(true).call());
  100. assertTrue("reflog should be disabled", reset.isReflogDisabled());
  101. // check if HEAD points to initial commit now
  102. ObjectId head = db.resolve(Constants.HEAD);
  103. assertEquals(initialCommit, head);
  104. // check if files were removed
  105. assertFalse(indexFile.exists());
  106. assertFalse(indexNestedFile.exists());
  107. assertTrue(untrackedFile.exists());
  108. // fileInIndex must no longer be in HEAD and in the index
  109. String fileInIndexPath = indexFile.getAbsolutePath();
  110. assertFalse(inHead(fileInIndexPath));
  111. assertFalse(inIndex(indexFile.getName()));
  112. assertReflogDisabled(head);
  113. assertEquals(prevHead, db.readOrigHead());
  114. }
  115. @Test
  116. public void testHardResetWithConflicts_OverwriteUntrackedFile() throws Exception {
  117. setupRepository();
  118. git.rm().setCached(true).addFilepattern("a.txt").call();
  119. assertTrue(new File(db.getWorkTree(), "a.txt").exists());
  120. git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call();
  121. assertTrue(new File(db.getWorkTree(), "a.txt").exists());
  122. assertEquals("content", read(new File(db.getWorkTree(), "a.txt")));
  123. }
  124. @Test
  125. public void testHardResetWithConflicts_DeleteFileFolderConflict() throws Exception {
  126. setupRepository();
  127. writeTrashFile("dir-or-file/c.txt", "content");
  128. git.add().addFilepattern("dir-or-file/c.txt").call();
  129. FileUtils.delete(new File(db.getWorkTree(), "dir-or-file"), FileUtils.RECURSIVE);
  130. writeTrashFile("dir-or-file", "content");
  131. git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call();
  132. assertFalse(new File(db.getWorkTree(), "dir-or-file").exists());
  133. }
  134. @Test
  135. public void testHardResetWithConflicts_CreateFolder_UnstagedChanges() throws Exception {
  136. setupRepository();
  137. writeTrashFile("dir-or-file/c.txt", "content");
  138. git.add().addFilepattern("dir-or-file/c.txt").call();
  139. git.commit().setMessage("adding dir-or-file/c.txt").call();
  140. FileUtils.delete(new File(db.getWorkTree(), "dir-or-file"), FileUtils.RECURSIVE);
  141. writeTrashFile("dir-or-file", "content");
  142. // bug 479266: cannot create folder "dir-or-file"
  143. git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call();
  144. assertTrue(new File(db.getWorkTree(), "dir-or-file/c.txt").exists());
  145. }
  146. @Test
  147. public void testHardResetWithConflicts_DeleteFolder_UnstagedChanges() throws Exception {
  148. setupRepository();
  149. ObjectId prevHead = db.resolve(Constants.HEAD);
  150. writeTrashFile("dir-or-file/c.txt", "content");
  151. git.add().addFilepattern("dir-or-file/c.txt").call();
  152. git.commit().setMessage("adding dir-or-file/c.txt").call();
  153. writeTrashFile("dir-or-file-2/d.txt", "content");
  154. git.add().addFilepattern("dir-or-file-2/d.txt").call();
  155. FileUtils.delete(new File(db.getWorkTree(), "dir-or-file-2"), FileUtils.RECURSIVE);
  156. writeTrashFile("dir-or-file-2", "content");
  157. // bug 479266: cannot delete folder "dir-or-file"
  158. git.reset().setMode(ResetType.HARD).setRef(prevHead.getName()).call();
  159. assertFalse(new File(db.getWorkTree(), "dir-or-file").exists());
  160. assertFalse(new File(db.getWorkTree(), "dir-or-file-2").exists());
  161. }
  162. @Test
  163. public void testResetToNonexistingHEAD() throws JGitInternalException,
  164. AmbiguousObjectException, IOException, GitAPIException {
  165. // create a file in the working tree of a fresh repo
  166. git = new Git(db);
  167. writeTrashFile("f", "content");
  168. try {
  169. git.reset().setRef(Constants.HEAD).call();
  170. fail("Expected JGitInternalException didn't occur");
  171. } catch (JGitInternalException e) {
  172. // got the expected exception
  173. }
  174. }
  175. @Test
  176. public void testSoftReset() throws JGitInternalException,
  177. AmbiguousObjectException, IOException, GitAPIException {
  178. setupRepository();
  179. ObjectId prevHead = db.resolve(Constants.HEAD);
  180. assertSameAsHead(git.reset().setMode(ResetType.SOFT)
  181. .setRef(initialCommit.getName()).call());
  182. // check if HEAD points to initial commit now
  183. ObjectId head = db.resolve(Constants.HEAD);
  184. assertEquals(initialCommit, head);
  185. // check if files still exist
  186. assertTrue(untrackedFile.exists());
  187. assertTrue(indexFile.exists());
  188. // fileInIndex must no longer be in HEAD but has to be in the index
  189. String fileInIndexPath = indexFile.getAbsolutePath();
  190. assertFalse(inHead(fileInIndexPath));
  191. assertTrue(inIndex(indexFile.getName()));
  192. assertReflog(prevHead, head);
  193. assertEquals(prevHead, db.readOrigHead());
  194. }
  195. @Test
  196. public void testMixedReset() throws JGitInternalException,
  197. AmbiguousObjectException, IOException, GitAPIException {
  198. setupRepository();
  199. ObjectId prevHead = db.resolve(Constants.HEAD);
  200. assertSameAsHead(git.reset().setMode(ResetType.MIXED)
  201. .setRef(initialCommit.getName()).call());
  202. // check if HEAD points to initial commit now
  203. ObjectId head = db.resolve(Constants.HEAD);
  204. assertEquals(initialCommit, head);
  205. // check if files still exist
  206. assertTrue(untrackedFile.exists());
  207. assertTrue(indexFile.exists());
  208. // fileInIndex must no longer be in HEAD and in the index
  209. String fileInIndexPath = indexFile.getAbsolutePath();
  210. assertFalse(inHead(fileInIndexPath));
  211. assertFalse(inIndex(indexFile.getName()));
  212. assertReflog(prevHead, head);
  213. assertEquals(prevHead, db.readOrigHead());
  214. }
  215. @Test
  216. public void testMixedResetRetainsSizeAndModifiedTime() throws Exception {
  217. git = new Git(db);
  218. Files.setLastModifiedTime(writeTrashFile("a.txt", "a").toPath(),
  219. FileTime.from(Instant.now().minusSeconds(60)));
  220. assertNotNull(git.add().addFilepattern("a.txt").call());
  221. assertNotNull(git.commit().setMessage("a commit").call());
  222. Files.setLastModifiedTime(writeTrashFile("b.txt", "b").toPath(),
  223. FileTime.from(Instant.now().minusSeconds(60)));
  224. assertNotNull(git.add().addFilepattern("b.txt").call());
  225. RevCommit commit2 = git.commit().setMessage("b commit").call();
  226. assertNotNull(commit2);
  227. DirCache cache = db.readDirCache();
  228. DirCacheEntry aEntry = cache.getEntry("a.txt");
  229. assertNotNull(aEntry);
  230. assertTrue(aEntry.getLength() > 0);
  231. assertTrue(aEntry.getLastModifiedInstant().compareTo(EPOCH) > 0);
  232. DirCacheEntry bEntry = cache.getEntry("b.txt");
  233. assertNotNull(bEntry);
  234. assertTrue(bEntry.getLength() > 0);
  235. assertTrue(bEntry.getLastModifiedInstant().compareTo(EPOCH) > 0);
  236. assertSameAsHead(git.reset().setMode(ResetType.MIXED)
  237. .setRef(commit2.getName()).call());
  238. cache = db.readDirCache();
  239. DirCacheEntry mixedAEntry = cache.getEntry("a.txt");
  240. assertNotNull(mixedAEntry);
  241. assertEquals(aEntry.getLastModifiedInstant(),
  242. mixedAEntry.getLastModifiedInstant());
  243. assertEquals(aEntry.getLastModifiedInstant(),
  244. mixedAEntry.getLastModifiedInstant());
  245. DirCacheEntry mixedBEntry = cache.getEntry("b.txt");
  246. assertNotNull(mixedBEntry);
  247. assertEquals(bEntry.getLastModifiedInstant(),
  248. mixedBEntry.getLastModifiedInstant());
  249. assertEquals(bEntry.getLastModifiedInstant(),
  250. mixedBEntry.getLastModifiedInstant());
  251. }
  252. @Test
  253. public void testMixedResetWithUnmerged() throws Exception {
  254. git = new Git(db);
  255. String file = "a.txt";
  256. writeTrashFile(file, "data");
  257. String file2 = "b.txt";
  258. writeTrashFile(file2, "data");
  259. git.add().addFilepattern(file).addFilepattern(file2).call();
  260. git.commit().setMessage("commit").call();
  261. DirCache index = db.lockDirCache();
  262. DirCacheBuilder builder = index.builder();
  263. builder.add(createEntry(file, FileMode.REGULAR_FILE, 1, ""));
  264. builder.add(createEntry(file, FileMode.REGULAR_FILE, 2, ""));
  265. builder.add(createEntry(file, FileMode.REGULAR_FILE, 3, ""));
  266. assertTrue(builder.commit());
  267. assertEquals("[a.txt, mode:100644, stage:1]"
  268. + "[a.txt, mode:100644, stage:2]"
  269. + "[a.txt, mode:100644, stage:3]",
  270. indexState(0));
  271. assertSameAsHead(git.reset().setMode(ResetType.MIXED).call());
  272. assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]",
  273. indexState(0));
  274. }
  275. @Test
  276. public void testPathsReset() throws Exception {
  277. setupRepository();
  278. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  279. .getEntry(indexFile.getName());
  280. assertNotNull(preReset);
  281. git.add().addFilepattern(untrackedFile.getName()).call();
  282. // 'a.txt' has already been modified in setupRepository
  283. // 'notAddedToIndex.txt' has been added to repository
  284. assertSameAsHead(git.reset().addPath(indexFile.getName())
  285. .addPath(untrackedFile.getName()).call());
  286. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  287. .getEntry(indexFile.getName());
  288. assertNotNull(postReset);
  289. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  290. Assert.assertEquals(prestage.getObjectId(), postReset.getObjectId());
  291. // check that HEAD hasn't moved
  292. ObjectId head = db.resolve(Constants.HEAD);
  293. assertEquals(secondCommit, head);
  294. // check if files still exist
  295. assertTrue(untrackedFile.exists());
  296. assertTrue(indexFile.exists());
  297. assertTrue(inHead(indexFile.getName()));
  298. assertTrue(inIndex(indexFile.getName()));
  299. assertFalse(inIndex(untrackedFile.getName()));
  300. }
  301. @Test
  302. public void testPathsResetOnDirs() throws Exception {
  303. setupRepository();
  304. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  305. .getEntry("dir/b.txt");
  306. assertNotNull(preReset);
  307. git.add().addFilepattern(untrackedFile.getName()).call();
  308. // 'dir/b.txt' has already been modified in setupRepository
  309. assertSameAsHead(git.reset().addPath("dir").call());
  310. DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
  311. .getEntry("dir/b.txt");
  312. assertNotNull(postReset);
  313. Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId());
  314. // check that HEAD hasn't moved
  315. ObjectId head = db.resolve(Constants.HEAD);
  316. assertEquals(secondCommit, head);
  317. // check if files still exist
  318. assertTrue(untrackedFile.exists());
  319. assertTrue(inHead("dir/b.txt"));
  320. assertTrue(inIndex("dir/b.txt"));
  321. }
  322. @Test
  323. public void testPathsResetWithRef() throws Exception {
  324. setupRepository();
  325. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  326. .getEntry(indexFile.getName());
  327. assertNotNull(preReset);
  328. git.add().addFilepattern(untrackedFile.getName()).call();
  329. // 'a.txt' has already been modified in setupRepository
  330. // 'notAddedToIndex.txt' has been added to repository
  331. // reset to the inital commit
  332. assertSameAsHead(git.reset().setRef(initialCommit.getName())
  333. .addPath(indexFile.getName()).addPath(untrackedFile.getName())
  334. .call());
  335. // check that HEAD hasn't moved
  336. ObjectId head = db.resolve(Constants.HEAD);
  337. assertEquals(secondCommit, head);
  338. // check if files still exist
  339. assertTrue(untrackedFile.exists());
  340. assertTrue(indexFile.exists());
  341. assertTrue(inHead(indexFile.getName()));
  342. assertFalse(inIndex(indexFile.getName()));
  343. assertFalse(inIndex(untrackedFile.getName()));
  344. }
  345. @Test
  346. public void testPathsResetWithUnmerged() throws Exception {
  347. setupRepository();
  348. String file = "a.txt";
  349. writeTrashFile(file, "data");
  350. git.add().addFilepattern(file).call();
  351. git.commit().setMessage("commit").call();
  352. DirCache index = db.lockDirCache();
  353. DirCacheBuilder builder = index.builder();
  354. builder.add(createEntry(file, FileMode.REGULAR_FILE, 1, ""));
  355. builder.add(createEntry(file, FileMode.REGULAR_FILE, 2, ""));
  356. builder.add(createEntry(file, FileMode.REGULAR_FILE, 3, ""));
  357. builder.add(createEntry("b.txt", FileMode.REGULAR_FILE));
  358. assertTrue(builder.commit());
  359. assertEquals("[a.txt, mode:100644, stage:1]"
  360. + "[a.txt, mode:100644, stage:2]"
  361. + "[a.txt, mode:100644, stage:3]"
  362. + "[b.txt, mode:100644]",
  363. indexState(0));
  364. assertSameAsHead(git.reset().addPath(file).call());
  365. assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]",
  366. indexState(0));
  367. }
  368. @Test
  369. public void testPathsResetOnUnbornBranch() throws Exception {
  370. git = new Git(db);
  371. writeTrashFile("a.txt", "content");
  372. git.add().addFilepattern("a.txt").call();
  373. // Should assume an empty tree, like in C Git 1.8.2
  374. assertSameAsHead(git.reset().addPath("a.txt").call());
  375. DirCache cache = db.readDirCache();
  376. DirCacheEntry aEntry = cache.getEntry("a.txt");
  377. assertNull(aEntry);
  378. }
  379. @Test(expected = JGitInternalException.class)
  380. public void testPathsResetToNonexistingRef() throws Exception {
  381. git = new Git(db);
  382. writeTrashFile("a.txt", "content");
  383. git.add().addFilepattern("a.txt").call();
  384. assertSameAsHead(
  385. git.reset().setRef("doesnotexist").addPath("a.txt").call());
  386. }
  387. @Test
  388. public void testResetDefaultMode() throws Exception {
  389. git = new Git(db);
  390. writeTrashFile("a.txt", "content");
  391. git.add().addFilepattern("a.txt").call();
  392. writeTrashFile("a.txt", "modified");
  393. // should use default mode MIXED
  394. assertSameAsHead(git.reset().call());
  395. DirCache cache = db.readDirCache();
  396. DirCacheEntry aEntry = cache.getEntry("a.txt");
  397. assertNull(aEntry);
  398. assertEquals("modified", read("a.txt"));
  399. }
  400. @Test
  401. public void testHardResetOnTag() throws Exception {
  402. setupRepository();
  403. String tagName = "initialtag";
  404. git.tag().setName(tagName).setObjectId(secondCommit)
  405. .setMessage("message").call();
  406. DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS())
  407. .getEntry(indexFile.getName());
  408. assertNotNull(preReset);
  409. git.add().addFilepattern(untrackedFile.getName()).call();
  410. assertSameAsHead(git.reset().setRef(tagName).setMode(HARD).call());
  411. ObjectId head = db.resolve(Constants.HEAD);
  412. assertEquals(secondCommit, head);
  413. }
  414. @Test
  415. public void testHardResetAfterSquashMerge() throws Exception {
  416. git = new Git(db);
  417. writeTrashFile("file1", "file1");
  418. git.add().addFilepattern("file1").call();
  419. RevCommit first = git.commit().setMessage("initial commit").call();
  420. assertTrue(new File(db.getWorkTree(), "file1").exists());
  421. createBranch(first, "refs/heads/branch1");
  422. checkoutBranch("refs/heads/branch1");
  423. writeTrashFile("file2", "file2");
  424. git.add().addFilepattern("file2").call();
  425. git.commit().setMessage("second commit").call();
  426. assertTrue(new File(db.getWorkTree(), "file2").exists());
  427. checkoutBranch("refs/heads/master");
  428. MergeResult result = git.merge()
  429. .include(db.exactRef("refs/heads/branch1"))
  430. .setSquash(true)
  431. .call();
  432. assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED,
  433. result.getMergeStatus());
  434. assertNotNull(db.readSquashCommitMsg());
  435. assertSameAsHead(git.reset().setMode(ResetType.HARD)
  436. .setRef(first.getName()).call());
  437. assertNull(db.readSquashCommitMsg());
  438. }
  439. @Test
  440. public void testHardResetOnUnbornBranch() throws Exception {
  441. git = new Git(db);
  442. File fileA = writeTrashFile("a.txt", "content");
  443. git.add().addFilepattern("a.txt").call();
  444. // Should assume an empty tree, like in C Git 1.8.2
  445. assertSameAsHead(git.reset().setMode(ResetType.HARD).call());
  446. DirCache cache = db.readDirCache();
  447. DirCacheEntry aEntry = cache.getEntry("a.txt");
  448. assertNull(aEntry);
  449. assertFalse(fileA.exists());
  450. assertNull(db.resolve(Constants.HEAD));
  451. }
  452. private void assertReflog(ObjectId prevHead, ObjectId head)
  453. throws IOException {
  454. // Check the reflog for HEAD
  455. String actualHeadMessage = db.getReflogReader(Constants.HEAD)
  456. .getLastEntry().getComment();
  457. String expectedHeadMessage = head.getName() + ": updating HEAD";
  458. assertEquals(expectedHeadMessage, actualHeadMessage);
  459. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  460. .getLastEntry().getNewId().getName());
  461. assertEquals(prevHead.getName(), db.getReflogReader(Constants.HEAD)
  462. .getLastEntry().getOldId().getName());
  463. // The reflog for master contains the same as the one for HEAD
  464. String actualMasterMessage = db.getReflogReader("refs/heads/master")
  465. .getLastEntry().getComment();
  466. String expectedMasterMessage = head.getName() + ": updating HEAD"; // yes!
  467. assertEquals(expectedMasterMessage, actualMasterMessage);
  468. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  469. .getLastEntry().getNewId().getName());
  470. assertEquals(prevHead.getName(), db
  471. .getReflogReader("refs/heads/master").getLastEntry().getOldId()
  472. .getName());
  473. }
  474. private void assertReflogDisabled(ObjectId head)
  475. throws IOException {
  476. // Check the reflog for HEAD
  477. String actualHeadMessage = db.getReflogReader(Constants.HEAD)
  478. .getLastEntry().getComment();
  479. String expectedHeadMessage = "commit: adding a.txt and dir/b.txt";
  480. assertEquals(expectedHeadMessage, actualHeadMessage);
  481. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  482. .getLastEntry().getOldId().getName());
  483. // The reflog for master contains the same as the one for HEAD
  484. String actualMasterMessage = db.getReflogReader("refs/heads/master")
  485. .getLastEntry().getComment();
  486. String expectedMasterMessage = "commit: adding a.txt and dir/b.txt";
  487. assertEquals(expectedMasterMessage, actualMasterMessage);
  488. assertEquals(head.getName(), db.getReflogReader(Constants.HEAD)
  489. .getLastEntry().getOldId().getName());
  490. }
  491. /**
  492. * Checks if a file with the given path exists in the HEAD tree
  493. *
  494. * @param path
  495. * @return true if the file exists
  496. * @throws IOException
  497. */
  498. private boolean inHead(String path) throws IOException {
  499. ObjectId headId = db.resolve(Constants.HEAD);
  500. try (RevWalk rw = new RevWalk(db);
  501. TreeWalk tw = TreeWalk.forPath(db, path,
  502. rw.parseTree(headId))) {
  503. return tw != null;
  504. }
  505. }
  506. /**
  507. * Checks if a file with the given path exists in the index
  508. *
  509. * @param path
  510. * @return true if the file exists
  511. * @throws IOException
  512. */
  513. private boolean inIndex(String path) throws IOException {
  514. DirCache dc = DirCache.read(db.getIndexFile(), db.getFS());
  515. return dc.getEntry(path) != null;
  516. }
  517. /**
  518. * Asserts that a certain ref is similar to repos HEAD.
  519. * @param ref
  520. * @throws IOException
  521. */
  522. private void assertSameAsHead(Ref ref) throws IOException {
  523. Ref headRef = db.exactRef(Constants.HEAD);
  524. assertEquals(headRef.getName(), ref.getName());
  525. assertEquals(headRef.getObjectId(), ref.getObjectId());
  526. }
  527. }