2011-01-09 15:03:49 -05:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Provides short mappings for common Git commands
|
|
|
|
#
|
|
|
|
# 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 3 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
# #
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
|
|
|
__git-short_cmdok()
|
2013-11-10 23:14:29 -05:00
|
|
|
{
|
2014-08-21 21:54:18 -04:00
|
|
|
local -r cmd="$1"
|
|
|
|
test "${cmd:0:1}" != -
|
2013-11-10 23:14:29 -05:00
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
|
|
|
__git-short_docomplete()
|
2011-01-09 15:03:49 -05:00
|
|
|
{
|
2014-08-21 21:54:18 -04:00
|
|
|
local -r cmd="$1" fn="$2"
|
|
|
|
|
2011-10-18 20:04:11 -04:00
|
|
|
# ignore problem commands
|
2014-08-21 21:54:18 -04:00
|
|
|
__git-short_cmdok "$cmd" || return
|
2011-10-18 20:04:11 -04:00
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
complete -o bashdefault -o default -o nospace -F"$fn" "$cmd" 2>/dev/null \
|
|
|
|
|| complete -o default -o nospace -F"$fn" "$cmd"
|
2011-01-09 15:03:49 -05:00
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
|
|
|
__git-short_shortmap()
|
2011-01-09 15:03:49 -05:00
|
|
|
{
|
|
|
|
# only perform completion when within a git dir
|
2014-08-21 21:54:18 -04:00
|
|
|
__gitdir >/dev/null || return
|
2011-01-09 15:03:49 -05:00
|
|
|
|
2013-07-08 21:59:26 -04:00
|
|
|
# populate variables used by various git completion functions
|
|
|
|
local cur words cword prev
|
|
|
|
_get_comp_words_by_ref -n =: cur words cword prev
|
|
|
|
|
2011-01-09 15:03:49 -05:00
|
|
|
# execute the associated completion function (column two of the shortmaps
|
|
|
|
# file)
|
|
|
|
$( awk "/^$1 / { print \$2 }" <<< "$__git_short_maps" )
|
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
|
|
|
__git-short_register_alias()
|
2011-10-18 20:04:11 -04:00
|
|
|
{
|
2014-08-21 21:54:18 -04:00
|
|
|
local -r cmd="$1"
|
|
|
|
|
2011-10-18 20:04:11 -04:00
|
|
|
# ignore invalid aliases (for which we define functions to handle them
|
|
|
|
# instead)
|
2014-08-21 21:54:18 -04:00
|
|
|
__git-short_cmdok "$cmd" || return
|
2011-10-18 20:04:11 -04:00
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
alias $cmd="__git-short_shortalias '$cmd'"
|
2011-10-18 20:04:11 -04:00
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
|
|
|
__git-short_shortalias()
|
2011-10-18 17:15:35 -04:00
|
|
|
{
|
2014-08-21 21:54:18 -04:00
|
|
|
local -r shortcmd="$1"
|
2011-10-18 17:15:35 -04:00
|
|
|
shift
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
# if we're not within a git dir, fall back to an actual command of this
|
|
|
|
# name
|
2011-10-18 17:15:35 -04:00
|
|
|
__gitdir >/dev/null || {
|
2014-08-21 21:54:18 -04:00
|
|
|
command "$shortcmd" "$@"
|
|
|
|
return
|
2011-10-18 17:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
local cmd="$(
|
|
|
|
grep "^$shortcmd " <<< "$__git_short_maps" \
|
|
|
|
| cut -d' ' -f3-
|
|
|
|
)"
|
|
|
|
|
2011-10-18 20:04:11 -04:00
|
|
|
if [ -z "$cmd" ]; then
|
|
|
|
return
|
2013-07-08 22:01:13 -04:00
|
|
|
elif grep -q '^|' <<< "$cmd"; then
|
2011-10-18 20:04:11 -04:00
|
|
|
eval "$( sed 's/^|//' <<< "$cmd" ) $@"
|
2014-08-21 21:54:18 -04:00
|
|
|
return
|
2011-10-18 20:04:11 -04:00
|
|
|
fi
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
# intentionally unquoted
|
2011-10-18 20:04:11 -04:00
|
|
|
$cmd "$@"
|
2011-10-18 17:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-08-21 21:54:18 -04:00
|
|
|
|
2011-10-18 20:04:11 -04:00
|
|
|
# functions that cannot be aliased
|
2011-10-19 20:22:40 -04:00
|
|
|
- () { __git-short_shortalias - "$@"; }
|
|
|
|
-- () { __git-short_shortalias -- "$@"; }
|
2011-10-18 17:15:35 -04:00
|
|
|
|
2011-01-09 15:03:49 -05:00
|
|
|
# load shortmaps from cwd (or provided path) and home dir (if available)
|
2011-10-18 17:15:35 -04:00
|
|
|
__git_short_maps=$(
|
|
|
|
cat ${1:-./shortmaps} ~/.git-shortmaps 2>/dev/null \
|
|
|
|
| sed 's/^\([^ ]\+ [^ ]\+\) :/\1 git /'
|
|
|
|
)
|
2011-01-09 15:03:49 -05:00
|
|
|
|
2011-10-19 17:27:49 -04:00
|
|
|
oldifs="$IFS"
|
2011-10-18 20:04:11 -04:00
|
|
|
|
2011-01-09 15:03:49 -05:00
|
|
|
# register each shortmap
|
|
|
|
IFS=$'\n'
|
|
|
|
for line in $__git_short_maps; do
|
|
|
|
IFS=$' '
|
|
|
|
set -- $line
|
|
|
|
|
2013-12-08 13:41:59 -05:00
|
|
|
[ -z "$1" ] && continue
|
2011-01-09 15:03:49 -05:00
|
|
|
|
2011-10-18 20:04:11 -04:00
|
|
|
__git-short_docomplete "$1" __git-short_shortmap
|
|
|
|
__git-short_register_alias "$1"
|
2011-01-09 15:03:49 -05:00
|
|
|
done
|
2011-10-19 17:27:49 -04:00
|
|
|
|
|
|
|
IFS="$oldifs"
|
2014-08-21 21:54:18 -04:00
|
|
|
|