From b29388a8f9cf3522e5f52b47572af7d8f61862a1 Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Wed, 19 Oct 2016 15:49:13 +0200 Subject: FIX #535: expand also GIT_DIR var on Repo-construct + Ignore "empty" GIT_DIR vars. + Improve documentation on the constructor `path` parameter. --- git/repo/base.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index c5cdce7c..d36864bb 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -124,6 +124,8 @@ class Repo(object): repo = Repo("~/Development/git-python.git") repo = Repo("$REPOSITORIES/Development/git-python.git") + if `None, current-directory is used. + The :envvar:`GIT_DIR` if set and not empty takes precendance over this parameter. :param odbt: Object DataBase type - a type which is constructed by providing the directory containing the database objects, i.e. .git/objects. It will @@ -136,17 +138,19 @@ class Repo(object): :raise InvalidGitRepositoryError: :raise NoSuchPathError: :return: git.Repo """ - epath = _expand_path(path or os.getcwd()) self.git = None # should be set for __del__ not to fail in case we raise + epath = os.getenv('GIT_DIR') + epath = _expand_path(epath or path or os.getcwd()) if not os.path.exists(epath): raise NoSuchPathError(epath) self.working_dir = None self._working_tree_dir = None self.git_dir = None - curpath = os.getenv('GIT_DIR', epath) - # walk up the path to find the .git dir + ## Walk up the path to find the `.git` dir. + # + curpath = epath while curpath: # ABOUT os.path.NORMPATH # It's important to normalize the paths, as submodules will otherwise initialize their -- cgit v1.2.1 From 9d5d143f72e4d588e3a0abb2ab82fa5a2c35e8aa Mon Sep 17 00:00:00 2001 From: Kostis Anagnostopoulos Date: Wed, 19 Oct 2016 16:01:47 +0200 Subject: repo: minor code and doc correcions. + Expansion of paths also `osp.normalize()` them. + Make Repo-fields --> class-fields to avoid initializations on construct. + Explain and rename `git.repo.fun.find_git_dir()` is for submodules (`find_submodule_git_dir()`). --- git/repo/base.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'git/repo/base.py') diff --git a/git/repo/base.py b/git/repo/base.py index d36864bb..bde9b936 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -47,7 +47,7 @@ from gitdb.util import ( from .fun import ( rev_parse, is_git_dir, - find_git_dir, + find_submodule_git_dir, touch, ) from git.compat import ( @@ -79,7 +79,7 @@ __all__ = ('Repo',) def _expand_path(p): - return os.path.abspath(os.path.expandvars(os.path.expanduser(p))) + return os.path.normpath(os.path.abspath(os.path.expandvars(os.path.expanduser(p)))) class Repo(object): @@ -98,6 +98,11 @@ class Repo(object): 'git_dir' is the .git repository directory, which is always set.""" DAEMON_EXPORT_FILE = 'git-daemon-export-ok' + git = None # Must exist, or __del__ will fail in case we raise on `__init__()` + working_dir = None + _working_tree_dir = None + git_dir = None + # precompiled regex re_whitespace = re.compile(r'\s+') re_hexsha_only = re.compile('^[0-9A-Fa-f]{40}$') @@ -138,16 +143,11 @@ class Repo(object): :raise InvalidGitRepositoryError: :raise NoSuchPathError: :return: git.Repo """ - self.git = None # should be set for __del__ not to fail in case we raise epath = os.getenv('GIT_DIR') epath = _expand_path(epath or path or os.getcwd()) if not os.path.exists(epath): raise NoSuchPathError(epath) - self.working_dir = None - self._working_tree_dir = None - self.git_dir = None - ## Walk up the path to find the `.git` dir. # curpath = epath @@ -157,20 +157,20 @@ class Repo(object): # repo instances with paths that depend on path-portions that will not exist after being # removed. It's just cleaner. if is_git_dir(curpath): - self.git_dir = os.path.normpath(curpath) + self.git_dir = curpath self._working_tree_dir = os.path.dirname(self.git_dir) break - gitpath = find_git_dir(join(curpath, '.git')) - if gitpath is not None: - self.git_dir = os.path.normpath(gitpath) + sm_gitpath = find_submodule_git_dir(join(curpath, '.git')) + if sm_gitpath is not None: + self.git_dir = os.path.normpath(sm_gitpath) self._working_tree_dir = curpath break if not search_parent_directories: break - curpath, dummy = os.path.split(curpath) - if not dummy: + curpath, tail = os.path.split(curpath) + if not tail: break # END while curpath -- cgit v1.2.1