Skip to content

Commit 0c2bfac

Browse files
authored
Merge pull request #2075 from bvanelli/2017-support-hasconfig-remote-url
feat: Add support for hasconfig git rule.
2 parents 963b6f8 + 6cf8633 commit 0c2bfac

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

git/config.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
CONFIG_LEVELS: ConfigLevels_Tup = ("system", "user", "global", "repository")
6767
"""The configuration level of a configuration file."""
6868

69-
CONDITIONAL_INCLUDE_REGEXP = re.compile(r"(?<=includeIf )\"(gitdir|gitdir/i|onbranch):(.+)\"")
69+
CONDITIONAL_INCLUDE_REGEXP = re.compile(r"(?<=includeIf )\"(gitdir|gitdir/i|onbranch|hasconfig:remote\.\*\.url):(.+)\"")
7070
"""Section pattern to detect conditional includes.
7171
7272
See: https://git-scm.com/docs/git-config#_conditional_includes
@@ -590,7 +590,11 @@ def _included_paths(self) -> List[Tuple[str, str]]:
590590

591591
if fnmatch.fnmatchcase(branch_name, value):
592592
paths += self.items(section)
593-
593+
elif keyword == "hasconfig:remote.*.url":
594+
for remote in self._repo.remotes:
595+
if fnmatch.fnmatchcase(remote.url, value):
596+
paths += self.items(section)
597+
break
594598
return paths
595599

596600
def read(self) -> None: # type: ignore[override]

test/test_config.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,41 @@ def test_conditional_includes_from_branch_name_error(self, rw_dir):
373373
assert not config._has_includes()
374374
assert config._included_paths() == []
375375

376+
@with_rw_directory
377+
def test_conditional_includes_remote_url(self, rw_dir):
378+
# Initiate mocked repository.
379+
repo = mock.Mock()
380+
repo.remotes = [mock.Mock(url="https://github.com/foo/repo")]
381+
382+
# Initiate config files.
383+
path1 = osp.join(rw_dir, "config1")
384+
path2 = osp.join(rw_dir, "config2")
385+
template = '[includeIf "hasconfig:remote.*.url:{}"]\n path={}\n'
386+
387+
# Ensure that config with hasconfig and full url is correct.
388+
with open(path1, "w") as stream:
389+
stream.write(template.format("https://github.com/foo/repo", path2))
390+
391+
with GitConfigParser(path1, repo=repo) as config:
392+
assert config._has_includes()
393+
assert config._included_paths() == [("path", path2)]
394+
395+
# Ensure that config with hasconfig and incorrect url is incorrect.
396+
with open(path1, "w") as stream:
397+
stream.write(template.format("incorrect", path2))
398+
399+
with GitConfigParser(path1, repo=repo) as config:
400+
assert not config._has_includes()
401+
assert config._included_paths() == []
402+
403+
# Ensure that config with hasconfig and url using glob pattern is correct.
404+
with open(path1, "w") as stream:
405+
stream.write(template.format("**/**github.com*/**", path2))
406+
407+
with GitConfigParser(path1, repo=repo) as config:
408+
assert config._has_includes()
409+
assert config._included_paths() == [("path", path2)]
410+
376411
def test_rename(self):
377412
file_obj = self._to_memcache(fixture_path("git_config"))
378413
with GitConfigParser(file_obj, read_only=False, merge_includes=False) as cw:

0 commit comments

Comments
 (0)