Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 285 lines (256 sloc) 8.219 kB
b48c4de @rtomayko Let's get this party started
authored
1 #!/usr/bin/env bash
2 #
3 # A customized bash environment suitable for git work.
4 #
9e62017 @rtomayko add ap as copyright holder
authored
5 # Copyright (C) 2008 Ryan Tomayko <http://tomayko.com/>
6 # Copyright (C) 2008 Aristotle Pagaltzis <http://plasmasturm.org/>
b48c4de @rtomayko Let's get this party started
authored
7 #
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with this program; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 # Distributed under the GNU General Public License, version 2.0.
22
23 # use to install the sh alias
056c241 @ap treat bash and perl as programming languages
ap authored
24 [[ $1 = '--configure' && $# = 1 ]] && {
b48c4de @rtomayko Let's get this party started
authored
25 set -e
26 git config --global alias.sh '!git-sh'
27 echo "alias 'sh' added to ~/.gitconfig"
28 exit 0
29 }
30
31 # we expect to be sourced into an interactive shell. when executed as a
b056482 @rtomayko bad --rcfile arg with arguments to git-sh
authored
32 # command, kick off a new shell and source us.
b48c4de @rtomayko Let's get this party started
authored
33 [ "$0" = 'bash' ] ||
b056482 @rtomayko bad --rcfile arg with arguments to git-sh
authored
34 exec /usr/bin/env bash --rcfile "$0" "$@"
b48c4de @rtomayko Let's get this party started
authored
35
36 # source the user's .bashrc file
37 [ -r ~/.bashrc ] && {
4449f2b @rtomayko redirect pushd/popd output to /dev/null
authored
38 pushd ~ > /dev/null
b48c4de @rtomayko Let's get this party started
authored
39 . .bashrc
4449f2b @rtomayko redirect pushd/popd output to /dev/null
authored
40 popd > /dev/null
b48c4de @rtomayko Let's get this party started
authored
41 }
42
43
c4a8b5e @rtomayko gitalias command; update .gitshrc example file
authored
44 # ALIASES + COMPLETION =========================================================
45
46 # gitcomp <alias> <command>
47 #
48 # Complete command named <alias> like standard git command named
49 # <command>. <command> must be a valid git command with completion.
50 #
51 # Examples:
52 # gitcomplete ci commit
53 # gitcomplete c checkout
54 gitcomplete() {
55 local alias="$1" command="$2"
56 complete -o default -o nospace -F _git_${command//-/_} $alias
57 }
58
59 # gitalias <alias>='<command> [<args>...]'
60 #
61 # Define a new shell alias (as with the alias builtin) named <alias>
62 # and enable command completion based on <command>. <command> must be
63 # a standard non-abbreviated git command name that has completion support.
64 #
65 # Examples:
66 # gitalias c=checkout
67 # gitalias ci='commit -v'
68 # gitalias r='rebase --interactive HEAD~10'
69 gitalias() {
70 local alias="${1%%=*}" command="${1#*=}"
71 local prog="${command##git }"
72 prog="${prog%% *}"
73 alias $alias="$command"
74 gitcomplete "$alias" "$prog"
75 }
76
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
77 # create aliases and configure bash completion for most porcelain commands
78
79 _git_cmd_cfg=(
80 'add alias'
81 'am alias stdcmpl'
82 'annotate alias'
83 'apply alias stdcmpl'
84 'archive alias'
85 'bisect alias stdcmpl'
86 'blame alias'
87 'branch alias stdcmpl'
8007693 @rtomayko remove bundle top-level alias
authored
88 'bundle stdcmpl'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
89 'cat-file alias'
90 'checkout alias stdcmpl'
91 'cherry alias stdcmpl'
92 'cherry-pick alias stdcmpl'
93 'clean alias'
94 'clone alias'
95 'commit alias stdcmpl'
96 'config alias stdcmpl'
97 'describe alias stdcmpl'
98 'diff alias stdcmpl'
092c271 @rtomayko make difftool a top-level alias, closes #9
authored
99 'difftool alias'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
100 'fetch alias stdcmpl'
101 'format-patch alias stdcmpl'
102 'fsck alias'
103 'gc alias stdcmpl'
104 'gui alias'
5057f22 @rtomayko add hash-object as top-level alias, closes #4
authored
105 'hash-object alias'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
106 'init alias'
107 'instaweb alias'
108 'log alias logcmpl'
109 'lost-found alias'
110 'ls-files alias'
111 'ls-remote alias stdcmpl'
112 'ls-tree alias stdcmpl'
113 'merge alias stdcmpl'
4fd9f28 @rtomayko merge-base, shortlog, and show-branch top-level aliases
authored
114 'merge-base alias stdcmpl'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
115 'mergetool alias'
116 'mv alias'
117 'name-rev stdcmpl'
118 'patch-id alias'
119 'peek-remote alias'
120 'prune alias'
121 'pull alias stdcmpl'
122 'push alias stdcmpl'
123 'quiltimport alias'
124 'rebase alias stdcmpl'
4586458 @rtomayko add top-level reflog alias
authored
125 'reflog alias'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
126 'remote alias stdcmpl'
127 'repack alias'
128 'repo-config alias'
129 'request-pull alias'
130 'reset alias stdcmpl'
131 'rev-list alias'
132 'rev-parse alias'
133 'revert alias'
134 'rm alias'
135 'send-email alias'
136 'send-pack alias'
4fd9f28 @rtomayko merge-base, shortlog, and show-branch top-level aliases
authored
137 'shortlog alias stdcmpl'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
138 'show alias stdcmpl'
4fd9f28 @rtomayko merge-base, shortlog, and show-branch top-level aliases
authored
139 'show-branch alias logcmpl'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
140 'stash alias stdcmpl'
141 'status alias'
142 'stripspace alias'
143 'submodule alias stdcmpl'
60dccb3 @mbrubeck Upgrade to the latest git-completion.bash
mbrubeck authored
144 'svn alias stdcmpl'
7f241f0 @ap get rid of repetition using data to capture differences
ap authored
145 'symbolic-ref alias'
146 'tag alias stdcmpl'
147 'tar-tree alias'
148 'var alias'
149 'whatchanged alias logcmpl'
150 )
151
152 for cfg in "${_git_cmd_cfg[@]}" ; do
153 read cmd opts <<< $cfg
154 for opt in $opts ; do
155 case $opt in
156 alias) alias $cmd="git $cmd" ;;
157 stdcmpl) complete -o default -o nospace -F _git_${cmd//-/_} $cmd ;;
158 logcmpl) complete -o default -o nospace -F _git_log $cmd ;;
159 esac
160 done
056c241 @ap treat bash and perl as programming languages
ap authored
161 done
b48c4de @rtomayko Let's get this party started
authored
162
1dfc298 @rtomayko load gitconfig aliases as top-level commands
authored
163 # Create aliases for everything defined in the gitconfig [alias] section.
164 _git_import_aliases () {
f6cf058 @rtomayko git-sh.bash is noexpandtab
authored
165 eval "$(
166 git config --get-regexp 'alias\..*' |
167 sed 's/^alias\.//' |
168 while read key command
169 do
170 if expr -- "$command" : '!' >/dev/null
a9a9083 @rtomayko allow git alias commands with single quotes
authored
171 then echo "alias $key='git $key'"
172 else echo "gitalias $key=\"git $command\""
f6cf058 @rtomayko git-sh.bash is noexpandtab
authored
173 fi
174 done
175 )"
1dfc298 @rtomayko load gitconfig aliases as top-level commands
authored
176 }
177
465bf8f @rtomayko grab PS1 colors from git-config(1)
authored
178 # PROMPT =======================================================================
b48c4de @rtomayko Let's get this party started
authored
179
2b6ae2c @rtomayko Merge remote-tracking branch 'alex-morega/dirty-stash'
authored
180 PS1='`_git_headname`!`_git_repo_state``_git_workdir``_git_dirty``_git_dirty_stash`> '
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
181
7447561 @rtomayko add git config values for all prompt colors
authored
182 ANSI_RESET="\001$(git config --get-color "" "reset")\002"
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
183
184 # detect whether the tree is in a dirty state. returns
185 _git_dirty() {
198f7dd @Daenyth git-sh.bash: Changed the "dirty" marker to be configurable
Daenyth authored
186 if git status 2>/dev/null | fgrep -q '(working directory clean)'; then
187 return 0
188 fi
5845443 @Daenyth Fix _git_dirty doing the opposite of what it should
Daenyth authored
189 local dirty_marker="`git config gitsh.dirty || echo ' *'`"
190 _git_apply_color "$dirty_marker" "color.sh.dirty" "red"
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
191 }
192
0326321 @mgax show flag for stashed changesets in PS1
mgax authored
193 # detect whether any changesets are stashed
194 _git_dirty_stash() {
195 if ! git rev-parse --verify refs/stash >/dev/null 2>&1; then
196 return 0
197 fi
198 local dirty_stash_marker="`git config gitsh.dirty-stash || echo ' $'`"
199 _git_apply_color "$dirty_stash_marker" "color.sh.dirty-stash" "red"
200 }
201
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
202 # detect the current branch; use 7-sha when not on branch
203 _git_headname() {
204 local br=`git symbolic-ref -q HEAD 2>/dev/null`
205 [ -n "$br" ] &&
7447561 @rtomayko add git config values for all prompt colors
authored
206 br=${br#refs/heads/} ||
207 br=`git rev-parse --short HEAD 2>/dev/null`
208 _git_apply_color "$br" "color.sh.branch" "yellow reverse"
b48c4de @rtomayko Let's get this party started
authored
209 }
210
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
211 # detect working directory relative to working tree root
7447561 @rtomayko add git config values for all prompt colors
authored
212 _git_workdir() {
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
213 subdir=`git rev-parse --show-prefix 2>/dev/null`
214 subdir="${subdir%/}"
215 workdir="${PWD%/$subdir}"
7447561 @rtomayko add git config values for all prompt colors
authored
216 _git_apply_color "${workdir/*\/}${subdir:+/$subdir}" "color.sh.workdir" "blue bold"
217 }
218
b60c050 @mgax show if repository is during a rebase or merge
mgax authored
219 # detect if the repository is in a special state (rebase or merge)
220 _git_repo_state() {
221 local git_dir="$(git rev-parse --show-cdup).git"
222 if test -d "$git_dir/rebase-merge"; then
223 local state_marker="(rebase)"
224 elif test -f "$git_dir/MERGE_HEAD"; then
225 local state_marker="(merge)"
226 else
227 return 0
228 fi
229 _git_apply_color "$state_marker" "color.sh.repo-state" "red"
230 }
231
7447561 @rtomayko add git config values for all prompt colors
authored
232 # determine whether color should be enabled. this checks git's color.ui
233 # option and then color.sh.
234 _git_color_enabled() {
235 [ `git config --get-colorbool color.sh true` = "true" ]
236 }
237
238 # apply a color to the first argument
239 _git_apply_color() {
240 local output="$1" color="$2" default="$3"
241 if _git_color_enabled ; then
242 color=`_git_color "$color" "$default"`
243 echo -ne "${color}${output}${ANSI_RESET}"
244 else
245 echo -ne "$output"
246 fi
247 }
248
249 # retrieve an ANSI color escape sequence from git config
250 _git_color() {
251 local color
252 color=`git config --get-color "$1" "$2" 2>/dev/null`
253 [ -n "$color" ] && echo -ne "\001$color\002"
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
254 }
b48c4de @rtomayko Let's get this party started
authored
255
af4b720 @rtomayko prompt refactoring / work around bash 4.0 PROMPT_COMMAND issue
authored
256 # HELP ========================================================================
b48c4de @rtomayko Let's get this party started
authored
257
258 _help_display() {
000015e @rtomayko help shows aliases from ~/.gitconfig
authored
259 local name value
9a69336 @rtomayko `help foo` means `git help foo`; and completion
authored
260 # show git's inbuilt help, after some tweaking...
000015e @rtomayko help shows aliases from ~/.gitconfig
authored
261 git --help | grep -v "See 'git help"
262
263 # show aliases defined in ~/.gitconfig
264 echo "Command aliases:"
265 git config --get-regexp 'alias\..*' |
266 sed 's/^alias\.//' |
267 sort |
268 while read name value
269 do printf " %-10s %-65s\n" "$name" "$value"
270 done
271
272 printf "\nSee 'help COMMAND' for more information on a specific command.\n"
b48c4de @rtomayko Let's get this party started
authored
273 }
274
275 help() {
b39afd9 @ap prefer git pager when one is configured
ap authored
276 local _git_pager=$(git config core.pager)
000015e @rtomayko help shows aliases from ~/.gitconfig
authored
277 if [ $# = 1 ];
278 then git help $1
279 else (_help_display | ${_git_pager:-${PAGER:-less}})
280 fi
b48c4de @rtomayko Let's get this party started
authored
281 }
9a69336 @rtomayko `help foo` means `git help foo`; and completion
authored
282 complete -o default -o nospace -F _git help
465bf8f @rtomayko grab PS1 colors from git-config(1)
authored
283
f6cf058 @rtomayko git-sh.bash is noexpandtab
authored
284 # vim: tw=80 noexpandtab
Something went wrong with that request. Please try again.