Subject: Re: Best way to update `HEAD` in mirrored repos

From: Junio C Hamano <[email-masked]>
Date: Fri, 10 Jun 2022 14:53:44 -0700

Johannes Schindelin <[email-masked]> writes:

> diff --git a/builtin/remote.c b/builtin/remote.c
> index eddd40c8f87..fead15adb97 100644
> --- a/builtin/remote.c
> +++ b/builtin/remote.c
> @@ -1344,7 +1344,7 @@ static int show(int argc, const char **argv)
>
>  static int set_head(int argc, const char **argv)
>  {
> -	int i, opt_a = 0, opt_d = 0, result = 0;
> +	int i, opt_a = 0, opt_d = 0, result = 0, is_mirror = 0;
>  	struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
>  	char *head_name = NULL;
>
> @@ -1357,8 +1357,16 @@ static int set_head(int argc, const char **argv)
>  	};
>  	argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
>  			     0);
> -	if (argc)
> -		strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
> +	if (argc) {
> +		struct remote *remote = remote_get(argv[0]);
> +
> +		if (!remote || !remote->mirror)
> +			strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
> +		else {
> +			is_mirror = 1;
> +			strbuf_addstr(&buf, "HEAD");
> +		}
> +	}

Good.

Regardless of the "mirror" issue, it makes tons of sense to ask the
remote API how the remote-tracking refs for the given remote is set
up, instead of assuming that it must be "refs/remotes/<remote>"
blindly like in the original code.

That way, we could even handle a case like so:

	[remote "frotz"]
		fetch = +refs/heads/*:refs/remotes/nitfol/*

Their HEAD should be mapped to refs/remotes/nitfol/HEAD on our end,
so set-head should be able to notice that, too, if we go further
with your "do not assume, instead ask remote API" approach.

Thanks.