From 52ab307935bd2bbda52f853f9fc6b49f01897727 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Fri, 9 Oct 2009 12:14:02 +0200 Subject: diff regex are now precompiled on class level, renamed a|b_blob to a|b_blob_id as it better reflects the actual value actor regex now precompiled on class level blob regex now precompiled on class level; made blame method more readable and faster although it can still be improved by making assumptions about the blame format and by reading the git command stream directly ( which is a general issue right now ) --- lib/git/diff.py | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) (limited to 'lib/git/diff.py') diff --git a/lib/git/diff.py b/lib/git/diff.py index db12f1e4..75450d70 100644 --- a/lib/git/diff.py +++ b/lib/git/diff.py @@ -29,20 +29,36 @@ class Diff(object): b_mode is None b_blob is NOne """ + + # precompiled regex + re_header = re.compile(r""" + #^diff[ ]--git + [ ]a/(?P\S+)[ ]b/(?P\S+)\n + (?:^similarity[ ]index[ ](?P\d+)%\n + ^rename[ ]from[ ](?P\S+)\n + ^rename[ ]to[ ](?P\S+)(?:\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|$))? + """, re.VERBOSE | re.MULTILINE) + re_is_null_hexsha = re.compile( r'^0{40}$' ) - def __init__(self, repo, a_path, b_path, a_blob, b_blob, a_mode, + def __init__(self, repo, a_path, b_path, a_blob_id, b_blob_id, a_mode, b_mode, new_file, deleted_file, rename_from, rename_to, diff): self.repo = repo - if not a_blob or re.search(r'^0{40}$', a_blob): + if not a_blob_id or self.re_is_null_hexsha.search(a_blob_id): self.a_blob = None else: - self.a_blob = blob.Blob(repo, id=a_blob, mode=a_mode, path=a_path) - if not b_blob or re.search(r'^0{40}$', b_blob): + self.a_blob = blob.Blob(repo, id=a_blob_id, mode=a_mode, path=a_path) + if not b_blob_id or self.re_is_null_hexsha.search(b_blob_id): self.b_blob = None else: - self.b_blob = blob.Blob(repo, id=b_blob, mode=b_mode, path=b_path) + self.b_blob = blob.Blob(repo, id=b_blob_id, mode=b_mode, path=b_path) self.a_mode = a_mode self.b_mode = b_mode @@ -68,29 +84,16 @@ class Diff(object): """ diffs = [] - diff_header = re.compile(r""" - #^diff[ ]--git - [ ]a/(?P\S+)[ ]b/(?P\S+)\n - (?:^similarity[ ]index[ ](?P\d+)%\n - ^rename[ ]from[ ](?P\S+)\n - ^rename[ ]to[ ](?P\S+)(?:\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|$))? - """, re.VERBOSE | re.MULTILINE).match - + diff_header = cls.re_header.match for diff in ('\n' + text).split('\ndiff --git')[1:]: header = diff_header(diff) a_path, b_path, similarity_index, rename_from, rename_to, \ old_mode, new_mode, new_file_mode, deleted_file_mode, \ - a_blob, b_blob, b_mode = header.groups() + a_blob_id, b_blob_id, b_mode = header.groups() new_file, deleted_file = bool(new_file_mode), bool(deleted_file_mode) - diffs.append(Diff(repo, a_path, b_path, a_blob, b_blob, + diffs.append(Diff(repo, a_path, b_path, a_blob_id, b_blob_id, old_mode or deleted_file_mode, new_mode or new_file_mode or b_mode, new_file, deleted_file, rename_from, rename_to, diff[header.end():])) -- cgit v1.2.1