-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-alias.sh
executable file
·213 lines (187 loc) · 5.2 KB
/
git-alias.sh
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
# Set, remove, list, or search git aliases by text/regex pattern.
# -Christopher Welborn 01-15-2016
app_name="git-alias"
app_version="0.1.2"
app_path="$(readlink -f "${BASH_SOURCE[0]}")"
app_script="${app_path##*/}"
app_dir="${app_path%/*}"
colr_file="$app_dir/colr.sh"
if hash colrc &>/dev/null; then
function colr {
# Even wrapped in a function, this is still faster than colr.sh and colr.py.
colrc "$@"
}
elif [[ -e "$colr_file" ]]; then
# shellcheck source=/home/cj/scripts/git-commands/colr.sh
source "$colr_file"
colr_auto_disable
else
logger --id=$$ "$app_script: missing $colr_file"
function colr {
echo -e "$1"
}
fi
cmdname_space=20
cmdname_indent="$(printf "%*s" "$((cmdname_space + 1))" " ")"
function alias_list {
# Search or list all aliases.
local pat=$1
local cmdname
local cmd
local found=0
local aliastype="global"
((do_local)) && aliastype="local"
echo -n "Listing $aliastype git aliases"
if [[ -z "$pat" ]]; then
echo ":"
else
echo " matching '$pat':"
fi
while read -r -s line; do
cmdname="$(cut -d' ' -f1 <<<"$line")"
# Make sure this is an actual command.
# Newlines in alias commands will wreck `read`.
if ! cmd="$(git "${gitargs[@]}" --get "alias.$cmdname" 2>/dev/null)"; then
continue
fi
# Fix any newlines that may be in the command.
# The real newline looks ugly, the escaped newline is unescaped by
# colr to make a real one. I don't know what else to do, except
# at least indent the real newline so that it looks like it belongs.
cmd="${cmd//$'\n'/\\n$cmdname_indent}"
echo -e "$(colr_alias "$cmdname" "$cmd")"
found=1
done < <(git "${gitargs[@]}" --get-regexp "alias..+?$pat.+?" | sed s/alias.//)
# Return an error exit status if nothing was found.
(( found )) || return 1
return 0
}
function alias_remove {
# Remove an alias from config.
local name=$1
local aliastype="global"
((do_local)) && aliastype="local"
if git "${gitargs[@]}" --unset "alias.${name}"; then
echo -e "Removed $aliastype: $(colr_cmdname "$name")"
else
echo_err "Failed to remove: $(colr_cmdname "$name")"
fi
}
function alias_set {
# Set an aliases value.
local name=$1
local value=$2
local aliastype="global"
((do_local)) && aliastype="local"
if [[ -z "$name" ]] || [[ -z "$value" ]]; then
echo_err "Name and value are required!"
else
if git "${gitargs[@]}" "alias.${name}" "$value"; then
echo -e "Set $aliastype: $(colr_alias "$name" "$value")"
else
echo_err "Failed to set: $(colr_cmdname "$name")"
fi
fi
}
function colr_alias {
# Colorize a command name and command.
echo -e -n "$(colr_cmdname "$1")" "$(colr_cmd "$2")"
}
function colr_cmd {
# Colorize command.
echo -e -n "$(colr "$(printf "%s" "$1")" blue)"
}
function colr_cmdname {
# Colorize command name.
local cmdfmt
cmdfmt="$(printf "%-*s" "$cmdname_space" "$1")"
echo -e -n "$(colr "$cmdfmt" green)"
}
function echo_err {
# Echo to stderr and return an error exit status.
echo "$@" 1>&2
return 1
}
function fail {
# Print a message and exit the program with an error exit status.
echo_err "$@"
exit 1
}
function fail_usage {
# Print a usage failure and exit the program.
print_usage "$@"
exit 1
}
function print_usage {
# Show usage reason if first arg is available.
[[ -n "$1" ]] && echo -e "\n$1\n"
echo "$app_name v. $app_version
Lists git aliases.
Usage:
$app_script -h | -v
$app_script [-l] [PATTERN]
$app_script [-l] NAME VALUE
$app_script [-l] -r NAME
Options:
NAME : Name of alias for setting or removing.
VALUE : Value for alias.
PATTERN : A regex/text pattern to search for.
-h,--help : Show this message.
-l,--local : Do not use global config, use local.
-r,--remove : Remove an alias.
-v,--version : Show $app_name version and exit.
"
}
declare -a userargs
do_remove=0
do_local=0
for arg; do
case "$arg" in
"-h"|"--help" )
print_usage ""
exit 0
;;
"-l"|"--local" )
do_local=1
;;
"-r"|"--remove" )
do_remove=1
;;
"-v"|"--version" )
echo -e "$app_name v. $app_version\n"
exit 0
;;
-*)
fail_usage "Unknown flag argument: $arg"
;;
*)
userargs=("${userargs[@]}" "$arg")
esac
done
if (( do_local )); then
gitargs=("config")
else
gitargs=("config" "--global")
fi
if (( do_remove )); then
# Remove an alias and exit.
(( ${#userargs[@]} == 1 )) || fail_usage "Incorrect number of arguments!"
alias_remove "${userargs[0]}"
exit
fi
# Listing/setting:
case ${#userargs[@]} in
0)
alias_list
;;
1)
alias_list "${userargs[0]}"
;;
2)
alias_set "${userargs[@]}"
;;
*)
print_usage "Too many arguments!"
exit 1
esac