{
int n;
const char *s = worktree_basename(path, &n);
- *new_branch = xstrndup(s, n);
- UNLEAK(*new_branch);
+ const char *branchname = xstrndup(s, n);
+ struct strbuf ref = STRBUF_INIT;
+
+ UNLEAK(branchname);
+ if (!strbuf_check_branch_ref(&ref, branchname) &&
+ ref_exists(ref.buf)) {
+ strbuf_release(&ref);
+ return branchname;
+ }
+
+ *new_branch = branchname;
if (guess_remote) {
struct object_id oid;
const char *remote =