Skip to content

Commit ef79e44

Browse files
committed
holy wow fzf is cool
1 parent e0c946d commit ef79e44

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

.githelpers

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ rename_branch_regex() {
189189
fi
190190
}
191191

192+
193+
# Lists the 10 most recently committed branches, sorted by relative commit time.
194+
# Highlights the current branch with an asterisk (*) and displays relative time in a concise format.
192195
list_branches_by_relative_commit() {
193196
current_branch=$(git branch --show-current)
194197
git for-each-ref refs/heads/ --sort=-committerdate --format='%(refname:short) ~ %(committerdate:relative)' \
@@ -203,14 +206,22 @@ list_branches_by_relative_commit() {
203206
}'
204207
}
205208

209+
# Interactively switch to a recent branch using a selection menu with j/k or arrow keys, and Enter to confirm.
206210
switch_recent_branches_by_index() {
207211
local branches=($(list_branches_by_relative_commit | awk '{print $NF}'))
208-
list_branches_by_relative_commit
209-
read -p "Select a branch by index: " index
210-
211-
if [[ $index -lt 0 || $index -ge ${#branches[@]} ]]; then
212-
echo "Invalid index ${index}" >&2
212+
local current_branch=$(git branch --show-current)
213+
214+
# Use fzf for interactive selection
215+
local selected_branch=$(list_branches_by_relative_commit | \
216+
fzf --ansi --height=10 --reverse --header="Select a branch to switch to:" \
217+
--bind="enter:accept" \
218+
--bind="j:down,k:up" \
219+
--color=hl:green --color=fg+:green --color=bg+:black)
220+
221+
if [ -n "$selected_branch" ]; then
222+
local branch_name=$(echo "$selected_branch" | awk '{print $NF}')
223+
git switch "$branch_name"
224+
else
225+
echo "No branch selected."
213226
fi
214-
215-
git switch "${branches[$((index - 1))]}"
216227
}

0 commit comments

Comments
 (0)