From 6e53eff9d5fafaf00afa991c2fb0d8de0a8ebdec Mon Sep 17 00:00:00 2001 From: Burak Yigit Kaya Date: Tue, 26 Jan 2016 23:27:24 +0200 Subject: [PATCH] Have consistent repo detection for `git` and `hg` --- xonsh/environ.py | 50 +++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/xonsh/environ.py b/xonsh/environ.py index 961fe5c39..0bb33269a 100644 --- a/xonsh/environ.py +++ b/xonsh/environ.py @@ -691,18 +691,37 @@ def locate_binary(name): return None +def _get_parent_dir_for(path, dir_name): + # walk up the directory tree to see if we are inside an hg repo + previous_path = '' + while path != previous_path: + if os.path.isdir(os.path.join(path, dir_name)): + return path + + previous_path = path + path, _ = os.path.split(path) + + return False + + def ensure_git(func): @wraps(func) def wrapper(*args, **kwargs): - # Get cwd or bail - kwargs['cwd'] = kwargs.get('cwd', _get_cwd()) - if kwargs['cwd'] is None: + cwd = kwargs.get('cwd', _get_cwd()) + if cwd is None: return # step out completely if git is not installed if locate_binary('git') is None: return + root_path = _get_parent_dir_for(cwd, '.git') + # Bail if we're not in a repo + if not root_path: + return + + kwargs['cwd'] = cwd + return func(*args, **kwargs) return wrapper @@ -710,27 +729,22 @@ def ensure_git(func): def ensure_hg(func): @wraps(func) def wrapper(*args, **kwargs): - kwargs['cwd'] = kwargs.get('cwd', _get_cwd()) - if kwargs['cwd'] is None: + cwd = kwargs.get('cwd', _get_cwd()) + if cwd is None: return - # walk up the directory tree to see if we are inside an hg repo - path = kwargs['cwd'].split(os.path.sep) - while len(path) > 0: - if os.path.exists(os.path.sep.join(path + ['.hg'])): - break - del path[-1] - - # bail if we aren't inside a repository - if path == []: - return - - kwargs['root'] = os.path.sep.join(path) - # step out completely if hg is not installed if locate_binary('hg') is None: return + root_path = _get_parent_dir_for(cwd, '.hg') + # Bail if we're not in a repo + if not root_path: + return + + kwargs['cwd'] = cwd + kwargs['root'] = root_path + return func(*args, **kwargs) return wrapper