Skip to content

Commit 9cd81a4

Browse files
committed
refactor: simplify get_current_branch function logic
Refactor the get_current_branch function to improve clarity and maintainability. The updated implementation uses conditional statements to handle directory input more cleanly, eliminating redundancy in the command execution for both specified and current directories.
1 parent 1aa5b20 commit 9cd81a4

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

lib/core.sh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ resolve_default_branch() {
143143
# Usage: get_current_branch [directory]
144144
# Returns: branch name, "HEAD" if detached, or empty
145145
get_current_branch() {
146-
local dir_flag=""
147-
# shellcheck disable=SC2086
148-
[ -n "${1:-}" ] && dir_flag="-C $1"
149-
git $dir_flag branch --show-current 2>/dev/null ||
150-
git $dir_flag rev-parse --abbrev-ref HEAD 2>/dev/null
146+
if [ -n "${1:-}" ]; then
147+
git -C "$1" branch --show-current 2>/dev/null ||
148+
git -C "$1" rev-parse --abbrev-ref HEAD 2>/dev/null
149+
else
150+
git branch --show-current 2>/dev/null ||
151+
git rev-parse --abbrev-ref HEAD 2>/dev/null
152+
fi
151153
}
152154

153155
# Get the current branch of a worktree (with detached HEAD normalization)

0 commit comments

Comments
 (0)