Skip to content

Keep a hunk header's length as the diff wrote it - #155

Closed
abhishekcpp007 wants to merge 1 commit into
matiasb:mainfrom
abhishekcpp007:hunk-range-length-one
Closed

abhishekcpp007 wants to merge 1 commit into
matiasb:mainfrom
abhishekcpp007:hunk-range-length-one

Conversation

@abhishekcpp007

Copy link
Copy Markdown

A hunk whose length is one loses its header form on the way back out. diff and git
leave the length off in that case, and str(PatchSet(...)) puts it back:

>>> from unidiff import PatchSet
>>> d = "--- a/f\n+++ b/f\n@@ -1 +1 @@\n-a\n+b\n"
>>> str(PatchSet(d)) == d
False
>>> str(PatchSet(d))
'--- a/f\n+++ b/f\n@@ -1,1 +1,1 @@\n-a\n+b\n'

git diff -U0 writes every hunk that way, so a patch read and written back is almost
never the same bytes. Over four repositories, 350 of 411 git show -U0 diffs failed to
round trip before this change and none fail after.

Hunk.__init__ already sees None for a length the header left out, and turned it into
1 without keeping the distinction. It now records it, and __str__ writes whichever form
the header used. A hunk built by hand still writes both lengths, so nothing changes for
code that constructs one.

73 tests passed before, 75 after.

@abhishekcpp007 abhishekcpp007 closed this by deleting the head repository Nov 21, 2026
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