Git development
 help / color / mirror / Atom feed
From: Tanga Joris <tangajoris@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, ps@pks.im, christian.couder@gmail.com,
	siddharthasthana31@gmail.com, kaartic.sivaraam@gmail.com,
	usmanakinyemi202@gmail.com, Tanga Joris <tangajoris@gmail.com>
Subject: [Outreachy PATCH] annotate: pass the arguments to blame unchanged
Date: Thu,  5 Nov 2026 10:31:01 +0300	[thread overview]
Message-ID: <20261105073101.1449292-1-tangajoris@gmail.com> (raw)

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


             reply	other threads:[~2026-11-05  7:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-05  7:31 Tanga Joris [this message]
2026-11-05 19:05 ` [Outreachy PATCH] annotate: pass the arguments to blame unchanged Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261105073101.1449292-1-tangajoris@gmail.com \
    --to=tangajoris@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=kaartic.sivaraam@gmail.com \
    --cc=ps@pks.im \
    --cc=siddharthasthana31@gmail.com \
    --cc=usmanakinyemi202@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox