Browse Source

[git_cl] Add missing_ok to SetConfig

Split from https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5648617

Change-Id: Id33b509937c4eb9a95f5f603593a79a1c5a3c109
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/5651982
Reviewed-by: Robbie Iannucci <iannucci@chromium.org>
Reviewed-by: Yiwei Zhang <yiwzhang@google.com>
Commit-Queue: Allen Li <ayatane@chromium.org>
Robert Iannucci 1 year ago
parent
commit
821e028ffd
1 changed files with 14 additions and 4 deletions
  1. 14 4
      scm.py

+ 14 - 4
scm.py

@@ -160,7 +160,7 @@ class GIT(object):
         return values[-1]
         return values[-1]
 
 
     @staticmethod
     @staticmethod
-    def GetConfigBool(cwd, key):
+    def GetConfigBool(cwd, key) -> bool:
         return GIT.GetConfig(cwd, key) == 'true'
         return GIT.GetConfig(cwd, key) == 'true'
 
 
     @staticmethod
     @staticmethod
@@ -189,11 +189,12 @@ class GIT(object):
                   *,
                   *,
                   value_pattern=None,
                   value_pattern=None,
                   modify_all=False,
                   modify_all=False,
-                  scope='local'):
+                  scope='local',
+                  missing_ok=True):
         """Sets or unsets one or more config values.
         """Sets or unsets one or more config values.
 
 
         Args:
         Args:
-            cwd: path to fetch `git config` for.
+            cwd: path to set `git config` for.
             key: The specific config key to affect.
             key: The specific config key to affect.
             value: The value to set. If this is None, `key` will be unset.
             value: The value to set. If this is None, `key` will be unset.
             value_pattern: For use with `modify_all=True`, allows
             value_pattern: For use with `modify_all=True`, allows
@@ -206,6 +207,10 @@ class GIT(object):
             scope: By default this is the local scope, but could be `system`,
             scope: By default this is the local scope, but could be `system`,
                 `global`, or `worktree`, depending on which config scope you
                 `global`, or `worktree`, depending on which config scope you
                 want to affect.
                 want to affect.
+            missing_ok: If `value` is None (i.e. this is an unset operation),
+                ignore retcode=5 from `git config` (meaning that the value is
+                not present). If `value` is not None, then this option has no
+                effect.
         """
         """
         GIT._clear_config(cwd)
         GIT._clear_config(cwd)
 
 
@@ -220,7 +225,12 @@ class GIT(object):
 
 
         if modify_all and value_pattern:
         if modify_all and value_pattern:
             args.append(value_pattern)
             args.append(value_pattern)
-        GIT.Capture(args, cwd=cwd)
+
+        accepted_retcodes = [0]
+        if value is None and missing_ok:
+            accepted_retcodes = [0, 5]
+
+        GIT.Capture(args, cwd=cwd, accepted_retcodes=accepted_retcodes)
 
 
     @staticmethod
     @staticmethod
     def SetBranchConfig(cwd, branch, key, value=None):
     def SetBranchConfig(cwd, branch, key, value=None):