diff options
author | Thomas Wolf <thomas.wolf@paranor.ch> | 2020-11-15 18:45:12 +0100 |
---|---|---|
committer | Thomas Wolf <thomas.wolf@paranor.ch> | 2020-11-17 14:27:28 +0100 |
commit | e84881ea6b67c6c3239df4e42cf73ffdadc13d56 (patch) | |
tree | 766ef29d89535fe4e91b2d8582b6d70c60c9f76e /org.eclipse.jgit.test | |
parent | ff6a827f4fa97815eac3b1a1ec53da8dfea41dd0 (diff) | |
download | jgit-e84881ea6b67c6c3239df4e42cf73ffdadc13d56.tar.gz jgit-e84881ea6b67c6c3239df4e42cf73ffdadc13d56.zip |
Allow to resolve a conflict by checking out a file
DirCacheEditor unconditionally applied a PathEdit to all stages in the
index. This gives wrong results if one wants to check out a file from
some commit to resolve a conflict: JGit would update the working tree
file multiple times (once per stage), and set all stages to point to
the checked-out blob.
C git replaces the stages by the entry for the checked-out file.
To support this, add a DirCacheEntry.setStage() method so that
CheckoutCommand can force the stage to zero. In DirCacheEditor, keep
only the zero stage if the PathEdit re-set the stage.
Bug: 568038
Change-Id: Ic7c635bb5aaa06ffaaeed50bc5e45702c56fc6d1
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
Diffstat (limited to 'org.eclipse.jgit.test')
3 files changed, 79 insertions, 2 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java index e0a1c1d61b..f52b715d39 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/PathCheckoutCommandTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011, Kevin Sawicki <kevin@github.com> and others + * Copyright (C) 2011, 2020 Kevin Sawicki <kevin@github.com> and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -24,6 +24,7 @@ import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.ObjectReader; import org.eclipse.jgit.lib.RepositoryState; import org.eclipse.jgit.lib.StoredConfig; @@ -310,6 +311,16 @@ public class PathCheckoutCommandTest extends RepositoryTestCase { } @Test + public void testCheckoutFileWithConflict() throws Exception { + setupConflictingState(); + assertEquals('[' + FILE1 + ']', + git.status().call().getConflicting().toString()); + git.checkout().setStartPoint(Constants.HEAD).addPath(FILE1).call(); + assertEquals("3", read(FILE1)); + assertTrue(git.status().call().isClean()); + } + + @Test public void testCheckoutOursWhenNoBase() throws Exception { String file = "added.txt"; diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheEntryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheEntryTest.java index 5477f565f4..8e84dfa318 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheEntryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCacheEntryTest.java @@ -242,6 +242,46 @@ public class DirCacheEntryTest { } @Test + public void testSetStage() { + DirCacheEntry e = new DirCacheEntry("some/path", DirCacheEntry.STAGE_1); + e.setAssumeValid(true); + e.setCreationTime(2L); + e.setFileMode(FileMode.EXECUTABLE_FILE); + e.setLastModified(EPOCH.plusMillis(3L)); + e.setLength(100L); + e.setObjectId(ObjectId + .fromString("0123456789012345678901234567890123456789")); + e.setUpdateNeeded(true); + e.setStage(DirCacheEntry.STAGE_2); + + assertTrue(e.isAssumeValid()); + assertEquals(2L, e.getCreationTime()); + assertEquals( + ObjectId.fromString("0123456789012345678901234567890123456789"), + e.getObjectId()); + assertEquals(FileMode.EXECUTABLE_FILE, e.getFileMode()); + assertEquals(EPOCH.plusMillis(3L), e.getLastModifiedInstant()); + assertEquals(100L, e.getLength()); + assertEquals(DirCacheEntry.STAGE_2, e.getStage()); + assertTrue(e.isUpdateNeeded()); + assertEquals("some/path", e.getPathString()); + + e.setStage(DirCacheEntry.STAGE_0); + + assertTrue(e.isAssumeValid()); + assertEquals(2L, e.getCreationTime()); + assertEquals( + ObjectId.fromString("0123456789012345678901234567890123456789"), + e.getObjectId()); + assertEquals(FileMode.EXECUTABLE_FILE, e.getFileMode()); + assertEquals(EPOCH.plusMillis(3L), e.getLastModifiedInstant()); + assertEquals(100L, e.getLength()); + assertEquals(DirCacheEntry.STAGE_0, e.getStage()); + assertTrue(e.isUpdateNeeded()); + assertEquals("some/path", e.getPathString()); + } + + @Test public void testCopyMetaDataWithStage() { copyMetaDataHelper(false); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java index 39a1f01803..5778d28d6f 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/dircache/DirCachePathEditTest.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011, Robin Rosenberg and others + * Copyright (C) 2011, 2020 Robin Rosenberg and others * * This program and the accompanying materials are made available under the * terms of the Eclipse Distribution License v. 1.0 which is available at @@ -124,6 +124,32 @@ public class DirCachePathEditTest { } @Test + public void testPathEditWithStagesAndReset() throws Exception { + DirCache dc = DirCache.newInCore(); + DirCacheBuilder builder = new DirCacheBuilder(dc, 3); + builder.add(createEntry("a", DirCacheEntry.STAGE_1)); + builder.add(createEntry("a", DirCacheEntry.STAGE_2)); + builder.add(createEntry("a", DirCacheEntry.STAGE_3)); + builder.finish(); + + DirCacheEditor editor = dc.editor(); + PathEdit edit = new PathEdit("a") { + + @Override + public void apply(DirCacheEntry ent) { + ent.setStage(DirCacheEntry.STAGE_0); + } + }; + editor.add(edit); + editor.finish(); + + assertEquals(1, dc.getEntryCount()); + DirCacheEntry entry = dc.getEntry(0); + assertEquals("a", entry.getPathString()); + assertEquals(DirCacheEntry.STAGE_0, entry.getStage()); + } + + @Test public void testFileReplacesTree() throws Exception { DirCache dc = DirCache.newInCore(); DirCacheEditor editor = dc.editor(); |