Skip to content

Commit e512a10

Browse files
committed
refactor: simplify worktree management by removing force flag handling
- Updated the `_try_worktree_add` and `create_worktree` functions to eliminate the use of a separate force flag, replacing it with an array for force arguments. - Improved clarity in the command usage documentation and streamlined the logic for adding and removing worktrees. - Enhanced maintainability by reducing complexity in the argument handling for worktree operations.
1 parent 1a4b73a commit e512a10

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

lib/core.sh

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -307,17 +307,16 @@ resolve_worktree() {
307307
}
308308

309309
# Try to create a worktree, handling the common log/add/report pattern.
310-
# Usage: _try_worktree_add <path> <step_msg> <ok_msg> <force_flag> [git_worktree_add_args...]
310+
# Usage: _try_worktree_add <path> <step_msg> <ok_msg> [git_worktree_add_args...]
311311
# Prints worktree path on success; returns 1 on failure (caller handles error).
312312
# Note: step_msg may be empty to skip the log_step call.
313313
_try_worktree_add() {
314-
local wt_path="$1" step_msg="$2" ok_msg="$3" force_flag="$4"
315-
shift 4
314+
local wt_path="$1" step_msg="$2" ok_msg="$3"
315+
shift 3
316316

317317
[ -n "$step_msg" ] && log_step "$step_msg"
318318

319-
# shellcheck disable=SC2086
320-
if git worktree add $force_flag "$wt_path" "$@" >&2; then
319+
if git worktree add "$wt_path" "$@" >&2; then
321320
log_info "$ok_msg"
322321
printf "%s" "$wt_path"
323322
return 0
@@ -364,10 +363,10 @@ create_worktree() {
364363
fi
365364

366365
worktree_path="$base_dir/${prefix}${sanitized_name}"
367-
local force_flag=""
366+
local force_args=()
368367

369368
if [ "$force" -eq 1 ]; then
370-
force_flag="--force"
369+
force_args=(--force)
371370
fi
372371

373372
# Check if worktree already exists
@@ -398,10 +397,10 @@ create_worktree() {
398397
_try_worktree_add "$worktree_path" \
399398
"Creating worktree from remote branch origin/$branch_name" \
400399
"Worktree created tracking origin/$branch_name" \
401-
"$force_flag" -b "$branch_name" "origin/$branch_name" && return 0
400+
"${force_args[@]}" -b "$branch_name" "origin/$branch_name" && return 0
402401
_try_worktree_add "$worktree_path" "" \
403402
"Worktree created tracking origin/$branch_name" \
404-
"$force_flag" "$branch_name" && return 0
403+
"${force_args[@]}" "$branch_name" && return 0
405404
fi
406405
log_error "Remote branch origin/$branch_name does not exist"
407406
return 1
@@ -412,7 +411,7 @@ create_worktree() {
412411
_try_worktree_add "$worktree_path" \
413412
"Creating worktree from local branch $branch_name" \
414413
"Worktree created with local branch $branch_name" \
415-
"$force_flag" "$branch_name" && return 0
414+
"${force_args[@]}" "$branch_name" && return 0
416415
fi
417416
log_error "Local branch $branch_name does not exist"
418417
return 1
@@ -422,7 +421,7 @@ create_worktree() {
422421
_try_worktree_add "$worktree_path" \
423422
"Creating new branch $branch_name from $from_ref" \
424423
"Worktree created with new branch $branch_name" \
425-
"$force_flag" -b "$branch_name" "$from_ref" && return 0
424+
"${force_args[@]}" -b "$branch_name" "$from_ref" && return 0
426425
log_error "Failed to create worktree with new branch"
427426
return 1
428427
;;
@@ -436,17 +435,17 @@ create_worktree() {
436435
fi
437436
_try_worktree_add "$worktree_path" "" \
438437
"Worktree created tracking origin/$branch_name" \
439-
"$force_flag" "$branch_name" && return 0
438+
"${force_args[@]}" "$branch_name" && return 0
440439
elif [ "$local_exists" -eq 1 ]; then
441440
_try_worktree_add "$worktree_path" \
442441
"Using existing local branch $branch_name" \
443442
"Worktree created with local branch $branch_name" \
444-
"$force_flag" "$branch_name" && return 0
443+
"${force_args[@]}" "$branch_name" && return 0
445444
else
446445
_try_worktree_add "$worktree_path" \
447446
"Creating new branch $branch_name from $from_ref" \
448447
"Worktree created with new branch $branch_name" \
449-
"$force_flag" -b "$branch_name" "$from_ref" && return 0
448+
"${force_args[@]}" -b "$branch_name" "$from_ref" && return 0
450449
fi
451450
;;
452451
esac
@@ -466,13 +465,13 @@ remove_worktree() {
466465
return 1
467466
fi
468467

469-
local force_flag=""
468+
local force_args=()
470469
if [ "$force" -eq 1 ]; then
471-
force_flag="--force"
470+
force_args=(--force)
472471
fi
473472

474473
local remove_output
475-
if remove_output=$(git worktree remove $force_flag "$worktree_path" 2>&1); then
474+
if remove_output=$(git worktree remove "${force_args[@]}" "$worktree_path" 2>&1); then
476475
log_info "Worktree removed: $worktree_path"
477476
return 0
478477
else

0 commit comments

Comments
 (0)