[PATCH v3 0/8] parseopt: add subcommand autocorrection
- From
- Jiamu Sun <39@barroit.sh>
- Date
- Mar 10, 2026, 11:40 UTC
- Message-ID
- <SY0P300MB0801C6F21C2D8F49892DF8E7CE46A@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
- In-Reply-To
- <SY0P300MB08013E35DCA8FC31B0662125CE78A@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
Git currently provides auto-correction for builtins and aliases, but lacks this functionality for subcommands parsed via the parse-options API. Subcommands are also commands, and typos will occur, too. Like:
git remote add-rul
So, this series introduces subcommand auto-correction.
By default, this implementation enables autocorrection for builtins with mandatory subcommands. However, for those using PARSE_OPT_SUBCOMMAND_OPTIONAL, autocorrection is skipped to avoid misinterpreting legitimate unknown arguments as mistyped subcommands.
To allow builtins with optional subcommands to explicitly opt in, this series adds the PARSE_OPT_SUBCOMMAND_AUTOCORR flag. This flag is subsequently enabled for git-remote and git-notes.
Additionally, the existing autocorrection logic is extracted from help.c so subcommand handling can reuse the same config parsing and prompt/delay logic.
Some split string literals are also combined so the full text is easier to grep for.
Changes in v3: - Align with the coding guildline
Changes in v2: - Reword the explanation of default autocorrection behavior
Jiamu Sun (8):
parseopt: extract subcommand handling from parse_options_step()
help: make autocorrect handling reusable
help: move tty check for autocorrection to autocorrect.c
autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTONLY
autocorrect: provide config resolution API
parseopt: autocorrect mistyped subcommands
parseopt: enable subcommand autocorrection for git-remote and
git-notes
help: add tests for subcommand autocorrectionMakefile | 1 + autocorrect.c | 89 +++++++++++++++++ autocorrect.h | 21 ++++ builtin/notes.c | 10 +- builtin/remote.c | 12 +-- help.c | 104 ++++---------------- parse-options.c | 156 ++++++++++++++++++++++-------- parse-options.h | 1 + t/t9004-autocorrect-subcommand.sh | 51 ++++++++++ 9 files changed, 305 insertions(+), 140 deletions(-) create mode 100644 autocorrect.c create mode 100644 autocorrect.h create mode 100755 t/t9004-autocorrect-subcommand.sh
base-commit: 795c338de725e13bd361214c6b768019fc45a2c1
-- 2.53.0