import re
+
try:
import hashlib
sha1_hash = hashlib.sha1
return (patchbuf, commentbuf)
-def patch_hash(str):
+def hash_patch(str):
+ # normalise spaces
str = str.replace('\r', '')
str = str.strip() + '\n'
- lines = str.split('\n')
prefixes = ['-', '+', ' ']
hash = sha1_hash()
if len(line) <= 0:
continue
- hunk_match = _hunk_re.match(line)
- filename_match = _filename_re.match(line)
+ hunk_match = _hunk_re.match(line)
+ filename_match = _filename_re.match(line)
if filename_match:
# normalise -p1 top-directories
line = filename_match.group(1) + ' ' + filename
-
- elif hunk_match:
- # remove line numbers
+ elif hunk_match:
+ # remove line numbers, but leave line counts
def fn(x):
if not x:
return 1
line = '@@ -%d +%d @@' % tuple(line_nos)
elif line[0] in prefixes:
+ # if we have a +, - or context line, leave as-is
pass
else:
+ # other lines are ignored
continue
- hash.update(line + '\n')
+ hash.update(line.encode('utf-8') + '\n')
+
+ return hash
if __name__ == '__main__':
import sys
-# (patch, comment) = parse_patch(sys.stdin.read())
-# if patch:
-# print "Patch: ------\n" + patch
-# if comment:
-# print "Comment: ----\n" + comment
- normalise_patch_content(sys.stdin.read())
+ (patch, comment) = parse_patch(sys.stdin.read())
+ if patch:
+ print "Patch: ------\n" + patch
+ print "hash: %s" % hash_patch(patch).hexdigest()
+ if comment:
+ print "Comment: ----\n" + comment