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

[PATCH v3 3/4] log: simplify --follow following renames for non-linear history

From
Miklos Vajna <vmiklos@collabora.com>
Date
Oct 22, 2026, 20:46 UTC
Message-ID
<ba2dc35c77b95803f184e63cc9933e3765bd1417.1792701579.git.vmiklos@collabora.com>
In-Reply-To
<cover.1792701579.git.vmiklos@collabora.com>

Code added in 304812ed33 (log: improve --follow following renames for non-linear history, 2026-06-22) implemented the feature by introducing a new propagate_follow_pathspec_to_parent() function which adds a new call to diff_tree_oid(). Also it introduces a new loop to record what path each parent of a merge commit should use when following a path.

This was working, but fixing its performance would require adding even more custom code, instead of just extending the existing code in log_tree_diff().

Remove propagate_follow_pathspec_to_parent() and the loop calling it for each parent. Instead, record the path to follow for each parent in the parent loop of log_tree_diff(), right after diff_tree_oid() updated the pathspec in case of a rename. Merges are not diffed by default, so with --follow, run the same loop for them as well, but without showing anything. With -c, --cc or --remerge-diff, show the merge diff after the loop. This gets rid of the additional call to diff_tree_oid(). The rework has no positive performance impact yet, it just simplifies the code.

This also fixes 'git log -m --follow': -m diffs the merge against each parent with rename following, so a rename found against the first parent updated the followed path, and the second parent was then diffed with the already renamed path. Also, propagate_follow_pathspec_to_parent() started from that same updated path, so history was lost on the side of the second parent. Now the path of the merge is restored before diffing each parent. Add a test for this, which shows the full history of the diamond case with -m.

Signed-off-by: Miklos Vajna <vmiklos@collabora.com>
---
 log-tree.c                  | 84 +++++++++++++++++--------------------
 t/t4219-log-follow-merge.sh | 16 +++++++
 2 files changed, 54 insertions(+), 46 deletions(-)
diff --git a/log-tree.c b/log-tree.c
index b4c97b86f5..c5ac6c2bcb 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -1153,33 +1153,6 @@ void release_follow_pathspec_slab(struct rev_info *opt)
 	FREE_AND_NULL(opt->follow_pathspec_slab);
 }
 
-/* Compute a path to follow in parent, if there is one */
-static void propagate_follow_pathspec_to_parent(struct rev_info *opt,
-						struct commit *commit,
-						struct commit *parent)
-{
-	struct diff_options diff_opts;
-	const char *path;
-
-	parse_commit_or_die(parent);
-	repo_diff_setup(opt->diffopt.repo, &diff_opts);
-	copy_pathspec(&diff_opts.pathspec, &opt->diffopt.pathspec);
-	diff_opts.flags.recursive = 1;
-	diff_opts.flags.follow_renames = 1;
-	diff_opts.output_format = DIFF_FORMAT_NO_OUTPUT;
-	diff_setup_done(&diff_opts);
-	diff_tree_oid(get_commit_tree_oid(parent),
-		      get_commit_tree_oid(commit),
-		      "", &diff_opts);
-
-	path = pathspec_single_path(&diff_opts.pathspec);
-	if (path)
-		remember_follow_pathspec(opt, parent, path);
-
-	diff_queue_clear(&diff_queued_diff);
-	diff_free(&diff_opts);
-}
-
 /* Show the remerge or combined diff of a merge commit */
 static int log_tree_merge_diff(struct rev_info *opt, struct commit *commit,
 			       struct commit_list *parents,
@@ -1208,10 +1181,12 @@ static int log_tree_merge_diff(struct rev_info *opt, struct commit *commit,
 static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log_info *log)
 {
 	int showed_log;
-	struct commit_list *parents;
+	struct commit_list *parents, *all_parents;
 	struct object_id *oid;
 	int is_merge;
 	int all_need_diff = opt->diff || opt->diffopt.flags.exit_with_status;
+	int follow_only = 0;
+	char *path = NULL;
 
 	if (!all_need_diff && !opt->merges_need_diff)
 		return 0;
@@ -1240,35 +1215,67 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
 	}
 
 	if (is_merge) {
-		if (opt->remerge_diff || opt->combine_merges)
+		int merge_diff = opt->remerge_diff || opt->combine_merges;
+
+		if (merge_diff && !opt->diffopt.flags.follow_renames)
 			return log_tree_merge_diff(opt, commit, parents, oid);
-		if (opt->separate_merges) {
+		if (opt->separate_merges && !merge_diff) {
 			if (!opt->first_parent_merges) {
 				/* Show parent info for multiple diffs */
 				log->parent = parents->item;
 			}
+		} else if (opt->diffopt.flags.follow_renames) {
+			/* Diff only to find the path to follow in parents */
+			follow_only = 1;
 		} else
 			return 0;
 	}
 
+	if (opt->diffopt.flags.follow_renames)
+		path = xstrdup_or_null(
+			pathspec_single_path(&opt->diffopt.pathspec));
+
 	showed_log = 0;
+	all_parents = parents;
 	for (;;) {
 		struct commit *parent = parents->item;
 
 		parse_commit_or_die(parent);
 		diff_tree_oid(get_commit_tree_oid(parent),
 			      oid, "", &opt->diffopt);
-		log_tree_diff_flush(opt);
+		if (follow_only) {
+			diff_queue_clear(&diff_queued_diff);
+			opt->diffopt.found_follow = 0;
+		} else
+			log_tree_diff_flush(opt);
 
 		showed_log |= !opt->loginfo;
 
+		/* The diff updated the pathspec in case of a rename */
+		if (path)
+			remember_follow_pathspec(opt, parent,
+				pathspec_single_path(&opt->diffopt.pathspec));
+
 		/* Set up the log info for the next parent, if any.. */
 		parents = parents->next;
 		if (!parents || opt->first_parent_merges)
 			break;
+		if (path)
+			set_pathspec_to_single_path(&opt->diffopt.pathspec,
+						    path);
+		if (follow_only)
+			continue;
 		log->parent = parents->item;
 		opt->loginfo = log;
 	}
+	if (follow_only && (opt->remerge_diff || opt->combine_merges)) {
+		/* Show the merge diff with the path of this commit */
+		if (path)
+			set_pathspec_to_single_path(&opt->diffopt.pathspec,
+						    path);
+		showed_log = log_tree_merge_diff(opt, commit, all_parents, oid);
+	}
+	free(path);
 	return showed_log;
 }
 
@@ -1306,21 +1313,6 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
 		fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
 	if (shown)
 		show_diff_of_diff(opt);
-
-	/* Record what path each parent of this commit should use */
-	if (opt->diffopt.flags.follow_renames) {
-		struct commit_list *parents = get_saved_parents(opt, commit);
-		if (parents && parents->next) {
-			struct commit_list *p;
-			for (p = parents; p; p = p->next)
-				propagate_follow_pathspec_to_parent(opt, commit,
-								    p->item);
-		} else if (parents) {
-			remember_follow_pathspec(opt, parents->item,
-				pathspec_single_path(&opt->diffopt.pathspec));
-		}
-	}
-
 	opt->loginfo = NULL;
 	maybe_flush_or_die(opt->diffopt.file, "stdout");
 	opt->diffopt.no_free = no_free;
diff --git a/t/t4219-log-follow-merge.sh b/t/t4219-log-follow-merge.sh
index 9616ac64ed..c8474ed151 100755
--- a/t/t4219-log-follow-merge.sh
+++ b/t/t4219-log-follow-merge.sh
@@ -144,6 +144,22 @@ do
 	'
 done
 
+test_expect_success '--follow follows renames through a fork with -m' '
+	git -C diamond log --follow -m --pretty=tformat:%s path >log &&
+	grep "^[A-Z]: " log | sort >actual &&
+	cat >expect <<-\EOF &&
+	A: add path0
+	B: modify path0 on upper
+	C: modify path0 on lower
+	M: merge with rename to path
+	M: merge with rename to path
+	X: rename path0 to path1
+	Y: rename path0 to path2
+	Z: modify path
+	EOF
+	test_cmp expect actual
+'
+
 # Build a merge that takes the renamed path from the second parent, while the
 # first parent only has the old path. $2 decides which side is committed first,
 # which decides the walk order.
-- 
2.56.0
Previous: Miklos VajnaNext: Patrick Steinhardt
Message 9 of 48 in “Performance regression in `git log --follow`”
  1. Patrick SteinhardtOct 13, 2026
  2. SZEDER GáborOct 13, 2026
  3. log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 15, 2026
  4. Junio C HamanoOct 15, 2026
  5. log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 19, 2026
  6. 0/4 log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 22, 2026
  7. 1/4 t4219: test more --follow cases across mergesMiklos Vajna, Oct 22, 2026
  8. 2/4 log: extract showing the remerge/combined diff of a merge commitMiklos Vajna, Oct 22, 2026
  9. 3/4 log: simplify --follow following renames for non-linear historyMiklos Vajna, Oct 22, 2026
  10. Patrick SteinhardtOct 26, 2026
  11. 4/4 log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 22, 2026
  12. Patrick SteinhardtOct 26, 2026
  13. Patrick SteinhardtOct 26, 2026
  14. 0/6 log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 30, 2026
  15. 1/6 t4219: test more --follow cases across mergesMiklos Vajna, Oct 30, 2026
  16. 2/6 log: extract showing the remerge/combined diff of a merge commitMiklos Vajna, Oct 30, 2026
  17. 3/6 log: fix -m --follow losing the history of later parentsMiklos Vajna, Oct 30, 2026
  18. 4/6 log: simplify --follow following renames for non-linear historyMiklos Vajna, Oct 30, 2026
  19. 5/6 log: speed up --follow following renames for non-linear historyMiklos Vajna, Oct 30, 2026
  20. Patrick SteinhardtNov 6, 2026
  21. Miklos VajnaNov 9, 2026
  22. 6/6 log: fix --follow following an edit on a side the merge did not takeMiklos Vajna, Oct 30, 2026
  23. Patrick SteinhardtNov 6, 2026
  24. Miklos VajnaNov 9, 2026
  25. Patrick SteinhardtNov 6, 2026
  26. Miklos VajnaNov 9, 2026
  27. 0/5 log: speed up --follow following renames for non-linear historyMiklos Vajna, Nov 9, 2026
  28. 1/5 t4219: test more --follow cases across mergesMiklos Vajna, Nov 9, 2026
  29. Justin ToblerNov 10, 2026
  30. 2/5 log: extract showing the remerge/combined diff of a merge commitMiklos Vajna, Nov 9, 2026
  31. 3/5 log: fix -m --follow losing the history of later parentsMiklos Vajna, Nov 9, 2026
  32. 4/5 log: simplify --follow following renames for non-linear historyMiklos Vajna, Nov 9, 2026
  33. Toon ClaesNov 10, 2026
  34. 5/5 log: speed up --follow following renames for non-linear historyMiklos Vajna, Nov 9, 2026
  35. Justin ToblerNov 11, 2026
  36. Patrick SteinhardtNov 11, 2026
  37. Miklos VajnaNov 11, 2026
  38. Patrick SteinhardtNov 11, 2026
  39. log: make --follow following renames for non-linear history optionalMiklos Vajna, Nov 12, 2026
  40. Junio C HamanoNov 12, 2026
  41. Patrick SteinhardtNov 13, 2026
  42. log: make --follow following renames for non-linear history optionalMiklos Vajna, Nov 16, 2026
  43. Patrick SteinhardtNov 16, 2026
  44. Junio C HamanoNov 16, 2026
  45. Miklos VajnaNov 17, 2026
  46. Junio C HamanoNov 11, 2026
  47. Junio C HamanoOct 16, 2026
  48. Miklos VajnaOct 14, 2026

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.