Skip to content

line-log: scope stat, check, and -G to -L line ranges - #2152

Open
mmontalbo wants to merge 7 commits into
gitgitgadget:masterfrom
mmontalbo:mm/line-log-stat-formats-followup
Open

line-log: scope stat, check, and -G to -L line ranges#2152
mmontalbo wants to merge 7 commits into
gitgitgadget:masterfrom
mmontalbo:mm/line-log-stat-formats-followup

Conversation

@mmontalbo

@mmontalbo mmontalbo commented Jun 15, 2026

Copy link
Copy Markdown

Teach -L to work with the --stat family of options and --check. Also,
teach -G to search within the line ranges specified by -L. This series
continues the previous work[1][2] extending -L to work with more diff
options, and, for example, enables:

Reviewing the --stat history of a single function:

$ git log --oneline -L:strbuf_grow:strbuf.c --stat
c13d0f7bd4 strbuf: use st_add3() in strbuf_grow()
 strbuf.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
...
b449f4cfc9 Rework strbuf API and semantics.
 strbuf.c | 6 ++++++
 1 file changed, 6 insertions(+)

Finding where a pattern changed inside one function with -G, while
ignoring matches elsewhere in the file:

$ git log --oneline -L:run_builtin:git.c -G RUN_SETUP
e5b17bda8b git: ensure correct git directory setup with -h
27bd38d4e5 git.c: treat RUN_SETUP_GENTLY and RUN_SETUP as mutually exclusive
...
47d0b4ff57 Clean up internal command handling

The first 4 commits are preparatory cleanup for the main feature
changes and could potentially be separated. The last 3 commits
implement the new features.

Patches:

  1. Rename line_range_callback to line_range_filter to more accurately
    reflect its role as a general filter for other diff features. Also,
    replace verbose comments with descriptive field and variable names.
  2. Simplify line-range filtering logic, which fixes a bug as a result.
  3. Make -L diff hunk header format consistent with default mode's
    header format for all cases by exposing xdiff's header generating
    function.
  4. Extract a line-range diff helper so the --stat, --check, and -G support
    can reuse it.
  5. Enable support for --stat family of options.
  6. Enable support for --check.
  7. Teach -G to search within -L line ranges.

Changes since v2:

  • Rewrite all prose in series from scratch to remove significant
    verbosity and extraneous detail that makes the series hard to reason
    about. Replace block comments with descriptive field and variable
    names.
  • Pare down and simplify some test implementations.
  • No other logical behavior changes.

[1] https://lore.kernel.org/git/pull.2094.v3.git.1780001267.gitgitgadget@gmail.com/
[2] https://lore.kernel.org/git/pull.2099.git.1777230630020.gitgitgadget@gmail.com/

Cc: "D. Ben Knoble" ben.knoble@gmail.com

@mmontalbo
mmontalbo force-pushed the mm/line-log-stat-formats-followup branch 8 times, most recently from 7977925 to 2ea00b6 Compare June 16, 2026 21:41
@mmontalbo mmontalbo changed the title line-log: support stat and check diff formats with -L line-log: range-scope stat, check, and -G under -L Jun 16, 2026
@mmontalbo
mmontalbo force-pushed the mm/line-log-stat-formats-followup branch 17 times, most recently from 2bc09ca to f69ccfb Compare June 18, 2026 16:29
@mmontalbo

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Jun 18, 2026

Copy link
Copy Markdown

Preview email sent as pull.2152.git.1781800932.gitgitgadget@gmail.com

@mmontalbo

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Jun 18, 2026

Copy link
Copy Markdown

Submitted as pull.2152.git.1781806593.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v1

To fetch this version to local tag pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v1:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v1

@gitgitgadget

gitgitgadget Bot commented Aug 17, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 18, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 21, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 24, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 26, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 29, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 29, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@gitgitgadget

gitgitgadget Bot commented Aug 31, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

@mmontalbo
mmontalbo force-pushed the mm/line-log-stat-formats-followup branch from 43bc512 to ee53c92 Compare September 2, 2026 21:07
@gitgitgadget

gitgitgadget Bot commented Sep 2, 2026

Copy link
Copy Markdown

There is an issue in commit 72a86b9:
diff: support --check with -L line ranges

  • Lines in the body of the commit messages should be wrapped between 60 and 76 characters.
    Indented lines, and lines without whitespace, are exempt

@mmontalbo
mmontalbo force-pushed the mm/line-log-stat-formats-followup branch 7 times, most recently from 8c40fed to 2f123c5 Compare September 3, 2026 03:06
@gitgitgadget

gitgitgadget Bot commented Sep 3, 2026

Copy link
Copy Markdown

There was a status update in the "Cooking" section about the branch mm/line-log-limited-ops on the Git mailing list:

The 'git log -L<range>:<path>' command has been taught to limit
various 'diff' operations, such as '--stat', '--check', and '-G', to
the specified range and path.

Needs review.
source: <pull.2152.v2.git.1782581342.gitgitgadget@gmail.com>

diff's line-range filtering logic uses the line_range_callback
struct to represent filtering state. However, this name does not
clearly reflect the role it plays. This is especially relevant as
we expand diff's line-range filtering to work with more options,
including --stat and -G.

Also, line_range_callback's fields are terse, while the comment
explaining line_range_callback is verbose and out of place compared to
its surroundings.

Rename line_range_callback to line_range_filter, and replace the verbose
comment with a concise one, instead preferring descriptive field and
variable names that are self-explanatory over comments.

No logical behavior change. Some fields are grouped under a new struct
in the newly renamed line_range_filter. Everything else is just a
rename.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Currently, the diff line-range filter buffers preimage removal lines
until a postimage line arrives. That line's number confirms whether the
preimage line falls in a relevant range. However, storing preimage
lines in a separate buffer is unnecessary. Worse, the logic has a bug:
a preimage line outside the target range is included when it
immediately follows an in-range postimage line.

Preimage lines will always precede their postimage counterpart both in
content line number and emission order from xdiff's line callback
function. So preimage lines can share the postimage buffer. The filter
flushes them based on whether the postimage lines fall within the
target range.

Remove logic related to storing preimage lines in a separate "removal"
buffer and prepending them to the accumulating_hunk's line buffer.
Instead, store those lines in the accumulating_hunk's line_buffer
immediately and flush everything as appropriate based on postimage
line numbers that arrive. This resolves the bug by construction.

Also, calculate the old and new line counts for the diff hunk header
when flushing rather than storing counters in line_range_filter to
simplify state management further.

Add a test to t/t4211-line-log.sh that verifies the preimage line
emission bug is fixed.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Currently, diff's line-range filter implements its own method for
emitting diff hunk headers. This mostly matches what xdiff itself
outputs, but there is a discrepancy for postimage or preimage sides
with 0 line changes. For a side with no lines (count 0), the begin is
the line before the change. The header omits the line count of 1.

Rather than fix this case in the line-range implementation, expose the
function xdiff uses to emit its headers. Reusing it keeps the header
format consistent with and without -L.

Update test scenarios and fixtures to reflect the now consistent header
format.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Extract logic for initializing the line-range filter and running a diff
for a specific line range. This logic is needed for any diff that
targets a line range independent of the current patch display path.

The subsequent commits use this logic to enable additional line range
targeted diff modes.

No logical behavior change.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Reuse the line_range_filter in builtin_diffstat() so -L supports
the stat formats and add tests verifying the new behavior.

Ungate the newly enabled options and drop "yet" from the generic
-L rejection message ("does not yet support the requested diff
format"). Some rejected formats do not fit -L at all, so "yet"
wrongly implies they are all awaiting support.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Reuse the line_range_filter in builtin_checkdiff() so -L supports
the --check option.

Add orig_hunk_fn field similar to orig_line_fn that forwards
xdiff_emit_hunk_fn calls when we flush filtered hunks. This is necessary
because --check relies on receiving calls to its checkdiff_consume_hunk
function for managing state.

Document and ungate the newly enabled option, and add tests verifying
the new behavior.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Teach -G to only search the line ranges specified by -L.
Teaching -S is left as future work, so it still matches the entire
file even if -L is specified.

Rather than being part of diff.c's builtin implementations, the
diffcore-pickaxe functionality interacts with xdiff-interface as a
separate component. Add a sibling to xdi_diff_outf(), called
diff_emit_line_ranges(), that limits emitted lines to the given line
ranges.

Use diff_emit_line_ranges() when searching text if line ranges have
been specified. If textconv is enabled, use normal diffing instead of
diff_emit_line_ranges() since line range tracking relies on the line
coordinates of the original, pre-textconv file.

Update documentation and add tests accordingly.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
@mmontalbo
mmontalbo force-pushed the mm/line-log-stat-formats-followup branch from 2f123c5 to cff5c12 Compare September 3, 2026 04:47
@mmontalbo

Copy link
Copy Markdown
Author

/preview

@gitgitgadget

gitgitgadget Bot commented Sep 3, 2026

Copy link
Copy Markdown

Preview email sent as pull.2152.v3.git.1788411011.gitgitgadget@gmail.com

@mmontalbo

Copy link
Copy Markdown
Author

/submit

@gitgitgadget

gitgitgadget Bot commented Sep 3, 2026

Copy link
Copy Markdown

Submitted as pull.2152.v3.git.1788411919.gitgitgadget@gmail.com

To fetch this version into FETCH_HEAD:

git fetch https://github.com/gitgitgadget/git/ pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v3

To fetch this version to local tag pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v3:

git fetch --no-tags https://github.com/gitgitgadget/git/ tag pr-2152/mmontalbo/mm/line-log-stat-formats-followup-v3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant