Permalink
Browse files

GitPlugin: minor optimization (use regex instead of string iteration)

  • Loading branch information...
1 parent d5940e0 commit 386f4ae2ea5aa5ed54d3574cec1d978f64fb81fb @hvr committed Mar 23, 2009
Showing with 13 additions and 6 deletions.
  1. +13 −6 tracext/git/PyGIT.py
View
@@ -20,7 +20,6 @@
from threading import Lock
from subprocess import Popen, PIPE
import cStringIO
-#from traceback import print_stack
__all__ = ["git_version", "GitError", "GitErrorSha", "Storage", "StorageFactory"]
@@ -62,14 +61,16 @@ def __execute(self, git_cmd, *cmd_args):
def __getattr__(self, name):
return partial(self.__execute, name.replace('_','-'))
- @staticmethod
- def is_sha(sha):
+ __is_sha_pat = re.compile(r'[0-9A-Fa-f]*$')
+
+ @classmethod
+ def is_sha(cls,sha):
"""returns whether sha is a potential sha id
(i.e. proper hexstring between 4 and 40 characters"""
- if len(sha) < 4 or len(sha) > 40:
+ if not (4 <= len(sha) <= 40):
return False
- HEXCHARS = "0123456789abcdefABCDEF"
- return all(s in HEXCHARS for s in sha)
+
+ return bool(cls.__is_sha_pat.match(sha))
# helper class for caching...
class SizedDict(dict):
@@ -690,6 +691,12 @@ def __chg_tuple():
if __name__ == '__main__':
import sys, logging, timeit
+ assert not GitCore.is_sha("123")
+ assert GitCore.is_sha("1a3f")
+ assert GitCore.is_sha("f"*40)
+ assert not GitCore.is_sha("x"+"f"*39)
+ assert not GitCore.is_sha("f"*41)
+
print "git version [%s]" % str(Storage.git_version())
# custom linux hack reading `/proc/<PID>/statm`

0 comments on commit 386f4ae

Please sign in to comment.