Skip to content

Commit bc0893c

Browse files
chooglengitster
authored andcommitted
branch: make create_branch() always create a branch
With the previous commit, there are no more invocations of create_branch() that do not create a branch because: * BRANCH_TRACK_OVERRIDE is no longer passed * clobber_head_ok = true and force = false is never passed Assert these situations, delete dead code and ensure that we're handling clobber_head_ok and force correctly by introducing tests for `git branch --force`. As a result, create_branch() now always creates a branch. Helped-by: Jonathan Tan <[email protected]> Signed-off-by: Glen Choo <[email protected]> Reviewed-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e89f151 commit bc0893c

File tree

3 files changed

+46
-32
lines changed

3 files changed

+46
-32
lines changed

branch.c

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -429,43 +429,40 @@ void create_branch(struct repository *r,
429429
char *real_ref;
430430
struct strbuf ref = STRBUF_INIT;
431431
int forcing = 0;
432-
int dont_change_ref = 0;
433-
434-
if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
435-
? validate_branchname(name, &ref)
436-
: validate_new_branchname(name, &ref, force)) {
437-
if (!force)
438-
dont_change_ref = 1;
439-
else
440-
forcing = 1;
432+
struct ref_transaction *transaction;
433+
struct strbuf err = STRBUF_INIT;
434+
char *msg;
435+
436+
if (track == BRANCH_TRACK_OVERRIDE)
437+
BUG("'track' cannot be BRANCH_TRACK_OVERRIDE. Did you mean to call dwim_and_setup_tracking()?");
438+
if (clobber_head_ok && !force)
439+
BUG("'clobber_head_ok' can only be used with 'force'");
440+
441+
if (clobber_head_ok ?
442+
validate_branchname(name, &ref) :
443+
validate_new_branchname(name, &ref, force)) {
444+
forcing = 1;
441445
}
442446

443447
dwim_branch_start(r, start_name, track, &real_ref, &oid);
444448

445449
if (reflog)
446450
log_all_ref_updates = LOG_REFS_NORMAL;
447451

448-
if (!dont_change_ref) {
449-
struct ref_transaction *transaction;
450-
struct strbuf err = STRBUF_INIT;
451-
char *msg;
452-
453-
if (forcing)
454-
msg = xstrfmt("branch: Reset to %s", start_name);
455-
else
456-
msg = xstrfmt("branch: Created from %s", start_name);
457-
458-
transaction = ref_transaction_begin(&err);
459-
if (!transaction ||
460-
ref_transaction_update(transaction, ref.buf,
461-
&oid, forcing ? NULL : null_oid(),
462-
0, msg, &err) ||
463-
ref_transaction_commit(transaction, &err))
464-
die("%s", err.buf);
465-
ref_transaction_free(transaction);
466-
strbuf_release(&err);
467-
free(msg);
468-
}
452+
if (forcing)
453+
msg = xstrfmt("branch: Reset to %s", start_name);
454+
else
455+
msg = xstrfmt("branch: Created from %s", start_name);
456+
transaction = ref_transaction_begin(&err);
457+
if (!transaction ||
458+
ref_transaction_update(transaction, ref.buf,
459+
&oid, forcing ? NULL : null_oid(),
460+
0, msg, &err) ||
461+
ref_transaction_commit(transaction, &err))
462+
die("%s", err.buf);
463+
ref_transaction_free(transaction);
464+
strbuf_release(&err);
465+
free(msg);
469466

470467
if (real_ref && track)
471468
setup_tracking(ref.buf + 11, real_ref, track, quiet);

branch.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ void dwim_and_setup_tracking(struct repository *r, const char *new_ref,
5252
*
5353
* - force enables overwriting an existing (non-head) branch
5454
*
55-
* - clobber_head_ok allows the currently checked out (hence existing)
56-
* branch to be overwritten; without 'force', it has no effect.
55+
* - clobber_head_ok, when enabled with 'force', allows the currently
56+
* checked out (head) branch to be overwritten
5757
*
5858
* - reflog creates a reflog for the branch
5959
*

t/t3200-branch.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ test_expect_success 'git branch abc should create a branch' '
4242
git branch abc && test_path_is_file .git/refs/heads/abc
4343
'
4444

45+
test_expect_success 'git branch abc should fail when abc exists' '
46+
test_must_fail git branch abc
47+
'
48+
49+
test_expect_success 'git branch --force abc should fail when abc is checked out' '
50+
test_when_finished git switch main &&
51+
git switch abc &&
52+
test_must_fail git branch --force abc HEAD~1
53+
'
54+
55+
test_expect_success 'git branch --force abc should succeed when abc exists' '
56+
git rev-parse HEAD~1 >expect &&
57+
git branch --force abc HEAD~1 &&
58+
git rev-parse abc >actual &&
59+
test_cmp expect actual
60+
'
61+
4562
test_expect_success 'git branch a/b/c should create a branch' '
4663
git branch a/b/c && test_path_is_file .git/refs/heads/a/b/c
4764
'

0 commit comments

Comments
 (0)