Subject: Re: [PATCH v2 2/3] clone: propagate empty remote HEAD even with other branches

From: Junio C Hamano <[email-masked]>
Date: Fri, 08 Jul 2022 08:41:47 -0700

Jeff King <[email-masked]> writes:

> So let's pull the HEAD setup out of this conditional entirely. This
> de-duplicates some of the code and the result is easy to follow, because
> helper functions like find_ref_by_name() do the right thing even in the
> empty-repo case (i.e., by returning NULL).

Nicely done.  The first stage becomes purely about optionally
fetching when there are some refs to fetch, and then we compute
where our HEAD should be separately.  Very clean.

> Rewritten in v2 to extract more of the logic. Bigger diff, but I think
> the end result is good. Try reading with "-w", as well.

I somehow found that the rendition with "-w" less easier to follow ;-)

> diff --git a/t/t5702-protocol-v2.sh b/t/t5702-protocol-v2.sh
> index 00ce9aec23..2b3a78b842 100755
> --- a/t/t5702-protocol-v2.sh
> +++ b/t/t5702-protocol-v2.sh
> @@ -250,6 +250,44 @@ test_expect_success 'bare clone propagates empty default branch' '
>  	grep "refs/heads/mydefaultbranch" file_empty_child.git/HEAD
>  '
>  
> +test_expect_success 'clone propagates unborn HEAD from non-empty repo' '
> +	test_when_finished "rm -rf file_unborn_parent file_unborn_child" &&
> +
> +	git init file_unborn_parent &&
> +	(
> +		cd file_unborn_parent &&
> +		git checkout -b branchwithstuff &&
> +		test_commit --no-tag stuff &&
> +		git symbolic-ref HEAD refs/heads/mydefaultbranch
> +	) &&
> +
> +	GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
> +	git -c init.defaultBranch=main -c protocol.version=2 \
> +		clone "file://$(pwd)/file_unborn_parent" \
> +		file_unborn_child 2>stderr &&
> +	grep "refs/heads/mydefaultbranch" file_unborn_child/.git/HEAD &&

We know they do not have mydefaultbranch2 branch, so this grep may
be sufficient, but peeking into the implementation detail of HEAD is
not necessary.  The kosher way is way more verbose and awkward,
though:

	git -C file_unborn_child symbolic-ref HEAD >actual &&
	echo refs/heads/mydefaultbranch >expect &&
	test_cmp actual expect &&

I dunno.

> +	grep "warning: remote HEAD refers to nonexistent ref" stderr
> +'
> +
> +test_expect_success 'bare clone propagates unborn HEAD from non-empty repo' '
> +	test_when_finished "rm -rf file_unborn_parent file_unborn_child.git" &&
> +
> +	git init file_unborn_parent &&
> +	(
> +		cd file_unborn_parent &&
> +		git checkout -b branchwithstuff &&
> +		test_commit --no-tag stuff &&
> +		git symbolic-ref HEAD refs/heads/mydefaultbranch
> +	) &&
> +
> +	GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
> +	git -c init.defaultBranch=main -c protocol.version=2 \
> +		clone --bare "file://$(pwd)/file_unborn_parent" \
> +		file_unborn_child.git 2>stderr &&
> +	grep "refs/heads/mydefaultbranch" file_unborn_child.git/HEAD &&

Likewise.

> +	! grep "warning:" stderr
> +'
> +
>  test_expect_success 'fetch with file:// using protocol v2' '
>  	test_when_finished "rm -f log" &&