|
|
|
@ -44,6 +44,7 @@ package org.eclipse.jgit.api;
|
|
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals; |
|
|
|
|
import static org.junit.Assert.assertFalse; |
|
|
|
|
import static org.junit.Assert.assertNotNull; |
|
|
|
|
import static org.junit.Assert.assertTrue; |
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
@ -58,6 +59,7 @@ import org.eclipse.jgit.api.errors.NoHeadException;
|
|
|
|
|
import org.eclipse.jgit.api.errors.NoMessageException; |
|
|
|
|
import org.eclipse.jgit.api.errors.WrongRepositoryStateException; |
|
|
|
|
import org.eclipse.jgit.dircache.DirCache; |
|
|
|
|
import org.eclipse.jgit.dircache.DirCacheEntry; |
|
|
|
|
import org.eclipse.jgit.errors.AmbiguousObjectException; |
|
|
|
|
import org.eclipse.jgit.lib.Constants; |
|
|
|
|
import org.eclipse.jgit.lib.ObjectId; |
|
|
|
@ -66,6 +68,7 @@ import org.eclipse.jgit.revwalk.RevCommit;
|
|
|
|
|
import org.eclipse.jgit.revwalk.RevWalk; |
|
|
|
|
import org.eclipse.jgit.treewalk.TreeWalk; |
|
|
|
|
import org.eclipse.jgit.util.FileUtils; |
|
|
|
|
import org.junit.Assert; |
|
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
public class ResetCommandTest extends RepositoryTestCase { |
|
|
|
@ -74,10 +77,14 @@ public class ResetCommandTest extends RepositoryTestCase {
|
|
|
|
|
|
|
|
|
|
private RevCommit initialCommit; |
|
|
|
|
|
|
|
|
|
private RevCommit secondCommit; |
|
|
|
|
|
|
|
|
|
private File indexFile; |
|
|
|
|
|
|
|
|
|
private File untrackedFile; |
|
|
|
|
|
|
|
|
|
private DirCacheEntry prestage; |
|
|
|
|
|
|
|
|
|
public void setupRepository() throws IOException, NoFilepatternException, |
|
|
|
|
NoHeadException, NoMessageException, ConcurrentRefUpdateException, |
|
|
|
|
JGitInternalException, WrongRepositoryStateException { |
|
|
|
@ -95,7 +102,10 @@ public class ResetCommandTest extends RepositoryTestCase {
|
|
|
|
|
|
|
|
|
|
// add file and commit it
|
|
|
|
|
git.add().addFilepattern("a.txt").call(); |
|
|
|
|
git.commit().setMessage("adding a.txt").call(); |
|
|
|
|
secondCommit = git.commit().setMessage("adding a.txt").call(); |
|
|
|
|
|
|
|
|
|
prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry( |
|
|
|
|
indexFile.getName()); |
|
|
|
|
|
|
|
|
|
// modify file and add to index
|
|
|
|
|
writer.print("new content"); |
|
|
|
@ -178,6 +188,66 @@ public class ResetCommandTest extends RepositoryTestCase {
|
|
|
|
|
assertReflog(prevHead, head); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
public void testPathsReset() 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
|
|
|
|
|
git.reset().addPath(indexFile.getName()) |
|
|
|
|
.addPath(untrackedFile.getName()).call(); |
|
|
|
|
|
|
|
|
|
DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS()) |
|
|
|
|
.getEntry(indexFile.getName()); |
|
|
|
|
assertNotNull(postReset); |
|
|
|
|
Assert.assertNotSame(preReset.getObjectId(), postReset.getObjectId()); |
|
|
|
|
Assert.assertEquals(prestage.getObjectId(), postReset.getObjectId()); |
|
|
|
|
|
|
|
|
|
// check that HEAD hasn't moved
|
|
|
|
|
ObjectId head = db.resolve(Constants.HEAD); |
|
|
|
|
assertTrue(head.equals(secondCommit)); |
|
|
|
|
// check if files still exist
|
|
|
|
|
assertTrue(untrackedFile.exists()); |
|
|
|
|
assertTrue(indexFile.exists()); |
|
|
|
|
assertTrue(inHead(indexFile.getName())); |
|
|
|
|
assertTrue(inIndex(indexFile.getName())); |
|
|
|
|
assertFalse(inIndex(untrackedFile.getName())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@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); |
|
|
|
|
assertTrue(head.equals(secondCommit)); |
|
|
|
|
// check if files still exist
|
|
|
|
|
assertTrue(untrackedFile.exists()); |
|
|
|
|
assertTrue(indexFile.exists()); |
|
|
|
|
assertTrue(inHead(indexFile.getName())); |
|
|
|
|
assertFalse(inIndex(indexFile.getName())); |
|
|
|
|
assertFalse(inIndex(untrackedFile.getName())); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void assertReflog(ObjectId prevHead, ObjectId head) |
|
|
|
|
throws IOException { |
|
|
|
|
// Check the reflog for HEAD
|
|
|
|
@ -187,9 +257,8 @@ public class ResetCommandTest extends RepositoryTestCase {
|
|
|
|
|
assertEquals(expectedHeadMessage, actualHeadMessage); |
|
|
|
|
assertEquals(head.getName(), db.getReflogReader(Constants.HEAD) |
|
|
|
|
.getLastEntry().getNewId().getName()); |
|
|
|
|
assertEquals(prevHead.getName(), db |
|
|
|
|
.getReflogReader(Constants.HEAD).getLastEntry().getOldId() |
|
|
|
|
.getName()); |
|
|
|
|
assertEquals(prevHead.getName(), db.getReflogReader(Constants.HEAD) |
|
|
|
|
.getLastEntry().getOldId().getName()); |
|
|
|
|
|
|
|
|
|
// The reflog for master contains the same as the one for HEAD
|
|
|
|
|
String actualMasterMessage = db.getReflogReader("refs/heads/master") |
|
|
|
@ -198,9 +267,9 @@ public class ResetCommandTest extends RepositoryTestCase {
|
|
|
|
|
assertEquals(expectedMasterMessage, actualMasterMessage); |
|
|
|
|
assertEquals(head.getName(), db.getReflogReader(Constants.HEAD) |
|
|
|
|
.getLastEntry().getNewId().getName()); |
|
|
|
|
assertEquals(prevHead.getName(), |
|
|
|
|
db.getReflogReader("refs/heads/master").getLastEntry() |
|
|
|
|
.getOldId().getName()); |
|
|
|
|
assertEquals(prevHead.getName(), db |
|
|
|
|
.getReflogReader("refs/heads/master").getLastEntry().getOldId() |
|
|
|
|
.getName()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|