]> source.dussan.org Git - jgit.git/commitdiff
Added support to IndexDiff to return information on conflicts 05/3105/4
authorBernard Leach <leachbj@bouncycastle.org>
Tue, 19 Apr 2011 21:51:40 +0000 (07:51 +1000)
committerChris Aniszczyk <caniszczyk@gmail.com>
Tue, 19 Apr 2011 22:11:01 +0000 (17:11 -0500)
Change-Id: I43c13eb72a44f80135c93525fce0c0280b0e64a2
Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/IndexDiffTest.java
org.eclipse.jgit/src/org/eclipse/jgit/lib/IndexDiff.java

index d98358746be3100a31c09dfd9fff936b32d8d7d4..80c779ddfd94eb679ad0cbabb994a928147b59b1 100644 (file)
@@ -50,11 +50,16 @@ import static org.junit.Assert.assertTrue;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.TreeSet;
 
 import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.MergeResult;
+import org.eclipse.jgit.api.MergeResult.MergeStatus;
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheEditor;
 import org.eclipse.jgit.dircache.DirCacheEntry;
+import org.eclipse.jgit.merge.MergeStrategy;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.treewalk.FileTreeIterator;
 import org.junit.Test;
 
@@ -139,6 +144,47 @@ public class IndexDiffTest extends RepositoryTestCase {
                assertEquals(0, diff.getMissing().size());
        }
 
+       @Test
+       public void testConflicting() throws Exception {
+               Git git = new Git(db);
+
+               writeTrashFile("a", "1\na\n3\n");
+               writeTrashFile("b", "1\nb\n3\n");
+               git.add().addFilepattern("a").addFilepattern("b").call();
+               RevCommit initialCommit = git.commit().setMessage("initial").call();
+
+               // create side branch with two modifications
+               createBranch(initialCommit, "refs/heads/side");
+               checkoutBranch("refs/heads/side");
+               writeTrashFile("a", "1\na(side)\n3\n");
+               writeTrashFile("b", "1\nb\n3\n(side)");
+               git.add().addFilepattern("a").addFilepattern("b").call();
+               RevCommit secondCommit = git.commit().setMessage("side").call();
+
+               // update a on master to generate conflict
+               checkoutBranch("refs/heads/master");
+               writeTrashFile("a", "1\na(main)\n3\n");
+               git.add().addFilepattern("a").call();
+               git.commit().setMessage("main").call();
+
+               // merge side with master
+               MergeResult result = git.merge().include(secondCommit.getId())
+                               .setStrategy(MergeStrategy.RESOLVE).call();
+               assertEquals(MergeStatus.CONFLICTING, result.getMergeStatus());
+
+               FileTreeIterator iterator = new FileTreeIterator(db);
+               IndexDiff diff = new IndexDiff(db, Constants.HEAD, iterator);
+               diff.diff();
+
+               assertEquals("[a, b]",
+                               new TreeSet<String>(diff.getChanged()).toString());
+               assertEquals("[a]", diff.getAdded().toString());
+               assertEquals("[]", diff.getRemoved().toString());
+               assertEquals("[a]", diff.getMissing().toString());
+               assertEquals("[a]", diff.getModified().toString());
+               assertEquals("[a]", diff.getConflicting().toString());
+       }
+
        @Test
        public void testUnchangedSimple() throws IOException {
                GitIndex index = new GitIndex(db);
index 73bda08581fe7c8a1a7e51dc5cc94b1cbf11f4b3..b2c05ae5f7cea107c581a8d1ab53cc6e8ce9a9a5 100644 (file)
@@ -52,6 +52,7 @@ import java.util.HashSet;
 import java.util.Set;
 
 import org.eclipse.jgit.dircache.DirCache;
+import org.eclipse.jgit.dircache.DirCacheEntry;
 import org.eclipse.jgit.dircache.DirCacheIterator;
 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
 import org.eclipse.jgit.errors.MissingObjectException;
@@ -76,6 +77,7 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter;
  * <li>removed files</li>
  * <li>missing files</li>
  * <li>modified files</li>
+ * <li>conflicting files</li>
  * <li>untracked files</li>
  * <li>files with assume-unchanged flag</li>
  * </ul>
@@ -153,6 +155,8 @@ public class IndexDiff {
 
        private Set<String> untracked = new HashSet<String>();
 
+       private Set<String> conflicts = new HashSet<String>();
+
        private Set<String> assumeUnchanged;
 
        private DirCache dirCache;
@@ -320,6 +324,12 @@ public class IndexDiff {
                                                modified.add(treeWalk.getPathString());
                                        }
                                }
+
+                               final DirCacheEntry dirCacheEntry = dirCacheIterator
+                                               .getDirCacheEntry();
+                               if (dirCacheEntry != null && dirCacheEntry.getStage() > 0) {
+                                       conflicts.add(treeWalk.getPathString());
+                               }
                        }
                }
 
@@ -377,6 +387,13 @@ public class IndexDiff {
                return untracked;
        }
 
+       /**
+        * @return list of files that are in conflict
+        */
+       public Set<String> getConflicting() {
+               return conflicts;
+       }
+
        /**
         * @return list of files with the flag assume-unchanged
         */