From 3b87cfb731e5ffd9c8daa8fbebd566f0fb82e91a Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sun, 30 Aug 2026 13:48:29 -0700 Subject: [PATCH] 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 --- builtin/checkout.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/builtin/checkout.c b/builtin/checkout.c index 774e4fd5b3..14542626e9 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -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