line-log: integrate -L output with the standard log-tree pipeline

`git log -L` has bypassed log_tree_diff() and log_tree_diff_flush() since the feature was introduced, short-circuiting from log_tree_commit() directly into line_log_print(). This skips the no_free save/restore (noted in a NEEDSWORK comment added by f8781bfda3), the always_show_header fallback, show_diff_of_diff(), and diff_free() cleanup. Restructure so that -L flows through log_tree_diff() -> log_tree_diff_flush(), the same path used by the normal single-parent and merge diff codepaths: - Rename line_log_print() to line_log_queue_pairs() and strip it down to just queuing pre-computed filepairs. The show_log(), separator, diffcore_std(), and diff_flush() calls are removed since log_tree_diff_flush() handles all of those. - In log_tree_diff(), call line_log_queue_pairs() then log_tree_diff_flush(), mirroring the diff_tree_oid() + flush pattern used by the single-parent and merge codepaths. - Remove the early return in log_tree_commit() that is no longer needed now that -L output flows through log_tree_diff() and log_tree_diff_flush(); this restores no_free save/restore, always_show_header, and diff_free() cleanup. Because show_log() is now deferred until after diffcore_std() inside log_tree_diff_flush(), pickaxe (-S, -G, --find-object) and --diff-filter now properly suppress commits when all pairs are filtered out. The blank-line separator between commit header and diff changes slightly: the old code printed one unconditionally, while log_tree_diff_flush() only emits one for verbose headers. This matches the rest of log output. Also reject --full-diff, which is not yet supported with -L: the filepairs are pre-computed during the history walk and scoped to tracked line ranges, so there is currently no full-tree diff to fall back to for display. Update tests accordingly. Signed-off-by: Michael Montalbo <mmontalbo@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Montalbo committed May 28, 2026 at 20:47 UTC 42d960748efa79a31e72cc36d983aca244dc167e
7 files changed +60 -43
line-log.c
+9 -21
@@ -13,7 +13,6 @@
13 #include "revision.h"
14 #include "xdiff-interface.h"
15 #include "strbuf.h"
16 -#include "log-tree.h"
16 #include "line-log.h"
17 #include "setup.h"
18 #include "strvec.h"
@@ -1004,29 +1003,18 @@ static int process_all_files(struct line_log_data **range_out,
1003 return changed;
1004 }
1005
1007 -int line_log_print(struct rev_info *rev, struct commit *commit)
1006 +void line_log_queue_pairs(struct rev_info *rev, struct commit *commit)
1007 {
1009 - show_log(rev);
1010 - if (!(rev->diffopt.output_format & DIFF_FORMAT_NO_OUTPUT)) {
1011 - struct line_log_data *range = lookup_line_range(rev, commit);
1012 - struct line_log_data *r;
1013 - const char *prefix = diff_line_prefix(&rev->diffopt);
1014 -
1015 - fprintf(rev->diffopt.file, "%s\n", prefix);
1016 -
1017 - for (r = range; r; r = r->next) {
1018 - if (r->pair) {
1019 - struct diff_filepair *p =
1020 - diff_filepair_dup(r->pair);
1021 - p->line_ranges = &r->ranges;
1022 - diff_q(&diff_queued_diff, p);
1023 - }
1024 - }
1008 + struct line_log_data *range = lookup_line_range(rev, commit);
1009 + struct line_log_data *r;
1010
1026 - diffcore_std(&rev->diffopt);
1027 - diff_flush(&rev->diffopt);
1011 + for (r = range; r; r = r->next) {
1012 + if (r->pair) {
1013 + struct diff_filepair *p = diff_filepair_dup(r->pair);
1014 + p->line_ranges = &r->ranges;
1015 + diff_q(&diff_queued_diff, p);
1016 + }
1017 }
1029 - return 1;
1018 }
1019
1020 static int bloom_filter_check(struct rev_info *rev,
line-log.h
+1 -1
@@ -46,7 +46,7 @@ int line_log_filter(struct rev_info *rev);
46 int line_log_process_ranges_arbitrary_commit(struct rev_info *rev,
47 struct commit *commit);
48
49 -int line_log_print(struct rev_info *rev, struct commit *commit);
49 +void line_log_queue_pairs(struct rev_info *rev, struct commit *commit);
50
51 void line_log_free(struct rev_info *rev);
52
log-tree.c
+6 -4
@@ -1105,6 +1105,12 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
1105 if (!all_need_diff && !opt->merges_need_diff)
1106 return 0;
1107
1108 + if (opt->line_level_traverse) {
1109 + line_log_queue_pairs(opt, commit);
1110 + log_tree_diff_flush(opt);
1111 + return !opt->loginfo;
1112 + }
1113 +
1114 parse_commit_or_die(commit);
1115 oid = get_commit_tree_oid(commit);
1116
@@ -1179,10 +1185,6 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
1185 opt->loginfo = &log;
1186 opt->diffopt.no_free = 1;
1187
1182 - /* NEEDSWORK: no restoring of no_free? Why? */
1183 - if (opt->line_level_traverse)
1184 - return line_log_print(opt, commit);
1185 -
1188 if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
1189 fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
1190 shown = log_tree_diff(opt, commit, &log);
revision.c
+4 -2
@@ -3179,8 +3179,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
3179 die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
3180
3181 if (revs->line_level_traverse &&
3182 - (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
3183 - die(_("-L does not yet support diff formats besides -p and -s"));
3182 + (revs->full_diff ||
3183 + (revs->diffopt.output_format &
3184 + ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT))))
3185 + die(_("-L does not yet support the requested diff format"));
3186
3187 if (revs->expand_tabs_in_log < 0)
3188 revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
t/t4211-line-log.sh
+40 -13
@@ -368,7 +368,6 @@ test_expect_success '-L diff output includes index and new file mode' '
368
369 test_expect_success '-L with --word-diff' '
370 cat >expect <<-\EOF &&
371 -
371 diff --git a/file.c b/file.c
372 --- a/file.c
373 +++ b/file.c
@@ -377,7 +376,6 @@ test_expect_success '-L with --word-diff' '
376 {
377 return [-F2;-]{+F2 + 2;+}
378 }
380 -
379 diff --git a/file.c b/file.c
380 new file mode 100644
381 --- /dev/null
@@ -433,7 +431,6 @@ test_expect_success 'show line-log with graph' '
431 null_blob=$(test_oid zero | cut -c1-7) &&
432 qz_to_tab_space >expect <<-EOF &&
433 * $head_oid Modify func2() in file.c
436 - |Z
434 | diff --git a/file.c b/file.c
435 | index $head_blob_old..$head_blob_new 100644
436 | --- a/file.c
@@ -445,7 +442,6 @@ test_expect_success 'show line-log with graph' '
442 | + return F2 + 2;
443 | }
444 * $root_oid Add func1() and func2() in file.c
448 - ZZ
445 diff --git a/file.c b/file.c
446 new file mode 100644
447 index $null_blob..$root_blob
@@ -494,23 +490,17 @@ test_expect_success '-L --find-object does not crash with merge and rename' '
490 --find-object=$(git rev-parse HEAD:file) >actual
491 '
492
497 -# Commit-level filtering with pickaxe does not yet work for -L.
498 -# show_log() prints the commit header before diffcore_std() runs
499 -# pickaxe, so commits cannot be suppressed even when no diff pairs
500 -# survive filtering. Fixing this would require deferring show_log()
501 -# until after diffcore_std(), which is a larger restructuring of the
502 -# log-tree output pipeline.
503 -test_expect_failure '-L -G should filter commits by pattern' '
493 +test_expect_success '-L -G should filter commits by pattern' '
494 git log --format="%s" --no-patch -L 1,1:file -G "nomatch" >actual &&
495 test_must_be_empty actual
496 '
497
508 -test_expect_failure '-L -S should filter commits by pattern' '
498 +test_expect_success '-L -S should filter commits by pattern' '
499 git log --format="%s" --no-patch -L 1,1:file -S "nomatch" >actual &&
500 test_must_be_empty actual
501 '
502
513 -test_expect_failure '-L --find-object should filter commits by object' '
503 +test_expect_success '-L --find-object should filter commits by object' '
504 git log --format="%s" --no-patch -L 1,1:file \
505 --find-object=$ZERO_OID >actual &&
506 test_must_be_empty actual
@@ -711,4 +701,41 @@ test_expect_success '-L with -G filters to diff-text matches' '
701 grep "F2 + 2" actual
702 '
703
704 +test_expect_success '-L with --diff-filter=M excludes root commit' '
705 + git checkout parent-oids &&
706 + git log -L:func2:file.c --diff-filter=M --format=%s --no-patch >actual &&
707 + # Root commit is an Add (A), not a Modify (M), so it should
708 + # be excluded; only the modification commit remains.
709 + echo "Modify func2() in file.c" >expect &&
710 + test_cmp expect actual
711 +'
712 +
713 +test_expect_success '-L with --diff-filter=A shows only root commit' '
714 + git checkout parent-oids &&
715 + git log -L:func2:file.c --diff-filter=A --format=%s --no-patch >actual &&
716 + echo "Add func1() and func2() in file.c" >expect &&
717 + test_cmp expect actual
718 +'
719 +
720 +test_expect_success '-L with -S suppresses non-matching commits' '
721 + git checkout parent-oids &&
722 + git log -L:func2:file.c -S "F2 + 2" --format=%s --no-patch >actual &&
723 + # Only the commit that changes the count of "F2 + 2" should appear.
724 + echo "Modify func2() in file.c" >expect &&
725 + test_cmp expect actual
726 +'
727 +
728 +test_expect_success '--full-diff is not yet supported with -L' '
729 + test_must_fail git log -L1,24:b.c --full-diff 2>err &&
730 + test_grep "does not yet support" err
731 +'
732 +
733 +test_expect_success '-L --oneline has no extra blank line before diff' '
734 + git checkout parent-oids &&
735 + git log --oneline -L:func2:file.c -1 >actual &&
736 + # Oneline header on line 1, diff starts immediately on line 2
737 + sed -n 2p actual >line2 &&
738 + test_grep "^diff --git" line2
739 +'
740 +
741 test_done
t/t4211/sha1/expect.parallel-change-f-to-main
-1
@@ -5,7 +5,6 @@ Date: Fri Apr 12 16:16:24 2013 +0200
5
6 Merge across the rename
7
8 -
8 commit 6ce3c4ff690136099bb17e1a8766b75764726ea7
9 Author: Thomas Rast <trast@student.ethz.ch>
10 Date: Thu Feb 28 10:49:50 2013 +0100
t/t4211/sha256/expect.parallel-change-f-to-main
-1
@@ -5,7 +5,6 @@ Date: Fri Apr 12 16:16:24 2013 +0200
5
6 Merge across the rename
7
8 -
8 commit 4f7a58195a92c400e28a2354328587f1ff14fb77f5cf894536f17ccbc72931b9
9 Author: Thomas Rast <trast@student.ethz.ch>
10 Date: Thu Feb 28 10:49:50 2013 +0100