Subject: Re: [PATCH v7] tracking branches: add advice to ambiguous refspec error

From: Junio C Hamano <[email-masked]>
Date: Fri, 01 Apr 2022 12:57:37 -0700

Glen Choo <[email-masked]> writes:

> "Tao Klerks via GitGitGadget" <[email-masked]> writes:
>
>> diff --git a/branch.c b/branch.c
>> index 6b31df539a5..182f1c5a556 100644
>> --- a/branch.c
>> +++ b/branch.c
>> @@ -18,17 +18,31 @@ struct tracking {
>>  	int matches;
>>  };
>>  
>> +struct find_tracked_branch_cb {
>> +	struct tracking *tracking;
>> +	struct string_list ambiguous_remotes;
>> +};
>> +
>>  static int find_tracked_branch(struct remote *remote, void *priv)
>>  {
>> -	struct tracking *tracking = priv;
>> +	struct find_tracked_branch_cb *ftb = priv;
>> +	struct tracking *tracking = ftb->tracking;
>>  
>>  	if (!remote_find_tracking(remote, &tracking->spec)) {
>> -		if (++tracking->matches == 1) {
>> +		switch (++tracking->matches) {
>> +		case 1:
>>  			string_list_append(tracking->srcs, tracking->spec.src);
>>  			tracking->remote = remote->name;
>> -		} else {
>> +			break;
>> +		case 2:
>> +			/* there are at least two remotes; backfill the first one */
>> +			string_list_append(&ftb->ambiguous_remotes, tracking->remote);
>> +			/* fall through */
>> +		default:
>> +			string_list_append(&ftb->ambiguous_remotes, remote->name);
>>  			free(tracking->spec.src);
>>  			string_list_clear(tracking->srcs, 0);
>> +		break;
>>  		}
>>  		tracking->spec.src = NULL;
>>  	}
>
> Ah I see, on the first iteration, we set the first remote's name to
> tracking->remote, and on the second iteration, we copy that value to the
> list before copying the _current_ remote's name to the list.
>
>> -test_expect_success 'avoid ambiguous track' '
>> +test_expect_success 'avoid ambiguous track and advise' '
>>  	git config branch.autosetupmerge true &&
>>  	git config remote.ambi1.url lalala &&
>>  	git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
>>  	git config remote.ambi2.url lilili &&
>>  	git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
>> -	test_must_fail git branch all1 main &&
>> +	cat <<-EOF >expected &&
>> +	fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
>> +	hint: There are multiple remotes whose fetch refspecs map to the remote
>> +	hint: tracking ref '\''refs/heads/main'\'':
>> +	hint:   ambi1
>> +	hint:   ambi2
>> +	hint: ''
>> +	hint: This is typically a configuration error.
>> +	hint: ''
>> +	hint: To support setting up tracking branches, ensure that
>> +	hint: different remotes'\'' fetch refspecs map into different
>> +	hint: tracking namespaces.
>> +	EOF
>> +	test_must_fail git branch all1 main 2>actual &&
>> +	test_cmp expected actual &&
>>  	test -z "$(git config branch.all1.merge)"
>>  '
>
> And this test shows that this indeed does what we think it does. Nicely
> done.
>
> I notice that we there is an instance of test -z "$(some git command)",
> which IIRC is discouraged because it would mask a failure in the git
> command, but that's not new and I don't think it needs to be fixed in
> this series anyway.
>
> Reviewed-by: Glen Choo <[email-masked]>

Thanks for giving the patch a careful read.

Will queue.

Thanks.