Skip to content

End hunks once the line counts in their header are used up - #74

Open
gnutix wants to merge 1 commit into
banga:mainfrom
gnutix:end-hunks-by-line-count
Open

gnutix wants to merge 1 commit into
banga:mainfrom
gnutix:end-hunks-by-line-count

Conversation

@gnutix

@gnutix gnutix commented Nov 7, 2026 •

Copy link
Copy Markdown

Hunks currently end only when a line starts another state, such as a commit header. That breaks in two ways:

  • Custom --pretty formats. With e.g. format.pretty = "Commit: %H%n…", git log -p never prints a line starting with commit , so every commit after the first is drawn as lines of the previous hunk.
  • Phantom lines. The blank line git prints between commits (and the input's trailing newline) is drawn as an extra numbered line at the end of each hunk.

This tracks the line counts from each hunk header (@@ -a,n +b,m @@, and the N+1 ranges of combined @@@ headers) and ends the hunk once they are used up. A trailing \ No newline at end of file still belongs to the hunk. The separator before a commit header is kept when the previous hunk ended this way.

Tests

  • New case with two commits in a custom pretty format, covering the no-newline marker too.
  • Existing snapshots change only by dropping the phantom numbered line at the end of hunks, including in merge-commit cases.

Before, the second commit's header rendered as hunk lines:

    2  - second                       2  + changed second
    2  C ommit:  1111111…             3  C ommit:  1111111…
    3  S ubject: First commit         4  S ubject: First commit

After:

    2  - second                       2  + changed second
Commit:  1111111…
Subject: First commit

🤖 Generated with Claude Code Opus 5.5

Hunks used to end only when a line started another state, such as a
`commit ` header. Output from `git log -p` with a custom --pretty format
(e.g. one starting with "Commit:") never produced such a line, so every
following commit was drawn as lines of the previous hunk. The blank line
git prints between commits was also drawn as a phantom numbered line at
the end of each hunk.

Now each hunk tracks the line counts from its header (also for combined
diffs) and ends when they reach zero, letting a trailing "\ No newline
at end of file" through.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017FPXTedgEC3D3TAaGpDdNe
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