{"thread":{"id":"66779","subject":"[Outreachy PATCH] annotate: pass the arguments to blame unchanged","startedAt":"2026-11-05T07:31:01Z","lastAt":"2026-11-05T19:05:18Z","messageCount":2,"participants":["Tanga Joris","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"557530","messageId":"20261105073101.1449292-1-tangajoris@gmail.com","threadId":"66779","inReplyTo":null,"subject":"[Outreachy PATCH] annotate: pass the arguments to blame unchanged","fromName":"Tanga Joris","fromEmail":"tangajoris@gmail.com","sentAt":"2026-11-05T07:31:01Z","receivedAt":"2026-11-05T07:31:01Z","isPatch":true,"sender":{"key":"tangajoris@gmail.com","avatar":null},"body":"cmd_annotate() puts \"-c\" in front of the user's arguments before\ncalling cmd_blame(), so \"--git-completion-helper\" is no longer the\nlone argument that parse-options looks for. It falls through to the\ndiff option parser, which answers it. So \"git annotate --<TAB>\"\noffers the options of \"git diff\" instead of annotate's own. There is\nno --porcelain, while \"git blame --<TAB>\" offers it.\n\nThe \"-c\" is not needed: cmd_blame() already checks whether argv[0]\nis \"annotate\" and sets OUTPUT_ANNOTATE_COMPAT itself. It has done\nthat since 7ceacdffc5 (\"blame -c\" should be compatible with\n\"annotate\", 2008-09-05). argv[0] is always \"annotate\" here. git.c is\nthe only caller of cmd_annotate(), and it strips \"git-\" (and \".exe\"\non Windows) from the command name first.\n\nCall cmd_blame() with the arguments as they are, and remove the\nstrvec and its copy. 9a48fc1da2 (builtin/annotate: fix leaking args\nvector, 2024-09-30) added the copy, because cmd_blame() changes the\narray it is given. It is not needed anymore. Since 1dd7c32daa (git:\nrefactor builtin handling to use a `struct strvec`, 2024-11-20),\nhandle_builtin() already gives every builtin its own copy of argv.\n\nApart from the completion helpers, \"git annotate\" gives the same\noutput and exit code as before.\n\nSigned-off-by: Tanga Joris <tangajoris@gmail.com>\n---\nI found this when I tried teaching git.c to set up gently for\n--git-completion-helper, like it does for -h and --help-all. With that\nchange alone, \"git annotate --git-completion-helper\" segfaults outside\na repository. I plan to send that change after tj/drop-no-parseopt is\nin 'next', because it also needs that topic.\n\nTo check that the output does not change, I ran \"git annotate\" with\nand without this patch, with 45 sets of arguments, and compared\nstdout, stderr and the exit code. The arguments cover -c, -l, -t, -e,\n-p, --porcelain, --incremental, -L, -M, -C and others, plus error\ncases. Only the two completion helpers differ. t8001 to t8015 pass,\nand t8001, t8003 and t8013 also pass with SANITIZE=leak.\n\nThe test is in t8003 and not t8001, because t8001 skips everything\nwithout Perl.\n\nannotate is the only builtin I found that changes its arguments before\nthey are parsed. describe, merge, reflog and cat-file also call other\ncommands, but they parse their own options first.\n\n builtin/annotate.c            | 22 +---------------------\n t/t8003-blame-corner-cases.sh | 10 ++++++++++\n 2 files changed, 11 insertions(+), 21 deletions(-)\n\ndiff --git a/builtin/annotate.c b/builtin/annotate.c\nindex 7f754f2309..c1f258993d 100644\n--- a/builtin/annotate.c\n+++ b/builtin/annotate.c\n@@ -6,31 +6,11 @@\n \n #include \"git-compat-util.h\"\n #include \"builtin.h\"\n-#include \"strvec.h\"\n \n int cmd_annotate(int argc,\n \t\t const char **argv,\n \t\t const char *prefix,\n \t\t struct repository *repo)\n {\n-\tstruct strvec args = STRVEC_INIT;\n-\tconst char **args_copy;\n-\tint ret;\n-\n-\tstrvec_pushl(&args, \"annotate\", \"-c\", NULL);\n-\tfor (int i = 1; i < argc; i++)\n-\t\tstrvec_push(&args, argv[i]);\n-\n-\t/*\n-\t * `cmd_blame()` ends up modifying the array, which causes memory leaks\n-\t * if we didn't copy the array here.\n-\t */\n-\tCALLOC_ARRAY(args_copy, args.nr + 1);\n-\tCOPY_ARRAY(args_copy, args.v, args.nr);\n-\n-\tret = cmd_blame(args.nr, args_copy, prefix, repo);\n-\n-\tstrvec_clear(&args);\n-\tfree(args_copy);\n-\treturn ret;\n+\treturn cmd_blame(argc, argv, prefix, repo);\n }\ndiff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh\nindex 30e7960ace..6824f33332 100755\n--- a/t/t8003-blame-corner-cases.sh\n+++ b/t/t8003-blame-corner-cases.sh\n@@ -303,4 +303,14 @@ test_expect_success 'blame does not coalesce non-adjacent result lines' '\n \ttest_cmp expect actual\n '\n \n+for opt in --git-completion-helper --git-completion-helper-all\n+do\n+\ttest_expect_success \"annotate $opt offers the same options as blame\" '\n+\t\tgit blame $opt >expect &&\n+\t\tgit annotate $opt >actual &&\n+\t\ttest_grep -e \"--porcelain\" actual &&\n+\t\ttest_cmp expect actual\n+\t'\n+done\n+\n test_done\n\nbase-commit: 0ffa3e35c7c82ab8d0de226149fb03b3f7674d05\n-- \n2.43.0\n\n\n"},{"id":"557601","messageId":"xmqq33tfumj5.fsf@gitster.g","threadId":"66779","inReplyTo":"20261105073101.1449292-1-tangajoris@gmail.com","subject":"Re: [Outreachy PATCH] annotate: pass the arguments to blame unchanged","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-05T19:05:18Z","receivedAt":"2026-11-05T19:05:18Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tanga Joris <tangajoris@gmail.com> writes:\n\n> cmd_annotate() puts \"-c\" in front of the user's arguments before\n> calling cmd_blame(), so \"--git-completion-helper\" is no longer the\n> lone argument that parse-options looks for. It falls through to the\n> diff option parser, which answers it. So \"git annotate --<TAB>\"\n> offers the options of \"git diff\" instead of annotate's own. There is\n> no --porcelain, while \"git blame --<TAB>\" offers it.\n\nMakes sense.\n\n>  builtin/annotate.c            | 22 +---------------------\n>  t/t8003-blame-corner-cases.sh | 10 ++++++++++\n>  2 files changed, 11 insertions(+), 21 deletions(-)\n>\n> diff --git a/builtin/annotate.c b/builtin/annotate.c\n> index 7f754f2309..c1f258993d 100644\n> --- a/builtin/annotate.c\n> +++ b/builtin/annotate.c\n> @@ -6,31 +6,11 @@\n>  \n>  #include \"git-compat-util.h\"\n>  #include \"builtin.h\"\n> -#include \"strvec.h\"\n>  \n>  int cmd_annotate(int argc,\n>  \t\t const char **argv,\n>  \t\t const char *prefix,\n>  \t\t struct repository *repo)\n>  {\n> -\tstruct strvec args = STRVEC_INIT;\n> -\tconst char **args_copy;\n> -\tint ret;\n> -\n> -\tstrvec_pushl(&args, \"annotate\", \"-c\", NULL);\n> -\tfor (int i = 1; i < argc; i++)\n> -\t\tstrvec_push(&args, argv[i]);\n> -\n> -\t/*\n> -\t * `cmd_blame()` ends up modifying the array, which causes memory leaks\n> -\t * if we didn't copy the array here.\n> -\t */\n> -\tCALLOC_ARRAY(args_copy, args.nr + 1);\n> -\tCOPY_ARRAY(args_copy, args.v, args.nr);\n> -\n> -\tret = cmd_blame(args.nr, args_copy, prefix, repo);\n> -\n> -\tstrvec_clear(&args);\n> -\tfree(args_copy);\n> -\treturn ret;\n> +\treturn cmd_blame(argc, argv, prefix, repo);\n>  }\n\nNice simplification.\n\n> diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh\n> index 30e7960ace..6824f33332 100755\n> --- a/t/t8003-blame-corner-cases.sh\n> +++ b/t/t8003-blame-corner-cases.sh\n> @@ -303,4 +303,14 @@ test_expect_success 'blame does not coalesce non-adjacent result lines' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +for opt in --git-completion-helper --git-completion-helper-all\n> +do\n> +\ttest_expect_success \"annotate $opt offers the same options as blame\" '\n\nMaking \"annotate\" trigger completion-helper is a great goal.  \"git\nhelp annotate\" does show the same set of options as \"blame\", so it\nis in line with what the commands actually support.\n\nLooking good.  Thanks.\n\n> +\t\tgit blame $opt >expect &&\n> +\t\tgit annotate $opt >actual &&\n> +\t\ttest_grep -e \"--porcelain\" actual &&\n> +\t\ttest_cmp expect actual\n> +\t'\n> +done\n> +\n>  test_done\n>\n> base-commit: 0ffa3e35c7c82ab8d0de226149fb03b3f7674d05\n\n"}]}