Skip to content

Commit 1d47514

Browse files
authored
Merge pull request #2224 from gitpython-developers/worktree-from-bare
Keep bare-repository worktrees non-bare
2 parents 589d8af + 0e8c23c commit 1d47514

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

.github/workflows/cygwin-test.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
- name: Install Cygwin
4646
uses: cygwin/cygwin-install-action@v6
4747
with:
48-
packages: git python39 python-pip-wheel python-setuptools-wheel python-wheel-wheel
48+
packages: curl git python39 python-setuptools-wheel
4949
add-to-path: false # No need to change $PATH outside the Cygwin environment.
5050

5151
- name: Arrange for verbose output
@@ -73,12 +73,15 @@ jobs:
7373
7474
- name: Set up virtual environment
7575
run: |
76+
pip_wheel=/usr/share/python-wheels/pip-26.0.1-py3-none-any.whl
77+
curl -fsSLo "$pip_wheel" https://files.pythonhosted.org/packages/de/f0/c81e05b613866b76d2d1066490adf1a3dbc4ee9d9c839961c3fc8a6997af/pip-26.0.1-py3-none-any.whl
78+
python3.9 -c 'import hashlib, pathlib, sys; assert hashlib.sha256(pathlib.Path(sys.argv[1]).read_bytes()).hexdigest() == sys.argv[2]' "$pip_wheel" bdb1b08f4274833d62c1aa29e20907365a2ceb950410df15fc9521bad440122b
7679
python3.9 -m venv .venv
7780
echo 'BASH_ENV=.venv/bin/activate' >>"$GITHUB_ENV"
7881
7982
- name: Update PyPA packages
8083
run: |
81-
python -m pip install -U pip 'setuptools; python_version<"3.12"' wheel
84+
python -m pip install -U 'setuptools; python_version<"3.12"' wheel
8285
8386
- name: Install project and test dependencies
8487
run: |

git/repo/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ def __init__(
386386
# Let's not assume the option exists, although it should.
387387
pass
388388

389+
# A linked worktree is not bare even when its main repository is.
390+
if self._bare and self._working_tree_dir and osp.isfile(osp.join(self.git_dir, "commondir")):
391+
self._bare = False
392+
389393
# Adjust the working directory in case we are actually bare - we didn't know
390394
# that in the first place.
391395
if self._bare:

test/test_repo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,23 @@ def test_git_work_tree_dotgit(self, rw_dir, use_relative_paths=False):
14591459

14601460
self.assertIsInstance(repo.heads["aaaaaaaa"], Head)
14611461

1462+
@with_rw_directory
1463+
def test_git_work_tree_from_bare_repo(self, rw_dir):
1464+
if Git().version_info[:3] < (2, 5, 1):
1465+
pytest.skip("worktree feature unsupported, needs git 2.5.1 or later")
1466+
1467+
bare_repo = self.rorepo.clone(join_path_native(rw_dir, "bare_repo"), bare=True)
1468+
worktree_path = join_path_native(rw_dir, "worktree_repo")
1469+
if Git.is_cygwin():
1470+
worktree_path = cygpath(worktree_path)
1471+
bare_repo.git.worktree("add", "--detach", worktree_path)
1472+
1473+
repo = Repo(worktree_path)
1474+
1475+
assert Git(worktree_path).rev_parse("--is-bare-repository") == "false"
1476+
assert not repo.bare
1477+
assert osp.samefile(repo.working_tree_dir, worktree_path)
1478+
14621479
def test_git_work_tree_dotgit_relative(self):
14631480
"""Check that we find .git as a worktree file containing a relative path
14641481
and find the worktree based on it."""

0 commit comments

Comments
 (0)