Subject: Re: [PATCH v2 1/1] cat-file: add mailmap subcommand to --batch-command

From: Junio C Hamano <[email-masked]>
Date: Sun, 29 Mar 2026 19:12:08 -0700

Siddharth Asthana <[email-masked]> writes:

> git-cat-file(1)'s --batch-command works with the --use-mailmap option,
> but this option needs to be set when the process is created. This means
> we cannot change this option mid-operation.
>
> At GitLab, Gitaly caches git-cat-file processes and it would be useful

Would "keeps interacting with a single 'cat-file' process" be more
accurate than "caches"?  The latter gives, at least to me,
connotations that may not be necessarily true, like (1) there is a
pool of cat-file processes waiting for Gitaly to connect and serve,
(2) a running Gitaly may decide to disconnect from cat-file from
time to time, and then reconnect to one of them when it becomes
necessary again, etc.

> if --batch-command supported toggling mailmap dynamically with existing
> processes.
>
> Add a `mailmap` subcommand to --batch-command that takes a single
> argument: `yes` to enable mailmap and `no` to disable it. When enabled,
> mailmap data is loaded from disk on first use and kept in memory so that
> toggling back on does not require reloading.
>
> Suggested-by: Junio C Hamano <[email-masked]>

This is over-crediting me.  The idea to unify the two commands into
one may have come from me, but that is at most helped-by but it is
perfectly fine without any credit.

> Signed-off-by: Siddharth Asthana <[email-masked]>
> ---

> diff --git a/builtin/cat-file.c b/builtin/cat-file.c
> index b6f12f41d6..a53926d2bb 100644
> --- a/builtin/cat-file.c
> +++ b/builtin/cat-file.c
> @@ -54,6 +54,7 @@ static const char *force_path;
>  
>  static struct string_list mailmap = STRING_LIST_INIT_NODUP;
>  static int use_mailmap;
> +static int mailmap_loaded;

Not part of this topic, but in case less experienced developers who
are watching from the sidelines wonder if we want to add this
file-scope global variable, this is perfectly fine.  Anything under
builtin/foo.c are meant to serve a single command "git foo" and does
not benefit from "let's sift globals into classes that belong to
different concepts in the system; most of which will be per
repository, so make them some part of the repository object"
movement as much as more library-ish parts of the system.

Until a specific command starts working on multiple repositories at
one time, that is.

> @@ -692,6 +693,24 @@ static void parse_cmd_info(struct batch_options *opt,
>  	batch_one_object(line, output, opt, data);
>  }
>  
> +static void parse_cmd_mailmap(struct batch_options *opt UNUSED,
> +			      const char *line,
> +			      struct strbuf *output UNUSED,
> +			      struct expand_data *data UNUSED)
> +{
> +	if (!strcmp(line, "yes")) {
> +		if (!mailmap_loaded) {
> +			read_mailmap(the_repository, &mailmap);
> +			mailmap_loaded = 1;
> +		}
> +		use_mailmap = 1;
> +	} else if (!strcmp(line, "no")) {
> +		use_mailmap = 0;
> +	} else {
> +		die(_("mailmap: unknown argument '%s', expected 'yes' or 'no'"), line);
> +	}
> +}

OK.

> @@ -725,9 +744,10 @@ static const struct parse_cmd {
>  	parse_cmd_fn_t fn;
>  	unsigned takes_args;
>  } commands[] = {
> -	{ "contents", parse_cmd_contents, 1},
> -	{ "info", parse_cmd_info, 1},
> -	{ "flush", NULL, 0},
> +	{ "contents", parse_cmd_contents, 1 },
> +	{ "info", parse_cmd_info, 1 },
> +	{ "flush", NULL, 0 },
> +	{ "mailmap", parse_cmd_mailmap, 1 },
>  };

Mixing style fixes to existing entries in the same patch that adds a
new feature by adding a new entry to the table is annoying than
having a preliminary clean-up patch that only fixes style and then
the main patch that adds the feature.

>  static void batch_objects_command(struct batch_options *opt,
> @@ -1127,8 +1147,10 @@ int cmd_cat_file(int argc,
>  	opt_cw = (opt == 'c' || opt == 'w');
>  	opt_epts = (opt == 'e' || opt == 'p' || opt == 't' || opt == 's');
>  
> -	if (use_mailmap)
> +	if (use_mailmap) {
>  		read_mailmap(the_repository, &mailmap);
> +		mailmap_loaded = 1;
> +	}

Hmph, interesting.  Two points.

 * It would make it easier to follow if these two lines are made
   into a small helper function to be called from here and from the
   "parse_cmd_mailmap()"?

 * Can we somehow make mailmap object itself slightly smarter so
   that it knows if it has already been asked to read the data from
   its sources?  It is a pretty dumb string_list, but from a cursory
   read of the code flow, it seems that mailmap.strdup_strings is
   initialized to be false in builtin/cat-file.c and then one of the
   first things done in mailmap.c::read_mailmap() is to flip that
   bit on, so the "yes" part of the parse_cmd_mailmap() might become

	if (yes)
		load_mailmap();

   with the helper load_mailmap() that may look like:

	static void load_mailmap(void)
	{
		if (mailmap.strdup_strings)
			return; /* we know read_mailmap() flips it on */
		read_mailmap(the_repository, &mailmap);
	}

   The first bullet point to introduce a small common helper will
   help hiding such an ugly implementation detail there.