Merge branch 'master' of github.com:pre-commit/pre-commit

This commit is contained in:
Ken Struys 2014-03-14 00:30:29 -07:00
commit cdea26a748
3 changed files with 59 additions and 5 deletions

View file

@ -1,14 +1,16 @@
import os
import pkg_resources
from plumbum import local
from pre_commit.util import memoize_by_cwd
# TODO: optimization: memoize based on local.cwd.getpath()
@memoize_by_cwd
def get_root():
return local['git']['rev-parse', '--show-toplevel']().strip()
@memoize_by_cwd
def get_pre_commit_path():
return os.path.join(get_root(), '.git/hooks/pre-commit')
@ -25,4 +27,4 @@ def remove_pre_commit():
def get_head_sha(git_repo_path):
with local.cwd(git_repo_path):
return (local['git']['rev-parse', 'HEAD'])().strip()
return (local['git']['rev-parse', 'HEAD'])().strip()

View file

@ -1,4 +1,8 @@
import functools
import os
class cached_property(object):
"""Like @property, but caches the value."""
@ -14,3 +18,19 @@ class cached_property(object):
value = self._func(obj)
obj.__dict__[self.__name__] = value
return value
def memoize_by_cwd(func):
"""Memoize a function call based on os.getcwd()."""
cache = {}
@functools.wraps(func)
def wrapper(*args):
cwd = os.getcwd()
key = (cwd,) + args
try:
return cache[key]
except KeyError:
ret = cache[key] = func(*args)
return ret
return wrapper