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

[PATCH] git-merge: Reduce heads before trying to merge them

From
Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
Date
Apr 17, 2012, 18:25 UTC
Message-ID
<1334687118-5386-1-git-send-email-michal.kiedrowicz@gmail.com>
In-Reply-To
<CA+55aFzGwPYNn2baFhEr4msBTV7__nkTSUqAZ7=PRVoYrchV5w@mail.gmail.com>

This makes us do proper fast-forward merges even for octopus merges, which could otherwise result in "merge commit" that only had one actual parent, and should have been a fast-forward.

Odd-case-triggered-by: James Morris <jmorris@namei.org>
Signed-off-by: Michał Kiedrowicz <michal.kiedrowicz@gmail.com>
---

This is the proposed fix sent as a proper patch with commit message stolen from Linus and testcases.

I'm not sure if I don't introduce a memleak with the call to reduce_heads() but other callers seem to not care, just like whole cmd_merge().

 builtin/merge.c               |    3 +++
 t/t7603-merge-reduce-heads.sh |   19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 0 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index 08e01e8..2d5930f 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1346,6 +1346,9 @@ int cmd_merge(int argc, const char **argv, const char *prefix)
 			allow_trivial = 0;
 	}
 
+	if (remoteheads->next)
+		remoteheads = reduce_heads(remoteheads);
+
 	if (!remoteheads->next)
 		common = get_merge_bases(head_commit, remoteheads->item, 1);
 	else {
diff --git a/t/t7603-merge-reduce-heads.sh b/t/t7603-merge-reduce-heads.sh
index 7e17eb4..a3b08a6 100755
--- a/t/t7603-merge-reduce-heads.sh
+++ b/t/t7603-merge-reduce-heads.sh
@@ -113,4 +113,23 @@ test_expect_success 'verify merge result' '
 	test $(git rev-parse HEAD^1) = $(git rev-parse E2) &&
 	test $(git rev-parse HEAD^2) = $(git rev-parse I2)
 '
+
+test_expect_success 'fast-forward to redundant refs' '
+	git reset --hard c0 &&
+	git merge c4 c5
+'
+
+test_expect_success 'verify merge result' '
+	test $(git rev-parse HEAD) = $(git rev-parse c5)
+'
+
+test_expect_success 'merge up-to-date redundant refs' '
+	git reset --hard c5 &&
+	git merge c0 c4
+'
+
+test_expect_success 'verify merge result' '
+	test $(git rev-parse HEAD) = $(git rev-parse c5)
+'
+
 test_done
-- 
1.7.8.4
Previous: Linus TorvaldsNext: Junio C Hamano
Message 16 of 22 in “Re: What's cooking in git.git (Apr 2012, #05; Thu, 12)”
  1. Michal KiedrowiczApr 16, 2012
  2. Linus TorvaldsApr 16, 2012
  3. Junio C HamanoApr 16, 2012
  4. Linus TorvaldsApr 16, 2012
  5. Junio C HamanoApr 16, 2012
  6. 0/4 merge: reduce set of parents consistentlyJunio C Hamano, Apr 17, 2012
  7. 1/4 git-merge: test octopus with redundant parentsJunio C Hamano, Apr 17, 2012
  8. 2/4 builtin/merge.c: remove "remoteheads" global variableJunio C Hamano, Apr 17, 2012
  9. 3/4 builtin/merge.c: collect other parents earlyJunio C Hamano, Apr 17, 2012
  10. 4/4 builtin/merge.c: reduce parents earlyJunio C Hamano, Apr 17, 2012
  11. Junio C HamanoApr 16, 2012
  12. Linus TorvaldsApr 16, 2012
  13. Linus TorvaldsApr 16, 2012
  14. Michał KiedrowiczApr 16, 2012
  15. Linus TorvaldsApr 17, 2012
  16. git-merge: Reduce heads before trying to merge themMichał Kiedrowicz, Apr 17, 2012
  17. Junio C HamanoApr 17, 2012
  18. Linus TorvaldsApr 17, 2012
  19. Junio C HamanoApr 17, 2012
  20. Michał KiedrowiczApr 18, 2012
  21. Junio C HamanoApr 18, 2012
  22. Junio C HamanoApr 19, 2012

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.