Skip to content

Commit 055f2c5

Browse files
committed
Merge branch 'jc/maint-1.7.3-checkout-describe' into maint
* jc/maint-1.7.3-checkout-describe: checkout -b <name>: correctly detect existing branch
2 parents e08afec + c17b229 commit 055f2c5

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
10711071
if (strbuf_check_branch_ref(&buf, opts.new_branch))
10721072
die(_("git checkout: we do not like '%s' as a branch name."),
10731073
opts.new_branch);
1074-
if (!get_sha1(buf.buf, rev)) {
1074+
if (ref_exists(buf.buf)) {
10751075
opts.branch_exists = 1;
10761076
if (!opts.new_branch_force)
10771077
die(_("git checkout: branch %s already exists"),

refs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,6 +1826,12 @@ int update_ref(const char *action, const char *refname,
18261826
return 0;
18271827
}
18281828

1829+
int ref_exists(char *refname)
1830+
{
1831+
unsigned char sha1[20];
1832+
return !!resolve_ref(refname, sha1, 1, NULL);
1833+
}
1834+
18291835
struct ref *find_ref_by_name(const struct ref *list, const char *name)
18301836
{
18311837
for ( ; list; list = list->next)

refs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ extern void warn_dangling_symref(FILE *fp, const char *msg_fmt, const char *refn
5454
*/
5555
extern void add_extra_ref(const char *refname, const unsigned char *sha1, int flags);
5656
extern void clear_extra_refs(void);
57+
extern int ref_exists(char *);
5758

5859
extern int peel_ref(const char *, unsigned char *);
5960

t/t2018-checkout-branch.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,15 @@ test_expect_success 'checkout -f -B to an existing branch with mergeable changes
169169
test_must_fail test_dirty_mergeable
170170
'
171171

172+
test_expect_success 'checkout -b <describe>' '
173+
git tag -f -m "First commit" initial initial &&
174+
git checkout -f change1 &&
175+
name=$(git describe) &&
176+
git checkout -b $name &&
177+
git diff --exit-code change1 &&
178+
echo "refs/heads/$name" >expect &&
179+
git symbolic-ref HEAD >actual &&
180+
test_cmp expect actual
181+
'
182+
172183
test_done

0 commit comments

Comments
 (0)