Skip to content

Commit 5fd795a

Browse files
committed
Read git's yes/no and on/off booleans in get_value
git accepts yes/no and on/off for a boolean as well as true/false (git_parse_maybe_bool_text in parse.c), and ConfigParser.getboolean on this class already accepted all of them. _string_to_value handled only true/false, under a comment claiming to "try boolean values as git uses them", so the two accessors disagreed about the same file: value get_value() getboolean() git config --type=bool yes 'yes' True true no 'no' False false on 'on' True true off 'off' False false Returning them as strings was worse than merely inexact. "no" and "off" are non-empty, so a caller testing the result of get_value got True for a value git reads as false — the inversion is silent, since nothing raises. _string_to_value now recognises the same spellings getboolean does. A value that is not a boolean is untouched, so "meld" is still returned as a string, and numeric values keep their existing behavior. Not changed here: get_value also diverges on numeric bases and suffixes ("0x10" and "1k" come back as strings, "010" as 10 where git reads octal 8). Those change the value rather than its type and are worth their own commit. Validation: test_get_value_reads_git_boolean_spellings covers the ten spellings and asserts the two accessors agree; it fails on the previous revision. test/test_config.py passes (40 passed, 2 skipped), ruff check and ruff format are clean. test/test_repo.py errors on this clone because init-tests-after-clone.sh has not been run, unchanged by this commit.
1 parent 41c3954 commit 5fd795a

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

git/config.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -956,11 +956,16 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]:
956956
continue
957957
# END for each numeric type
958958

959-
# Try boolean values as git uses them.
959+
# Try boolean values as git uses them. git accepts yes/no and on/off as
960+
# well as true/false (git_parse_maybe_bool_text in parse.c), and so does
961+
# ConfigParser.getboolean on this class, so only get_value lagged behind.
962+
# Leaving them as strings was worse than merely inexact: "no" and "off"
963+
# are non-empty, so a caller testing the result got True for a value git
964+
# reads as false.
960965
vl = valuestr.lower()
961-
if vl == "false":
966+
if vl in ("false", "no", "off"):
962967
return False
963-
if vl == "true":
968+
if vl in ("true", "yes", "on"):
964969
return True
965970

966971
if not isinstance(valuestr, str):

test/test_config.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,38 @@ def test_backslash_line_continuation(self):
167167
key = "co" if section == "alias" else "k"
168168
self.assertEqual(config.get_value(section, key), expected)
169169

170+
def test_get_value_reads_git_boolean_spellings(self):
171+
# git accepts yes/no and on/off as well as true/false, and getboolean on
172+
# this class already did. get_value returned them as strings, so "no" and
173+
# "off" arrived as non-empty (truthy) values for a caller testing them.
174+
cases = [
175+
(b"true", True),
176+
(b"TRUE", True),
177+
(b"yes", True),
178+
(b"Yes", True),
179+
(b"on", True),
180+
(b"On", True),
181+
(b"false", False),
182+
(b"no", False),
183+
(b"off", False),
184+
(b"Off", False),
185+
]
186+
for raw, expected in cases:
187+
config_file = io.BytesIO(b"[core]\n\tflag = " + raw + b"\n")
188+
config_file.name = "boolean_spellings.config"
189+
config = GitConfigParser(config_file)
190+
config.read()
191+
self.assertIs(config.get_value("core", "flag"), expected, raw.decode())
192+
# The two accessors must not disagree about the same value.
193+
self.assertIs(config.getboolean("core", "flag"), expected, raw.decode())
194+
195+
# A value that is not a boolean at all still comes back untouched.
196+
config_file = io.BytesIO(b"[core]\n\tflag = meld\n")
197+
config_file.name = "boolean_spellings.config"
198+
config = GitConfigParser(config_file)
199+
config.read()
200+
self.assertEqual(config.get_value("core", "flag"), "meld")
201+
170202
@with_rw_directory
171203
def test_comment_backslash_does_not_continue_value(self, rw_dir):
172204
config_path = osp.join(rw_dir, "config")

0 commit comments

Comments
 (0)