From 22d7ec2971edf8d375c4fc9ef9d92526bad73626 Mon Sep 17 00:00:00 2001 From: Christian Halstrick Date: Tue, 12 Apr 2016 14:38:37 +0200 Subject: [PATCH] Fix calling of clean/smudge filters from Checkout,MergeCommands When CheckoutCommand or MergeCommand is called then not in all situation the treewalks have been prepared to support clean/smudge filters. Fix this Bug: 491505 Change-Id: Iab5608049221c46d06812552ab97299e44d59e64 --- .../eclipse/jgit/api/CheckoutCommandTest.java | 37 ++++++++++--------- .../jgit/dircache/DirCacheCheckout.java | 12 ++++-- .../org/eclipse/jgit/merge/ResolveMerger.java | 5 ++- .../merge/StrategySimpleTwoWayInCore.java | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java index 8162ac457..0bb6610a2 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/CheckoutCommandTest.java @@ -88,7 +88,6 @@ import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.util.FileUtils; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class CheckoutCommandTest extends RepositoryTestCase { @@ -740,11 +739,9 @@ public class CheckoutCommandTest extends RepositoryTestCase { } @Test - @Ignore - public void testSmudgeAndClean() throws IOException, GitAPIException { - // @TODO: fix this test - File clean_filter = writeTempFile("sed s/V1/@version/g -"); - File smudge_filter = writeTempFile("sed s/@version/V1/g -"); + public void testSmudgeAndClean() throws Exception { + File clean_filter = writeTempFile("sed s/V1/@version/g"); + File smudge_filter = writeTempFile("sed s/@version/V1/g"); try (Git git2 = new Git(db)) { StoredConfig config = git.getRepository().getConfig(); @@ -753,33 +750,39 @@ public class CheckoutCommandTest extends RepositoryTestCase { config.setString("filter", "tstFilter", "clean", "sh " + slashify(clean_filter.getPath())); config.save(); - writeTrashFile(".gitattributes", "*.txt filter=tstFilter"); + writeTrashFile(".gitattributes", "filterTest.txt filter=tstFilter"); git2.add().addFilepattern(".gitattributes").call(); git2.commit().setMessage("add attributes").call(); - writeTrashFile("filterTest.txt", "hello world, V1"); + fsTick(writeTrashFile("filterTest.txt", "hello world, V1\n")); git2.add().addFilepattern("filterTest.txt").call(); - git2.commit().setMessage("add filterText.txt").call(); + RevCommit one = git2.commit().setMessage("add filterText.txt").call(); assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=tstFilter][Test.txt, mode:100644, content:Some other change][filterTest.txt, mode:100644, content:hello world, @version]", + "[.gitattributes, mode:100644, content:filterTest.txt filter=tstFilter][Test.txt, mode:100644, content:Some change][filterTest.txt, mode:100644, content:hello world, @version\n]", indexState(CONTENT)); - git2.checkout().setCreateBranch(true).setName("test2").call(); - writeTrashFile("filterTest.txt", "bon giorno world, V1"); + fsTick(writeTrashFile("filterTest.txt", "bon giorno world, V1\n")); git2.add().addFilepattern("filterTest.txt").call(); - git2.commit().setMessage("modified filterText.txt").call(); + RevCommit two = git2.commit().setMessage("modified filterTest.txt").call(); assertTrue(git2.status().call().isClean()); assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=tstFilter][Test.txt, mode:100644, content:Some other change][filterTest.txt, mode:100644, content:bon giorno world, @version]", + "[.gitattributes, mode:100644, content:filterTest.txt filter=tstFilter][Test.txt, mode:100644, content:Some change][filterTest.txt, mode:100644, content:bon giorno world, @version\n]", indexState(CONTENT)); - git2.checkout().setName("refs/heads/test").call(); + git2.checkout().setName(one.getName()).call(); assertTrue(git2.status().call().isClean()); assertEquals( - "[.gitattributes, mode:100644, content:*.txt filter=tstFilter][Test.txt, mode:100644, content:Some other change][filterTest.txt, mode:100644, content:hello world, @version]", + "[.gitattributes, mode:100644, content:filterTest.txt filter=tstFilter][Test.txt, mode:100644, content:Some change][filterTest.txt, mode:100644, content:hello world, @version\n]", indexState(CONTENT)); - assertEquals("hello world, V1", read("filterTest.txt")); + assertEquals("hello world, V1\n", read("filterTest.txt")); + + git2.checkout().setName(two.getName()).call(); + assertTrue(git2.status().call().isClean()); + assertEquals( + "[.gitattributes, mode:100644, content:filterTest.txt filter=tstFilter][Test.txt, mode:100644, content:Some change][filterTest.txt, mode:100644, content:bon giorno world, @version\n]", + indexState(CONTENT)); + assertEquals("bon giorno world, V1\n", read("filterTest.txt")); } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java index 3fcaa3839..fc4cc9093 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/dircache/DirCacheCheckout.java @@ -280,8 +280,9 @@ public class DirCacheCheckout { addTree(walk, headCommitTree); addTree(walk, mergeCommitTree); - walk.addTree(new DirCacheBuildIterator(builder)); + int dciPos = walk.addTree(new DirCacheBuildIterator(builder)); walk.addTree(workingTree); + workingTree.setDirCacheIterator(walk, dciPos); while (walk.next()) { processEntry(walk.getTree(0, CanonicalTreeParser.class), @@ -320,8 +321,9 @@ public class DirCacheCheckout { walk = new NameConflictTreeWalk(repo); addTree(walk, mergeCommitTree); - walk.addTree(new DirCacheBuildIterator(builder)); + int dciPos = walk.addTree(new DirCacheBuildIterator(builder)); walk.addTree(workingTree); + workingTree.setDirCacheIterator(walk, dciPos); while (walk.next()) { processEntry(walk.getTree(0, CanonicalTreeParser.class), @@ -1093,8 +1095,10 @@ public class DirCacheCheckout { private boolean isModifiedSubtree_IndexWorkingtree(String path) throws CorruptObjectException, IOException { try (NameConflictTreeWalk tw = new NameConflictTreeWalk(repo)) { - tw.addTree(new DirCacheIterator(dc)); - tw.addTree(new FileTreeIterator(repo)); + int dciPos = tw.addTree(new DirCacheIterator(dc)); + FileTreeIterator fti = new FileTreeIterator(repo); + tw.addTree(fti); + fti.setDirCacheIterator(tw, dciPos); tw.setRecursive(true); tw.setFilter(PathFilter.create(path)); DirCacheIterator dcIt; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java b/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java index de08e4b6a..e224d71d4 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java @@ -1005,13 +1005,14 @@ public class ResolveMerger extends ThreeWayMerger { builder = dircache.builder(); DirCacheBuildIterator buildIt = new DirCacheBuildIterator(builder); - tw = new NameConflictTreeWalk(reader); + tw = new NameConflictTreeWalk(db, reader); tw.addTree(baseTree); tw.addTree(headTree); tw.addTree(mergeTree); - tw.addTree(buildIt); + int dciPos = tw.addTree(buildIt); if (workingTreeIterator != null) { tw.addTree(workingTreeIterator); + workingTreeIterator.setDirCacheIterator(tw, dciPos); } else { tw.setFilter(TreeFilter.ANY_DIFF); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/merge/StrategySimpleTwoWayInCore.java b/org.eclipse.jgit/src/org/eclipse/jgit/merge/StrategySimpleTwoWayInCore.java index f18a52213..ec903c139 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/merge/StrategySimpleTwoWayInCore.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/merge/StrategySimpleTwoWayInCore.java @@ -106,7 +106,7 @@ public class StrategySimpleTwoWayInCore extends ThreeWayMergeStrategy { InCoreMerger(final Repository local) { super(local); - tw = new NameConflictTreeWalk(reader); + tw = new NameConflictTreeWalk(local, reader); cache = DirCache.newInCore(); }