From 8dfa1685aac22a83ba1f60d1b2d52abf5a3d842f Mon Sep 17 00:00:00 2001 From: "Timothy B. Hartman" Date: Fri, 24 Feb 2017 13:39:49 -0500 Subject: [PATCH] check for GIT_WORK_TREE --- AUTHORS | 1 + git/repo/base.py | 2 +- git/test/test_repo.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index 34cf8cb4b..bf0f5e055 100644 --- a/AUTHORS +++ b/AUTHORS @@ -16,5 +16,6 @@ Contributors are: -Vincent Driessen -Phil Elson -Bernard `Guyzmo` Pratz +-Timothy B. Hartman Portions derived from other open source works and are clearly marked. diff --git a/git/repo/base.py b/git/repo/base.py index 85f2d0369..2585bc5a1 100644 --- a/git/repo/base.py +++ b/git/repo/base.py @@ -133,7 +133,7 @@ def __init__(self, path=None, odbt=DefaultDBType, search_parent_directories=Fals # removed. It's just cleaner. if is_git_dir(curpath): self.git_dir = curpath - self._working_tree_dir = os.path.dirname(self.git_dir) + self._working_tree_dir = os.getenv('GIT_WORK_TREE', os.path.dirname(self.git_dir)) break sm_gitpath = find_submodule_git_dir(osp.join(curpath, '.git')) diff --git a/git/test/test_repo.py b/git/test/test_repo.py index 9ad80ee63..5dc7bbb08 100644 --- a/git/test/test_repo.py +++ b/git/test/test_repo.py @@ -925,3 +925,32 @@ def test_work_tree_unsupported(self, rw_dir): raise AssertionError(ex, "It's ok if TC not running from `master`.") self.failUnlessRaises(InvalidGitRepositoryError, Repo, worktree_path) + + @with_rw_directory + def test_git_work_tree_env(self, rw_dir): + """Check that we yield to GIT_WORK_TREE""" + # clone a repo + # move .git directory to a subdirectory + # set GIT_DIR and GIT_WORK_TREE appropriately + # check that repo.working_tree_dir == rw_dir + git = Git(rw_dir) + self.rorepo.clone(join_path_native(rw_dir, 'master_repo')) + + repo_dir = join_path_native(rw_dir, 'master_repo') + old_git_dir = join_path_native(repo_dir, '.git') + new_subdir = join_path_native(repo_dir, 'gitdir') + new_git_dir = join_path_native(new_subdir, 'git') + os.mkdir(new_subdir) + os.rename(old_git_dir, new_git_dir) + + oldenv = os.environ.copy() + os.environ['GIT_DIR'] = new_git_dir + os.environ['GIT_WORK_TREE'] = repo_dir + + try: + r = Repo() + self.assertEqual(r.working_tree_dir, repo_dir) + self.assertEqual(r.working_dir, repo_dir) + finally: + os.environ = oldenv +