Skip to content

Commit 912ddd0

Browse files
committed
Normalize gitdir include patterns on Windows
1 parent 833a48e commit 912ddd0

2 files changed

Lines changed: 14 additions & 11 deletions

File tree

git/config.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -576,9 +576,11 @@ def _all_items(section: str) -> List[Tuple[str, str]]:
576576
value = match.group(2).strip()
577577

578578
if keyword in ["gitdir", "gitdir/i"]:
579-
value = osp.expanduser(value)
579+
value = osp.expanduser(value).replace("\\", "/")
580+
git_dir = os.fspath(self._repo.git_dir).replace("\\", "/") if self._repo.git_dir else None
580581

581-
if not any(value.startswith(s) for s in ["./", "/"]):
582+
drive, _tail = osp.splitdrive(value)
583+
if not drive and not any(value.startswith(s) for s in ["./", "/"]):
582584
value = "**/" + value
583585
if value.endswith("/"):
584586
value += "**"
@@ -590,9 +592,8 @@ def _all_items(section: str) -> List[Tuple[str, str]]:
590592
lambda m: f"[{m.group().lower()!r}{m.group().upper()!r}]",
591593
value,
592594
)
593-
if self._repo.git_dir:
594-
if fnmatch.fnmatchcase(os.fspath(self._repo.git_dir), value):
595-
paths += _all_items(section)
595+
if git_dir and fnmatch.fnmatchcase(git_dir, value):
596+
paths += _all_items(section)
596597

597598
elif keyword == "onbranch":
598599
try:

test/test_config.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import io
88
import os
99
import os.path as osp
10-
import sys
1110
from unittest import mock
1211

1312
import pytest
@@ -411,11 +410,6 @@ def test_multiple_include_paths_with_same_key(self, rw_dir):
411410
assert cr.get_value("user", "name") == "from-inc1"
412411
assert cr.get_value("core", "bar") == "from-inc2"
413412

414-
@pytest.mark.xfail(
415-
sys.platform == "win32",
416-
reason='Second config._has_includes() assertion fails (for "config is included if path is matching git_dir")',
417-
raises=AssertionError,
418-
)
419413
@with_rw_directory
420414
def test_conditional_includes_from_git_dir(self, rw_dir):
421415
# Initiate repository path.
@@ -443,6 +437,14 @@ def test_conditional_includes_from_git_dir(self, rw_dir):
443437
assert config._has_includes()
444438
assert config._included_paths() == [("path", path2)]
445439

440+
# Ensure that Git's forward-slash syntax matches native Windows paths.
441+
with open(path1, "w") as stream:
442+
stream.write(template.format("gitdir", git_dir.replace("\\", "/"), path2))
443+
444+
with GitConfigParser(path1, repo=repo) as config:
445+
assert config._has_includes()
446+
assert config._included_paths() == [("path", path2)]
447+
446448
# Ensure that config is ignored if case is incorrect.
447449
with open(path1, "w") as stream:
448450
stream.write(template.format("gitdir", git_dir.upper(), path2))

0 commit comments

Comments
 (0)