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

[PATCH 3/3] read-tree -m 3-way: handle more trivial merges internally

From
Junio C Hamano <junkio@cox.net>
Date
Jun 9, 2005, 07:06 UTC
Message-ID
<7v64woroui.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<7voeagrp11.fsf_-_@assigned-by-dhcp.cox.net>

This patch teaches "read-tree -m O A B" that some more trivial cases can be handled internally. This allows us to loosen otherwise too strict index requirements in case #5ALT, where both branches create a new file identically --- the previous code required index to be up-to-date and aborted the merge when it is not, but there is no reason to require it to be up-to-date in this case.

The test vector has been updated to match the new behaviour as well.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 read-tree.c                 |   16 ++++++++++++++++
 t/t1000-read-tree-m-3way.sh |   27 +++++++++------------------
 2 files changed, 25 insertions(+), 18 deletions(-)
diff --git a/read-tree.c b/read-tree.c
--- a/read-tree.c
+++ b/read-tree.c
@@ -69,6 +69,12 @@ static struct cache_entry *merge_entries
 		if (same(o,b))
 			return a;
 	}
+	/* #5ALT */
+	if (!o && a && b && same(a,b)) {
+		/* Match what git-merge-one-file-script does */
+		printf("Adding %s\n", a->name);
+		return a;
+	}
 	return NULL;
 }
 
@@ -161,6 +167,16 @@ static int threeway_merge(struct cache_e
 		return merged_entry(merge, i, dst);
 	if (i)
 		verify_uptodate(i);
+
+	/* #6ALT, #8ALT, and #10ALT */
+	if ((o && !a && !b) ||
+	    (o && !a && b && same(o, b)) ||
+	    (o && a && !b && same(o, a))) {
+		/* Match what git-merge-one-file-script does */
+		printf("Removing %s\n", o->name); 
+		return 0;
+	}
+
 	count = 0;
 	if (o) { *dst++ = o; count++; }
 	if (a) { *dst++ = a; count++; }
diff --git a/t/t1000-read-tree-m-3way.sh b/t/t1000-read-tree-m-3way.sh
--- a/t/t1000-read-tree-m-3way.sh
+++ b/t/t1000-read-tree-m-3way.sh
@@ -75,21 +75,18 @@ In addition:
 . ../lib-read-tree-m-3way.sh
 
 ################################################################
-# This is the "no trivial merge unless all three exists" table.
+# Trivial "majority when 3 stages exist" merge plus #5ALT, #6ALT,
+# #8ALT, #10ALT trivial merges.
 
 cat >expected <<\EOF
 100644 X 2	AA
 100644 X 3	AA
 100644 X 2	AN
-100644 X 1	DD
 100644 X 3	DF
 100644 X 2	DF/DF
 100644 X 1	DM
 100644 X 3	DM
-100644 X 1	DN
-100644 X 3	DN
-100644 X 2	LL
-100644 X 3	LL
+100644 X 0	LL
 100644 X 1	MD
 100644 X 2	MD
 100644 X 1	MM
@@ -97,8 +94,6 @@ cat >expected <<\EOF
 100644 X 3	MM
 100644 X 0	MN
 100644 X 3	NA
-100644 X 1	ND
-100644 X 2	ND
 100644 X 0	NM
 100644 X 0	NN
 100644 X 0	SS
@@ -108,11 +103,8 @@ cat >expected <<\EOF
 100644 X 2	Z/AA
 100644 X 3	Z/AA
 100644 X 2	Z/AN
-100644 X 1	Z/DD
 100644 X 1	Z/DM
 100644 X 3	Z/DM
-100644 X 1	Z/DN
-100644 X 3	Z/DN
 100644 X 1	Z/MD
 100644 X 2	Z/MD
 100644 X 1	Z/MM
@@ -120,8 +112,6 @@ cat >expected <<\EOF
 100644 X 3	Z/MM
 100644 X 0	Z/MN
 100644 X 3	Z/NA
-100644 X 1	Z/ND
-100644 X 2	Z/ND
 100644 X 0	Z/NM
 100644 X 0	Z/NN
 EOF
@@ -289,23 +279,24 @@ test_expect_failure \
      git-read-tree -m $tree_O $tree_A $tree_B"
 
 test_expect_success \
-    '5 - must match and be up-to-date in !O && A && B && A==B case.' \
+    '5 - must match in !O && A && B && A==B case.' \
     "rm -f .git/index LL &&
      cp .orig-A/LL LL &&
      git-update-cache --add LL &&
      git-read-tree -m $tree_O $tree_A $tree_B &&
      check_result"
 
-test_expect_failure \
-    '5 (fail) - must match and be up-to-date in !O && A && B && A==B case.' \
+test_expect_success \
+    '5 - must match in !O && A && B && A==B case.' \
     "rm -f .git/index LL &&
      cp .orig-A/LL LL &&
      git-update-cache --add LL &&
      echo extra >>LL &&
-     git-read-tree -m $tree_O $tree_A $tree_B"
+     git-read-tree -m $tree_O $tree_A $tree_B &&
+     check_result"
 
 test_expect_failure \
-    '5 (fail) - must match and be up-to-date in !O && A && B && A==B case.' \
+    '5 (fail) - must match in !O && A && B && A==B case.' \
     "rm -f .git/index LL &&
      cp .orig-A/LL LL &&
      echo extra >>LL &&
------------
Previous: Junio C HamanoNext: Linus Torvalds
Message 17 of 33 in “Handling merge conflicts a bit more gracefully..”
  1. Linus TorvaldsJun 8, 2005
  2. Junio C HamanoJun 8, 2005
  3. Linus TorvaldsJun 8, 2005
  4. Junio C HamanoJun 9, 2005
  5. Linus TorvaldsJun 9, 2005
  6. Junio C HamanoJun 9, 2005
  7. Junio C HamanoJun 9, 2005
  8. Linus TorvaldsJun 9, 2005
  9. Junio C HamanoJun 9, 2005
  10. Linus TorvaldsJun 9, 2005
  11. Junio C HamanoJun 9, 2005
  12. Linus TorvaldsJun 9, 2005
  13. Junio C HamanoJun 9, 2005
  14. 0/3 Handling merge conflicts a bit more gracefullyJunio C Hamano, Jun 9, 2005
  15. 1/3 read-tree.c: rename local variables used in 3-way merge code.Junio C Hamano, Jun 9, 2005
  16. 2/3 read-tree -m 3-way: loosen index requirements that is too strict.Junio C Hamano, Jun 9, 2005
  17. 3/3 read-tree -m 3-way: handle more trivial merges internallyJunio C Hamano, Jun 9, 2005
  18. Linus TorvaldsJun 9, 2005
  19. Junio C HamanoJun 9, 2005
  20. Linus TorvaldsJun 9, 2005
  21. Junio C HamanoJun 9, 2005
  22. Add git-diff-stages command.Junio C Hamano, Jun 9, 2005
  23. Linus TorvaldsJun 9, 2005
  24. diff-stages: unuglify the too big main() function.Junio C Hamano, Jun 11, 2005
  25. Junio C HamanoJun 10, 2005
  26. Herbert XuJun 18, 2005
  27. Linus TorvaldsJun 18, 2005
  28. Jeff GarzikJun 9, 2005
  29. Linus TorvaldsJun 9, 2005
  30. read-tree.c: rename local variables used in 3-way merge code.Junio C Hamano, Jun 9, 2005
  31. Handle entry removals during merge correctly.Junio C Hamano, Jun 9, 2005
  32. read-tree -m 3-way: loosen an index requirement that was too strict.Junio C Hamano, Jun 9, 2005
  33. read-tree -m 3-way: handle more trivial merges internally.Junio C Hamano, Jun 9, 2005

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.