Skip to content

Commit 147d071

Browse files
committed
Merge branch 'jc/checkout-out-of-unborn' into maint
* jc/checkout-out-of-unborn: git checkout -b: allow switching out of an unborn branch
2 parents 57d6b07 + abe1998 commit 147d071

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

builtin/checkout.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,17 @@ static int parse_branchname_arg(int argc, const char **argv,
922922
return argcount;
923923
}
924924

925+
static int switch_unborn_to_new_branch(struct checkout_opts *opts)
926+
{
927+
int status;
928+
struct strbuf branch_ref = STRBUF_INIT;
929+
930+
strbuf_addf(&branch_ref, "refs/heads/%s", opts->new_branch);
931+
status = create_symref("HEAD", branch_ref.buf, "checkout -b");
932+
strbuf_release(&branch_ref);
933+
return status;
934+
}
935+
925936
int cmd_checkout(int argc, const char **argv, const char *prefix)
926937
{
927938
struct checkout_opts opts;
@@ -1093,5 +1104,13 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10931104
if (opts.writeout_stage)
10941105
die(_("--ours/--theirs is incompatible with switching branches."));
10951106

1107+
if (!new.commit) {
1108+
unsigned char rev[20];
1109+
int flag;
1110+
1111+
if (!read_ref_full("HEAD", rev, 0, &flag) &&
1112+
(flag & REF_ISSYMREF) && is_null_sha1(rev))
1113+
return switch_unborn_to_new_branch(&opts);
1114+
}
10961115
return switch_branches(&opts, &new);
10971116
}

t/t2015-checkout-unborn.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
test_description='checkout from unborn branch protects contents'
3+
test_description='checkout from unborn branch'
44
. ./test-lib.sh
55

66
test_expect_success 'setup' '
@@ -37,4 +37,13 @@ test_expect_success 'checkout from unborn merges identical index contents' '
3737
git checkout -b new origin
3838
'
3939

40+
test_expect_success 'checking out another branch from unborn state' '
41+
git checkout --orphan newroot &&
42+
git checkout -b anothername &&
43+
test_must_fail git show-ref --verify refs/heads/newroot &&
44+
git symbolic-ref HEAD >actual &&
45+
echo refs/heads/anothername >expect &&
46+
test_cmp expect actual
47+
'
48+
4049
test_done

0 commit comments

Comments
 (0)