Skip to content
This repository was archived by the owner on Mar 2, 2020. It is now read-only.

Commit 0fcc32e

Browse files
author
Hank Chan
committed
feat(git-commands):
1 parent 7b4cd21 commit 0fcc32e

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

git-commands

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#!/usr/bin/env bash
2+
#!/usr/bin/env zsh
3+
#
4+
# Based on `git help -a` provided by `hub`, show all the available git commands, both native and custom.
5+
#
6+
# git help -a
7+
# Ref: https://stackoverflow.com/questions/7866353/git-list-all-available-commands
8+
9+
################ Preparation ################
10+
# The line that divides the whole text into two: regular and custom commands
11+
dividing_line_number=$(git help -a |
12+
grep -n 'git commands available from elsewhere on your $PATH' |
13+
sed 's/[^0-9]*//g')
14+
15+
whole_line_number=$(git help -a | wc -l)
16+
17+
############# Child Functions ###############
18+
function native_commands() {
19+
git help -a |
20+
head -n "$dividing_line_number" |
21+
grep "^ [a-z]" |
22+
tr ' ' '\n' |
23+
grep -v "^$" |
24+
while read line; do
25+
echo -e "\e[00m$line\e[0m"
26+
done
27+
}
28+
29+
function custom_commands() {
30+
git help -a |
31+
tail -n $(($whole_line_number - $dividing_line_number)) |
32+
grep "^ [a-z]" |
33+
tr ' ' '\n' |
34+
grep -v "^$" |
35+
while read line; do
36+
echo -e "\e[94m$line\e[0m"
37+
done
38+
}
39+
40+
function all_commands() {
41+
native_commands && custom_commands
42+
}
43+
44+
################ Main ################
45+
function main() {
46+
list=$("$1" |
47+
# -u: unique key to prevent duplicates
48+
# -k: sort only after the specified character, after 6th character on 1st field
49+
# Ref: https://unix.stackexchange.com/questions/157923/sorting-strings-with-ansi-escape-codes
50+
sort -u -k 1.6)
51+
52+
# If fzf exists
53+
if type fzf >/dev/null 2>&1; then
54+
55+
_help_preview="man git-{} || {echo '\n*****************************' && git-{} -h}"
56+
result=$(
57+
echo -e "$list" |
58+
fzf --ansi --reverse --height=90% --cycle \
59+
--preview="$_help_preview" \
60+
--preview-window=right:75%
61+
)
62+
63+
# Execute a git command if a command selected
64+
[[ -n "$result" ]] && git $result
65+
66+
else
67+
echo -e '\e[94m`fzf` is not detected on $PATH, defaulting to `git help -a` \e[0m \n'
68+
git help -a
69+
70+
fi
71+
}
72+
73+
################ Help ################
74+
usage="usage: $(basename "$0") [<options>]
75+
76+
List all available git commands with help using fzf.
77+
Native git commands in white, \e[94mcustom git commands in blue\e[0m.
78+
79+
where:
80+
-h show this help text
81+
-n list only the native git commands
82+
-c list only the custom git commands
83+
-a list all commands, which is also default"
84+
85+
############# Parse options ##############
86+
while getopts ':hnca:' option; do
87+
case "$option" in
88+
h)
89+
echo -e "$usage"
90+
exit
91+
;;
92+
n)
93+
main native_commands
94+
exit
95+
;;
96+
c)
97+
main custom_commands
98+
exit
99+
;;
100+
a)
101+
main all_commands
102+
exit
103+
;;
104+
:)
105+
main all_commands
106+
exit
107+
;;
108+
\?)
109+
printf "illegal option: -%s\n" "$OPTARG" >&2
110+
echo -e "$usage" >&2
111+
exit 1
112+
;;
113+
esac
114+
done
115+
shift $((OPTIND - 1))
116+
117+
# Default case when no option provided
118+
main all_commands

0 commit comments

Comments
 (0)