checkout: validate new branch name in checkout_branch()

In checkout_main(), new branch name validation is performed before
dispatching to checkout_branch() or checkout_paths().  Checking out
paths does not create new branches, so this validation only belongs
in checkout_branch().

Move the validate_branchname() and validate_new_branchname() calls
from checkout_main() into checkout_branch().  checkout_paths()
checks and fails if '.new_branch' is set before doing anything,
which indicates that this change is safe and makes good sense.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
seen
Junio C Hamano 2026-08-30 13:48:29 -07:00
parent e3eda4a6f7
commit 3b87cfb731
1 changed files with 11 additions and 11 deletions

View File

@ -1734,6 +1734,17 @@ static int checkout_branch(struct checkout_opts *opts,
free(full_ref);
}

if (opts->new_branch) {
struct strbuf buf = STRBUF_INIT;

if (opts->new_branch_force)
opts->branch_exists = validate_branchname(opts->new_branch, &buf);
else
opts->branch_exists =
validate_new_branchname(opts->new_branch, &buf, 0);
strbuf_release(&buf);
}

if (!new_branch_info->commit && opts->new_branch) {
struct object_id rev;
int flag;
@ -2062,17 +2073,6 @@ static int checkout_main(int argc, const char **argv, const char *prefix,
die(_("you must specify path(s) to restore"));
}

if (opts->new_branch) {
struct strbuf buf = STRBUF_INIT;

if (opts->new_branch_force)
opts->branch_exists = validate_branchname(opts->new_branch, &buf);
else
opts->branch_exists =
validate_new_branchname(opts->new_branch, &buf, 0);
strbuf_release(&buf);
}

if (opts->patch_mode || opts->pathspec.nr)
ret = checkout_paths(opts, &new_branch_info);
else