Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ResetCommandTest.java 22KB

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