Subject: Re: [PATCH] checkout -p: handle tree arguments correctly again

From: Junio C Hamano <[email-masked]>
Date: Sat, 19 Dec 2020 09:40:43 -0800

"Johannes Schindelin via GitGitGadget" <[email-masked]>
writes:

>     I literally just ran into this segmentation fault after rebasing Git for
>     Windows onto -rc1, and did not really think that the regression was
>     introduced in the v2.30.0 cycle, but was proven wrong by my
>     investigation: it was introduced by v2.30.0-rc0151^23.

As "checkout -p branch~4" is easier than "checkout -p branch~4:", I
think you have to work harder to give a true tree object rather than
the containing commit object.

It still is curious how you stumbled onto this, but I am glad you
did ;-)

> @@ -482,7 +482,7 @@ static int checkout_paths(const struct checkout_opts *opts,
>  		 * properly. However, there is special logic for the HEAD case
>  		 * so we mustn't replace that.
>  		 */
> -		if (rev && strcmp(rev, "HEAD"))
> +		if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))

The comment above talks about why "&& strcmp()" is there.  We now
have two.

I'd squash this in while queuing.  Thanks.

 builtin/checkout.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git c/builtin/checkout.c w/builtin/checkout.c
index 6f24b381c5..728c67e8ca 100644
--- c/builtin/checkout.c
+++ w/builtin/checkout.c
@@ -479,7 +479,9 @@ static int checkout_paths(const struct checkout_opts *opts,
 		 * with the hex of the commit (whether it's in `...` form or
 		 * not) for the run_add_interactive() machinery to work
 		 * properly. However, there is special logic for the HEAD case
-		 * so we mustn't replace that.
+		 * so we mustn't replace that.  Also, when we were given a
+		 * tree-object, new_branch_info->commit would be NULL, but we
+		 * do not have to do any replacement, either.
 		 */
 		if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))
 			rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);