All of lore.kernel.org
 help / color / mirror / Atom feed
* [Outreachy PATCH] annotate: pass the arguments to blame unchanged
@ 2026-11-05  7:31 Tanga Joris
  2026-11-05 19:05 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Tanga Joris @ 2026-11-05  7:31 UTC (permalink / raw)
  To: git
  Cc: gitster, ps, christian.couder, siddharthasthana31,
	kaartic.sivaraam, usmanakinyemi202, Tanga Joris

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Outreachy PATCH] annotate: pass the arguments to blame unchanged
  2026-11-05  7:31 [Outreachy PATCH] annotate: pass the arguments to blame unchanged Tanga Joris
@ 2026-11-05 19:05 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2026-11-05 19:05 UTC (permalink / raw)
  To: Tanga Joris
  Cc: git, ps, christian.couder, siddharthasthana31, kaartic.sivaraam,
	usmanakinyemi202

Tanga Joris <tangajoris@gmail.com> writes:

> 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.

Makes sense.

>  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);
>  }

Nice simplification.

> 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" '

Making "annotate" trigger completion-helper is a great goal.  "git
help annotate" does show the same set of options as "blame", so it
is in line with what the commands actually support.

Looking good.  Thanks.

> +		git blame $opt >expect &&
> +		git annotate $opt >actual &&
> +		test_grep -e "--porcelain" actual &&
> +		test_cmp expect actual
> +	'
> +done
> +
>  test_done
>
> base-commit: 0ffa3e35c7c82ab8d0de226149fb03b3f7674d05

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-05 19:05 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-05  7:31 [Outreachy PATCH] annotate: pass the arguments to blame unchanged Tanga Joris
2026-11-05 19:05 ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.