Skip to content

Commit 2292ce4

Browse files
sbeyergitster
authored andcommitted
checkout: don't crash on file checkout before running post-checkout hook
In the case of git init echo exit >.git/hooks/post-checkout chmod +x .git/hooks/post-checkout touch foo git add foo rm foo git checkout -- foo git-checkout resulted in a Segmentation fault, because there is no new branch set for the post-checkout hook. This patch makes use of the null SHA as it is set for the old branch. While at it, I removed the xstrdup() around the sha1_to_hex(...) calls in builtin-checkout.c/post_checkout_hook() because sha1_to_hex() uses four buffers for the hex-dumped SHA and we only need two. (Duplicating one buffer is only needed if we need more than four.) Signed-off-by: Stephan Beyer <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7bbd8d6 commit 2292ce4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

builtin-checkout.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ static int post_checkout_hook(struct commit *old, struct commit *new,
4747

4848
memset(&proc, 0, sizeof(proc));
4949
argv[0] = name;
50-
argv[1] = xstrdup(sha1_to_hex(old ? old->object.sha1 : null_sha1));
51-
argv[2] = xstrdup(sha1_to_hex(new->object.sha1));
50+
argv[1] = sha1_to_hex(old ? old->object.sha1 : null_sha1);
51+
argv[2] = sha1_to_hex(new ? new->object.sha1 : null_sha1);
52+
/* "new" can be NULL when checking out from the index before
53+
a commit exists. */
5254
argv[3] = changed ? "1" : "0";
5355
argv[4] = NULL;
5456
proc.argv = argv;

0 commit comments

Comments
 (0)