# shellcheck shell=bash

cmd_create_worktree() {
    if [ -z "${1-}" ]; then 
        err "Branch name required"
        echo "Usage: gw create <branch>"
        return 1
    fi
    local branch_name="$1"
    local root; root="$(git_require_repo_root)" || return 1
    local shared_root; shared_root="$(git_shared_root 2>/dev/null)"
    [[ -z "$shared_root" ]] && shared_root="$root"
    local source_root="$root"

    local dir; dir="$(git_build_worktree_path "$branch_name")"

    info "🌳 Creating branch '$branch_name' and worktree at '$dir'..."
    dir="$(git_create_branch_and_worktree "$branch_name")"
    gw_env_copy_files "$source_root" "$dir" "$shared_root"
    info "✅ Success! Worktree created at '$dir'."
    echo "cd \"$dir\""
    (cd "$dir" && git_status_short)
}
