[PATCH v2 2/8] checkout: validate new branch name in checkout_branch()
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Aug 30, 2026, 20:48 UTC
- Message-ID
- <20260830204835.1040408-3-gitster@pobox.com>
- In-Reply-To
- <20260830204835.1040408-1-gitster@pobox.com>
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> --- 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
-- 2.55.0-884-g76cf8659c2