]> source.dussan.org Git - jgit.git/commitdiff
PatchApplier fix - init cache with provided tree 06/199706/4
authorNitzan Gur-Furman <nitzan@google.com>
Tue, 31 Jan 2023 21:01:21 +0000 (22:01 +0100)
committerNitzan Gur-Furman <nitzan@google.com>
Thu, 2 Feb 2023 11:39:26 +0000 (12:39 +0100)
This change only affects inCore repositories.
Before this change, any file that wasn't part of the patch
wasn't read, and therefore wasn't part of the output tree.

Change-Id: I246ef957088f17aaf367143f7a0b3af0f8264ffb
Bug: Google b/267270348

org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PostImage [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PreImage [new file with mode: 0644]
org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/XAndY.patch [new file with mode: 0644]
org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java
org.eclipse.jgit/src/org/eclipse/jgit/patch/PatchApplier.java

diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PostImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PostImage
new file mode 100644 (file)
index 0000000..6b664d9
--- /dev/null
@@ -0,0 +1,2 @@
+This file
+should not be changed.
\ No newline at end of file
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PreImage b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/Unaffected_PreImage
new file mode 100644 (file)
index 0000000..6b664d9
--- /dev/null
@@ -0,0 +1,2 @@
+This file
+should not be changed.
\ No newline at end of file
diff --git a/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/XAndY.patch b/org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/XAndY.patch
new file mode 100644 (file)
index 0000000..35950f3
--- /dev/null
@@ -0,0 +1,32 @@
+diff --git a/X b/X
+index a3648a1..2d44096 100644
+--- a/X
++++ b/X
+@@ -2,2 +2,3 @@ a
+ b
++c
+ d
+@@ -16,4 +17,2 @@ p
+ q
+-r
+-s
+ t
+@@ -22,4 +21,8 @@ v
+ w
+-x
+-y
++0
++1
++2
++3
++4
++5
+ z
+diff --git a/Y b/Y
+index 2e65efe..7898192 100644
+--- a/Y
++++ b/Y
+@@ -1 +1 @@
+-a
+\ No newline at end of file
++a
\ No newline at end of file
index bcde022d40d3c482f366e9a7425bb5da7acae553..893fd61556e4c0dc364888b89f186e2fe614bade 100644 (file)
@@ -24,6 +24,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
+import org.eclipse.jgit.annotations.Nullable;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.errors.PatchApplyException;
 import org.eclipse.jgit.api.errors.PatchFormatException;
@@ -71,27 +72,21 @@ public class PatchApplierTest {
                        this.inCore = inCore;
                }
 
+               void init(final String aName) throws Exception {
+                       init(aName, true, true);
+               }
+
                protected void init(String aName, boolean preExists, boolean postExists)
                                throws Exception {
                        // Patch and pre/postimage are read from data
                        // org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/
                        this.name = aName;
                        if (postExists) {
-                               postImage = IO
-                                               .readWholeStream(getTestResource(name + "_PostImage"), 0)
-                                               .array();
-                               expectedText = new String(postImage, StandardCharsets.UTF_8);
+                               expectedText = initPostImage(aName);
                        }
 
-                       File f = new File(db.getWorkTree(), name);
                        if (preExists) {
-                               preImage = IO
-                                               .readWholeStream(getTestResource(name + "_PreImage"), 0)
-                                               .array();
-                               try (Git git = new Git(db)) {
-                                       Files.write(f.toPath(), preImage);
-                                       git.add().addFilepattern(name).call();
-                               }
+                               initPreImage(aName);
                        }
                        try (Git git = new Git(db)) {
                                RevCommit base = git.commit().setMessage("PreImage").call();
@@ -99,8 +94,22 @@ public class PatchApplierTest {
                        }
                }
 
-               void init(final String aName) throws Exception {
-                       init(aName, true, true);
+               protected void initPreImage(String aName) throws Exception {
+                       File f = new File(db.getWorkTree(), aName);
+                       preImage = IO
+                                       .readWholeStream(getTestResource(aName + "_PreImage"), 0)
+                                       .array();
+                       try (Git git = new Git(db)) {
+                               Files.write(f.toPath(), preImage);
+                               git.add().addFilepattern(aName).call();
+                       }
+               }
+
+               protected String initPostImage(String aName) throws Exception {
+                       postImage = IO
+                                       .readWholeStream(getTestResource(aName + "_PostImage"), 0)
+                                       .array();
+                       return new String(postImage, StandardCharsets.UTF_8);
                }
 
                protected Result applyPatch()
@@ -118,27 +127,33 @@ public class PatchApplierTest {
                        return PatchApplierTest.class.getClassLoader()
                                        .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile);
                }
+
                void verifyChange(Result result, String aName) throws Exception {
                        verifyChange(result, aName, true);
                }
 
                protected void verifyContent(Result result, String path, boolean exists)
                                throws Exception {
+                       verifyContent(result, path, exists ? expectedText : null);
+               }
+
+               protected void verifyContent(Result result, String path,
+                               @Nullable String expectedContent) throws Exception {
                        if (inCore) {
                                byte[] output = readBlob(result.getTreeId(), path);
-                               if (!exists)
+                               if (expectedContent == null)
                                        assertNull(output);
                                else {
                                        assertNotNull(output);
-                                       assertEquals(expectedText,
+                                       assertEquals(expectedContent,
                                                        new String(output, StandardCharsets.UTF_8));
                                }
                        } else {
                                File f = new File(db.getWorkTree(), path);
-                               if (!exists)
+                               if (expectedContent == null)
                                        assertFalse(f.exists());
                                else
-                                       checkFile(f, expectedText);
+                                       checkFile(f, expectedContent);
                        }
                }
 
@@ -154,7 +169,7 @@ public class PatchApplierTest {
                                        RevWalk rw = tr.getRevWalk()) {
                                db.incrementOpen();
                                RevTree tree = rw.parseTree(treeish);
-                               try (TreeWalk tw = TreeWalk.forPath(db,path,tree)){
+                               try (TreeWalk tw = TreeWalk.forPath(db, path, tree)) {
                                        if (tw == null) {
                                                return null;
                                        }
@@ -300,7 +315,7 @@ public class PatchApplierTest {
                        assertTrue(result.getPaths().contains("RenameNoHunks"));
                        assertTrue(result.getPaths().contains("nested/subdir/Renamed"));
 
-                       verifyContent(result,"nested/subdir/Renamed", true);
+                       verifyContent(result, "nested/subdir/Renamed", true);
                }
 
                @Test
@@ -312,7 +327,7 @@ public class PatchApplierTest {
                        assertTrue(result.getPaths().contains("RenameWithHunks"));
                        assertTrue(result.getPaths().contains("nested/subdir/Renamed"));
 
-                       verifyContent(result,"nested/subdir/Renamed", true);
+                       verifyContent(result, "nested/subdir/Renamed", true);
                }
 
                @Test
@@ -355,6 +370,16 @@ public class PatchApplierTest {
                        verifyChange(result, "ShiftDown2");
                }
 
+               @Test
+               public void testDoesNotAffectUnrelatedFiles() throws Exception {
+                       initPreImage("Unaffected");
+                       String expectedUnaffectedText = initPostImage("Unaffected");
+                       init("X");
+
+                       Result result = applyPatch();
+                       verifyChange(result, "X");
+                       verifyContent(result, "Unaffected", expectedUnaffectedText);
+               }
        }
 
        public static class InCore extends Base {
index 2a4de1331dc91560a4bac687aa3517a429855cae..98a2804ee4fe098ed0cddbff5bf257dc36701500 100644 (file)
@@ -194,7 +194,7 @@ public class PatchApplier {
                                throw new PatchFormatException(p.getErrors());
                        }
 
-                       DirCache dirCache = (inCore()) ? DirCache.newInCore()
+                       DirCache dirCache = inCore() ? DirCache.read(reader, beforeTree)
                                        : repo.lockDirCache();
 
                        DirCacheBuilder dirCacheBuilder = dirCache.builder();