Skip to content

check for GIT_WORK_TREE #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ Contributors are:
-Vincent Driessen <me _at_ nvie.com>
-Phil Elson <pelson _dot_ pub _at_ gmail.com>
-Bernard `Guyzmo` Pratz <guyzmo+gitpython+pub@m0g.net>
-Timothy B. Hartman <tbhartman _at_ gmail.com>

Portions derived from other open source works and are clearly marked.
2 changes: 1 addition & 1 deletion git/repo/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
29 changes: 29 additions & 0 deletions git/test/test_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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