[Outreachy PATCH] annotate: pass the arguments to blame unchanged
- From
- Tanga Joris <tangajoris@gmail.com>
- Date
- Nov 5, 2026, 07:31 UTC
- Message-ID
- <20261105073101.1449292-1-tangajoris@gmail.com>
cmd_annotate() puts "-c" in front of the user's arguments before calling cmd_blame(), so "--git-completion-helper" is no longer the lone argument that parse-options looks for. It falls through to the diff option parser, which answers it. So "git annotate --<TAB>" offers the options of "git diff" instead of annotate's own. There is no --porcelain, while "git blame --<TAB>" offers it.
The "-c" is not needed: cmd_blame() already checks whether argv[0] is "annotate" and sets OUTPUT_ANNOTATE_COMPAT itself. It has done that since 7ceacdffc5 ("blame -c" should be compatible with "annotate", 2008-09-05). argv[0] is always "annotate" here. git.c is the only caller of cmd_annotate(), and it strips "git-" (and ".exe" on Windows) from the command name first.
Call cmd_blame() with the arguments as they are, and remove the strvec and its copy. 9a48fc1da2 (builtin/annotate: fix leaking args vector, 2024-09-30) added the copy, because cmd_blame() changes the array it is given. It is not needed anymore. Since 1dd7c32daa (git: refactor builtin handling to use a `struct strvec`, 2024-11-20), handle_builtin() already gives every builtin its own copy of argv.
Apart from the completion helpers, "git annotate" gives the same output and exit code as before.
Signed-off-by: Tanga Joris <tangajoris@gmail.com> --- I found this when I tried teaching git.c to set up gently for --git-completion-helper, like it does for -h and --help-all. With that change alone, "git annotate --git-completion-helper" segfaults outside a repository. I plan to send that change after tj/drop-no-parseopt is in 'next', because it also needs that topic.
To check that the output does not change, I ran "git annotate" with and without this patch, with 45 sets of arguments, and compared stdout, stderr and the exit code. The arguments cover -c, -l, -t, -e, -p, --porcelain, --incremental, -L, -M, -C and others, plus error cases. Only the two completion helpers differ. t8001 to t8015 pass, and t8001, t8003 and t8013 also pass with SANITIZE=leak.
The test is in t8003 and not t8001, because t8001 skips everything without Perl.
annotate is the only builtin I found that changes its arguments before they are parsed. describe, merge, reflog and cat-file also call other commands, but they parse their own options first.
builtin/annotate.c | 22 +--------------------- t/t8003-blame-corner-cases.sh | 10 ++++++++++ 2 files changed, 11 insertions(+), 21 deletions(-)
diff --git a/builtin/annotate.c b/builtin/annotate.c index 7f754f2309..c1f258993d 100644 --- a/builtin/annotate.c +++ b/builtin/annotate.c @@ -6,31 +6,11 @@ #include "git-compat-util.h" #include "builtin.h" -#include "strvec.h" int cmd_annotate(int argc, const char **argv, const char *prefix, struct repository *repo) { - struct strvec args = STRVEC_INIT; - const char **args_copy; - int ret; - - strvec_pushl(&args, "annotate", "-c", NULL); - for (int i = 1; i < argc; i++) - strvec_push(&args, argv[i]); - - /* - * `cmd_blame()` ends up modifying the array, which causes memory leaks - * if we didn't copy the array here. - */ - CALLOC_ARRAY(args_copy, args.nr + 1); - COPY_ARRAY(args_copy, args.v, args.nr); - - ret = cmd_blame(args.nr, args_copy, prefix, repo); - - strvec_clear(&args); - free(args_copy); - return ret; + return cmd_blame(argc, argv, prefix, repo); } diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh index 30e7960ace..6824f33332 100755 --- a/t/t8003-blame-corner-cases.sh +++ b/t/t8003-blame-corner-cases.sh @@ -303,4 +303,14 @@ test_expect_success 'blame does not coalesce non-adjacent result lines' ' test_cmp expect actual ' +for opt in --git-completion-helper --git-completion-helper-all +do + test_expect_success "annotate $opt offers the same options as blame" ' + git blame $opt >expect && + git annotate $opt >actual && + test_grep -e "--porcelain" actual && + test_cmp expect actual + ' +done + test_done base-commit: 0ffa3e35c7c82ab8d0de226149fb03b3f7674d05
-- 2.43.0