Skip to content

Commit 5b88532

Browse files
committed
Ensure that detached HEAD does not raise when comparing branch name.
1 parent 3787f62 commit 5b88532

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

Diff for: git/config.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,13 @@ def _included_paths(self):
493493
paths += self.items(section)
494494

495495
elif keyword == "onbranch":
496-
if fnmatch.fnmatchcase(self._repo.active_branch.name, value):
496+
try:
497+
branch_name = self._repo.active_branch.name
498+
except TypeError:
499+
# Ignore section if active branch cannot be retrieved.
500+
continue
501+
502+
if fnmatch.fnmatchcase(branch_name, value):
497503
paths += self.items(section)
498504

499505
return paths

Diff for: test/test_config.py

+17
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,23 @@ def test_conditional_includes_from_branch_name(self, rw_dir):
345345
assert config._has_includes()
346346
assert config._included_paths() == [("path", path2)]
347347

348+
@with_rw_directory
349+
def test_conditional_includes_from_branch_name_error(self, rw_dir):
350+
# Initiate mocked repository to raise an error if HEAD is detached.
351+
repo = mock.Mock()
352+
type(repo).active_branch = mock.PropertyMock(side_effect=TypeError)
353+
354+
# Initiate config file.
355+
path1 = osp.join(rw_dir, "config1")
356+
357+
# Ensure that config is ignored when active branch cannot be found.
358+
with open(path1, "w") as stream:
359+
stream.write("[includeIf \"onbranch:foo\"]\n path=/path\n")
360+
361+
with GitConfigParser(path1, repo=repo) as config:
362+
assert not config._has_includes()
363+
assert config._included_paths() == []
364+
348365
def test_rename(self):
349366
file_obj = self._to_memcache(fixture_path('git_config'))
350367
with GitConfigParser(file_obj, read_only=False, merge_includes=False) as cw:

0 commit comments

Comments
 (0)