Currently, stgit has three "boolean" configuration values which we
support, "autoimerge", "keepoptimized" and "refreshsubmodules". However,
we only support "yes" and non-"yes" as valid values. (i.e. either the
value is "yes" and it is deemed true, or it is not "yes").
The standard for git config is to use the "--bool" parameter, which
interprets configuration values as boolean literals:
"yes", "on", "true" and any positive number are true
"no", "off", "false", zero, and the empty string are false
Additionally, a key defined without "= <value>" is taken as true.
Since this is the standard method for git, add a new getbool function
for the config module, which will parse values in this way.
We'll now use this getbool() method for each of these values, ensuring
that these boolean configurations are treated similar to proper git
configuration.
Note that we do not use get() directly in implementing getbool(). This
is because we need to distinguish the case of a defined key with an
undefined value to be True. Since get() converts KeyError exceptions
into None, we would not be able to distinguish the cases of an undefined
key (which should be False) with a key that has an undefined value
(which should be True).
I thought to add new tests for these, but since we don't directly expose
the config.py module, and I don't see an easy way to add tests for
python code explicitly
stgit/commands/pull.py | 2 +-
stgit/commands/refresh.py | 2 +-
stgit/config.py | 24 ++++++++++++++++++++++++
stgit/git.py | 2 +-
stgit/lib/transaction.py | 2 +-
5 files changed, 28 insertions(+), 4 deletions(-)
diff --git a/stgit/commands/pull.py b/stgit/commands/pull.py
index 75a883444577..ed268eeb998b 100644
--- a/stgit/commands/pull.py
+++ b/stgit/commands/pull.py
@@ -114,7 +114,7 @@ def func(parser, options, args):
post_rebase(crt_series, applied, options.nopush, options.merged)
# maybe tidy up
- if config.get('stgit.keepoptimized') == 'yes':
+ if config.getbool('stgit.keepoptimized'):
git.repack()
print_crt_patch(crt_series)
diff --git a/stgit/commands/refresh.py b/stgit/commands/refresh.py
index 631167cf5445..b780ba397460 100644
--- a/stgit/commands/refresh.py
+++ b/stgit/commands/refresh.py
@@ -270,7 +270,7 @@ def func(parser, options, args):
# If submodules was not specified on the command line, infer a default
# from configuration.
if options.submodules is None:
- options.submodules = (config.get('stgit.refreshsubmodules') == 'yes')
+ options.submodules = (config.getbool('stgit.refreshsubmodules'))
stack = directory.repository.current_stack
patch_name = get_patch(stack, options.patch)
diff --git a/stgit/config.py b/stgit/config.py
index b9df20a8c635..8ac53d64558b 100644
--- a/stgit/config.py
+++ b/stgit/config.py
@@ -91,6 +91,30 @@ class GitConfig(object):
raise GitConfigException('Value for "%s" is not an integer: "%s"' %
(name, value))
+ def getbool(self, name):
+ """Report the canonicalized boolean value for a given key."""
+ # We cannot directly call get() because we need to use the KeyError in
+ # order to distinguish between the case of a key with an undefined
+ # value, and a completely undefined key. Git expects the former to be
+ # reported as "true".
+ self.load()
+ try:
+ value = self.__cache[name][-1]
+ except KeyError:
+ return None
+ if value is None:
+ # The key is defined, but the value is not, so treat it as true.
+ return True
+ elif value in ['yes', 'on', 'true']:
+ return True
+ elif value in ['no', 'off', 'false', '']:
+ return False
+ elif value.isdigit():
+ return bool(value)
+ else:
+ raise GitConfigException('Value for "%s" is not a booleain: "%s"' %
+ (name, value))
+
def getstartswith(self, name):
self.load()
return ((n, v[-1]) for (n, v) in self.__cache.items()
diff --git a/stgit/git.py b/stgit/git.py
index 826e81f399fb..92a26fc08409 100644
--- a/stgit/git.py
+++ b/stgit/git.py
@@ -607,7 +607,7 @@ def merge_recursive(base, head1, head2):
output = p.output_lines()
if p.exitcode:
# There were conflicts
- if config.get('stgit.autoimerge') == 'yes':
+ if config.getbool('stgit.autoimerge'):
mergetool()
else:
conflicts = [l for l in output if l.startswith('CONFLICT')]
diff --git a/stgit/lib/transaction.py b/stgit/lib/transaction.py
index a19796cb5859..1afa31782071 100644
--- a/stgit/lib/transaction.py
+++ b/stgit/lib/transaction.py
@@ -362,7 +362,7 @@ class StackTransaction(object):
self.__halt('Index/worktree dirty')
try:
interactive = (allow_interactive and
- config.get('stgit.autoimerge') == 'yes')
+ config.getbool('stgit.autoimerge'))
iw.merge(base, ours, theirs, interactive = interactive)
tree = iw.index.write_tree()
self.__current_tree = tree
--
2.15.0