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

[PATCH 07/14] merge: clarify collect_parents() logic

From
Junio C Hamano <gitster@pobox.com>
Date
Apr 26, 2015, 05:26 UTC
Message-ID
<1430025967-24479-8-git-send-email-gitster@pobox.com>
In-Reply-To
<1430025967-24479-1-git-send-email-gitster@pobox.com>
Clarify this small function in three ways.
 - The function initially collects all commits to be merged into a
   commit_list "remoteheads"; the "remotes" pointer always points at
   the tail of this list (either the remoteheads variable itself, or
   the ->next slot of the element at the end of the list) to help
   elongate the list by repeated calls to commit_list_insert().
   Because the new element appended by commit_list_insert() will
   always have its ->next slot NULLed out, there is no need for us
   to assign NULL to *remotes to terminate the list at the end.
 - The variable "head_subsumed" always confused me every time I read
   this code.  What is happening here is that we inspect what the
   caller told us to merge (including the current HEAD) and come up
   with the list of parents to be recorded for the resulting merge
   commit, omitting commits that are ancestor of other commits.
   This filtering may remove the current HEAD from the resulting
   parent list---and we signal that fact with this variable, so that
   we can later record it as the first parent when "--no-ff" is in
   effect.
 - The "parents" list is created for this function by reduce_heads()
   and was not deallocated after its use, even though the loop
   control was written in such a way to allow us to do so by taking
   the "next" element in a separate variable so that it can be used
   in the next-step part of the loop control.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 builtin/merge.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/builtin/merge.c b/builtin/merge.c
index b2d0332..d2e36e2 100644
--- a/builtin/merge.c
+++ b/builtin/merge.c
@@ -1061,11 +1061,19 @@ static struct commit_list *collect_parents(struct commit *head_commit,
 					 "not something we can merge");
 		remotes = &commit_list_insert(commit, remotes)->next;
 	}
-	*remotes = NULL;
 
+	/*
+	 * Is the current HEAD reachable from another commit being
+	 * merged?  If so we do not want to record it as a parent of
+	 * the resulting merge, unless --no-ff is given.  We will flip
+	 * this variable to 0 when we find HEAD among the independent
+	 * tips being merged.
+	 */
+	*head_subsumed = 1;
+
+	/* Find what parents to record by checking independent ones. */
 	parents = reduce_heads(remoteheads);
 
-	*head_subsumed = 1; /* we will flip this to 0 when we find it */
 	for (remoteheads = NULL, remotes = &remoteheads;
 	     parents;
 	     parents = next) {
@@ -1075,6 +1083,7 @@ static struct commit_list *collect_parents(struct commit *head_commit,
 			*head_subsumed = 0;
 		else
 			remotes = &commit_list_insert(commit, remotes)->next;
+		free(parents);
 	}
 	return remoteheads;
 }
-- 
2.4.0-rc3-238-g36f5934
Previous: Junio C HamanoNext: Junio C Hamano
Message 15 of 42 in “[BUG] "git pull" will regress between 'master' and 'pu'”
  1. Junio C HamanoApr 19, 2015
  2. Jeff KingApr 19, 2015
  3. brian m. carlsonApr 19, 2015
  4. Jeff KingApr 19, 2015
  5. Junio C HamanoApr 20, 2015
  6. Jeff KingApr 20, 2015
  7. Junio C HamanoApr 20, 2015
  8. 00/14 Teach "git merge FETCH_HEAD" octopus mergesJunio C Hamano, Apr 26, 2015
  9. 01/14 merge: simplify code flowJunio C Hamano, Apr 26, 2015
  10. 02/14 t5520: style fixesJunio C Hamano, Apr 26, 2015
  11. 03/14 t5520: test pulling an octopus into an unborn branchJunio C Hamano, Apr 26, 2015
  12. 04/14 merge: clarify "pulling into void" special caseJunio C Hamano, Apr 26, 2015
  13. 05/14 merge: do not check argc to determine number of remote headsJunio C Hamano, Apr 26, 2015
  14. 06/14 merge: small leakfix and code simplificationJunio C Hamano, Apr 26, 2015
  15. 07/14 merge: clarify collect_parents() logicJunio C Hamano, Apr 26, 2015
  16. 08/14 merge: split reduce_parents() out of collect_parents()Junio C Hamano, Apr 26, 2015
  17. 09/14 merge: narrow scope of merge_namesJunio C Hamano, Apr 26, 2015
  18. 10/14 merge: extract prepare_merge_message() logic outJunio C Hamano, Apr 26, 2015
  19. 11/14 merge: make collect_parents() auto-generate the merge messageJunio C Hamano, Apr 26, 2015
  20. 12/14 merge: decide if we auto-generate the message early in collect_parents()Junio C Hamano, Apr 26, 2015
  21. 13/14 merge: handle FETCH_HEAD internallyJunio C Hamano, Apr 26, 2015
  22. 14/14 merge: deprecate 'git merge <message> HEAD <commit>' syntaxJunio C Hamano, Apr 26, 2015
  23. 00/15 Teach "git merge FETCH_HEAD" octopus mergesJunio C Hamano, Apr 29, 2015
  24. 01/15 merge: test the top-level merge driverJunio C Hamano, Apr 29, 2015
  25. 02/15 merge: simplify code flowJunio C Hamano, Apr 29, 2015
  26. 03/15 t5520: style fixesJunio C Hamano, Apr 29, 2015
  27. Paul TanMay 1, 2015
  28. Junio C HamanoMay 3, 2015
  29. 04/15 t5520: test pulling an octopus into an unborn branchJunio C Hamano, Apr 29, 2015
  30. 05/15 merge: clarify "pulling into void" special caseJunio C Hamano, Apr 29, 2015
  31. 06/15 merge: do not check argc to determine number of remote headsJunio C Hamano, Apr 29, 2015
  32. 07/15 merge: small leakfix and code simplificationJunio C Hamano, Apr 29, 2015
  33. 08/15 merge: clarify collect_parents() logicJunio C Hamano, Apr 29, 2015
  34. 09/15 merge: split reduce_parents() out of collect_parents()Junio C Hamano, Apr 29, 2015
  35. 10/15 merge: narrow scope of merge_namesJunio C Hamano, Apr 29, 2015
  36. 11/15 merge: extract prepare_merge_message() logic outJunio C Hamano, Apr 29, 2015
  37. 12/15 merge: make collect_parents() auto-generate the merge messageJunio C Hamano, Apr 29, 2015
  38. 13/15 merge: decide if we auto-generate the message early in collect_parents()Junio C Hamano, Apr 29, 2015
  39. 14/15 merge: handle FETCH_HEAD internallyJunio C Hamano, Apr 29, 2015
  40. 15/15 merge: deprecate 'git merge <message> HEAD <commit>' syntaxJunio C Hamano, Apr 29, 2015
  41. Junio C HamanoApr 20, 2015
  42. Johannes SchindelinApr 21, 2015

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.