diff --git a/lib/core.sh b/lib/core.sh index e53e95b..9319ba1 100644 --- a/lib/core.sh +++ b/lib/core.sh @@ -633,11 +633,18 @@ _try_worktree_add() { return 0 } -# Build and validate folder name from branch/custom/override. +# Validate the branch operand and build the folder name. # Prints sanitized folder name on success; returns 1 on validation failure. # Usage: _resolve_folder_name [custom_name] [folder_override] _resolve_folder_name() { local branch_name="$1" custom_name="${2:-}" folder_override="${3:-}" + + # Interactive input and provider branch names bypass the CLI flag parser. + if [[ "$branch_name" == -* ]]; then + log_error "Branch name cannot start with a dash" + return 1 + fi + local sanitized_name if [ -n "$folder_override" ]; then diff --git a/tests/cmd_create_integration.bats b/tests/cmd_create_integration.bats index 62f700e..bd47ff0 100644 --- a/tests/cmd_create_integration.bats +++ b/tests/cmd_create_integration.bats @@ -29,6 +29,19 @@ _cmd_create_capture_stdout() { [ -d "$TEST_WORKTREES_DIR/new-feature" ] } +@test "new CLI rejects a prompted branch that Git would read as an option" { + git update-ref refs/remotes/origin/--detach HEAD + local before + before=$(git worktree list --porcelain) + + run bash -c 'printf "%s\n" --detach | "$1" new --from HEAD --no-fetch --no-copy --no-hooks --no-sparse' _ "$PROJECT_ROOT/bin/gtr" + + [ "$status" -ne 0 ] + [[ "$output" == *"Branch name cannot start with a dash"* ]] + [ "$(git worktree list --porcelain)" = "$before" ] + [ ! -e "$TEST_WORKTREES_DIR" ] +} + @test "new CLI keeps ordinary next steps readable" { run "$PROJECT_ROOT/bin/gtr" new feature/ordinary --from HEAD --no-fetch --no-copy --no-hooks --yes [ "$status" -eq 0 ]