Skip to content

Commit 98858e3

Browse files
committed
refactor: enhance current_branch function for better detection
Update the current_branch function to handle cases where the branch is either empty or set to HEAD, normalizing the output to indicate a detached state. Additionally, refactor cmd_list to utilize the updated current_branch function, improving code clarity and maintainability.
1 parent 25ede69 commit 98858e3

2 files changed

Lines changed: 4 additions & 10 deletions

File tree

bin/gtr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,10 +1055,7 @@ cmd_list() {
10551055
if [ "$porcelain" -eq 1 ]; then
10561056
# Output: path<tab>branch<tab>status
10571057
local branch status
1058-
branch=$(get_current_branch "$repo_root")
1059-
if [ -z "$branch" ] || [ "$branch" = "HEAD" ]; then
1060-
branch="(detached)"
1061-
fi
1058+
branch=$(current_branch "$repo_root")
10621059
status=$(worktree_status "$repo_root")
10631060
printf "%s\t%s\t%s\n" "$repo_root" "$branch" "$status"
10641061

@@ -1086,10 +1083,7 @@ cmd_list() {
10861083

10871084
# Always show repo root first
10881085
local branch
1089-
branch=$(get_current_branch "$repo_root")
1090-
if [ -z "$branch" ] || [ "$branch" = "HEAD" ]; then
1091-
branch="(detached)"
1092-
fi
1086+
branch=$(current_branch "$repo_root")
10931087
printf "%-30s %s\n" "$branch [main repo]" "$repo_root"
10941088

10951089
# Show worktrees sorted by branch name

lib/core.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ current_branch() {
166166
local branch
167167
branch=$(get_current_branch "$worktree_path")
168168

169-
# Normalize detached HEAD
170-
if [ "$branch" = "HEAD" ]; then
169+
# Normalize detached HEAD or empty (failed detection)
170+
if [ -z "$branch" ] || [ "$branch" = "HEAD" ]; then
171171
branch="(detached)"
172172
fi
173173

0 commit comments

Comments
 (0)