From 5fd795a4981d02c1cd9050e41793b3dc64ff2438 Mon Sep 17 00:00:00 2001 From: Roshan Ramani Date: Mon, 7 Sep 2026 12:10:23 +0530 Subject: [PATCH 1/2] Read git's yes/no and on/off booleans in get_value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- git/config.py | 11 ++++++++--- test/test_config.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/git/config.py b/git/config.py index aef881d2e..bb7fda19f 100644 --- a/git/config.py +++ b/git/config.py @@ -956,11 +956,16 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: continue # END for each numeric type - # Try boolean values as git uses them. + # Try boolean values as git uses them. git accepts yes/no and on/off as + # well as true/false (git_parse_maybe_bool_text in parse.c), and so does + # ConfigParser.getboolean on this class, so only get_value lagged behind. + # Leaving them as strings was worse than merely inexact: "no" and "off" + # are non-empty, so a caller testing the result got True for a value git + # reads as false. vl = valuestr.lower() - if vl == "false": + if vl in ("false", "no", "off"): return False - if vl == "true": + if vl in ("true", "yes", "on"): return True if not isinstance(valuestr, str): diff --git a/test/test_config.py b/test/test_config.py index 3107d8074..3031721de 100644 --- a/test/test_config.py +++ b/test/test_config.py @@ -167,6 +167,38 @@ def test_backslash_line_continuation(self): key = "co" if section == "alias" else "k" self.assertEqual(config.get_value(section, key), expected) + def test_get_value_reads_git_boolean_spellings(self): + # git accepts yes/no and on/off as well as true/false, and getboolean on + # this class already did. get_value returned them as strings, so "no" and + # "off" arrived as non-empty (truthy) values for a caller testing them. + cases = [ + (b"true", True), + (b"TRUE", True), + (b"yes", True), + (b"Yes", True), + (b"on", True), + (b"On", True), + (b"false", False), + (b"no", False), + (b"off", False), + (b"Off", False), + ] + for raw, expected in cases: + config_file = io.BytesIO(b"[core]\n\tflag = " + raw + b"\n") + config_file.name = "boolean_spellings.config" + config = GitConfigParser(config_file) + config.read() + self.assertIs(config.get_value("core", "flag"), expected, raw.decode()) + # The two accessors must not disagree about the same value. + self.assertIs(config.getboolean("core", "flag"), expected, raw.decode()) + + # A value that is not a boolean at all still comes back untouched. + config_file = io.BytesIO(b"[core]\n\tflag = meld\n") + config_file.name = "boolean_spellings.config" + config = GitConfigParser(config_file) + config.read() + self.assertEqual(config.get_value("core", "flag"), "meld") + @with_rw_directory def test_comment_backslash_does_not_continue_value(self, rw_dir): config_path = osp.join(rw_dir, "config") From 297cd26412e63c1f94c3ef8bdcf27c54956328ab Mon Sep 17 00:00:00 2001 From: Byron Date: Mon, 7 Sep 2026 11:26:45 +0200 Subject: [PATCH 2/2] review --- git/config.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/git/config.py b/git/config.py index bb7fda19f..0da90bab2 100644 --- a/git/config.py +++ b/git/config.py @@ -10,36 +10,34 @@ import abc import configparser as cp import fnmatch -from functools import wraps import inspect -from io import BufferedReader, IOBase import logging import os import os.path as osp import re import sys - -from git.compat import defenc, force_text -from git.util import LockFile +from functools import wraps +from io import BufferedReader, IOBase # typing------------------------------------------------------- - from typing import ( + IO, + TYPE_CHECKING, Any, Callable, + Dict, Generic, - IO, List, - Dict, Sequence, - TYPE_CHECKING, Tuple, TypeVar, Union, cast, ) -from git.types import Lit_config_levels, ConfigLevels_Tup, PathLike, assert_never, _T +from git.compat import defenc, force_text +from git.types import _T, ConfigLevels_Tup, Lit_config_levels, PathLike, assert_never +from git.util import LockFile if TYPE_CHECKING: from io import BytesIO @@ -956,12 +954,7 @@ def _string_to_value(self, valuestr: str) -> Union[int, float, str, bool]: continue # END for each numeric type - # Try boolean values as git uses them. git accepts yes/no and on/off as - # well as true/false (git_parse_maybe_bool_text in parse.c), and so does - # ConfigParser.getboolean on this class, so only get_value lagged behind. - # Leaving them as strings was worse than merely inexact: "no" and "off" - # are non-empty, so a caller testing the result got True for a value git - # reads as false. + # Try boolean values as git uses them. vl = valuestr.lower() if vl in ("false", "no", "off"): return False