largefiles: reuse hexsha1() to centralize hash calculation logic into it
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Mon, 27 Mar 2017 09:44:34 +0900
changeset 31652 d5cbbee542eb
parent 31651 d3140fd322bf
child 31653 32d998dc2a00
largefiles: reuse hexsha1() to centralize hash calculation logic into it This patch also renames argument of hexsha1(), not only for readability ("data" isn't good name for file-like object), but also for reviewability (including hexsha1() code helps reviewers to confirm how these functions are similar). BTW, copyandhash() has also similar logic, but it can't reuse hexsha1(), because it writes read-in data into specified fileobj simultaneously.
hgext/largefiles/lfutil.py
--- a/hgext/largefiles/lfutil.py	Sun Mar 26 19:11:41 2017 +0900
+++ b/hgext/largefiles/lfutil.py	Mon Mar 27 09:44:34 2017 +0900
@@ -373,11 +373,8 @@
 def hashfile(file):
     if not os.path.exists(file):
         return ''
-    hasher = hashlib.sha1('')
     with open(file, 'rb') as fd:
-        for data in util.filechunkiter(fd):
-            hasher.update(data)
-    return hasher.hexdigest()
+        return hexsha1(fd)
 
 def getexecutable(filename):
     mode = os.stat(filename).st_mode
@@ -398,11 +395,11 @@
         url = join(url, a)
     return url
 
-def hexsha1(data):
+def hexsha1(fileobj):
     """hexsha1 returns the hex-encoded sha1 sum of the data in the file-like
     object data"""
     h = hashlib.sha1()
-    for chunk in util.filechunkiter(data):
+    for chunk in util.filechunkiter(fileobj):
         h.update(chunk)
     return h.hexdigest()