]> source.dussan.org Git - jgit.git/commitdiff
Fix merge/cherry-picking in CRLF mode 60/22060/3
authorRobin Rosenberg <robin.rosenberg@dewire.com>
Sun, 15 Dec 2013 15:57:01 +0000 (16:57 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Sun, 2 Mar 2014 19:30:42 +0000 (20:30 +0100)
This fixes a case where we have CRLF in the repo but
LF in the worktree and are in autocrlf mode.

Change-Id: I0388270c1cf0fd22dfd513bcaa404eb97268d39d
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/merge/ResolveMergerTest.java
org.eclipse.jgit/src/org/eclipse/jgit/merge/ResolveMerger.java

index 54de8cfb47bf9093668f1b81ffeb2fea6d2c6db1..dd06168c304136aaa554fa1c10a56279ecb9ace0 100644 (file)
@@ -53,6 +53,7 @@ import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.MergeResult;
 import org.eclipse.jgit.api.MergeResult.MergeStatus;
 import org.eclipse.jgit.api.errors.CheckoutConflictException;
+import org.eclipse.jgit.api.errors.GitAPIException;
 import org.eclipse.jgit.api.errors.JGitInternalException;
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.errors.NoMergeBaseException;
@@ -264,6 +265,37 @@ public class ResolveMergerTest extends RepositoryTestCase {
                                indexState(CONTENT));
        }
 
+       @Theory
+       public void mergeWithCrlfInWT(MergeStrategy strategy) throws IOException,
+                       GitAPIException {
+               Git git = Git.wrap(db);
+               db.getConfig().setString("core", null, "autocrlf", "false");
+               db.getConfig().save();
+               writeTrashFile("crlf.txt", "some\r\ndata\r\n");
+               git.add().addFilepattern("crlf.txt").call();
+               git.commit().setMessage("base").call();
+
+               git.branchCreate().setName("brancha").call();
+
+               writeTrashFile("crlf.txt", "some\r\nmore\r\ndata\r\n");
+               git.add().addFilepattern("crlf.txt").call();
+               git.commit().setMessage("on master").call();
+
+               git.checkout().setName("brancha").call();
+               writeTrashFile("crlf.txt", "some\r\ndata\r\ntoo\r\n");
+               git.add().addFilepattern("crlf.txt").call();
+               git.commit().setMessage("on brancha").call();
+
+               db.getConfig().setString("core", null, "autocrlf", "input");
+               db.getConfig().save();
+
+               MergeResult mergeResult = git.merge().setStrategy(strategy)
+                               .include(db.resolve("master"))
+                               .call();
+               assertEquals(MergeResult.MergeStatus.MERGED,
+                               mergeResult.getMergeStatus());
+       }
+
        /**
         * Merging two equal subtrees when the index does not contain any file in
         * that subtree should lead to a merged state.
index 414746dc423d7e5ce9f8c9f911c819350339fb82..a891c85911621428775e73137d7502cf87c6dfbe 100644 (file)
@@ -439,7 +439,7 @@ public class ResolveMerger extends ThreeWayMerger {
                                        else {
                                                // the preferred version THEIRS has a different mode
                                                // than ours. Check it out!
-                                               if (isWorktreeDirty(work))
+                                               if (isWorktreeDirty(work, ourDce))
                                                        return false;
                                                // we know about length and lastMod only after we have written the new content.
                                                // This will happen later. Set these values to 0 for know.
@@ -477,7 +477,7 @@ public class ResolveMerger extends ThreeWayMerger {
                        // THEIRS. THEIRS is chosen.
 
                        // Check worktree before checking out THEIRS
-                       if (isWorktreeDirty(work))
+                       if (isWorktreeDirty(work, ourDce))
                                return false;
                        if (nonTree(modeT)) {
                                // we know about length and lastMod only after we have written
@@ -535,7 +535,7 @@ public class ResolveMerger extends ThreeWayMerger {
 
                if (nonTree(modeO) && nonTree(modeT)) {
                        // Check worktree before modifying files
-                       if (isWorktreeDirty(work))
+                       if (isWorktreeDirty(work, ourDce))
                                return false;
 
                        // Don't attempt to resolve submodule link conflicts
@@ -566,7 +566,7 @@ public class ResolveMerger extends ThreeWayMerger {
                                // OURS was deleted checkout THEIRS
                                if (modeO == 0) {
                                        // Check worktree before checking out THEIRS
-                                       if (isWorktreeDirty(work))
+                                       if (isWorktreeDirty(work, ourDce))
                                                return false;
                                        if (nonTree(modeT)) {
                                                if (e != null)
@@ -625,7 +625,8 @@ public class ResolveMerger extends ThreeWayMerger {
                return isDirty;
        }
 
-       private boolean isWorktreeDirty(WorkingTreeIterator work) {
+       private boolean isWorktreeDirty(WorkingTreeIterator work,
+                       DirCacheEntry ourDce) throws IOException {
                if (work == null)
                        return false;
 
@@ -633,9 +634,15 @@ public class ResolveMerger extends ThreeWayMerger {
                final int modeO = tw.getRawMode(T_OURS);
 
                // Worktree entry has to match ours to be considered clean
-               boolean isDirty = work.isModeDifferent(modeO);
-               if (!isDirty && nonTree(modeF))
-                       isDirty = !tw.idEqual(T_FILE, T_OURS);
+               boolean isDirty;
+               if (ourDce != null)
+                       isDirty = work.isModified(ourDce, true, reader);
+               else {
+                       isDirty = work.isModeDifferent(modeO);
+                       if (!isDirty && nonTree(modeF))
+                               isDirty = !tw.idEqual(T_FILE, T_OURS);
+               }
+
                // Ignore existing empty directories
                if (isDirty && modeF == FileMode.TYPE_TREE
                                && modeO == FileMode.TYPE_MISSING)