@Test public void testHardResetAfterSquashMerge() throws Exception { Git g = new Git(db); writeTrashFile("file1", "file1"); g.add().addFilepattern("file1").call(); RevCommit first = g.commit().setMessage("initial commit").call(); assertTrue(resolve(db.getWorkTree(), "file1").exists()); createBranch(first, "refs/heads/branch1"); checkoutBranch("refs/heads/branch1"); writeTrashFile("file2", "file2"); g.add().addFilepattern("file2").call(); g.commit().setMessage("second commit").call(); assertTrue(resolve(db.getWorkTree(), "file2").exists()); checkoutBranch("refs/heads/master"); MergeResult result = g.merge().include(db.getRef("branch1")).setSquash(true).call(); assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED, result.getMergeStatus()); assertNotNull(db.readSquashCommitMsg()); g.reset().setMode(ResetType.HARD).setRef(first.getName()).call(); assertNull(db.readSquashCommitMsg()); }
@Test(expected = JGitInternalException.class) public void testPathsResetToNonexistingRef() throws Exception { git = new Git(db); writeTrashFile("a.txt", "content"); git.add().addFilepattern("a.txt").call(); git.reset().setRef("doesnotexist").addPath("a.txt").call(); }
@Test public void testPathsResetWithUnmerged() throws Exception { setupRepository(); String file = "a.txt"; writeTrashFile(file, "data"); git.add().addFilepattern(file).call(); git.commit().setMessage("commit").call(); DirCache index = db.lockDirCache(); DirCacheBuilder builder = index.builder(); builder.add(createEntry(file, FileMode.REGULAR_FILE, 1, "")); builder.add(createEntry(file, FileMode.REGULAR_FILE, 2, "")); builder.add(createEntry(file, FileMode.REGULAR_FILE, 3, "")); builder.add(createEntry("b.txt", FileMode.REGULAR_FILE)); assertTrue(builder.commit()); assertEquals( "[a.txt, mode:100644, stage:1]" + "[a.txt, mode:100644, stage:2]" + "[a.txt, mode:100644, stage:3]" + "[b.txt, mode:100644]", indexState(0)); git.reset().addPath(file).call(); assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]", indexState(0)); }
@Test public void testPathsResetWithRef() throws Exception { setupRepository(); DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName()); assertNotNull(preReset); git.add().addFilepattern(untrackedFile.getName()).call(); // 'a.txt' has already been modified in setupRepository // 'notAddedToIndex.txt' has been added to repository // reset to the inital commit git.reset() .setRef(initialCommit.getName()) .addPath(indexFile.getName()) .addPath(untrackedFile.getName()) .call(); // check that HEAD hasn't moved ObjectId head = db.resolve(Constants.HEAD); assertEquals(secondCommit, head); // check if files still exist assertTrue(untrackedFile.exists()); assertTrue(indexFile.exists()); assertTrue(inHead(indexFile.getName())); assertFalse(inIndex(indexFile.getName())); assertFalse(inIndex(untrackedFile.getName())); }
@Test public void testPathsResetOnUnbornBranch() throws Exception { git = new Git(db); writeTrashFile("a.txt", "content"); git.add().addFilepattern("a.txt").call(); // Should assume an empty tree, like in C Git 1.8.2 git.reset().addPath("a.txt").call(); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); assertNull(aEntry); }
@Test public void testHardResetOnUnbornBranch() throws Exception { git = new Git(db); File fileA = writeTrashFile("a.txt", "content"); git.add().addFilepattern("a.txt").call(); // Should assume an empty tree, like in C Git 1.8.2 git.reset().setMode(ResetType.HARD).call(); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); assertNull(aEntry); assertFalse(fileA.exists()); assertNull(db.resolve(Constants.HEAD)); }
@Test public void testResetToNonexistingHEAD() throws JGitInternalException, AmbiguousObjectException, IOException, GitAPIException { // create a file in the working tree of a fresh repo git = new Git(db); writeTrashFile("f", "content"); try { git.reset().setRef(Constants.HEAD).call(); fail("Expected JGitInternalException didn't occur"); } catch (JGitInternalException e) { // got the expected exception } }
@Test public void testHardResetOnTag() throws Exception { setupRepository(); String tagName = "initialtag"; git.tag().setName(tagName).setObjectId(secondCommit).setMessage("message").call(); DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName()); assertNotNull(preReset); git.add().addFilepattern(untrackedFile.getName()).call(); git.reset().setRef(tagName).setMode(HARD).call(); ObjectId head = db.resolve(Constants.HEAD); assertEquals(secondCommit, head); }
@Test public void testSoftReset() throws JGitInternalException, AmbiguousObjectException, IOException, GitAPIException { setupRepository(); ObjectId prevHead = db.resolve(Constants.HEAD); git.reset().setMode(ResetType.SOFT).setRef(initialCommit.getName()).call(); // check if HEAD points to initial commit now ObjectId head = db.resolve(Constants.HEAD); assertEquals(initialCommit, head); // check if files still exist assertTrue(untrackedFile.exists()); assertTrue(indexFile.exists()); // fileInIndex must no longer be in HEAD but has to be in the index String fileInIndexPath = indexFile.getAbsolutePath(); assertFalse(inHead(fileInIndexPath)); assertTrue(inIndex(indexFile.getName())); assertReflog(prevHead, head); assertEquals(prevHead, db.readOrigHead()); }
@Test public void testMixedResetRetainsSizeAndModifiedTime() throws Exception { git = new Git(db); writeTrashFile("a.txt", "a").setLastModified(System.currentTimeMillis() - 60 * 1000); assertNotNull(git.add().addFilepattern("a.txt").call()); assertNotNull(git.commit().setMessage("a commit").call()); writeTrashFile("b.txt", "b").setLastModified(System.currentTimeMillis() - 60 * 1000); assertNotNull(git.add().addFilepattern("b.txt").call()); RevCommit commit2 = git.commit().setMessage("b commit").call(); assertNotNull(commit2); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); assertNotNull(aEntry); assertTrue(aEntry.getLength() > 0); assertTrue(aEntry.getLastModified() > 0); DirCacheEntry bEntry = cache.getEntry("b.txt"); assertNotNull(bEntry); assertTrue(bEntry.getLength() > 0); assertTrue(bEntry.getLastModified() > 0); git.reset().setMode(ResetType.MIXED).setRef(commit2.getName()).call(); cache = db.readDirCache(); DirCacheEntry mixedAEntry = cache.getEntry("a.txt"); assertNotNull(mixedAEntry); assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified()); assertEquals(aEntry.getLastModified(), mixedAEntry.getLastModified()); DirCacheEntry mixedBEntry = cache.getEntry("b.txt"); assertNotNull(mixedBEntry); assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); }
@Test public void testPathsResetOnDirs() throws Exception { setupRepository(); DirCacheEntry preReset = DirCache.read(db.getIndexFile(), db.getFS()).getEntry("dir/b.txt"); assertNotNull(preReset); git.add().addFilepattern(untrackedFile.getName()).call(); // 'dir/b.txt' has already been modified in setupRepository git.reset().addPath("dir").call(); DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS()).getEntry("dir/b.txt"); assertNotNull(postReset); Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId()); // check that HEAD hasn't moved ObjectId head = db.resolve(Constants.HEAD); assertEquals(secondCommit, head); // check if files still exist assertTrue(untrackedFile.exists()); assertTrue(inHead("dir/b.txt")); assertTrue(inIndex("dir/b.txt")); }