git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH 2/2] Resort entries in "normal" order before looking for conflicts

From
Robin Rosenberg <robin.rosenberg@dewire.com>
Date
Feb 5, 2008, 23:46 UTC
Message-ID
<1202255166-4581-3-git-send-email-robin.rosenberg@dewire.com>
In-Reply-To
<1202255166-4581-2-git-send-email-robin.rosenberg@dewire.com>

When checking out another branch we get a problem if our version contains a tree name that is longer, but has another tree as a prefix.

        Our tree
          a/a
          a.a/b
        Other tree
          a/a

The conflict comes from names not being compared properly in git order.

Directory/File conflicts are not easy to handle. This solution is cheating, but solved a real user's problem.

This commit adds test cases for the particular problem plus one similar test that didn't fail before either.

Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
---
 .../tst/org/spearce/jgit/lib/ReadTreeTest.java     |   16 ++++++++-
 .../src/org/spearce/jgit/lib/IndexTreeWalker.java  |   35 ++++++++++++--------
 2 files changed, 36 insertions(+), 15 deletions(-)
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ReadTreeTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ReadTreeTest.java
index 7ac48c9..6faedc7 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ReadTreeTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ReadTreeTest.java
@@ -477,7 +477,21 @@ public class ReadTreeTest extends RepositoryTestCase {
 		
 		assertNoConflicts();
 	}
-	
+
+	public void testCloseNameConflictsX0() throws IOException {
+		setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "b.b/b.b","b.b/b.bs"), mkmap("a/a", "a/a-c") );
+		checkout();
+		go();
+		assertNoConflicts();
+	}
+
+	public void testCloseNameConflicts1() throws IOException {
+		setupCase(mkmap("a/a", "a/a-c"), mkmap("a/a","a/a", "a.a/a.a","a.a/a.a"), mkmap("a/a", "a/a-c") );
+		checkout();
+		go();
+		assertNoConflicts();
+	}
+
 	private void checkout() throws IOException {
 		readTree = new WorkDirCheckout(db, trash, head, index, merge);
 		readTree.checkout();
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java b/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java
index 93d5bb2..bc2fb23 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/IndexTreeWalker.java
@@ -19,6 +19,8 @@ package org.spearce.jgit.lib;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Arrays;
+import java.util.Comparator;
 
 import org.spearce.jgit.lib.GitIndex.Entry;
 
@@ -85,9 +87,28 @@ public class IndexTreeWalker {
 		walk(mainTree, newTree, "/");
 	}
 
+	static Comparator<TreeEntry> treeEntryPlainComparator() {
+		return new Comparator<TreeEntry>() {
+			public int compare(TreeEntry o1, TreeEntry o2) {
+				return o1.getName().compareTo(o2.getName());
+			}
+		};
+	}
+
+	static Comparator<Entry> indexEntryPlainComparator() {
+		return new Comparator<Entry>() {
+			public int compare(Entry o1, Entry o2) {
+				return o1.getName().compareTo(o2.getName());
+			}
+		};
+	}
+
 	private void walk(Tree tree, Tree auxTree, String curDir) throws IOException {
 		TreeEntry[] treeMembers = tree == null ? new TreeEntry[0] : tree.members();
 		TreeEntry[] auxTreeMembers = auxTree == null ? new TreeEntry[0] : auxTree.members();
+		Arrays.sort(treeMembers, treeEntryPlainComparator());
+		Arrays.sort(auxTreeMembers, treeEntryPlainComparator());
+		Arrays.sort(indexMembers, indexEntryPlainComparator());
 		int treeCounter = 0;
 		int auxTreeCounter = 0;
 		
@@ -308,18 +329,4 @@ public class IndexTreeWalker {
 		}
 		return t1.getName().compareTo(t2.getName());
 	}
-	
-	static int compare(byte[] name1, byte[] name2) {
-		for (int i = 0; i < name1.length && i < name2.length; i++) {
-			if (name1[i] < name2[i])
-				return -1;
-			if (name1[i] > name2[i])
-				return 1;
-		}
-		if (name1.length < name2.length)
-			return -1;
-		if (name2.length < name1.length)
-			return 1;
-		return 0;
-	}
 }
-- 
1.5.4.rc4.25.g81cc
Previous: Robin RosenbergNext: Robin Rosenberg
Message 3 of 7 in “[EGIT Patches] Sort order from hell fixes”
  1. Robin RosenbergFeb 5, 2008
  2. 1/2 Fix git sort order compare bugRobin Rosenberg, Feb 5, 2008
  3. 2/2 Resort entries in "normal" order before looking for conflictsRobin Rosenberg, Feb 5, 2008
  4. Robin RosenbergFeb 6, 2008
  5. Robin RosenbergFeb 10, 2008
  6. Robin RosenbergFeb 10, 2008
  7. David WatsonFeb 10, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.