]> arthur.barton.de Git - bup.git/commitdiff
git: allow config retrieval as bool or int
authorJohannes Berg <johannes@sipsolutions.net>
Wed, 8 Jan 2020 21:48:25 +0000 (22:48 +0100)
committerRob Browning <rlb@defaultvalue.org>
Sun, 13 Jun 2021 16:32:17 +0000 (11:32 -0500)
Allow retrieving configuration values as bool or int,
letting 'git config' normalize all the values instead
of trying to do it ourselves in bup.

Also use git config --null to avoid having to worry about
any whitespace issues, although .strip() on the output
would probably work just as well. Previously, the result
would include a terminating newline written out by the
git config command, which is not desirable.

Signed-off-by: Johannes Berg <johannes@sipsolutions.net>
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/git.py

index 61238bd250f42c72cef19914286a0d4e9ed3d7a6..f6ae9e3765472f0e3e85c0764c72c384b49c9ff0 100644 (file)
@@ -66,14 +66,29 @@ def _git_exo(cmd, **kwargs):
         raise GitError('%r returned %d' % (cmd, proc.returncode))
     return result
 
-def git_config_get(option, repo_dir=None):
-    cmd = (b'git', b'config', b'--get', option)
-    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
-                         env=_gitenv(repo_dir=repo_dir),
+def git_config_get(option, repo_dir=None, opttype=None):
+    cmd = [b'git', b'config', b'--null']
+    if opttype == 'int':
+        cmd.extend([b'--int'])
+    elif opttype == 'bool':
+        cmd.extend([b'--bool'])
+    else:
+        assert opttype is None
+    cmd.extend([b'--get', option])
+    env=None
+    if repo_dir:
+        env = _gitenv(repo_dir=repo_dir)
+    p = subprocess.Popen(cmd, stdout=subprocess.PIPE, env=env,
                          close_fds=True)
-    r = p.stdout.read()
+    # with --null, git writes out a trailing \0 after the value
+    r = p.stdout.read()[:-1]
     rc = p.wait()
     if rc == 0:
+        if opttype == 'int':
+            return int(r)
+        elif opttype == 'bool':
+            # git converts to 'true' or 'false'
+            return r == b'true'
         return r
     if rc != 1:
         raise GitError('%r returned %d' % (cmd, rc))