提交 8338f771 编写于 作者: E Erik Faye-Lund 提交者: Junio C Hamano

checkout: do not corrupt HEAD on empty repo

In abe19980 ("git checkout -b: allow switching out of an unborn
branch"), a code-path overly-optimisticly assumed that a
branch-name was specified. This is not always the case, and as
a result a NULL-pointer was attempted printed to .git/HEAD.

This could lead to at least two different failure modes:
 1) vsnprintf formated the NULL-string as something useful (e.g
    "(null)")
 2) vsnprintf crashed

Neither were very convenient for formatting a new HEAD-reference.

To fix this, reintroduce some strictness so we only take this
new codepath if a banch-name was specified.
Signed-off-by: NErik Faye-Lund <kusmabite@gmail.com>
Signed-off-by: NJunio C Hamano <gitster@pobox.com>
上级 abe19980
......@@ -1100,7 +1100,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
if (opts.writeout_stage)
die(_("--ours/--theirs is incompatible with switching branches."));
if (!new.commit) {
if (!new.commit && opts.new_branch) {
unsigned char rev[20];
int flag;
......
......@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' '
test_cmp expect actual
'
test_expect_success 'checking out in a newly created repo' '
test_create_repo empty &&
(
cd empty &&
git symbolic-ref HEAD >expect &&
test_must_fail git checkout &&
git symbolic-ref HEAD >actual &&
test_cmp expect actual
)
'
test_done
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册