Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ResetCommandTest.java 22KB

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