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

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