@@ -219,12 +219,15 @@ _emit_worktree_record() {
219219 [ -z " $branch " ] && branch=" (detached)"
220220 status=$( _worktree_record_status " $wt_detached " " $wt_locked " " $wt_prunable " )
221221
222- printf " %s\t%s\t%s\t%s\n" " $is_main " " $wt_path " " $branch " " $status "
222+ printf " is_main %s\n" " $is_main "
223+ printf " path %s\n" " $wt_path "
224+ printf " branch %s\n" " $branch "
225+ printf " status %s\n\n" " $status "
223226}
224227
225228# List registered git worktrees for a repository.
226229# Usage: list_worktree_records repo_root
227- # Output: is_main<TAB> path<TAB> branch<TAB> status
230+ # Output: blank-line-delimited records with is_main/ path/ branch/ status fields
228231list_worktree_records () {
229232 local repo_root=" $1 "
230233 local repo_root_canonical
@@ -293,17 +296,47 @@ worktree_status() {
293296 local repo_root
294297 repo_root=$( _resolve_main_repo_root) || return 1
295298
296- local is_main path branch record_status
297- while IFS=$' \t ' read -r is_main path branch record_status; do
298- if [ " $path " = " $target_path " ] || [ " $path " = " $target_path_canonical " ]; then
299- found=1
300- status=" $record_status "
301- break
302- fi
299+ local is_main=" " path=" " branch=" " record_status=" " line path_canonical
300+ while IFS= read -r line; do
301+ case " $line " in
302+ " " )
303+ [ -z " $path " ] && continue
304+ path_canonical=$( canonicalize_path " $path " || printf " %s" " $path " )
305+ if [ " $path " = " $target_path " ] || [ " $path_canonical " = " $target_path_canonical " ]; then
306+ found=1
307+ status=" $record_status "
308+ break
309+ fi
310+ is_main=" "
311+ path=" "
312+ branch=" "
313+ record_status=" "
314+ ;;
315+ " is_main " * )
316+ is_main=" ${line# is_main } "
317+ ;;
318+ " path " * )
319+ path=" ${line# path } "
320+ ;;
321+ " branch " * )
322+ branch=" ${line# branch } "
323+ ;;
324+ " status " * )
325+ record_status=" ${line# status } "
326+ ;;
327+ esac
303328 done << EOF
304329$( list_worktree_records " $repo_root " )
305330EOF
306331
332+ if [ " $found " -eq 0 ] && [ -n " $path " ]; then
333+ path_canonical=$( canonicalize_path " $path " || printf " %s" " $path " )
334+ if [ " $path " = " $target_path " ] || [ " $path_canonical " = " $target_path_canonical " ]; then
335+ found=1
336+ status=" $record_status "
337+ fi
338+ fi
339+
307340 # If worktree not found in git's list
308341 if [ " $found " -eq 0 ]; then
309342 status=" missing"
@@ -361,16 +394,37 @@ resolve_target() {
361394 fi
362395
363396 # Last resort: ask git for all worktrees (catches non-gtr-managed worktrees)
364- local is_main wt_path wt_branch _wt_status
365- while IFS=$' \t ' read -r is_main wt_path wt_branch _wt_status; do
366- if [ " $wt_branch " = " $identifier " ]; then
367- printf " %s\t%s\t%s\n" " $is_main " " $wt_path " " $wt_branch "
368- return 0
369- fi
397+ local is_main=" " wt_path=" " wt_branch=" " line
398+ while IFS= read -r line; do
399+ case " $line " in
400+ " " )
401+ if [ " $wt_branch " = " $identifier " ]; then
402+ printf " %s\t%s\t%s\n" " $is_main " " $wt_path " " $wt_branch "
403+ return 0
404+ fi
405+ is_main=" "
406+ wt_path=" "
407+ wt_branch=" "
408+ ;;
409+ " is_main " * )
410+ is_main=" ${line# is_main } "
411+ ;;
412+ " path " * )
413+ wt_path=" ${line# path } "
414+ ;;
415+ " branch " * )
416+ wt_branch=" ${line# branch } "
417+ ;;
418+ esac
370419 done << EOF
371420$( list_worktree_records " $repo_root " )
372421EOF
373422
423+ if [ " $wt_branch " = " $identifier " ]; then
424+ printf " %s\t%s\t%s\n" " $is_main " " $wt_path " " $wt_branch "
425+ return 0
426+ fi
427+
374428 log_error " Worktree not found for branch: $identifier "
375429 return 1
376430}
@@ -622,13 +676,32 @@ list_worktree_branches() {
622676 local records
623677 records=$( list_worktree_records " $repo_root " )
624678
625- local is_main path branch status
626- while IFS=$' \t ' read -r is_main path branch status; do
627- [ " $is_main " = " 1" ] && continue
628- [ -z " $branch " ] && continue
629- [ " $branch " = " (detached)" ] && continue
630- printf " %s\n" " $branch "
679+ local is_main=" " path=" " branch=" " line
680+ while IFS= read -r line; do
681+ case " $line " in
682+ " " )
683+ if [ " $is_main " != " 1" ] && [ -n " $branch " ] && [ " $branch " != " (detached)" ]; then
684+ printf " %s\n" " $branch "
685+ fi
686+ is_main=" "
687+ path=" "
688+ branch=" "
689+ ;;
690+ " is_main " * )
691+ is_main=" ${line# is_main } "
692+ ;;
693+ " path " * )
694+ path=" ${line# path } "
695+ ;;
696+ " branch " * )
697+ branch=" ${line# branch } "
698+ ;;
699+ esac
631700 done << EOF
632701$records
633702EOF
703+
704+ if [ " $is_main " != " 1" ] && [ -n " $branch " ] && [ " $branch " != " (detached)" ]; then
705+ printf " %s\n" " $branch "
706+ fi
634707}
0 commit comments