summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeremy Retailleau <jeremy.retailleau@gmail.com>2020-09-03 11:24:10 -0700
committerJeremy Retailleau <jeremy.retailleau@gmail.com>2020-09-03 11:24:10 -0700
commit5b88532a5346a9a7e8f0e45fec14632a9bfe2c89 (patch)
tree8b897d6ef521ce66cd346456ce87ab5315cf1c84
parent3787f622e59c2fecfa47efc114c409f51a27bbe7 (diff)
downloadgitpython-5b88532a5346a9a7e8f0e45fec14632a9bfe2c89.tar.gz
Ensure that detached HEAD does not raise when comparing branch name.
-rw-r--r--git/config.py8
-rw-r--r--test/test_config.py17
2 files changed, 24 insertions, 1 deletions
diff --git a/git/config.py b/git/config.py
index b49f0790..9f09efe2 100644
--- a/git/config.py
+++ b/git/config.py
@@ -493,7 +493,13 @@ class GitConfigParser(with_metaclass(MetaParserBuilder, cp.RawConfigParser, obje
paths += self.items(section)
elif keyword == "onbranch":
- if fnmatch.fnmatchcase(self._repo.active_branch.name, value):
+ try:
+ branch_name = self._repo.active_branch.name
+ except TypeError:
+ # Ignore section if active branch cannot be retrieved.
+ continue
+
+ if fnmatch.fnmatchcase(branch_name, value):
paths += self.items(section)
return paths
diff --git a/test/test_config.py b/test/test_config.py
index 406d794e..8892b839 100644
--- a/test/test_config.py
+++ b/test/test_config.py
@@ -345,6 +345,23 @@ class TestBase(TestCase):
assert config._has_includes()
assert config._included_paths() == [("path", path2)]
+ @with_rw_directory
+ def test_conditional_includes_from_branch_name_error(self, rw_dir):
+ # Initiate mocked repository to raise an error if HEAD is detached.
+ repo = mock.Mock()
+ type(repo).active_branch = mock.PropertyMock(side_effect=TypeError)
+
+ # Initiate config file.
+ path1 = osp.join(rw_dir, "config1")
+
+ # Ensure that config is ignored when active branch cannot be found.
+ with open(path1, "w") as stream:
+ stream.write("[includeIf \"onbranch:foo\"]\n path=/path\n")
+
+ with GitConfigParser(path1, repo=repo) as config:
+ assert not config._has_includes()
+ assert config._included_paths() == []
+
def test_rename(self):
file_obj = self._to_memcache(fixture_path('git_config'))
with GitConfigParser(file_obj, read_only=False, merge_includes=False) as cw: