diff options
author | Han-Wen NIenhuys <hanwen@google.com> | 2022-09-15 04:16:48 -0400 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org <gerrit@eclipse.org> | 2022-09-15 04:16:48 -0400 |
commit | 21a497843cb804f5e714d8c93526422639efcc80 (patch) | |
tree | acfadae1cd630cf7e7c9dadf3da5f9ac71fedc68 /org.eclipse.jgit.test/tst | |
parent | 85182df26779dd6373d188b6adc936454b4c9189 (diff) | |
parent | acde6c8f5b538f900cfede9035584fd2ed654154 (diff) | |
download | jgit-21a497843cb804f5e714d8c93526422639efcc80.tar.gz jgit-21a497843cb804f5e714d8c93526422639efcc80.zip |
Merge "Split out ApplyCommand logic to PatchApplier class"
Diffstat (limited to 'org.eclipse.jgit.test/tst')
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java | 386 | ||||
-rw-r--r-- | org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java | 534 |
2 files changed, 534 insertions, 386 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java index 40764b739b..a746823525 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/ApplyCommandTest.java @@ -19,19 +19,13 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import org.eclipse.jgit.api.errors.PatchApplyException; import org.eclipse.jgit.api.errors.PatchFormatException; -import org.eclipse.jgit.attributes.FilterCommand; -import org.eclipse.jgit.attributes.FilterCommandFactory; -import org.eclipse.jgit.attributes.FilterCommandRegistry; import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.junit.RepositoryTestCase; -import org.eclipse.jgit.lib.Config; -import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.util.FS; import org.eclipse.jgit.util.IO; import org.junit.Test; @@ -68,189 +62,6 @@ public class ApplyCommandTest extends RepositoryTestCase { } } - @Test - public void testCrLf() throws Exception { - try { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, true); - ApplyResult result = init("crlf", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "crlf"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "crlf"), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - @Test - public void testCrLfOff() throws Exception { - try { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, false); - ApplyResult result = init("crlf", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "crlf"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "crlf"), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - @Test - public void testCrLfEmptyCommitted() throws Exception { - try { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, true); - ApplyResult result = init("crlf3", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "crlf3"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "crlf3"), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - @Test - public void testCrLfNewFile() throws Exception { - try { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, true); - ApplyResult result = init("crlf4", false, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "crlf4"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "crlf4"), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - @Test - public void testPatchWithCrLf() throws Exception { - try { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, false); - ApplyResult result = init("crlf2", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "crlf2"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "crlf2"), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - @Test - public void testPatchWithCrLf2() throws Exception { - String name = "crlf2"; - try (Git git = new Git(db)) { - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, false); - a = new RawText(readFile(name + "_PreImage")); - write(new File(db.getWorkTree(), name), - a.getString(0, a.size(), false)); - - git.add().addFilepattern(name).call(); - git.commit().setMessage("PreImage").call(); - - b = new RawText(readFile(name + "_PostImage")); - - db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF, true); - ApplyResult result = git.apply() - .setPatch(getTestResource(name + ".patch")).call(); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), name), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), name), - b.getString(0, b.size(), false)); - } finally { - db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, - ConfigConstants.CONFIG_KEY_AUTOCRLF); - } - } - - // Clean/smudge filter for testFiltering. The smudgetest test resources were - // created with C git using a clean filter sed -e "s/A/E/g" and the smudge - // filter sed -e "s/E/A/g". To keep the test independent of the presence of - // sed, implement this with a built-in filter. - private static class ReplaceFilter extends FilterCommand { - - private final char toReplace; - - private final char replacement; - - ReplaceFilter(InputStream in, OutputStream out, char toReplace, - char replacement) { - super(in, out); - this.toReplace = toReplace; - this.replacement = replacement; - } - - @Override - public int run() throws IOException { - int b = in.read(); - if (b < 0) { - in.close(); - out.close(); - return -1; - } - if ((b & 0xFF) == toReplace) { - b = replacement; - } - out.write(b); - return 1; - } - } - - @Test - public void testFiltering() throws Exception { - // Set up filter - FilterCommandFactory clean = (repo, in, out) -> { - return new ReplaceFilter(in, out, 'A', 'E'); - }; - FilterCommandFactory smudge = (repo, in, out) -> { - return new ReplaceFilter(in, out, 'E', 'A'); - }; - FilterCommandRegistry.register("jgit://builtin/a2e/clean", clean); - FilterCommandRegistry.register("jgit://builtin/a2e/smudge", smudge); - try (Git git = new Git(db)) { - Config config = db.getConfig(); - config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", - "clean", "jgit://builtin/a2e/clean"); - config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", - "smudge", "jgit://builtin/a2e/smudge"); - write(new File(db.getWorkTree(), ".gitattributes"), - "smudgetest filter=a2e"); - git.add().addFilepattern(".gitattributes").call(); - git.commit().setMessage("Attributes").call(); - ApplyResult result = init("smudgetest", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "smudgetest"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "smudgetest"), - b.getString(0, b.size(), false)); - - } finally { - // Tear down filter - FilterCommandRegistry.unregister("jgit://builtin/a2e/clean"); - FilterCommandRegistry.unregister("jgit://builtin/a2e/smudge"); - } - } - private void checkBinary(String name, boolean hasPreImage) throws Exception { checkBinary(name, hasPreImage, 1); @@ -280,21 +91,6 @@ public class ApplyCommandTest extends RepositoryTestCase { } @Test - public void testBinaryDelta() throws Exception { - checkBinary("delta", true); - } - - @Test - public void testBinaryLiteral() throws Exception { - checkBinary("literal", true); - } - - @Test - public void testBinaryLiteralAdd() throws Exception { - checkBinary("literal_add", false); - } - - @Test public void testEncodingChange() throws Exception { // This is a text patch that changes a file containing ÄÖÜ in UTF-8 to // the same characters in ISO-8859-1. The patch file itself uses mixed @@ -410,188 +206,6 @@ public class ApplyCommandTest extends RepositoryTestCase { b.getString(0, b.size(), false)); } - @Test - public void testModifyM2() throws Exception { - ApplyResult result = init("M2", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - if (FS.DETECTED.supportsExecute()) { - assertTrue(FS.DETECTED.canExecute(result.getUpdatedFiles().get(0))); - } - checkFile(new File(db.getWorkTree(), "M2"), - b.getString(0, b.size(), false)); - } - - @Test - public void testModifyM3() throws Exception { - ApplyResult result = init("M3", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - if (FS.DETECTED.supportsExecute()) { - assertFalse( - FS.DETECTED.canExecute(result.getUpdatedFiles().get(0))); - } - checkFile(new File(db.getWorkTree(), "M3"), - b.getString(0, b.size(), false)); - } - - @Test - public void testModifyX() throws Exception { - ApplyResult result = init("X"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "X"), result.getUpdatedFiles() - .get(0)); - checkFile(new File(db.getWorkTree(), "X"), - b.getString(0, b.size(), false)); - } - - @Test - public void testModifyY() throws Exception { - ApplyResult result = init("Y"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "Y"), result.getUpdatedFiles() - .get(0)); - checkFile(new File(db.getWorkTree(), "Y"), - b.getString(0, b.size(), false)); - } - - @Test - public void testModifyZ() throws Exception { - ApplyResult result = init("Z"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "Z"), result.getUpdatedFiles() - .get(0)); - checkFile(new File(db.getWorkTree(), "Z"), - b.getString(0, b.size(), false)); - } - - @Test - public void testModifyNL1() throws Exception { - ApplyResult result = init("NL1"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NL1"), result - .getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "NL1"), - b.getString(0, b.size(), false)); - } - - @Test - public void testNonASCII() throws Exception { - ApplyResult result = init("NonASCII"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NonASCII"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "NonASCII"), - b.getString(0, b.size(), false)); - } - - @Test - public void testNonASCII2() throws Exception { - ApplyResult result = init("NonASCII2"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NonASCII2"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "NonASCII2"), - b.getString(0, b.size(), false)); - } - - @Test - public void testNonASCIIAdd() throws Exception { - ApplyResult result = init("NonASCIIAdd"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NonASCIIAdd"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "NonASCIIAdd"), - b.getString(0, b.size(), false)); - } - - @Test - public void testNonASCIIAdd2() throws Exception { - ApplyResult result = init("NonASCIIAdd2", false, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NonASCIIAdd2"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "NonASCIIAdd2"), - b.getString(0, b.size(), false)); - } - - @Test - public void testNonASCIIDel() throws Exception { - ApplyResult result = init("NonASCIIDel", true, false); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "NonASCIIDel"), - result.getUpdatedFiles().get(0)); - assertFalse(new File(db.getWorkTree(), "NonASCIIDel").exists()); - } - - @Test - public void testRenameNoHunks() throws Exception { - ApplyResult result = init("RenameNoHunks", true, true); - assertEquals(2, result.getUpdatedFiles().size()); - assertTrue(result.getUpdatedFiles().contains(new File(db.getWorkTree(), "RenameNoHunks"))); - assertTrue(result.getUpdatedFiles().contains(new File(db.getWorkTree(), "nested/subdir/Renamed"))); - checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"), - b.getString(0, b.size(), false)); - } - - @Test - public void testRenameWithHunks() throws Exception { - ApplyResult result = init("RenameWithHunks", true, true); - assertEquals(2, result.getUpdatedFiles().size()); - assertTrue(result.getUpdatedFiles().contains(new File(db.getWorkTree(), "RenameWithHunks"))); - assertTrue(result.getUpdatedFiles().contains(new File(db.getWorkTree(), "nested/subdir/Renamed"))); - checkFile(new File(db.getWorkTree(), "nested/subdir/Renamed"), - b.getString(0, b.size(), false)); - } - - @Test - public void testCopyWithHunks() throws Exception { - ApplyResult result = init("CopyWithHunks", true, true); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "CopyResult"), result.getUpdatedFiles() - .get(0)); - checkFile(new File(db.getWorkTree(), "CopyResult"), - b.getString(0, b.size(), false)); - } - - @Test - public void testShiftUp() throws Exception { - ApplyResult result = init("ShiftUp"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "ShiftUp"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "ShiftUp"), - b.getString(0, b.size(), false)); - } - - @Test - public void testShiftUp2() throws Exception { - ApplyResult result = init("ShiftUp2"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "ShiftUp2"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "ShiftUp2"), - b.getString(0, b.size(), false)); - } - - @Test - public void testShiftDown() throws Exception { - ApplyResult result = init("ShiftDown"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "ShiftDown"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "ShiftDown"), - b.getString(0, b.size(), false)); - } - - @Test - public void testShiftDown2() throws Exception { - ApplyResult result = init("ShiftDown2"); - assertEquals(1, result.getUpdatedFiles().size()); - assertEquals(new File(db.getWorkTree(), "ShiftDown2"), - result.getUpdatedFiles().get(0)); - checkFile(new File(db.getWorkTree(), "ShiftDown2"), - b.getString(0, b.size(), false)); - } - private static byte[] readFile(String patchFile) throws IOException { final InputStream in = getTestResource(patchFile); if (in == null) { diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java new file mode 100644 index 0000000000..c77a19f1b4 --- /dev/null +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/patch/PatchApplierTest.java @@ -0,0 +1,534 @@ +/* + * Copyright (C) 2022, Google Inc. 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 + * https://www.eclipse.org/org/documents/edl-v10.php. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +package org.eclipse.jgit.patch; + +import static org.eclipse.jgit.lib.Constants.OBJ_BLOB; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.errors.PatchApplyException; +import org.eclipse.jgit.api.errors.PatchFormatException; +import org.eclipse.jgit.attributes.FilterCommand; +import org.eclipse.jgit.attributes.FilterCommandFactory; +import org.eclipse.jgit.attributes.FilterCommandRegistry; +import org.eclipse.jgit.junit.RepositoryTestCase; +import org.eclipse.jgit.junit.TestRepository; +import org.eclipse.jgit.lib.Config; +import org.eclipse.jgit.lib.ConfigConstants; +import org.eclipse.jgit.lib.ObjectId; +import org.eclipse.jgit.lib.ObjectInserter; +import org.eclipse.jgit.patch.PatchApplier.Result; +import org.eclipse.jgit.revwalk.RevCommit; +import org.eclipse.jgit.revwalk.RevTree; +import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.treewalk.TreeWalk; +import org.eclipse.jgit.util.FS; +import org.eclipse.jgit.util.IO; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + PatchApplierTest.WithWorktree. class, // + PatchApplierTest.InCore.class, // +}) +public class PatchApplierTest { + + public abstract static class Base extends RepositoryTestCase { + + protected String name; + + /** data before patching. */ + protected byte[] preImage; + /** expected data after patching. */ + protected byte[] postImage; + + protected String expectedText; + protected RevTree baseTip; + public boolean inCore; + + Base(boolean inCore) { + this.inCore = inCore; + } + + protected void init(final String name, final boolean preExists, + final boolean postExists) throws Exception { + /* Patch and pre/postimage are read from data org.eclipse.jgit.test/tst-rsrc/org/eclipse/jgit/diff/ */ + this.name = name; + if (postExists) { + postImage = IO + .readWholeStream(getTestResource(name + "_PostImage"), 0) + .array(); + expectedText = new String(postImage, StandardCharsets.UTF_8); + } + + 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(); + } + } + try (Git git = new Git(db)) { + RevCommit base = git.commit().setMessage("PreImage").call(); + baseTip = base.getTree(); + } + } + + void init(final String name) throws Exception { + init(name, true, true); + } + + protected Result applyPatch() + throws PatchApplyException, PatchFormatException, IOException { + InputStream patchStream = getTestResource(name + ".patch"); + if (inCore) { + try (ObjectInserter oi = db.newObjectInserter()) { + return new PatchApplier(db, baseTip, oi).applyPatch(patchStream); + } + } + return new PatchApplier(db).applyPatch(patchStream); + } + + protected static InputStream getTestResource(String patchFile) { + return PatchApplierTest.class.getClassLoader() + .getResourceAsStream("org/eclipse/jgit/diff/" + patchFile); + } + void verifyChange(Result result, String name) throws Exception { + verifyChange(result, name, true); + } + + protected void verifyContent(Result result, String path, boolean exists) throws Exception { + if (inCore) { + byte[] output = readBlob(result.getTreeId(), path); + if (!exists) + assertNull(output); + else { + assertNotNull(output); + assertEquals(new String(output, StandardCharsets.UTF_8), expectedText); + } + } else { + File f = new File(db.getWorkTree(), path); + if (!exists) + assertFalse(f.exists()); + else + checkFile(f, expectedText); + } + } + + void verifyChange(Result result, String name, boolean exists) throws Exception { + assertEquals(1, result.getPaths().size()); + verifyContent(result, name, exists); + } + + protected byte[] readBlob(ObjectId treeish, String path) throws Exception { + try (TestRepository<?> tr = new TestRepository<>(db); + RevWalk rw = tr.getRevWalk()) { + db.incrementOpen(); + RevTree tree = rw.parseTree(treeish); + try (TreeWalk tw = TreeWalk.forPath(db,path,tree)){ + if (tw == null) { + return null; + } + return tw.getObjectReader().open(tw.getObjectId(0), OBJ_BLOB).getBytes(); + } + } + } + + protected void checkBinary(Result result, int numberOfFiles) throws Exception { + assertEquals(numberOfFiles, result.getPaths().size()); + if (inCore) { + assertArrayEquals(postImage, readBlob(result.getTreeId(), result.getPaths().get(0))); + } else { + File f = new File(db.getWorkTree(), name); + assertArrayEquals(postImage, Files.readAllBytes(f.toPath())); + } + } + + /* tests */ + + @Test + public void testBinaryDelta() throws Exception { + init("delta"); + checkBinary(applyPatch(), 1); + } + + @Test + public void testBinaryLiteral() throws Exception { + init("literal"); + checkBinary(applyPatch(), 1); + } + + @Test + public void testBinaryLiteralAdd() throws Exception { + init("literal_add", false, true); + checkBinary(applyPatch(), 1); + } + + @Test + public void testModifyM2() throws Exception { + init("M2", true, true); + + Result result = applyPatch(); + + if (!inCore && FS.DETECTED.supportsExecute()) { + assertEquals(1, result.getPaths().size()); + File f = new File(db.getWorkTree(), result.getPaths().get(0)); + assertTrue(FS.DETECTED.canExecute(f)); + } + + verifyChange(result, "M2"); + } + + @Test + public void testModifyM3() throws Exception { + init("M3", true, true); + + Result result = applyPatch(); + + verifyChange(result, "M3"); + if (!inCore && FS.DETECTED.supportsExecute()) { + File f = new File(db.getWorkTree(), result.getPaths().get(0)); + assertFalse(FS.DETECTED.canExecute(f)); + } + } + + @Test + public void testModifyX() throws Exception { + init("X"); + + Result result = applyPatch(); + verifyChange(result, "X"); + } + + @Test + public void testModifyY() throws Exception { + init("Y"); + + Result result = applyPatch(); + + verifyChange(result, "Y"); + } + + @Test + public void testModifyZ() throws Exception { + init("Z"); + + Result result = applyPatch(); + verifyChange(result, "Z"); + } + + @Test + public void testNonASCII() throws Exception { + init("NonASCII"); + + Result result = applyPatch(); + verifyChange(result, "NonASCII"); + } + + @Test + public void testNonASCII2() throws Exception { + init("NonASCII2"); + + Result result = applyPatch(); + verifyChange(result, "NonASCII2"); + } + + @Test + public void testNonASCIIAdd() throws Exception { + init("NonASCIIAdd"); + + Result result = applyPatch(); + verifyChange(result, "NonASCIIAdd"); + } + + @Test + public void testNonASCIIAdd2() throws Exception { + init("NonASCIIAdd2", false, true); + + Result result = applyPatch(); + verifyChange(result, "NonASCIIAdd2"); + } + + @Test + public void testNonASCIIDel() throws Exception { + init("NonASCIIDel", true, false); + + Result result = applyPatch(); + verifyChange(result, "NonASCIIDel", false); + assertEquals("NonASCIIDel", result.getPaths().get(0)); + } + + @Test + public void testRenameNoHunks() throws Exception { + init("RenameNoHunks", true, true); + + Result result = applyPatch(); + + assertEquals(2, result.getPaths().size()); + assertTrue(result.getPaths().contains("RenameNoHunks")); + assertTrue(result.getPaths().contains("nested/subdir/Renamed")); + + verifyContent(result,"nested/subdir/Renamed", true); + } + + @Test + public void testRenameWithHunks() throws Exception { + init("RenameWithHunks", true, true); + + Result result = applyPatch(); + assertEquals(2, result.getPaths().size()); + assertTrue(result.getPaths().contains("RenameWithHunks")); + assertTrue(result.getPaths().contains("nested/subdir/Renamed")); + + verifyContent(result,"nested/subdir/Renamed", true); + } + + @Test + public void testCopyWithHunks() throws Exception { + init("CopyWithHunks", true, true); + + Result result = applyPatch(); + verifyChange(result, "CopyResult", true); + } + + @Test + public void testShiftUp() throws Exception { + init("ShiftUp"); + + Result result = applyPatch(); + verifyChange(result, "ShiftUp"); + } + + @Test + public void testShiftUp2() throws Exception { + init("ShiftUp2"); + + Result result = applyPatch(); + verifyChange(result, "ShiftUp2"); + } + + @Test + public void testShiftDown() throws Exception { + init("ShiftDown"); + + Result result = applyPatch(); + verifyChange(result, "ShiftDown"); + } + + @Test + public void testShiftDown2() throws Exception { + init("ShiftDown2"); + + Result result = applyPatch(); + verifyChange(result, "ShiftDown2"); + } + } + + public static class InCore extends Base { + + public InCore() { + super(true); + } + } + + public static class WithWorktree extends Base { + public WithWorktree() { super(false); } + + @Test + public void testModifyNL1() throws Exception { + init("NL1"); + + Result result = applyPatch(); + verifyChange(result, "NL1"); + } + + @Test + public void testCrLf() throws Exception { + try { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, true); + init("crlf", true, true); + + Result result = applyPatch(); + + verifyChange(result, "crlf"); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + @Test + public void testCrLfOff() throws Exception { + try { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, false); + init("crlf", true, true); + + Result result = applyPatch(); + + verifyChange(result, "crlf"); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + @Test + public void testCrLfEmptyCommitted() throws Exception { + try { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, true); + init("crlf3", true, true); + + Result result = applyPatch(); + + verifyChange(result, "crlf3"); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + @Test + public void testCrLfNewFile() throws Exception { + try { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, true); + init("crlf4", false, true); + + Result result = applyPatch(); + + verifyChange(result, "crlf4"); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + @Test + public void testPatchWithCrLf() throws Exception { + try { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, false); + init("crlf2", true, true); + + Result result = applyPatch(); + + verifyChange(result, "crlf2"); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + @Test + public void testPatchWithCrLf2() throws Exception { + String name = "crlf2"; + try (Git git = new Git(db)) { + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, false); + init(name, true, true); + db.getConfig().setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF, true); + + Result result = applyPatch(); + + verifyChange(result, name); + } finally { + db.getConfig().unset(ConfigConstants.CONFIG_CORE_SECTION, null, + ConfigConstants.CONFIG_KEY_AUTOCRLF); + } + } + + // Clean/smudge filter for testFiltering. The smudgetest test resources were + // created with C git using a clean filter sed -e "s/A/E/g" and the smudge + // filter sed -e "s/E/A/g". To keep the test independent of the presence of + // sed, implement this with a built-in filter. + private static class ReplaceFilter extends FilterCommand { + + private final char toReplace; + + private final char replacement; + + ReplaceFilter(InputStream in, OutputStream out, char toReplace, + char replacement) { + super(in, out); + this.toReplace = toReplace; + this.replacement = replacement; + } + + @Override + public int run() throws IOException { + int b = in.read(); + if (b < 0) { + in.close(); + out.close(); + return -1; + } + if ((b & 0xFF) == toReplace) { + b = replacement; + } + out.write(b); + return 1; + } + } + + @Test + public void testFiltering() throws Exception { + // Set up filter + FilterCommandFactory clean = (repo, in, out) -> new ReplaceFilter(in, out, 'A', 'E'); + FilterCommandFactory smudge = (repo, in, out) -> new ReplaceFilter(in, out, 'E', 'A'); + FilterCommandRegistry.register("jgit://builtin/a2e/clean", clean); + FilterCommandRegistry.register("jgit://builtin/a2e/smudge", smudge); + Config config = db.getConfig(); + try (Git git = new Git(db)) { + config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", + "clean", "jgit://builtin/a2e/clean"); + config.setString(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", + "smudge", "jgit://builtin/a2e/smudge"); + write(new File(db.getWorkTree(), ".gitattributes"), + "smudgetest filter=a2e"); + git.add().addFilepattern(".gitattributes").call(); + git.commit().setMessage("Attributes").call(); + init("smudgetest", true, true); + + Result result = applyPatch(); + + verifyChange(result, name); + } finally { + config.unset(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", + "clean"); + config.unset(ConfigConstants.CONFIG_FILTER_SECTION, "a2e", + "smudge"); + // Tear down filter + FilterCommandRegistry.unregister("jgit://builtin/a2e/clean"); + FilterCommandRegistry.unregister("jgit://builtin/a2e/smudge"); + } + } + } +} |