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
iffnmatch.fnmatchcase(branch_name, value):
592592
paths+=self.items(section)
593-
593+
elifkeyword=="hasconfig:remote.*.url":
594+
forremoteinself._repo.remotes:
595+
iffnmatch.fnmatchcase(remote.url, value):
596+
paths+=self.items(section)
597+
break
594598
returnpaths
595599

596600
defread(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
assertnotconfig._has_includes()
374374
assertconfig._included_paths() == []
375375

376+
@with_rw_directory
377+
deftest_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+
withopen(path1, "w") asstream:
389+
stream.write(template.format("https://github.com/foo/repo", path2))
390+
391+
withGitConfigParser(path1, repo=repo) asconfig:
392+
assertconfig._has_includes()
393+
assertconfig._included_paths() == [("path", path2)]
394+
395+
# Ensure that config with hasconfig and incorrect url is incorrect.
396+
withopen(path1, "w") asstream:
397+
stream.write(template.format("incorrect", path2))
398+
399+
withGitConfigParser(path1, repo=repo) asconfig:
400+
assertnotconfig._has_includes()
401+
assertconfig._included_paths() == []
402+
403+
# Ensure that config with hasconfig and url using glob pattern is correct.
404+
withopen(path1, "w") asstream:
405+
stream.write(template.format("**/**github.com*/**", path2))
406+
407+
withGitConfigParser(path1, repo=repo) asconfig:
408+
assertconfig._has_includes()
409+
assertconfig._included_paths() == [("path", path2)]
410+
376411
deftest_rename(self):
377412
file_obj=self._to_memcache(fixture_path("git_config"))
378413
withGitConfigParser(file_obj, read_only=False, merge_includes=False) ascw:

0 commit comments

Comments
(0)