]> git.ipfire.org Git - thirdparty/git.git/commitdiff
line-log: integrate -L output with the standard log-tree pipeline
authorMichael Montalbo <mmontalbo@gmail.com>
Mon, 25 May 2026 19:40:57 +0000 (19:40 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 25 May 2026 22:57:50 +0000 (07:57 +0900)
`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>
line-log.c
line-log.h
log-tree.c
revision.c
t/t4211-line-log.sh
t/t4211/sha1/expect.parallel-change-f-to-main
t/t4211/sha256/expect.parallel-change-f-to-main

index 858a899cd2a61db85532196ffcdf3b71336051eb..7ee55b05cc50771aaee169873d885bd1f1711142 100644 (file)
@@ -13,7 +13,6 @@
 #include "revision.h"
 #include "xdiff-interface.h"
 #include "strbuf.h"
-#include "log-tree.h"
 #include "line-log.h"
 #include "setup.h"
 #include "strvec.h"
@@ -1004,29 +1003,18 @@ static int process_all_files(struct line_log_data **range_out,
        return changed;
 }
 
-int line_log_print(struct rev_info *rev, struct commit *commit)
+void line_log_queue_pairs(struct rev_info *rev, struct commit *commit)
 {
-       show_log(rev);
-       if (!(rev->diffopt.output_format & DIFF_FORMAT_NO_OUTPUT)) {
-               struct line_log_data *range = lookup_line_range(rev, commit);
-               struct line_log_data *r;
-               const char *prefix = diff_line_prefix(&rev->diffopt);
-
-               fprintf(rev->diffopt.file, "%s\n", prefix);
-
-               for (r = range; r; r = r->next) {
-                       if (r->pair) {
-                               struct diff_filepair *p =
-                                       diff_filepair_dup(r->pair);
-                               p->line_ranges = &r->ranges;
-                               diff_q(&diff_queued_diff, p);
-                       }
-               }
+       struct line_log_data *range = lookup_line_range(rev, commit);
+       struct line_log_data *r;
 
-               diffcore_std(&rev->diffopt);
-               diff_flush(&rev->diffopt);
+       for (r = range; r; r = r->next) {
+               if (r->pair) {
+                       struct diff_filepair *p = diff_filepair_dup(r->pair);
+                       p->line_ranges = &r->ranges;
+                       diff_q(&diff_queued_diff, p);
+               }
        }
-       return 1;
 }
 
 static int bloom_filter_check(struct rev_info *rev,
index 04a6ea64d3d68f2725a4029d76188fd007839b70..99e1755ce3d568adbe9b94b42ca86a6e34f9a87a 100644 (file)
@@ -46,7 +46,7 @@ int line_log_filter(struct rev_info *rev);
 int line_log_process_ranges_arbitrary_commit(struct rev_info *rev,
                                                    struct commit *commit);
 
-int line_log_print(struct rev_info *rev, struct commit *commit);
+void line_log_queue_pairs(struct rev_info *rev, struct commit *commit);
 
 void line_log_free(struct rev_info *rev);
 
index 7e048701d0c5b47cb30fbea159b48bb785dd3b96..88b3019293b7258a0ea59cd6f5e9418e2e7049ff 100644 (file)
@@ -1105,6 +1105,12 @@ static int log_tree_diff(struct rev_info *opt, struct commit *commit, struct log
        if (!all_need_diff && !opt->merges_need_diff)
                return 0;
 
+       if (opt->line_level_traverse) {
+               line_log_queue_pairs(opt, commit);
+               log_tree_diff_flush(opt);
+               return !opt->loginfo;
+       }
+
        parse_commit_or_die(commit);
        oid = get_commit_tree_oid(commit);
 
@@ -1179,10 +1185,6 @@ int log_tree_commit(struct rev_info *opt, struct commit *commit)
        opt->loginfo = &log;
        opt->diffopt.no_free = 1;
 
-       /* NEEDSWORK: no restoring of no_free?  Why? */
-       if (opt->line_level_traverse)
-               return line_log_print(opt, commit);
-
        if (opt->track_linear && !opt->linear && !opt->reverse_output_stage)
                fprintf(opt->diffopt.file, "\n%s\n", opt->break_bar);
        shown = log_tree_diff(opt, commit, &log);
index 4a8e24bc38d572aa2b9e47baa0f25194341c3a29..c903f7a1b4c4c8fe80d3574a7829042b5e0d6f34 100644 (file)
@@ -3179,8 +3179,10 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
                die(_("the option '%s' requires '%s'"), "--grep-reflog", "--walk-reflogs");
 
        if (revs->line_level_traverse &&
-           (revs->diffopt.output_format & ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT)))
-               die(_("-L does not yet support diff formats besides -p and -s"));
+           (revs->full_diff ||
+            (revs->diffopt.output_format &
+             ~(DIFF_FORMAT_PATCH | DIFF_FORMAT_NO_OUTPUT))))
+               die(_("-L does not yet support the requested diff format"));
 
        if (revs->expand_tabs_in_log < 0)
                revs->expand_tabs_in_log = revs->expand_tabs_in_log_default;
index aaf197d2edc4d851943e5e1f03087542414905a8..e3937138a94055fbfec5d1ebf4cfa95c9d4543e5 100755 (executable)
@@ -368,7 +368,6 @@ test_expect_success '-L diff output includes index and new file mode' '
 
 test_expect_success '-L with --word-diff' '
        cat >expect <<-\EOF &&
-
        diff --git a/file.c b/file.c
        --- a/file.c
        +++ b/file.c
@@ -377,7 +376,6 @@ test_expect_success '-L with --word-diff' '
        {
            return [-F2;-]{+F2 + 2;+}
        }
-
        diff --git a/file.c b/file.c
        new file mode 100644
        --- /dev/null
@@ -433,7 +431,6 @@ test_expect_success 'show line-log with graph' '
        null_blob=$(test_oid zero | cut -c1-7) &&
        qz_to_tab_space >expect <<-EOF &&
        * $head_oid Modify func2() in file.c
-       |Z
        | diff --git a/file.c b/file.c
        | index $head_blob_old..$head_blob_new 100644
        | --- a/file.c
@@ -445,7 +442,6 @@ test_expect_success 'show line-log with graph' '
        | +    return F2 + 2;
        |  }
        * $root_oid Add func1() and func2() in file.c
-       ZZ
          diff --git a/file.c b/file.c
          new file mode 100644
          index $null_blob..$root_blob
@@ -494,23 +490,17 @@ test_expect_success '-L --find-object does not crash with merge and rename' '
                --find-object=$(git rev-parse HEAD:file) >actual
 '
 
-# Commit-level filtering with pickaxe does not yet work for -L.
-# show_log() prints the commit header before diffcore_std() runs
-# pickaxe, so commits cannot be suppressed even when no diff pairs
-# survive filtering.  Fixing this would require deferring show_log()
-# until after diffcore_std(), which is a larger restructuring of the
-# log-tree output pipeline.
-test_expect_failure '-L -G should filter commits by pattern' '
+test_expect_success '-L -G should filter commits by pattern' '
        git log --format="%s" --no-patch -L 1,1:file -G "nomatch" >actual &&
        test_must_be_empty actual
 '
 
-test_expect_failure '-L -S should filter commits by pattern' '
+test_expect_success '-L -S should filter commits by pattern' '
        git log --format="%s" --no-patch -L 1,1:file -S "nomatch" >actual &&
        test_must_be_empty actual
 '
 
-test_expect_failure '-L --find-object should filter commits by object' '
+test_expect_success '-L --find-object should filter commits by object' '
        git log --format="%s" --no-patch -L 1,1:file \
                --find-object=$ZERO_OID >actual &&
        test_must_be_empty actual
@@ -711,4 +701,41 @@ test_expect_success '-L with -G filters to diff-text matches' '
        grep "F2 + 2" actual
 '
 
+test_expect_success '-L with --diff-filter=M excludes root commit' '
+       git checkout parent-oids &&
+       git log -L:func2:file.c --diff-filter=M --format=%s --no-patch >actual &&
+       # Root commit is an Add (A), not a Modify (M), so it should
+       # be excluded; only the modification commit remains.
+       echo "Modify func2() in file.c" >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '-L with --diff-filter=A shows only root commit' '
+       git checkout parent-oids &&
+       git log -L:func2:file.c --diff-filter=A --format=%s --no-patch >actual &&
+       echo "Add func1() and func2() in file.c" >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '-L with -S suppresses non-matching commits' '
+       git checkout parent-oids &&
+       git log -L:func2:file.c -S "F2 + 2" --format=%s --no-patch >actual &&
+       # Only the commit that changes the count of "F2 + 2" should appear.
+       echo "Modify func2() in file.c" >expect &&
+       test_cmp expect actual
+'
+
+test_expect_success '--full-diff is not yet supported with -L' '
+       test_must_fail git log -L1,24:b.c --full-diff 2>err &&
+       test_grep "does not yet support" err
+'
+
+test_expect_success '-L --oneline has no extra blank line before diff' '
+       git checkout parent-oids &&
+       git log --oneline -L:func2:file.c -1 >actual &&
+       # Oneline header on line 1, diff starts immediately on line 2
+       sed -n 2p actual >line2 &&
+       test_grep "^diff --git" line2
+'
+
 test_done
index 65a8cc673a6fcaab590b0d591d7fb6d5aca71bfc..6d7a20103631ccb5f44bb8baa8429fa4569a6307 100644 (file)
@@ -5,7 +5,6 @@ Date:   Fri Apr 12 16:16:24 2013 +0200
 
     Merge across the rename
 
-
 commit 6ce3c4ff690136099bb17e1a8766b75764726ea7
 Author: Thomas Rast <trast@student.ethz.ch>
 Date:   Thu Feb 28 10:49:50 2013 +0100
index 3178989253a8852d07d2ef3cd09dae8dede2f2b3..c93e03bef40544529455271ea9b8dee8a3e4331d 100644 (file)
@@ -5,7 +5,6 @@ Date:   Fri Apr 12 16:16:24 2013 +0200
 
     Merge across the rename
 
-
 commit 4f7a58195a92c400e28a2354328587f1ff14fb77f5cf894536f17ccbc72931b9
 Author: Thomas Rast <trast@student.ethz.ch>
 Date:   Thu Feb 28 10:49:50 2013 +0100