-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-recent-refs
More file actions
executable file
·67 lines (58 loc) · 1.43 KB
/
git-recent-refs
File metadata and controls
executable file
·67 lines (58 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env bash
# Usage: git-recent-refs [N|pattern]
set -o pipefail
local_only=false
if [[ "$1" == "--local" || "$1" == "-l" ]]; then
local_only=true
shift
fi
ref_root="refs"
if [[ "$local_only" == true ]]; then
ref_root="refs/heads"
fi
mapfile -t all_recent_branches < <(
git for-each-ref --sort=-committerdate "$ref_root" | awk '
/refs\/.*(HEAD|master)$/{next}
{
gsub("refs/(remotes/[^/]+|tags|heads)/", "")
if (!a[$3]++) print $3
}'
)
if [[ -z "$1" || "$1" =~ ^[0-9]+$ ]]; then
limit=${1:-25}
select_branches=("${all_recent_branches[@]:0:limit}")
else
select_branches=()
needle=${1,,}
for b in "${all_recent_branches[@]}"; do
if [[ "${b,,}" == *"$needle"* ]]; then
select_branches+=("$b")
fi
done
fi
if ((${#select_branches[@]} == 0)); then
echo "No matching branches found" >&2
exit 1
fi
if command -v fzf >/dev/null 2>&1; then
branch=$(
printf '%s\n' "${select_branches[@]}" |
fzf --prompt='Checkout branch: ' --height=40% --reverse
)
if [[ -n "$branch" ]]; then
git checkout "$branch"
exit $?
else
exit 1
fi
else
PS3='Checkout branch: '
select branch in "${select_branches[@]}"; do
if [[ -n "$branch" ]]; then
git checkout "$branch"
exit $?
else
echo "Invalid selection" >&2
fi
done
fi