From ee68c316a779f1098069e9d8d7bf17d194f4aa3e Mon Sep 17 00:00:00 2001 From: Tanga Joris Date: Fri, 6 Nov 2026 01:34:56 +0300 Subject: [PATCH 1/3] parse-options: answer --git-completion-helper before command runs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fast-import parses its options only after reading the "feature" lines from its input, and it creates a temporary pack file before that. So "git fast-import --git-completion-helper" waits for input, and each run leaves a tmp_pack_* file behind. bash completion runs the helper with the terminal as input. The next commit removes the NO_PARSEOPT flag from fast-import, which would make "git fast-import --" freeze without this patch. show_usage_with_options_if_asked() already answers -h and --help-all before a command does any work. Teach it to answer the completion helper too. Then fast-import, and the other commands that call it, reply without reading input. Reported-by: René Scharfe Helped-by: Junio C Hamano Helped-by: Kaartic Sivaraam Helped-by: Cynthia Uche Signed-off-by: Tanga Joris Signed-off-by: Junio C Hamano --- parse-options.c | 6 ++++++ parse-options.h | 6 ++++++ t/t9300-fast-import.sh | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) diff --git a/parse-options.c b/parse-options.c index 283a0f1dc2..f2a23b79a4 100644 --- a/parse-options.c +++ b/parse-options.c @@ -1536,6 +1536,12 @@ void show_usage_with_options_if_asked(int ac, const char **av, usage_with_options_internal(NULL, usagestr, opts, USAGE_FULL, USAGE_TO_STDOUT); exit(0); + } else if (!strcmp(av[1], "--git-completion-helper")) { + show_gitcomp(opts, 0); + exit(0); + } else if (!strcmp(av[1], "--git-completion-helper-all")) { + show_gitcomp(opts, 1); + exit(0); } } } diff --git a/parse-options.h b/parse-options.h index f68b51ceca..775a028f10 100644 --- a/parse-options.h +++ b/parse-options.h @@ -427,6 +427,12 @@ int parse_options(int argc, const char **argv, const char *prefix, NORETURN void usage_with_options(const char * const *usagestr, const struct option *options); +/* + * Programs that call parse_options() early do not need this function. + * If your program has to answer help requests (-h and --help-all) or + * the completion helper before parse_options() runs, call this + * function to answer them. + */ void show_usage_with_options_if_asked(int ac, const char **av, const char * const *usage, const struct option *options); diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh index d9de2ef0d8..f6935d81d1 100755 --- a/t/t9300-fast-import.sh +++ b/t/t9300-fast-import.sh @@ -2914,6 +2914,22 @@ test_expect_success 'R: blob appears only once' ' test 1 = $n ' +test_expect_success 'R: --git-completion-helper does not read from stdin' ' + find .git/objects/pack -name "tmp_pack_*" >before && + echo garbage | git fast-import --git-completion-helper >out && + test_grep -e "--date-format=" out && + find .git/objects/pack -name "tmp_pack_*" >after && + test_cmp before after +' + +test_expect_success 'R: --git-completion-helper-all does not read from stdin' ' + find .git/objects/pack -name "tmp_pack_*" >before && + echo garbage | git fast-import --git-completion-helper-all >all && + test_grep -e "--allow-unsafe-features" all && + find .git/objects/pack -name "tmp_pack_*" >after && + test_cmp before after +' + ### ### series S (mark and path parsing) ### From 5d735e50ef3ae9a83a9cd66f1c21f895a4d7165d Mon Sep 17 00:00:00 2001 From: Tanga Joris Date: Fri, 6 Nov 2026 01:34:57 +0300 Subject: [PATCH 2/3] git.c: drop NO_PARSEOPT from builtins that use parse-options The NO_PARSEOPT flag in the commands[] table marks builtins that parse their options by hand. Builtins marked with it are left out of "git --list-cmds=parseopt", which bash completion uses to decide whether it can ask a command for its options with "--git-completion-helper". Four builtins have since been converted to parse-options, but are still marked NO_PARSEOPT: - patch-id, in 2871f4d447 (builtin: patch-id: add --verbatim as a command mode, 2022-10-24) - diff-pairs, which has used parse-options since it was introduced in 5bd10b2adc (builtin: introduce diff-pairs command, 2025-02-28) - fast-import, in 863937696e (fast-import: use parse_options() for command line options, 2026-08-11) - var, in e19f0779c6 (var: add "-z" output mode, 2026-09-26) This causes "git fast-import --", "git patch-id --" and "git diff-pairs --" to offer no options, even though they are listed by "--git-completion-helper". Remove the flag from these four builtins, like 66fa6e8ed8 (git.c: update NO_PARSEOPT markings, 2022-08-19) did for others. Signed-off-by: Tanga Joris Signed-off-by: Junio C Hamano --- git.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/git.c b/git.c index d7b5f88e52..2d27ad4175 100644 --- a/git.c +++ b/git.c @@ -571,11 +571,11 @@ static struct cmd_struct commands[] = { { "diff", cmd_diff, NO_PARSEOPT }, { "diff-files", cmd_diff_files, RUN_SETUP | NEED_WORK_TREE | NO_PARSEOPT }, { "diff-index", cmd_diff_index, RUN_SETUP | NO_PARSEOPT }, - { "diff-pairs", cmd_diff_pairs, RUN_SETUP | NO_PARSEOPT }, + { "diff-pairs", cmd_diff_pairs, RUN_SETUP }, { "diff-tree", cmd_diff_tree, RUN_SETUP | NO_PARSEOPT }, { "difftool", cmd_difftool, RUN_SETUP_GENTLY }, { "fast-export", cmd_fast_export, RUN_SETUP }, - { "fast-import", cmd_fast_import, RUN_SETUP | NO_PARSEOPT }, + { "fast-import", cmd_fast_import, RUN_SETUP }, { "fetch", cmd_fetch, RUN_SETUP }, { "fetch-pack", cmd_fetch_pack, RUN_SETUP | NO_PARSEOPT }, { "fmt-merge-msg", cmd_fmt_merge_msg, RUN_SETUP }, @@ -626,7 +626,7 @@ static struct cmd_struct commands[] = { { "pack-redundant", cmd_pack_redundant, RUN_SETUP | NO_PARSEOPT | DEPRECATED }, #endif { "pack-refs", cmd_pack_refs, RUN_SETUP }, - { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY | NO_PARSEOPT }, + { "patch-id", cmd_patch_id, RUN_SETUP_GENTLY }, { "pickaxe", cmd_blame, RUN_SETUP }, { "prune", cmd_prune, RUN_SETUP }, { "prune-packed", cmd_prune_packed, RUN_SETUP }, @@ -676,7 +676,7 @@ static struct cmd_struct commands[] = { { "upload-archive--writer", cmd_upload_archive_writer, NO_PARSEOPT }, { "upload-pack", cmd_upload_pack }, { "url-parse", cmd_url_parse }, - { "var", cmd_var, RUN_SETUP_GENTLY | NO_PARSEOPT }, + { "var", cmd_var, RUN_SETUP_GENTLY }, { "verify-commit", cmd_verify_commit, RUN_SETUP }, { "verify-pack", cmd_verify_pack }, { "verify-tag", cmd_verify_tag, RUN_SETUP }, From 1db50e0e62b3036b25b6b71360ffbfb6605b5cbc Mon Sep 17 00:00:00 2001 From: Tanga Joris Date: Fri, 6 Nov 2026 01:34:58 +0300 Subject: [PATCH 3/3] t0012: catch builtins that keep NO_PARSEOPT after conversion When a builtin is converted to parse-options, the NO_PARSEOPT flag in git.c has to be removed by hand. No test checks this. The previous commit removes the flag from four builtins where it was left behind. Add a test that runs "git --git-completion-helper" for every builtin left out of "git --list-cmds=parseopt". The test fails if the command prints an option starting with "--". Without the previous commit, it fails for diff-pairs, fast-import and patch-id. The test skips diff, diff-files, diff-index, diff-tree and rev-list, because they print the diff options. It also skips rev-parse, because it prints the argument back. It does not catch var, because var only has short options (like -l) and the helper only lists long options. Suggested-by: Kaartic Sivaraam Signed-off-by: Tanga Joris Signed-off-by: Junio C Hamano --- t/t0012-help.sh | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/t/t0012-help.sh b/t/t0012-help.sh index b0a3fdbbbc..41197c67e2 100755 --- a/t/t0012-help.sh +++ b/t/t0012-help.sh @@ -267,4 +267,29 @@ do ' done parseopt && + tr " " "\n" sorted-parseopt && + sort builtins >sorted-builtins && + comm -23 sorted-builtins sorted-parseopt >no-parseopt +' + +while read builtin +do + case "$builtin" in + # these answer with the diff options + diff|diff-files|diff-index|diff-tree|rev-list) + continue ;; + # rev-parse prints unknown arguments back + rev-parse) + continue ;; + esac + + test_expect_success "$builtin does not answer --git-completion-helper" ' + test_might_fail git $builtin --git-completion-helper \ + output 2>/dev/null && + test_grep ! "^--" output + ' +done