]> git.ipfire.org Git - thirdparty/git.git/commitdiff
parseopt: enable subcommand autocorrection for git-remote and git-notes
authorJiamu Sun <39@barroit.sh>
Thu, 23 Apr 2026 01:37:58 +0000 (10:37 +0900)
committerJunio C Hamano <gitster@pobox.com>
Thu, 23 Apr 2026 02:02:28 +0000 (11:02 +0900)
Add PARSE_OPT_SUBCOMMAND_AUTOCORRECT to enable autocorrection for
subcommands parsed with PARSE_OPT_SUBCOMMAND_OPTIONAL.

Apply this to git-remote and git-notes, so mistyped subcommands can be
automatically corrected, and builtin entry points no longer need to
handle the unknown subcommand error path themselves.

This is safe. Both builtins either resolve to a single subcommand or
take no subcommand at all, meaning any unknown argument encountered by
the parser must be a mistyped subcommand.

Signed-off-by: Jiamu Sun <39@barroit.sh>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/notes.c
builtin/remote.c
parse-options.c
parse-options.h

index 9af602bdd7b402a32c92b8d24c232ed22d09f99f..f9bf350df4e8885ae71c99a71bf79d320c8581f0 100644 (file)
@@ -1149,14 +1149,10 @@ int cmd_notes(int argc,
 
        repo_config(the_repository, git_default_config, NULL);
        argc = parse_options(argc, argv, prefix, options, git_notes_usage,
-                            PARSE_OPT_SUBCOMMAND_OPTIONAL);
-       if (!fn) {
-               if (argc) {
-                       error(_("unknown subcommand: `%s'"), argv[0]);
-                       usage_with_options(git_notes_usage, options);
-               }
+                            PARSE_OPT_SUBCOMMAND_OPTIONAL |
+                            PARSE_OPT_SUBCOMMAND_AUTOCORRECT);
+       if (!fn)
                fn = list;
-       }
 
        if (override_notes_ref) {
                struct strbuf sb = STRBUF_INIT;
index 0fddaa177331f649be5564536244124ceadce66c..51fe99e0803ebc946b9fa9e9a930f4aef94658cd 100644 (file)
@@ -1953,15 +1953,11 @@ int cmd_remote(int argc,
        };
 
        argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
-                            PARSE_OPT_SUBCOMMAND_OPTIONAL);
+                            PARSE_OPT_SUBCOMMAND_OPTIONAL |
+                            PARSE_OPT_SUBCOMMAND_AUTOCORRECT);
 
-       if (fn) {
+       if (fn)
                return !!fn(argc, argv, prefix, repo);
-       } else {
-               if (argc) {
-                       error(_("unknown subcommand: `%s'"), argv[0]);
-                       usage_with_options(builtin_remote_usage, options);
-               }
+       else
                return !!show_all();
-       }
 }
index faf357b729c4ec1e2e76b61d1b63103b59779a98..a1258134df72c7d02af00153bd2847f7d633265d 100644 (file)
@@ -719,14 +719,16 @@ static enum parse_opt_result handle_subcommand(struct parse_opt_ctx_t *ctx,
        if (!err)
                return PARSE_OPT_SUBCOMMAND;
 
-       /*
-        * arg is neither a short or long option nor a subcommand.  Since this
-        * command has a default operation mode, we have to treat this arg and
-        * all remaining args as args meant to that default operation mode.
-        * So we are done parsing.
-        */
-       if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
+       if (ctx->flags & PARSE_OPT_SUBCOMMAND_OPTIONAL &&
+           !(ctx->flags & PARSE_OPT_SUBCOMMAND_AUTOCORRECT)) {
+               /*
+                * arg is neither a short or long option nor a subcommand.
+                * Since this command has a default operation mode, we have to
+                * treat this arg and all remaining args as args meant to that
+                * default operation mode.  So we are done parsing.
+                */
                return PARSE_OPT_DONE;
+       }
 
        find_subcommands(&cmds, options);
        assumed = autocorrect_subcommand(arg, &cmds);
index 706de9729f6b3f00b41f21251ca7fe6c2c982d79..e5fd4da4055b4a59ea1554b201035cd22363fb85 100644 (file)
@@ -40,6 +40,7 @@ enum parse_opt_flags {
        PARSE_OPT_ONE_SHOT = 1 << 5,
        PARSE_OPT_SHELL_EVAL = 1 << 6,
        PARSE_OPT_SUBCOMMAND_OPTIONAL = 1 << 7,
+       PARSE_OPT_SUBCOMMAND_AUTOCORRECT = 1 << 8,
 };
 
 enum parse_opt_option_flags {