Skip to content

Commit 5c29f19

Browse files
dschogitster
authored andcommitted
checkout -p: handle tree arguments correctly again
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 5602b50 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 35166b1 commit 5c29f19

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

builtin/checkout.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,9 +479,11 @@ static int checkout_paths(const struct checkout_opts *opts,
479479
* with the hex of the commit (whether it's in `...` form or
480480
* not) for the run_add_interactive() machinery to work
481481
* properly. However, there is special logic for the HEAD case
482-
* so we mustn't replace that.
482+
* so we mustn't replace that. Also, when we were given a
483+
* tree-object, new_branch_info->commit would be NULL, but we
484+
* do not have to do any replacement, either.
483485
*/
484-
if (rev && strcmp(rev, "HEAD"))
486+
if (rev && new_branch_info->commit && strcmp(rev, "HEAD"))
485487
rev = oid_to_hex_r(rev_oid, &new_branch_info->commit->object.oid);
486488

487489
if (opts->checkout_index && opts->checkout_worktree)

t/t2016-checkout-patch.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,9 @@ test_expect_success PERL 'none of this moved HEAD' '
123123
verify_saved_head
124124
'
125125

126+
test_expect_success PERL 'empty tree can be handled' '
127+
test_when_finished "git reset --hard" &&
128+
git checkout -p $(test_oid empty_tree) --
129+
'
130+
126131
test_done

0 commit comments

Comments
 (0)