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/diff.py | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'git/diff.py') diff --git a/git/diff.py b/git/diff.py index de3aa1e8..c4fd4b27 100644 --- a/git/diff.py +++ b/git/diff.py @@ -198,16 +198,18 @@ class Diff(object): # precompiled regex re_header = re.compile(r""" ^diff[ ]--git - [ ](?:a/)?(?P.+?)[ ](?:b/)?(?P.+?)\n - (?:^similarity[ ]index[ ](?P\d+)%\n - ^rename[ ]from[ ](?P\S+)\n - ^rename[ ]to[ ](?P\S+)(?:\n|$))? + [ ](?:a/)?(?P.+?)[ ](?:b/)?(?P.+?)\n + (?:^similarity[ ]index[ ]\d+%\n + ^rename[ ]from[ ](?P.*)\n + ^rename[ ]to[ ](?P.*)(?:\n|$))? (?:^old[ ]mode[ ](?P\d+)\n ^new[ ]mode[ ](?P\d+)(?:\n|$))? (?:^new[ ]file[ ]mode[ ](?P.+)(?:\n|$))? (?:^deleted[ ]file[ ]mode[ ](?P.+)(?:\n|$))? (?:^index[ ](?P[0-9A-Fa-f]+) \.\.(?P[0-9A-Fa-f]+)[ ]?(?P.+)?(?:\n|$))? + (?:^---[ ](?:a/)?(?P.*)(?:\n|$))? + (?:^\+\+\+[ ](?:b/)?(?P.*)(?:\n|$))? """.encode('ascii'), re.VERBOSE | re.MULTILINE) # can be used for comparisons NULL_HEX_SHA = "0" * 40 @@ -231,15 +233,14 @@ class Diff(object): if self.b_mode: self.b_mode = mode_str_to_int(self.b_mode) - if a_blob_id is None: + if a_blob_id is None or a_blob_id == self.NULL_HEX_SHA: self.a_blob = None else: - assert self.a_mode is not None self.a_blob = Blob(repo, hex_to_bin(a_blob_id), mode=self.a_mode, path=a_path) - if b_blob_id is None: + + if b_blob_id is None or b_blob_id == self.NULL_HEX_SHA: self.b_blob = None else: - assert self.b_mode is not None self.b_blob = Blob(repo, hex_to_bin(b_blob_id), mode=self.b_mode, path=b_path) self.new_file = new_file @@ -329,11 +330,22 @@ class Diff(object): index = DiffIndex() previous_header = None for header in cls.re_header.finditer(text): - a_path, b_path, similarity_index, rename_from, rename_to, \ + a_path_fallback, b_path_fallback, \ + rename_from, rename_to, \ old_mode, new_mode, new_file_mode, deleted_file_mode, \ - a_blob_id, b_blob_id, b_mode = header.groups() + a_blob_id, b_blob_id, b_mode, \ + a_path, b_path = header.groups() new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode) + a_path = a_path or rename_from or a_path_fallback + b_path = b_path or rename_to or b_path_fallback + + if a_path == b'/dev/null': + a_path = None + + if b_path == b'/dev/null': + b_path = None + # Our only means to find the actual text is to see what has not been matched by our regex, # and then retro-actively assin it to our index if previous_header is not None: -- cgit v1.2.1 From cdf7c5aca2201cf9dfc3cd301264da4ea352b737 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 15 Apr 2016 01:39:58 +0200 Subject: Fix regex This makes sure we're not matching a \n here by accident. It's now almost the same as the original that used \S+, except that spaces are not eaten at the end of the string (for files that end in a space). --- git/diff.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'git/diff.py') diff --git a/git/diff.py b/git/diff.py index c4fd4b27..fd13f988 100644 --- a/git/diff.py +++ b/git/diff.py @@ -208,8 +208,8 @@ class Diff(object): (?:^deleted[ ]file[ ]mode[ ](?P.+)(?:\n|$))? (?:^index[ ](?P[0-9A-Fa-f]+) \.\.(?P[0-9A-Fa-f]+)[ ]?(?P.+)?(?:\n|$))? - (?:^---[ ](?:a/)?(?P.*)(?:\n|$))? - (?:^\+\+\+[ ](?:b/)?(?P.*)(?:\n|$))? + (?:^---[ ](?:a/)?(?P[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))? + (?:^\+\+\+[ ](?:b/)?(?P[^\t\n\r\f\v]*)[\t\r\f\v]*(?:\n|$))? """.encode('ascii'), re.VERBOSE | re.MULTILINE) # can be used for comparisons NULL_HEX_SHA = "0" * 40 -- cgit v1.2.1 From 1445b59bb41c4b1a94b7cb0ec6864c98de63814b Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Fri, 15 Apr 2016 08:32:45 +0200 Subject: Fix order of regex parts When both old/new mode and rename from/to lines are found, they will appear in different order. --- git/diff.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'git/diff.py') diff --git a/git/diff.py b/git/diff.py index fd13f988..d4affd30 100644 --- a/git/diff.py +++ b/git/diff.py @@ -199,11 +199,11 @@ class Diff(object): re_header = re.compile(r""" ^diff[ ]--git [ ](?:a/)?(?P.+?)[ ](?:b/)?(?P.+?)\n + (?:^old[ ]mode[ ](?P\d+)\n + ^new[ ]mode[ ](?P\d+)(?:\n|$))? (?:^similarity[ ]index[ ]\d+%\n ^rename[ ]from[ ](?P.*)\n ^rename[ ]to[ ](?P.*)(?:\n|$))? - (?:^old[ ]mode[ ](?P\d+)\n - ^new[ ]mode[ ](?P\d+)(?:\n|$))? (?:^new[ ]file[ ]mode[ ](?P.+)(?:\n|$))? (?:^deleted[ ]file[ ]mode[ ](?P.+)(?:\n|$))? (?:^index[ ](?P[0-9A-Fa-f]+) @@ -331,8 +331,9 @@ class Diff(object): previous_header = None for header in cls.re_header.finditer(text): a_path_fallback, b_path_fallback, \ + old_mode, new_mode, \ rename_from, rename_to, \ - old_mode, new_mode, new_file_mode, deleted_file_mode, \ + new_file_mode, deleted_file_mode, \ a_blob_id, b_blob_id, b_mode, \ a_path, b_path = header.groups() new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode) -- cgit v1.2.1