]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout -p: handle tree arguments correctly again
authorJohannes Schindelin <johannes.schindelin@gmx.de>
Sat, 19 Dec 2020 14:55:59 +0000 (14:55 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 21 Dec 2020 22:06:09 +0000 (14:06 -0800)
This fixes a segmentation fault.

The bug is caused by dereferencing `new_branch_info->commit` when it is
`NULL`, which is the case when the tree-ish argument is actually a tree,
not a commit-ish. This was introduced in 5602b500c3c (builtin/checkout:
fix `git checkout -p HEAD...` bug, 2020-10-07), where we tried to ensure
that the special tree-ish `HEAD...` is handled correctly.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
t/t2016-checkout-patch.sh

index 9a33b005ccd194da3e86c7139a5de33d36f5083e..728c67e8ca583250d0ad5f4bcb2da4e93d2b85e9 100644 (file)
@@ -479,9 +479,11 @@ 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 && strcmp(rev, "HEAD"))
+               if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))
                        rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);
 
                if (opts->checkout_index && opts->checkout_worktree)
index d91a329eb31af78110ced886c2680346f94236fc..abfd586c32b017e7f00d9cfc1a85cddb4f150d43 100755 (executable)
@@ -123,4 +123,9 @@ test_expect_success PERL 'none of this moved HEAD' '
        verify_saved_head
 '
 
+test_expect_success PERL 'empty tree can be handled' '
+       test_when_finished "git reset --hard" &&
+       git checkout -p $(test_oid empty_tree) --
+'
+
 test_done