diff options
Diffstat (limited to 'org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java | 343 |
1 files changed, 225 insertions, 118 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java index 9997c8c426..99873e1be1 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ResetCommandTest.java @@ -1,47 +1,15 @@ /* - * Copyright (C) 2011-2013, Chris Aniszczyk <caniszczyk@gmail.com> - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011-2018, Chris Aniszczyk <caniszczyk@gmail.com> and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.api; +import static java.time.Instant.EPOCH; import static org.eclipse.jgit.api.ResetCommand.ResetType.HARD; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -52,7 +20,9 @@ import static org.junit.Assert.fail; import java.io.File; import java.io.IOException; -import java.io.PrintWriter; +import java.nio.file.Files; +import java.nio.file.attribute.FileTime; +import java.time.Instant; import org.eclipse.jgit.api.ResetCommand.ResetType; import org.eclipse.jgit.api.errors.GitAPIException; @@ -65,11 +35,14 @@ import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.FileMode; import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.Ref; +import org.eclipse.jgit.lib.RefDatabase; 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.Assume; import org.junit.Test; public class ResetCommandTest extends RepositoryTestCase { @@ -82,6 +55,8 @@ public class ResetCommandTest extends RepositoryTestCase { private File indexFile; + private File indexNestedFile; + private File untrackedFile; private DirCacheEntry prestage; @@ -93,45 +68,25 @@ public class ResetCommandTest extends RepositoryTestCase { git = new Git(db); initialCommit = git.commit().setMessage("initial commit").call(); - // create nested file - File dir = new File(db.getWorkTree(), "dir"); - FileUtils.mkdir(dir); - File nestedFile = new File(dir, "b.txt"); - FileUtils.createNewFile(nestedFile); + // create file + indexFile = writeTrashFile("a.txt", "content"); - PrintWriter nesterFileWriter = new PrintWriter(nestedFile); - nesterFileWriter.print("content"); - nesterFileWriter.flush(); + // create nested file + indexNestedFile = writeTrashFile("dir/b.txt", "content"); - // create file - indexFile = new File(db.getWorkTree(), "a.txt"); - FileUtils.createNewFile(indexFile); - PrintWriter writer = new PrintWriter(indexFile); - writer.print("content"); - writer.flush(); - - // add file and commit it - git.add().addFilepattern("dir").addFilepattern("a.txt").call(); - secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt") - .call(); + // add files and commit them + git.add().addFilepattern("a.txt").addFilepattern("dir/b.txt").call(); + secondCommit = git.commit().setMessage("adding a.txt and dir/b.txt").call(); - prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry( - indexFile.getName()); + prestage = DirCache.read(db.getIndexFile(), db.getFS()).getEntry(indexFile.getName()); - // modify file and add to index - writer.print("new content"); - writer.close(); - nesterFileWriter.print("new content"); - nesterFileWriter.close(); - git.add().addFilepattern("a.txt").addFilepattern("dir").call(); + // modify files and add to index + writeTrashFile("a.txt", "new content"); + writeTrashFile("dir/b.txt", "new content"); + git.add().addFilepattern("a.txt").addFilepattern("dir/b.txt").call(); // create a file not added to the index - untrackedFile = new File(db.getWorkTree(), - "notAddedToIndex.txt"); - FileUtils.createNewFile(untrackedFile); - PrintWriter writer2 = new PrintWriter(untrackedFile); - writer2.print("content"); - writer2.close(); + untrackedFile = writeTrashFile("notAddedToIndex.txt", "content"); } @Test @@ -139,13 +94,16 @@ public class ResetCommandTest extends RepositoryTestCase { AmbiguousObjectException, IOException, GitAPIException { setupRepository(); ObjectId prevHead = db.resolve(Constants.HEAD); - git.reset().setMode(ResetType.HARD).setRef(initialCommit.getName()) - .call(); + ResetCommand reset = git.reset(); + assertSameAsHead(reset.setMode(ResetType.HARD) + .setRef(initialCommit.getName()).call()); + assertFalse("reflog should be enabled", reset.isReflogDisabled()); // check if HEAD points to initial commit now ObjectId head = db.resolve(Constants.HEAD); assertEquals(initialCommit, head); // check if files were removed assertFalse(indexFile.exists()); + assertFalse(indexNestedFile.exists()); assertTrue(untrackedFile.exists()); // fileInIndex must no longer be in HEAD and in the index String fileInIndexPath = indexFile.getAbsolutePath(); @@ -156,6 +114,91 @@ public class ResetCommandTest extends RepositoryTestCase { } @Test + public void testHardResetReflogDisabled() throws Exception { + setupRepository(); + ObjectId prevHead = db.resolve(Constants.HEAD); + ResetCommand reset = git.reset(); + assertSameAsHead(reset.setMode(ResetType.HARD) + .setRef(initialCommit.getName()).disableRefLog(true).call()); + assertTrue("reflog should be disabled", reset.isReflogDisabled()); + // check if HEAD points to initial commit now + ObjectId head = db.resolve(Constants.HEAD); + assertEquals(initialCommit, head); + // check if files were removed + assertFalse(indexFile.exists()); + assertFalse(indexNestedFile.exists()); + assertTrue(untrackedFile.exists()); + // fileInIndex must no longer be in HEAD and in the index + String fileInIndexPath = indexFile.getAbsolutePath(); + assertFalse(inHead(fileInIndexPath)); + assertFalse(inIndex(indexFile.getName())); + assertReflogDisabled(head); + assertEquals(prevHead, db.readOrigHead()); + } + + @Test + public void testHardResetWithConflicts_OverwriteUntrackedFile() throws Exception { + setupRepository(); + + git.rm().setCached(true).addFilepattern("a.txt").call(); + assertTrue(new File(db.getWorkTree(), "a.txt").exists()); + + git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call(); + assertTrue(new File(db.getWorkTree(), "a.txt").exists()); + assertEquals("content", read(new File(db.getWorkTree(), "a.txt"))); + } + + @Test + public void testHardResetWithConflicts_DeleteFileFolderConflict() throws Exception { + setupRepository(); + + writeTrashFile("dir-or-file/c.txt", "content"); + git.add().addFilepattern("dir-or-file/c.txt").call(); + + FileUtils.delete(new File(db.getWorkTree(), "dir-or-file"), FileUtils.RECURSIVE); + writeTrashFile("dir-or-file", "content"); + + git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call(); + assertFalse(new File(db.getWorkTree(), "dir-or-file").exists()); + } + + @Test + public void testHardResetWithConflicts_CreateFolder_UnstagedChanges() throws Exception { + setupRepository(); + + writeTrashFile("dir-or-file/c.txt", "content"); + git.add().addFilepattern("dir-or-file/c.txt").call(); + git.commit().setMessage("adding dir-or-file/c.txt").call(); + + FileUtils.delete(new File(db.getWorkTree(), "dir-or-file"), FileUtils.RECURSIVE); + writeTrashFile("dir-or-file", "content"); + + // bug 479266: cannot create folder "dir-or-file" + git.reset().setMode(ResetType.HARD).setRef(Constants.HEAD).call(); + assertTrue(new File(db.getWorkTree(), "dir-or-file/c.txt").exists()); + } + + @Test + public void testHardResetWithConflicts_DeleteFolder_UnstagedChanges() throws Exception { + setupRepository(); + ObjectId prevHead = db.resolve(Constants.HEAD); + + writeTrashFile("dir-or-file/c.txt", "content"); + git.add().addFilepattern("dir-or-file/c.txt").call(); + git.commit().setMessage("adding dir-or-file/c.txt").call(); + + writeTrashFile("dir-or-file-2/d.txt", "content"); + git.add().addFilepattern("dir-or-file-2/d.txt").call(); + FileUtils.delete(new File(db.getWorkTree(), "dir-or-file-2"), FileUtils.RECURSIVE); + writeTrashFile("dir-or-file-2", "content"); + + // bug 479266: cannot delete folder "dir-or-file" + git.reset().setMode(ResetType.HARD).setRef(prevHead.getName()).call(); + assertFalse(new File(db.getWorkTree(), "dir-or-file").exists()); + assertFalse(new File(db.getWorkTree(), "dir-or-file-2").exists()); + } + + @Test public void testResetToNonexistingHEAD() throws JGitInternalException, AmbiguousObjectException, IOException, GitAPIException { @@ -176,8 +219,8 @@ public class ResetCommandTest extends RepositoryTestCase { AmbiguousObjectException, IOException, GitAPIException { setupRepository(); ObjectId prevHead = db.resolve(Constants.HEAD); - git.reset().setMode(ResetType.SOFT).setRef(initialCommit.getName()) - .call(); + assertSameAsHead(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); @@ -197,8 +240,8 @@ public class ResetCommandTest extends RepositoryTestCase { AmbiguousObjectException, IOException, GitAPIException { setupRepository(); ObjectId prevHead = db.resolve(Constants.HEAD); - git.reset().setMode(ResetType.MIXED).setRef(initialCommit.getName()) - .call(); + assertSameAsHead(git.reset().setMode(ResetType.MIXED) + .setRef(initialCommit.getName()).call()); // check if HEAD points to initial commit now ObjectId head = db.resolve(Constants.HEAD); assertEquals(initialCommit, head); @@ -218,13 +261,13 @@ public class ResetCommandTest extends RepositoryTestCase { public void testMixedResetRetainsSizeAndModifiedTime() throws Exception { git = new Git(db); - writeTrashFile("a.txt", "a").setLastModified( - System.currentTimeMillis() - 60 * 1000); + Files.setLastModifiedTime(writeTrashFile("a.txt", "a").toPath(), + FileTime.from(Instant.now().minusSeconds(60))); assertNotNull(git.add().addFilepattern("a.txt").call()); assertNotNull(git.commit().setMessage("a commit").call()); - writeTrashFile("b.txt", "b").setLastModified( - System.currentTimeMillis() - 60 * 1000); + Files.setLastModifiedTime(writeTrashFile("b.txt", "b").toPath(), + FileTime.from(Instant.now().minusSeconds(60))); assertNotNull(git.add().addFilepattern("b.txt").call()); RevCommit commit2 = git.commit().setMessage("b commit").call(); assertNotNull(commit2); @@ -234,26 +277,31 @@ public class ResetCommandTest extends RepositoryTestCase { DirCacheEntry aEntry = cache.getEntry("a.txt"); assertNotNull(aEntry); assertTrue(aEntry.getLength() > 0); - assertTrue(aEntry.getLastModified() > 0); + assertTrue(aEntry.getLastModifiedInstant().compareTo(EPOCH) > 0); DirCacheEntry bEntry = cache.getEntry("b.txt"); assertNotNull(bEntry); assertTrue(bEntry.getLength() > 0); - assertTrue(bEntry.getLastModified() > 0); + assertTrue(bEntry.getLastModifiedInstant().compareTo(EPOCH) > 0); - git.reset().setMode(ResetType.MIXED).setRef(commit2.getName()).call(); + assertSameAsHead(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()); + assertEquals(aEntry.getLastModifiedInstant(), + mixedAEntry.getLastModifiedInstant()); + assertEquals(aEntry.getLastModifiedInstant(), + mixedAEntry.getLastModifiedInstant()); DirCacheEntry mixedBEntry = cache.getEntry("b.txt"); assertNotNull(mixedBEntry); - assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); - assertEquals(bEntry.getLastModified(), mixedBEntry.getLastModified()); + assertEquals(bEntry.getLastModifiedInstant(), + mixedBEntry.getLastModifiedInstant()); + assertEquals(bEntry.getLastModifiedInstant(), + mixedBEntry.getLastModifiedInstant()); } @Test @@ -280,7 +328,7 @@ public class ResetCommandTest extends RepositoryTestCase { + "[a.txt, mode:100644, stage:3]", indexState(0)); - git.reset().setMode(ResetType.MIXED).call(); + assertSameAsHead(git.reset().setMode(ResetType.MIXED).call()); assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]", indexState(0)); @@ -298,8 +346,8 @@ public class ResetCommandTest extends RepositoryTestCase { // 'a.txt' has already been modified in setupRepository // 'notAddedToIndex.txt' has been added to repository - git.reset().addPath(indexFile.getName()) - .addPath(untrackedFile.getName()).call(); + assertSameAsHead(git.reset().addPath(indexFile.getName()) + .addPath(untrackedFile.getName()).call()); DirCacheEntry postReset = DirCache.read(db.getIndexFile(), db.getFS()) .getEntry(indexFile.getName()); @@ -329,7 +377,7 @@ public class ResetCommandTest extends RepositoryTestCase { git.add().addFilepattern(untrackedFile.getName()).call(); // '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()) .getEntry("dir/b.txt"); @@ -358,9 +406,9 @@ public class ResetCommandTest extends RepositoryTestCase { // '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(); + assertSameAsHead(git.reset().setRef(initialCommit.getName()) + .addPath(indexFile.getName()).addPath(untrackedFile.getName()) + .call()); // check that HEAD hasn't moved ObjectId head = db.resolve(Constants.HEAD); @@ -397,7 +445,7 @@ public class ResetCommandTest extends RepositoryTestCase { + "[b.txt, mode:100644]", indexState(0)); - git.reset().addPath(file).call(); + assertSameAsHead(git.reset().addPath(file).call()); assertEquals("[a.txt, mode:100644]" + "[b.txt, mode:100644]", indexState(0)); @@ -409,7 +457,7 @@ public class ResetCommandTest extends RepositoryTestCase { 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(); + assertSameAsHead(git.reset().addPath("a.txt").call()); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); @@ -421,7 +469,8 @@ public class ResetCommandTest extends RepositoryTestCase { git = new Git(db); writeTrashFile("a.txt", "content"); git.add().addFilepattern("a.txt").call(); - git.reset().setRef("doesnotexist").addPath("a.txt").call(); + assertSameAsHead( + git.reset().setRef("doesnotexist").addPath("a.txt").call()); } @Test @@ -431,7 +480,7 @@ public class ResetCommandTest extends RepositoryTestCase { git.add().addFilepattern("a.txt").call(); writeTrashFile("a.txt", "modified"); // should use default mode MIXED - git.reset().call(); + assertSameAsHead(git.reset().call()); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); @@ -452,7 +501,7 @@ public class ResetCommandTest extends RepositoryTestCase { 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); assertEquals(secondCommit, head); @@ -460,31 +509,34 @@ public class ResetCommandTest extends RepositoryTestCase { @Test public void testHardResetAfterSquashMerge() throws Exception { - Git g = new Git(db); + git = new Git(db); writeTrashFile("file1", "file1"); - g.add().addFilepattern("file1").call(); - RevCommit first = g.commit().setMessage("initial commit").call(); + git.add().addFilepattern("file1").call(); + RevCommit first = git.commit().setMessage("initial commit").call(); assertTrue(new File(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(); + git.add().addFilepattern("file2").call(); + git.commit().setMessage("second commit").call(); assertTrue(new File(db.getWorkTree(), "file2").exists()); checkoutBranch("refs/heads/master"); - MergeResult result = g.merge().include(db.getRef("branch1")) - .setSquash(true).call(); + MergeResult result = git.merge() + .include(db.exactRef("refs/heads/branch1")) + .setSquash(true) + .call(); assertEquals(MergeResult.MergeStatus.FAST_FORWARD_SQUASHED, result.getMergeStatus()); assertNotNull(db.readSquashCommitMsg()); - g.reset().setMode(ResetType.HARD).setRef(first.getName()).call(); + assertSameAsHead(git.reset().setMode(ResetType.HARD) + .setRef(first.getName()).call()); assertNull(db.readSquashCommitMsg()); } @@ -495,7 +547,7 @@ public class ResetCommandTest extends RepositoryTestCase { 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(); + assertSameAsHead(git.reset().setMode(ResetType.HARD).call()); DirCache cache = db.readDirCache(); DirCacheEntry aEntry = cache.getEntry("a.txt"); @@ -504,30 +556,75 @@ public class ResetCommandTest extends RepositoryTestCase { assertNull(db.resolve(Constants.HEAD)); } + @Test + public void testHardResetFileMode() throws Exception { + Assume.assumeTrue("Test must be able to set executable bit", + db.getFS().supportsExecute()); + git = new Git(db); + File a = writeTrashFile("a.txt", "aaa"); + File b = writeTrashFile("b.txt", "bbb"); + db.getFS().setExecute(b, true); + assertFalse(db.getFS().canExecute(a)); + assertTrue(db.getFS().canExecute(b)); + git.add().addFilepattern("a.txt").addFilepattern("b.txt").call(); + RevCommit commit = git.commit().setMessage("files created").call(); + db.getFS().setExecute(a, true); + db.getFS().setExecute(b, false); + assertTrue(db.getFS().canExecute(a)); + assertFalse(db.getFS().canExecute(b)); + git.add().addFilepattern("a.txt").addFilepattern("b.txt").call(); + git.commit().setMessage("change exe bits").call(); + Ref ref = git.reset().setRef(commit.getName()).setMode(HARD).call(); + assertSameAsHead(ref); + assertEquals(commit.getId(), ref.getObjectId()); + assertFalse(db.getFS().canExecute(a)); + assertTrue(db.getFS().canExecute(b)); + } + private void assertReflog(ObjectId prevHead, ObjectId head) throws IOException { // Check the reflog for HEAD - String actualHeadMessage = db.getReflogReader(Constants.HEAD) + RefDatabase refDb = db.getRefDatabase(); + String actualHeadMessage = refDb.getReflogReader(Constants.HEAD) .getLastEntry().getComment(); String expectedHeadMessage = head.getName() + ": updating HEAD"; assertEquals(expectedHeadMessage, actualHeadMessage); - assertEquals(head.getName(), db.getReflogReader(Constants.HEAD) + assertEquals(head.getName(), refDb.getReflogReader(Constants.HEAD) .getLastEntry().getNewId().getName()); - assertEquals(prevHead.getName(), db.getReflogReader(Constants.HEAD) + assertEquals(prevHead.getName(), refDb.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") + String actualMasterMessage = refDb.getReflogReader("refs/heads/master") .getLastEntry().getComment(); String expectedMasterMessage = head.getName() + ": updating HEAD"; // yes! assertEquals(expectedMasterMessage, actualMasterMessage); - assertEquals(head.getName(), db.getReflogReader(Constants.HEAD) + assertEquals(head.getName(), refDb.getReflogReader(Constants.HEAD) .getLastEntry().getNewId().getName()); - assertEquals(prevHead.getName(), db - .getReflogReader("refs/heads/master").getLastEntry().getOldId() - .getName()); + assertEquals(prevHead.getName(), + refDb.getReflogReader("refs/heads/master").getLastEntry() + .getOldId().getName()); } + private void assertReflogDisabled(ObjectId head) + throws IOException { + RefDatabase refDb = db.getRefDatabase(); + // Check the reflog for HEAD + String actualHeadMessage = refDb.getReflogReader(Constants.HEAD) + .getLastEntry().getComment(); + String expectedHeadMessage = "commit: adding a.txt and dir/b.txt"; + assertEquals(expectedHeadMessage, actualHeadMessage); + assertEquals(head.getName(), refDb.getReflogReader(Constants.HEAD) + .getLastEntry().getOldId().getName()); + + // The reflog for master contains the same as the one for HEAD + String actualMasterMessage = refDb.getReflogReader("refs/heads/master") + .getLastEntry().getComment(); + String expectedMasterMessage = "commit: adding a.txt and dir/b.txt"; + assertEquals(expectedMasterMessage, actualMasterMessage); + assertEquals(head.getName(), refDb.getReflogReader(Constants.HEAD) + .getLastEntry().getOldId().getName()); + } /** * Checks if a file with the given path exists in the HEAD tree * @@ -556,4 +653,14 @@ public class ResetCommandTest extends RepositoryTestCase { 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.exactRef(Constants.HEAD); + assertEquals(headRef.getName(), ref.getName()); + assertEquals(headRef.getObjectId(), ref.getObjectId()); + } } |