Skip to content

diff: simplify line-range filter by classifying removals immediately#2099

Open
mmontalbo wants to merge 1 commit intogitgitgadget:masterfrom
mmontalbo:mm/line-log-immediate-classify
Open

diff: simplify line-range filter by classifying removals immediately#2099
mmontalbo wants to merge 1 commit intogitgitgadget:masterfrom
mmontalbo:mm/line-log-immediate-classify

Conversation

@mmontalbo
Copy link
Copy Markdown

@mmontalbo mmontalbo commented Apr 25, 2026

The line-range filter buffered '-' (removal) lines in a
pending_rm strbuf, deferring their classification until a '+'
or ' ' line arrived to reveal the post-image position.

This buffering is unnecessary. Removal lines are pre-image
content that occupies no post-image space, so they do not
advance lno_post. Within a hunk, xdiff emits removals before
additions for each change, so a '-' line always arrives while
lno_post is at the same position that the following '+' or ' '
line will occupy. Each line can therefore be classified against
the tracked ranges as it arrives, without waiting for a
non-removal line to confirm the position.

The buffering also had a bug: flush_rhunk() unconditionally
drained the pending buffer when the range hunk was active, even
if lno_post had advanced past the tracked range. This caused
deletions immediately after the tracked function to be
incorrectly included in patch output.

Remove the pending_rm buffer and classify '-' lines using the
same in_range check applied to '+' and ' ' lines. This
simplifies the filter, removes three struct fields and a helper
function, and makes the flush_rhunk() bug impossible by
construction.

The line-range filter buffered '-' (removal) lines in a pending_rm
strbuf, deferring their classification until a '+' or ' ' line
arrived to reveal the post-image position.

This buffering is unnecessary.  Removal lines are pre-image content
that occupies no post-image space, so they do not advance lno_post.
Within a hunk, xdiff emits removals before additions for each
change, so a '-' line always arrives while lno_post is at the same
position that the following '+' or ' ' line will occupy.  Each
line can therefore be classified against the tracked ranges as it
arrives, without waiting for a non-removal line to confirm the
position.

The buffering also had a bug: flush_rhunk() unconditionally drained
the pending buffer when the range hunk was active, even if lno_post
had advanced past the tracked range.  This caused deletions
immediately after the tracked function to be incorrectly included
in patch output.

Remove the pending_rm buffer and classify '-' lines using the same
in_range check applied to '+' and ' ' lines.  This simplifies the
filter, removes three struct fields and a helper function, and
makes the flush_rhunk() bug impossible by construction.

Signed-off-by: Michael Montalbo <mmontalbo@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant