Browse Source

Fix ResetCommand to return the resulting ref

ResetCommand was not returning the updated ref as a result of the call()
method. Since the ResetCommand is always updating the same ref (HEAD)
this should always be the HEAD ref.

Bug: 440750
Change-Id: I7974975c3ab05e68c208384e69cf0692ded6e8db
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
stable-4.3
Christian Halstrick 9 years ago committed by Matthias Sohn
parent
commit
7182cb2a26
  1. 56
      org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java
  2. 4
      org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

56
org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java

@ -65,6 +65,7 @@ import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.FileMode;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.treewalk.TreeWalk; import org.eclipse.jgit.treewalk.TreeWalk;
@ -139,8 +140,8 @@ public class ResetCommandTest extends RepositoryTestCase {
AmbiguousObjectException, IOException, GitAPIException { AmbiguousObjectException, IOException, GitAPIException {
setupRepository(); setupRepository();
ObjectId prevHead = db.resolve(Constants.HEAD); ObjectId prevHead = db.resolve(Constants.HEAD);
git.reset().setMode(ResetType.HARD).setRef(initialCommit.getName()) assertSameAsHead(git.reset().setMode(ResetType.HARD)
.call(); .setRef(initialCommit.getName()).call());
// check if HEAD points to initial commit now // check if HEAD points to initial commit now
ObjectId head = db.resolve(Constants.HEAD); ObjectId head = db.resolve(Constants.HEAD);
assertEquals(initialCommit, head); assertEquals(initialCommit, head);
@ -176,8 +177,8 @@ public class ResetCommandTest extends RepositoryTestCase {
AmbiguousObjectException, IOException, GitAPIException { AmbiguousObjectException, IOException, GitAPIException {
setupRepository(); setupRepository();
ObjectId prevHead = db.resolve(Constants.HEAD); ObjectId prevHead = db.resolve(Constants.HEAD);
git.reset().setMode(ResetType.SOFT).setRef(initialCommit.getName()) assertSameAsHead(git.reset().setMode(ResetType.SOFT)
.call(); .setRef(initialCommit.getName()).call());
// check if HEAD points to initial commit now // check if HEAD points to initial commit now
ObjectId head = db.resolve(Constants.HEAD); ObjectId head = db.resolve(Constants.HEAD);
assertEquals(initialCommit, head); assertEquals(initialCommit, head);
@ -197,8 +198,8 @@ public class ResetCommandTest extends RepositoryTestCase {
AmbiguousObjectException, IOException, GitAPIException { AmbiguousObjectException, IOException, GitAPIException {
setupRepository(); setupRepository();
ObjectId prevHead = db.resolve(Constants.HEAD); ObjectId prevHead = db.resolve(Constants.HEAD);
git.reset().setMode(ResetType.MIXED).setRef(initialCommit.getName()) assertSameAsHead(git.reset().setMode(ResetType.MIXED)
.call(); .setRef(initialCommit.getName()).call());
// check if HEAD points to initial commit now // check if HEAD points to initial commit now
ObjectId head = db.resolve(Constants.HEAD); ObjectId head = db.resolve(Constants.HEAD);
assertEquals(initialCommit, head); assertEquals(initialCommit, head);
@ -241,7 +242,8 @@ public class ResetCommandTest extends RepositoryTestCase {
assertTrue(bEntry.getLength() > 0); assertTrue(bEntry.getLength() > 0);
assertTrue(bEntry.getLastModified() > 0); assertTrue(bEntry.getLastModified() > 0);
git.reset().setMode(ResetType.MIXED).setRef(commit2.getName()).call(); assertSameAsHead(git.reset().setMode(ResetType.MIXED)
.setRef(commit2.getName()).call());
cache = db.readDirCache(); cache = db.readDirCache();
@ -280,7 +282,7 @@ public class ResetCommandTest extends RepositoryTestCase {
+ "[a.txt, mode:100644, stage:3]", + "[a.txt, mode:100644, stage:3]",
indexState(0)); indexState(0));
git.reset().setMode(ResetType.MIXED).call(); assertSameAsHead(git.reset().setMode(ResetType.MIXED).call());
assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]", assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]",
indexState(0)); indexState(0));
@ -298,8 +300,8 @@ public class ResetCommandTest extends RepositoryTestCase {
// 'a.txt' has already been modified in setupRepository // 'a.txt' has already been modified in setupRepository
// 'notAddedToIndex.txt' has been added to repository // 'notAddedToIndex.txt' has been added to repository
git.reset().addPath(indexFile.getName()) assertSameAsHead(git.reset().addPath(indexFile.getName())
.addPath(untrackedFile.getName()).call(); .addPath(untrackedFile.getName()).call());
DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS()) DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
.getEntry(indexFile.getName()); .getEntry(indexFile.getName());
@ -329,7 +331,7 @@ public class ResetCommandTest extends RepositoryTestCase {
git.add().addFilepattern(untrackedFile.getName()).call(); git.add().addFilepattern(untrackedFile.getName()).call();
// 'dir/b.txt' has already been modified in setupRepository // 'dir/b.txt' has already been modified in setupRepository
git.reset().addPath("dir").call(); assertSameAsHead(git.reset().addPath("dir").call());
DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS()) DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS())
.getEntry("dir/b.txt"); .getEntry("dir/b.txt");
@ -358,9 +360,9 @@ public class ResetCommandTest extends RepositoryTestCase {
// 'a.txt' has already been modified in setupRepository // 'a.txt' has already been modified in setupRepository
// 'notAddedToIndex.txt' has been added to repository // 'notAddedToIndex.txt' has been added to repository
// reset to the inital commit // reset to the inital commit
git.reset().setRef(initialCommit.getName()) assertSameAsHead(git.reset().setRef(initialCommit.getName())
.addPath(indexFile.getName()) .addPath(indexFile.getName()).addPath(untrackedFile.getName())
.addPath(untrackedFile.getName()).call(); .call());
// check that HEAD hasn't moved // check that HEAD hasn't moved
ObjectId head = db.resolve(Constants.HEAD); ObjectId head = db.resolve(Constants.HEAD);
@ -397,7 +399,7 @@ public class ResetCommandTest extends RepositoryTestCase {
+ "[b.txt, mode:100644]", + "[b.txt, mode:100644]",
indexState(0)); indexState(0));
git.reset().addPath(file).call(); assertSameAsHead(git.reset().addPath(file).call());
assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]", assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]",
indexState(0)); indexState(0));
@ -409,7 +411,7 @@ public class ResetCommandTest extends RepositoryTestCase {
writeTrashFile("a.txt", "content"); writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("a.txt").call();
// Should assume an empty tree, like in C Git 1.8.2 // Should assume an empty tree, like in C Git 1.8.2
git.reset().addPath("a.txt").call(); assertSameAsHead(git.reset().addPath("a.txt").call());
DirCache cache = db.readDirCache(); DirCache cache = db.readDirCache();
DirCacheEntry aEntry = cache.getEntry("a.txt"); DirCacheEntry aEntry = cache.getEntry("a.txt");
@ -421,7 +423,8 @@ public class ResetCommandTest extends RepositoryTestCase {
git = new Git(db); git = new Git(db);
writeTrashFile("a.txt", "content"); writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("a.txt").call();
git.reset().setRef("doesnotexist").addPath("a.txt").call(); assertSameAsHead(
git.reset().setRef("doesnotexist").addPath("a.txt").call());
} }
@Test @Test
@ -431,7 +434,7 @@ public class ResetCommandTest extends RepositoryTestCase {
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("a.txt").call();
writeTrashFile("a.txt", "modified"); writeTrashFile("a.txt", "modified");
// should use default mode MIXED // should use default mode MIXED
git.reset().call(); assertSameAsHead(git.reset().call());
DirCache cache = db.readDirCache(); DirCache cache = db.readDirCache();
DirCacheEntry aEntry = cache.getEntry("a.txt"); DirCacheEntry aEntry = cache.getEntry("a.txt");
@ -452,7 +455,7 @@ public class ResetCommandTest extends RepositoryTestCase {
git.add().addFilepattern(untrackedFile.getName()).call(); git.add().addFilepattern(untrackedFile.getName()).call();
git.reset().setRef(tagName).setMode(HARD).call(); assertSameAsHead(git.reset().setRef(tagName).setMode(HARD).call());
ObjectId head = db.resolve(Constants.HEAD); ObjectId head = db.resolve(Constants.HEAD);
assertEquals(secondCommit, head); assertEquals(secondCommit, head);
@ -486,7 +489,8 @@ public class ResetCommandTest extends RepositoryTestCase {
result.getMergeStatus()); result.getMergeStatus());
assertNotNull(db.readSquashCommitMsg()); assertNotNull(db.readSquashCommitMsg());
g.reset().setMode(ResetType.HARD).setRef(first.getName()).call(); assertSameAsHead(g.reset().setMode(ResetType.HARD)
.setRef(first.getName()).call());
assertNull(db.readSquashCommitMsg()); assertNull(db.readSquashCommitMsg());
} }
@ -497,7 +501,7 @@ public class ResetCommandTest extends RepositoryTestCase {
File fileA = writeTrashFile("a.txt", "content"); File fileA = writeTrashFile("a.txt", "content");
git.add().addFilepattern("a.txt").call(); git.add().addFilepattern("a.txt").call();
// Should assume an empty tree, like in C Git 1.8.2 // Should assume an empty tree, like in C Git 1.8.2
git.reset().setMode(ResetType.HARD).call(); assertSameAsHead(git.reset().setMode(ResetType.HARD).call());
DirCache cache = db.readDirCache(); DirCache cache = db.readDirCache();
DirCacheEntry aEntry = cache.getEntry("a.txt"); DirCacheEntry aEntry = cache.getEntry("a.txt");
@ -558,4 +562,14 @@ public class ResetCommandTest extends RepositoryTestCase {
return dc.getEntry(path) != null; return dc.getEntry(path) != null;
} }
/**
* Asserts that a certain ref is similar to repos HEAD.
* @param ref
* @throws IOException
*/
private void assertSameAsHead(Ref ref) throws IOException {
Ref headRef = db.getRef(Constants.HEAD);
assertEquals(headRef.getName(), ref.getName());
assertEquals(headRef.getObjectId(), ref.getObjectId());
}
} }

4
org.eclipse.jgit/src/org/eclipse/jgit/api/ResetCommand.java

@ -190,10 +190,8 @@ public class ResetCommand extends GitCommand<Ref> {
ObjectId origHead = ru.getOldObjectId(); ObjectId origHead = ru.getOldObjectId();
if (origHead != null) if (origHead != null)
repo.writeOrigHead(origHead); repo.writeOrigHead(origHead);
result = ru.getRef();
} else {
result = repo.getRef(Constants.HEAD);
} }
result = repo.exactRef(Constants.HEAD);
if (mode == null) if (mode == null)
mode = ResetType.MIXED; mode = ResetType.MIXED;

Loading…
Cancel
Save