git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Johannes Schindelin via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Johannes Sixt <j6t@kdbg.org>, Elijah Newren <newren@gmail.com>,
	Johannes Schindelin <johannes.schindelin@gmx.de>,
	Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: [PATCH v2] log: --remerge-diff needs to keep around commit parents
Date: Mon, 11 Nov 2024 18:33:29 +0000	[thread overview]
Message-ID: <pull.1825.v2.git.1731350009491.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1825.git.1731073435641.gitgitgadget@gmail.com>

From: Johannes Schindelin <johannes.schindelin@gmx.de>

To show a remerge diff, the merge needs to be recreated. For that to
work, the merge base(s) need to be found, which means that the commits'
parents have to be traversed until common ancestors are found (if any).

However, one optimization that hails all the way back to
cb115748ec0d (Some more memory leak avoidance, 2006-06-17) is to release
the commit's list of parents immediately after showing it. This can break
the merge base computation.

Note that it matters more clearly when traversing the commits in
reverse: In that instance, if a parent of a merge commit has been shown
as part of the `git log` command, by the time the merge commit's diff
needs to be computed, that parent commit's list of parent commits will
have been set to `NULL` and as a result no merge base will be found.

Let's fix this by special-casing the `remerge_diff` mode, similar to
what we did with reflogs in f35650dff6a4 (log: do not free parents when
walking reflog, 2017-07-07).

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
    log: --remerge-diff needs to keep around commit parents
    
    This fixes a bug that is pretty much as old as the remerge-diff
    machinery itself. I noticed it while writing my reply to Hannes Sixt's
    concerns about my range-diff --diff-merges=<mode> patch
    (https://lore.kernel.org/git/af576487-5de2-fba3-b341-3c082322c9ec@gmx.de/).
    
    Changes since v1:
    
     * Amended the code comment of the affected conditional code block in
       harmony with the newly-added condition.

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1825%2Fdscho%2Flog-remerge-diff-needs-commit-parents-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1825/dscho/log-remerge-diff-needs-commit-parents-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1825

Range-diff vs v1:

 1:  68238315c14 ! 1:  afff27f8222 log: --remerge-diff needs to keep around commit parents
     @@ builtin/log.c: static int cmd_log_walk_no_free(struct rev_info *rev)
      +		if (!rev->reflog_info && !rev->remerge_diff) {
       			/*
       			 * We may show a given commit multiple times when
     - 			 * walking the reflogs.
     +-			 * walking the reflogs.
     ++			 * walking the reflogs. Therefore we still need it.
     ++			 *
     ++			 * Likewise, we potentially still need the parents
     ++			 * of * already shown commits to determine merge
     ++			 * bases when showing remerge diffs.
     + 			 */
     + 			free_commit_buffer(the_repository->parsed_objects,
     + 					   commit);
      
       ## t/t4069-remerge-diff.sh ##
      @@ t/t4069-remerge-diff.sh: test_expect_success 'remerge-diff turns off history simplification' '


 builtin/log.c           | 8 ++++++--
 t/t4069-remerge-diff.sh | 7 +++++++
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/builtin/log.c b/builtin/log.c
index c0a8bb95e98..2f88a3e607d 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -522,10 +522,14 @@ static int cmd_log_walk_no_free(struct rev_info *rev)
 			 * but we didn't actually show the commit.
 			 */
 			rev->max_count++;
-		if (!rev->reflog_info) {
+		if (!rev->reflog_info && !rev->remerge_diff) {
 			/*
 			 * We may show a given commit multiple times when
-			 * walking the reflogs.
+			 * walking the reflogs. Therefore we still need it.
+			 *
+			 * Likewise, we potentially still need the parents
+			 * of * already shown commits to determine merge
+			 * bases when showing remerge diffs.
 			 */
 			free_commit_buffer(the_repository->parsed_objects,
 					   commit);
diff --git a/t/t4069-remerge-diff.sh b/t/t4069-remerge-diff.sh
index 07323ebafe0..a68c6bfa036 100755
--- a/t/t4069-remerge-diff.sh
+++ b/t/t4069-remerge-diff.sh
@@ -317,4 +317,11 @@ test_expect_success 'remerge-diff turns off history simplification' '
 	test_cmp expect actual
 '
 
+test_expect_success 'remerge-diff with --reverse' '
+	git log -1 --remerge-diff --oneline ab_resolution^ >expect &&
+	git log -1 --remerge-diff --oneline ab_resolution >>expect &&
+	git log -2 --remerge-diff --oneline ab_resolution --reverse >actual &&
+	test_cmp expect actual
+'
+
 test_done

base-commit: bea9ecd24b0c3bf06cab4a851694fe09e7e51408
-- 
gitgitgadget

  parent reply	other threads:[~2024-11-11 18:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-08 13:43 [PATCH] log: --remerge-diff needs to keep around commit parents Johannes Schindelin via GitGitGadget
2024-11-08 15:47 ` Elijah Newren
2024-11-11  1:52   ` Junio C Hamano
2024-11-11  1:46 ` Junio C Hamano
2024-11-11 18:32   ` Johannes Schindelin
2024-11-11 22:13     ` Junio C Hamano
2024-11-11 18:33 ` Johannes Schindelin via GitGitGadget [this message]
2024-12-12 10:29   ` [PATCH v3] " Johannes Schindelin via GitGitGadget
2024-12-13 15:02     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1825.v2.git.1731350009491.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=j6t@kdbg.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=newren@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).