Repository navigation
Strip a/ and b/ prefixes for rename-only and mode-only git diffs - #66
Closed
afonsojanu wants to merge 1 commit into
Closed
afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
A git diff that only renames a file or changes its mode has neither an "index <sha>..<sha>" line nor a unified hunk, since there is no content to show. parse_header() relies on one of those to recognize the diff as a git diff and route it to the git-aware parser that strips the a/ and b/ prefixes from the paths. Without them, the generic diffcmd_header regex matches the "diff --git a/x b/y" line first (it happens to fit that pattern too) and the raw, prefixed paths leak into old_path/new_path. Give the git-specific "diff --git" line priority over the generic one in parse_diff_header, and let parse_git_header fall back to the paths from that line even when no blob hashes were found, so a plain rename or mode change still gets a usable header. Fixes #43.
Author
|
Part of a general cleanup on my end, closing this along with the rest of what I had open. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A git diff that only renames a file, or only changes its mode, has no
content to show, so git omits both the
index <sha>..<sha>line and theunified
---/+++hunk lines. For example:parse_header()normally relies on one of those two things being presentto recognize the diff as git-flavored and route it to
parse_git_header,which knows to strip the
a//b/prefixes. When neither is there, thegeneric
diffcmd_headerregex ends up matching thediff --git a/x b/yline instead, since that pattern happens to fit it too, and the raw,
prefixed paths leak straight into
old_path/new_path:>>> whatthepatch.parse_patch(text)[0].header header(index_path=None, old_path='a/myfile2.txt', old_version=None, new_path='b/myfile3.txt', new_version=None)This fixes it two ways:
parse_diff_headernow checks the git-specificdiff --git a/... b/...line ahead of the generic one, so it's always routed to
parse_git_header.parse_git_header's fallback (which uses the paths straight from thediff --gitcommand line when nothing more specific was found) nolonger requires blob hashes to be present, since a pure rename or mode
change never has them.
Added two regression tests with fixtures for a rename-only diff and a
mode-only diff, both of which fail against the old code with the
a//b/prefixes still attached and pass with the fix. The rest of the suite
(
tests/test_patch.pyandtests/test_apply.py) still passes locally.Fixes #43.