-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-search
More file actions
executable file
·82 lines (62 loc) · 1.89 KB
/
git-search
File metadata and controls
executable file
·82 lines (62 loc) · 1.89 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env bash
source "utils.sh"
IFS=$'\n'
partials=("$@")
all_branches=$(git branch -a | grep -v "remotes/origin/HEAD" | grep -i "${partials[0]}" | cut -c3-)
for ((i = 1; i < ${#partials[@]}; i++)); do
all_branches=$(printf "%s\n" "${all_branches[@]}" | grep -i "${partials[$i]}")
done
if [[ "${all_branches}" == "" ]]; then
writeln "No branches found matching your parameters"
exit 0
fi
# Remove the "remotes/origin/" prefix if it's there
found_branches=()
for branch in ${all_branches}; do
if [[ "${branch}" == remotes/origin/* ]]; then
found_branches+=("${branch:15}")
else
found_branches+=("${branch}")
fi
done
# Remove duplicates
mapfile -t found_branches < <(printf "%s\n" "${found_branches[@]}" | sort -u)
# We need the count in a couple of places, so put it in a var
found_branches_cnt=${#found_branches[@]}
if [[ "${found_branches_cnt}" == 0 ]]; then
writeln "No branches matching criteria found"
exit 0
fi
writeln "<green>${found_branches_cnt}</green> match(es) found:"
echo ""
i=1
arr=()
for branch in "${found_branches[@]}"; do
str=${branch}
for partial in "${partials[@]}"; do
str="${str//${partial}/<magenta>${partial}</magenta>}"
done
writeln "${i}. ${str}"
((i = i + 1))
arr+=("${branch}")
done
write "\nWhich branch would you like to checkout? "
read -r choice
if ! [[ "${choice}" =~ [0-9] ]]; then
if [[ "${found_branches_cnt}" == "1" ]]; then
writeln "Invalid selection."
else
writeln "Invalid selection. Choose between 1 and ${found_branches_cnt} only"
fi
exit 2
fi
new_branch="${arr[$choice - 1]}"
if [[ "${new_branch}" == "" ]]; then
if [[ "${found_branches_cnt}" == "1" ]]; then
writeln "Invalid selection."
else
writeln "Invalid selection. Choose between 1 and ${found_branches_cnt} only"
fi
exit 2
fi
git new "${new_branch}"