Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ class GitConfigParser(cp.RawConfigParser, metaclass=MetaParserBuilder):
re_comment = re.compile(r"^\s*[#;]")
# } END configuration

optvalueonly_source = r"\s*(?P<option>[^:=\s][^:=]*)"
optvalueonly_source = r"\s*(?P<option>[^:=\s#;][^:=#;]*)"

OPTVALUEONLY = re.compile(optvalueonly_source)

Expand Down Expand Up @@ -594,7 +594,7 @@ def parse_value(value: str) -> str:
cursect.add(optname, optval)
else:
# A valueless option is an implicit boolean true, not an empty value.
mo = self.OPTVALUEONLY.match(line)
mo = self.OPTVALUEONLY.fullmatch(line)
Comment thread
Byron marked this conversation as resolved.
if mo:
optname = self.optionxform(mo.group("option").rstrip())
cursect.add(optname, None)
Expand Down
33 changes: 33 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,39 @@ def test_implicit_boolean_round_trip(self, rw_dir):
self.assertIs(config.getboolean("flag", "empty"), False)
self.assertEqual(dict(config.items_all("flag"))["multiple"], ["false", None, "", None])

@with_rw_directory
def test_implicit_boolean_rejects_comments_like_git(self, rw_dir):
config_path = osp.join(rw_dir, "config")
comments = (
" # comment",
" ; comment",
"# comment = value",
"; comment = value",
"\t# comment: value",
Comment thread
Byron marked this conversation as resolved.
"\t; comment: value",
)
for comment in comments:
with self.subTest(comment=comment):
content = ("[flag]\n\tenabled%s\n" % comment).encode(defenc)
with open(config_path, "wb") as config_file:
config_file.write(content)

result = subprocess.run(
["git", "config", "--file", config_path, "--bool", "flag.enabled"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
self.assertEqual(result.returncode, 128, result.stderr)
with GitConfigParser(config_path) as config:
with self.assertRaises(cp.ParsingError):
config.getboolean("flag", "enabled")
with GitConfigParser(config_path, read_only=False) as config:
with self.assertRaises(cp.ParsingError):
config.set_value("other", "value", "updated")

with open(config_path, "rb") as config_file:
self.assertEqual(config_file.read(), content)

def test_config_with_quotes(self):
cr = GitConfigParser(fixture_path("git_config_with_quotes"), read_only=True)

Expand Down
Loading