-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit-commands
executable file
·133 lines (114 loc) · 3.76 KB
/
git-commands
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env bash
#
# This script started out as a fork of:
# https://github.com/hankchanocd/git-commands
#
# Based on `git help -a` provided by `hub`, show all the available git
# commands, both native and custom.
#
# git help -a
# Ref: https://stackoverflow.com/questions/7866353/git-list-all-available-commands
# -- Ensure Correct git help options ------------------------------------------
help_options='-a'
# https://github.com/git/git/blob/b697d92f/Documentation/RelNotes/2.20.0.txt#L17-L18
git help --no-verbose > /dev/null 2>&1 && help_options='--no-verbose -a'
# Retrieve the git help, only once
# shellcheck disable=SC2086
git_help_all=$(git help $help_options)
# -- Preparation --------------------------------------------------------------
# The line that divides the whole text into two: regular and custom commands
dividing_line_number=$(
grep -n "git commands available from elsewhere on your \$PATH" \
<<< "$git_help_all" | sed 's/[^0-9]*//g'
)
whole_line_number=$(
wc -l <<< "$git_help_all"
)
# shellcheck disable=SC2034
black="$(tput setaf 0)"
red="$(tput setaf 1)"
# shellcheck disable=SC2034
green="$(tput setaf 2)"
# shellcheck disable=SC2034
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
# shellcheck disable=SC2034
magenta="$(tput setaf 5)"
# shellcheck disable=SC2034
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
reset="$(tput sgr0)"
# -- Child Functions ----------------------------------------------------------
function native_commands() {
while read -r line; do
echo -e "${white}${line}${reset}"
done < <(
head -n "$dividing_line_number" \
<<< "$git_help_all" | grep "^ [a-z]" | tr ' ' '\n' | grep -v "^$"
)
}
function custom_commands() {
while read -r line; do
echo -e "${blue}${line}${reset}"
done < <(
tail -n $((whole_line_number - dividing_line_number)) \
<<< "$git_help_all" | grep "^ [a-z]" | tr ' ' '\n' | grep -v "^$"
)
}
function all_commands() {
native_commands && custom_commands
}
# -- Main ---------------------------------------------------------------------
function main() {
# If fzf exists
if command -v fzf > /dev/null 2>&1; then
while result=$(
fzf --ansi --reverse --height=90% --cycle \
--header="Preview: alt-j:↓,alt-k:↑,^f:pg↓,^b:pg↑" \
--bind "alt-j:preview-down,alt-k:preview-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up" \
--preview-window=right:75% \
--preview="COLUMNS=$(( $(tput cols) * 3/4 )) git help git-{} || {
echo '\n' && (cd /tmp && git-{} -h 2> /dev/null)
}" <<< "$("${1:-all_commands}" | sort -u -k 1.6)"
# Ref: https://unix.stackexchange.com/questions/157923/sorting-strings-with-ansi-escape-codes
); do
[[ -n "$result" ]] && git help "$result"
done
else
cat <<EOT
${red}"fzf" is not detected in your \$PATH. Falling back to: "git help $help_options"${reset}
$git_help_all
EOT
fi
}
# -- Help ---------------------------------------------------------------------
usage() {
cat <<EOT >&2
usage: ${0##*/} [<options>]
List all available git commands with help using fzf.
Native git commands in white, ${blue}custom git commands in blue${reset}.
where:
-h show this help text
-n list only the native git commands
-c list only the custom git commands
-a list all commands (default)
Preview window keybindings:
alt-j:preview-down
alt-k:preview-up
ctrl-f:preview-page-down
ctrl-b:preview-page-up
EOT
}
# -- Parse options ------------------------------------------------------------
while getopts ':hnca:' option; do
case "$option" in
h) usage;;
n) main native_commands;;
c) main custom_commands;;
a) main;;
:) main;;
\?) printf "Illegal option: -%s\n" "$OPTARG" >&2
usage && exit 1;;
esac
done
[[ $OPTIND -eq 1 ]] && main