This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
git-sh.bash
317 lines (284 loc) · 8.94 KB
/
git-sh.bash
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env bash
#
# A customized bash environment suitable for git work.
#
# Copyright (C) 2008 Ryan Tomayko <http://tomayko.com/>
# Copyright (C) 2008 Aristotle Pagaltzis <http://plasmasturm.org/>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# Distributed under the GNU General Public License, version 2.0.
# use to install the sh alias
[[ $1 = '--configure' && $# = 1 ]] && {
set -e
git config --global alias.sh '!git-sh'
echo "alias 'sh' added to ~/.gitconfig"
exit 0
}
# we expect to be sourced into an interactive shell. when executed as a
# command, kick off a new shell and source us.
[ "$0" = 'bash' ] ||
exec /usr/bin/env bash --rcfile "$0" "$@"
# source the user's .bashrc file
[ -r ~/.bashrc ] && {
pushd ~ > /dev/null
. .bashrc
popd > /dev/null
}
# ALIASES + COMPLETION =========================================================
# gitcomp <alias> <command>
#
# Complete command named <alias> like standard git command named
# <command>. <command> must be a valid git command with completion.
#
# Examples:
# gitcomplete ci commit
# gitcomplete c checkout
gitcomplete() {
local alias="$1" command="$2"
complete -o default -o nospace -F _git_${command//-/_} $alias
}
# gitalias <alias>='<command> [<args>...]'
#
# Define a new shell alias (as with the alias builtin) named <alias>
# and enable command completion based on <command>. <command> must be
# a standard non-abbreviated git command name that has completion support.
#
# Examples:
# gitalias c=checkout
# gitalias ci='commit -v'
# gitalias r='rebase --interactive HEAD~10'
gitalias() {
local alias="${1%%=*}" command="${1#*=}"
local prog="${command##git }"
prog="${prog%% *}"
alias $alias="$command"
gitcomplete "$alias" "$prog"
}
# create aliases and configure bash completion for most porcelain commands
_git_cmd_cfg=(
'add alias'
'am alias stdcmpl'
'annotate alias'
'apply alias stdcmpl'
'archive alias'
'bisect alias stdcmpl'
'blame alias'
'branch alias stdcmpl'
'bundle stdcmpl'
'cat-file alias'
'checkout alias stdcmpl'
'cherry alias stdcmpl'
'cherry-pick alias stdcmpl'
'clean alias'
'clone alias'
'commit alias stdcmpl'
'config alias stdcmpl'
'describe alias stdcmpl'
'diff alias stdcmpl'
'difftool alias'
'fetch alias stdcmpl'
'format-patch alias stdcmpl'
'fsck alias'
'gc alias stdcmpl'
'gui alias'
'hash-object alias'
'init alias'
'instaweb alias'
'log alias logcmpl'
'lost-found alias'
'ls-files alias'
'ls-remote alias stdcmpl'
'ls-tree alias stdcmpl'
'merge alias stdcmpl'
'merge-base alias stdcmpl'
'mergetool alias'
'mv alias'
'name-rev stdcmpl'
'patch-id alias'
'peek-remote alias'
'prune alias'
'pull alias stdcmpl'
'push alias stdcmpl'
'quiltimport alias'
'rebase alias stdcmpl'
'reflog alias'
'remote alias stdcmpl'
'repack alias'
'repo-config alias'
'request-pull alias'
'reset alias stdcmpl'
'rev-list alias'
'rev-parse alias'
'revert alias'
'rm alias'
'send-email alias'
'send-pack alias'
'shortlog alias stdcmpl'
'show alias stdcmpl'
'show-branch alias logcmpl'
'stash alias stdcmpl'
'status alias'
'stripspace alias'
'submodule alias stdcmpl'
'svn alias stdcmpl'
'symbolic-ref alias'
'tag alias stdcmpl'
'tar-tree alias'
'var alias'
'whatchanged alias logcmpl'
)
for cfg in "${_git_cmd_cfg[@]}" ; do
read cmd opts <<< $cfg
for opt in $opts ; do
case $opt in
alias) alias $cmd="git $cmd" ;;
stdcmpl) complete -o default -o nospace -F _git_${cmd//-/_} $cmd ;;
logcmpl) complete -o default -o nospace -F _git_log $cmd ;;
esac
done
done
# Create aliases for everything defined in the gitconfig [alias] section.
_git_import_aliases () {
eval "$(
git config --get-regexp 'alias\..*' |
sed 's/^alias\.//' |
while read key command
do
if expr -- "$command" : '!' >/dev/null
then echo "alias $key='git $key'"
else echo "gitalias $key=\"git $command\""
fi
done
)"
}
# PROMPT =======================================================================
PS1='`_git_headname``_git_upstream_state`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
ANSI_RESET="\001$(git config --get-color "" "reset")\002"
# detect whether the tree is in a dirty state.
_git_dirty() {
if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
return 0
fi
local dirty_marker="`git config gitsh.dirty 2>/dev/null || echo ' *'`"
if ! git diff --quiet 2>/dev/null ; then
_git_apply_color "$dirty_marker" "color.sh.dirty" "red"
elif ! git diff --staged --quiet 2>/dev/null ; then
_git_apply_color "$dirty_marker" "color.sh.dirty-staged" "yellow"
else
return 0
fi
}
# detect whether any changesets are stashed
_git_dirty_stash() {
if ! git rev-parse --verify refs/stash >/dev/null 2>&1; then
return 0
fi
local dirty_stash_marker="`git config gitsh.dirty-stash 2>/dev/null || echo ' $'`"
_git_apply_color "$dirty_stash_marker" "color.sh.dirty-stash" "red"
}
# detect the current branch; use 7-sha when not on branch
_git_headname() {
local br=`git symbolic-ref -q HEAD 2>/dev/null`
[ -n "$br" ] &&
br=${br#refs/heads/} ||
br=`git rev-parse --short HEAD 2>/dev/null`
_git_apply_color "$br" "color.sh.branch" "yellow reverse"
}
# detect the deviation from the upstream branch
_git_upstream_state() {
local p=""
# Find how many commits we are ahead/behind our upstream
local count="$(git rev-list --count --left-right "@{upstream}"...HEAD 2>/dev/null)"
# calculate the result
case "$count" in
"") # no upstream
p="" ;;
"0 0") # equal to upstream
p=" u=" ;;
"0 "*) # ahead of upstream
p=" u+${count#0 }" ;;
*" 0") # behind upstream
p=" u-${count% 0}" ;;
*) # diverged from upstream
p=" u+${count#* }-${count% *}" ;;
esac
_git_apply_color "$p" "color.sh.upstream-state" "yellow bold"
}
# detect working directory relative to working tree root
_git_workdir() {
subdir=`git rev-parse --show-prefix 2>/dev/null`
subdir="${subdir%/}"
workdir="${PWD%/$subdir}"
_git_apply_color "${workdir/*\/}${subdir:+/$subdir}" "color.sh.workdir" "blue bold"
}
# detect if the repository is in a special state (rebase or merge)
_git_repo_state() {
local git_dir="$(git rev-parse --show-cdup 2>/dev/null).git"
if test -d "$git_dir/rebase-merge" -o -d "$git_dir/rebase-apply"; then
local state_marker="(rebase)"
elif test -f "$git_dir/MERGE_HEAD"; then
local state_marker="(merge)"
elif test -f "$git_dir/CHERRY_PICK_HEAD"; then
local state_marker="(cherry-pick)"
else
return 0
fi
_git_apply_color "$state_marker" "color.sh.repo-state" "red"
}
# determine whether color should be enabled. this checks git's color.ui
# option and then color.sh.
_git_color_enabled() {
[ `git config --get-colorbool color.sh true` = "true" ]
}
# apply a color to the first argument
_git_apply_color() {
local output="$1" color="$2" default="$3"
if _git_color_enabled ; then
color=`_git_color "$color" "$default"`
echo -ne "${color}${output}${ANSI_RESET}"
else
echo -ne "$output"
fi
}
# retrieve an ANSI color escape sequence from git config
_git_color() {
local color
color=`git config --get-color "$1" "$2" 2>/dev/null`
[ -n "$color" ] && echo -ne "\001$color\002"
}
# HELP ========================================================================
_help_display() {
local name value
# show git's inbuilt help, after some tweaking...
git --help | grep -v "See 'git help"
# show aliases defined in ~/.gitconfig
echo "Command aliases:"
git config --get-regexp 'alias\..*' |
sed 's/^alias\.//' |
sort |
while read name value
do printf " %-10s %-65s\n" "$name" "$value"
done
printf "\nSee 'help COMMAND' for more information on a specific command.\n"
}
help() {
local _git_pager=$(git config core.pager)
if [ $# = 1 ];
then git help $1
else (_help_display | ${_git_pager:-${PAGER:-less}})
fi
}
complete -o default -o nospace -F _git help
# vim: tw=80 noexpandtab