Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
GitPlugin: minor optimization (use regex instead of string iteration)
Browse files Browse the repository at this point in the history
  • Loading branch information
hvr committed Mar 23, 2009
1 parent d5940e0 commit 386f4ae
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions tracext/git/PyGIT.py
Expand Up @@ -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"]

Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit 386f4ae

Please sign in to comment.