From e77128e5344ce7d84302facc08d17c3151037ec3 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Thu, 14 Apr 2016 21:27:39 +0200 Subject: Make diff patch parsing more reliable The a_path and b_path cannot reliably be read from the first diff line as it's ambiguous. From the git-diff manpage: > The a/ and b/ filenames are the same unless rename/copy is involved. > Especially, **even for a creation or a deletion**, /dev/null is not > used in place of the a/ or b/ filenames. This patch changes the a_path and b_path detection to read it from the more reliable locations further down the diff headers. Two use cases are fixed by this: - As the man page snippet above states, for new/deleted files the a or b path will now be properly None. - File names with spaces in it are now properly parsed. Working on this patch, I realized the --- and +++ lines really belong to the diff header, not the diff contents. This means that when parsing the patch format, the --- and +++ will now be swallowed, and not end up anymore as part of the diff contents. The diff contents now always start with an @@ line. This may be a breaking change for some users that rely on this behaviour. However, those users could now access that information more reliably via the normal Diff properties a_path and b_path now. --- git/test/fixtures/diff_file_with_spaces | 7 +++++++ git/test/fixtures/diff_initial | 2 -- 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 git/test/fixtures/diff_file_with_spaces (limited to 'git/test/fixtures') diff --git a/git/test/fixtures/diff_file_with_spaces b/git/test/fixtures/diff_file_with_spaces new file mode 100644 index 00000000..a9f0b06c --- /dev/null +++ b/git/test/fixtures/diff_file_with_spaces @@ -0,0 +1,7 @@ +diff --git a/file with spaces b/file with spaces +new file mode 100644 +index 0000000000000000000000000000000000000000..75c620d7b0d3b0100415421a97f553c979d75174 +--- /dev/null ++++ b/file with spaces +@@ -0,0 +1 @@ ++ohai diff --git a/git/test/fixtures/diff_initial b/git/test/fixtures/diff_initial index 6037c677..648d7043 100644 --- a/git/test/fixtures/diff_initial +++ b/git/test/fixtures/diff_initial @@ -1,5 +1,3 @@ ---- /dev/null -+++ b/CHANGES @@ -0,0 +1,7 @@ +======= +CHANGES -- cgit v1.2.1