Skip to content

Commit f05a1c2

Browse files
committed
refactor: extract unpack_target helper for TSV parsing (#113)
Replace 10 instances of echo|cut TSV unpacking from resolve_target() output with a single unpack_target() function using IFS-based read splitting. Eliminates ~30 subshell forks per invocation.
1 parent fc333ac commit f05a1c2

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

bin/gtr

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -371,9 +371,8 @@ cmd_create() {
371371
# Check if folder_name would resolve to main repo (collision with current branch)
372372
local resolve_result
373373
if resolve_result=$(resolve_target "$folder_name" "$repo_root" "$base_dir" "$prefix" 2>/dev/null); then
374-
local is_main
375-
is_main=$(printf "%s" "$resolve_result" | cut -f1)
376-
if [ "$is_main" = "1" ]; then
374+
unpack_target "$resolve_result"
375+
if [ "$_ctx_is_main" = "1" ]; then
377376
# Collision: folder name matches current branch, use branch name instead
378377
next_steps_id="$branch_name"
379378
else
@@ -440,9 +439,8 @@ cmd_remove() {
440439
# Resolve target branch
441440
local target is_main worktree_path branch_name
442441
target=$(resolve_target "$identifier" "$repo_root" "$base_dir" "$prefix") || continue
443-
is_main=$(echo "$target" | cut -f1)
444-
worktree_path=$(echo "$target" | cut -f2)
445-
branch_name=$(echo "$target" | cut -f3)
442+
unpack_target "$target"
443+
is_main="$_ctx_is_main" worktree_path="$_ctx_worktree_path" branch_name="$_ctx_branch"
446444

447445
# Cannot remove main repository
448446
if [ "$is_main" = "1" ]; then
@@ -536,9 +534,8 @@ cmd_rename() {
536534
# Resolve old worktree
537535
local target is_main old_path old_branch
538536
target=$(resolve_target "$old_identifier" "$repo_root" "$base_dir" "$prefix") || exit 1
539-
is_main=$(echo "$target" | cut -f1)
540-
old_path=$(echo "$target" | cut -f2)
541-
old_branch=$(echo "$target" | cut -f3)
537+
unpack_target "$target"
538+
is_main="$_ctx_is_main" old_path="$_ctx_worktree_path" old_branch="$_ctx_branch"
542539

543540
# Cannot rename main repository
544541
if [ "$is_main" = "1" ]; then
@@ -622,9 +619,8 @@ cmd_go() {
622619
# Resolve target branch
623620
local target is_main worktree_path branch
624621
target=$(resolve_target "$identifier" "$repo_root" "$base_dir" "$prefix") || exit 1
625-
is_main=$(echo "$target" | cut -f1)
626-
worktree_path=$(echo "$target" | cut -f2)
627-
branch=$(echo "$target" | cut -f3)
622+
unpack_target "$target"
623+
is_main="$_ctx_is_main" worktree_path="$_ctx_worktree_path" branch="$_ctx_branch"
628624

629625
# Human messages to stderr so stdout can be used in command substitution
630626
if [ "$is_main" = "1" ]; then
@@ -680,9 +676,8 @@ cmd_run() {
680676
# Resolve target branch
681677
local target is_main worktree_path branch
682678
target=$(resolve_target "$identifier" "$repo_root" "$base_dir" "$prefix") || exit 1
683-
is_main=$(echo "$target" | cut -f1)
684-
worktree_path=$(echo "$target" | cut -f2)
685-
branch=$(echo "$target" | cut -f3)
679+
unpack_target "$target"
680+
is_main="$_ctx_is_main" worktree_path="$_ctx_worktree_path" branch="$_ctx_branch"
686681

687682
# Human messages to stderr (like cmd_go)
688683
if [ "$is_main" = "1" ]; then
@@ -757,7 +752,8 @@ cmd_copy() {
757752
# Resolve source path
758753
local src_target src_path
759754
src_target=$(resolve_target "$source" "$repo_root" "$base_dir" "$prefix") || exit 1
760-
src_path=$(echo "$src_target" | cut -f2)
755+
unpack_target "$src_target"
756+
src_path="$_ctx_worktree_path"
761757

762758
# Get patterns (flag > config)
763759
if [ -z "$patterns" ]; then
@@ -798,8 +794,8 @@ cmd_copy() {
798794
for target_id in $targets; do
799795
local dst_target dst_path dst_branch
800796
dst_target=$(resolve_target "$target_id" "$repo_root" "$base_dir" "$prefix") || continue
801-
dst_path=$(echo "$dst_target" | cut -f2)
802-
dst_branch=$(echo "$dst_target" | cut -f3)
797+
unpack_target "$dst_target"
798+
dst_path="$_ctx_worktree_path" dst_branch="$_ctx_branch"
803799

804800
# Skip if source == destination
805801
[ "$src_path" = "$dst_path" ] && continue
@@ -860,8 +856,8 @@ cmd_editor() {
860856
# Resolve target branch
861857
local target worktree_path branch
862858
target=$(resolve_target "$identifier" "$repo_root" "$base_dir" "$prefix") || exit 1
863-
worktree_path=$(echo "$target" | cut -f2)
864-
branch=$(echo "$target" | cut -f3)
859+
unpack_target "$target"
860+
worktree_path="$_ctx_worktree_path" branch="$_ctx_branch"
865861

866862
if [ "$editor" = "none" ]; then
867863
# Just open in GUI file browser
@@ -934,8 +930,8 @@ cmd_ai() {
934930
# Resolve target branch
935931
local target worktree_path branch
936932
target=$(resolve_target "$identifier" "$repo_root" "$base_dir" "$prefix") || exit 1
937-
worktree_path=$(echo "$target" | cut -f2)
938-
branch=$(echo "$target" | cut -f3)
933+
unpack_target "$target"
934+
worktree_path="$_ctx_worktree_path" branch="$_ctx_branch"
939935

940936
log_step "Starting $ai_tool for: $branch"
941937
echo "Directory: $worktree_path"

lib/core.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ resolve_target() {
271271
return 1
272272
}
273273

274+
# Unpack TSV output from resolve_target into globals.
275+
# Sets: _ctx_is_main, _ctx_worktree_path, _ctx_branch
276+
# Usage: unpack_target "$target_string"
277+
unpack_target() {
278+
local IFS=$'\t'
279+
# shellcheck disable=SC2162
280+
read _ctx_is_main _ctx_worktree_path _ctx_branch <<< "$1"
281+
}
282+
274283
# Create a new git worktree
275284
# Usage: create_worktree base_dir prefix branch_name from_ref track_mode [skip_fetch] [force] [custom_name] [folder_override]
276285
# track_mode: auto, remote, local, or none

0 commit comments

Comments
 (0)