summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2025-09-15 10:23:20 -0400
committerLUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com>2025-09-17 12:54:30 -0700
commitd30414bb5331d91784c536d30e22c2ccb8126b7a (patch)
tree03b21e8932c7510ae11fb9b27137108f51e585ce
parent80d1a5ad3ec862c64a3bbe9919d4547340950183 (diff)
downloadgit-repo-d30414bb5331d91784c536d30e22c2ccb8126b7a.tar.gz
forall: fix crash with no command
When callback= is used, optparse does not automatically initialize The destination when a dest= is not specified. Refine the test to allow dest= options when callback= is used even when it seems like it is otherwise redundant. Bug: b/436611422 Change-Id: I5185f95cb857ca6d37357cac77fb117a83db9c0c Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/509861 Tested-by: Mike Frysinger <vapier@google.com> Commit-Queue: Mike Frysinger <vapier@google.com> Reviewed-by: Gavin Mak <gavinmak@google.com>
-rw-r--r--subcmds/forall.py3
-rw-r--r--tests/test_subcmds.py7
2 files changed, 8 insertions, 2 deletions
diff --git a/subcmds/forall.py b/subcmds/forall.py
index 4bae46af..57a41fc1 100644
--- a/subcmds/forall.py
+++ b/subcmds/forall.py
@@ -133,7 +133,7 @@ without iterating through the remaining projects.
133 133
134 @staticmethod 134 @staticmethod
135 def _cmd_option(option, _opt_str, _value, parser): 135 def _cmd_option(option, _opt_str, _value, parser):
136 setattr(parser.values, option.dest or "command", list(parser.rargs)) 136 setattr(parser.values, option.dest, list(parser.rargs))
137 while parser.rargs: 137 while parser.rargs:
138 del parser.rargs[0] 138 del parser.rargs[0]
139 139
@@ -161,6 +161,7 @@ without iterating through the remaining projects.
161 p.add_option( 161 p.add_option(
162 "-c", 162 "-c",
163 "--command", 163 "--command",
164 dest="command",
164 help="command (and arguments) to execute", 165 help="command (and arguments) to execute",
165 action="callback", 166 action="callback",
166 callback=self._cmd_option, 167 callback=self._cmd_option,
diff --git a/tests/test_subcmds.py b/tests/test_subcmds.py
index 2d680fb7..0683f1dd 100644
--- a/tests/test_subcmds.py
+++ b/tests/test_subcmds.py
@@ -94,7 +94,12 @@ class AllCommands(unittest.TestCase):
94 """Block redundant dest= arguments.""" 94 """Block redundant dest= arguments."""
95 95
96 def _check_dest(opt): 96 def _check_dest(opt):
97 if opt.dest is None or not opt._long_opts: 97 """Check the dest= setting."""
98 # If the destination is not set, nothing to check.
99 # If long options are not set, then there's no implicit destination.
100 # If callback is used, then a destination might be needed because
101 # optparse cannot assume a value is always stored.
102 if opt.dest is None or not opt._long_opts or opt.callback:
98 return 103 return
99 104
100 long = opt._long_opts[0] 105 long = opt._long_opts[0]