1
0
Fork 0
git-shortmaps/bash_completion

122 lines
2.7 KiB
Bash
Raw Normal View History

#!/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/>.
# #
__git-short_cmdok()
{
local -r cmd="$1"
test "${cmd:0:1}" != -
}
__git-short_docomplete()
{
local -r cmd="$1" fn="$2"
2011-10-18 20:04:11 -04:00
# ignore problem commands
__git-short_cmdok "$cmd" || return
2011-10-18 20:04:11 -04:00
complete -o bashdefault -o default -o nospace -F"$fn" "$cmd" 2>/dev/null \
|| complete -o default -o nospace -F"$fn" "$cmd"
}
__git-short_shortmap()
{
# only perform completion when within a git dir
__gitdir >/dev/null || return
# populate variables used by various git completion functions
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
# execute the associated completion function (column two of the shortmaps
# file)
$( awk "/^$1 / { print \$2 }" <<< "$__git_short_maps" )
}
__git-short_register_alias()
2011-10-18 20:04:11 -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)
__git-short_cmdok "$cmd" || return
2011-10-18 20:04:11 -04:00
alias $cmd="__git-short_shortalias '$cmd'"
2011-10-18 20:04:11 -04:00
}
__git-short_shortalias()
{
local -r shortcmd="$1"
shift
# if we're not within a git dir, fall back to an actual command of this
# name
__gitdir >/dev/null || {
command "$shortcmd" "$@"
return
}
local cmd="$(
grep "^$shortcmd " <<< "$__git_short_maps" \
| cut -d' ' -f3-
)"
2011-10-18 20:04:11 -04:00
if [ -z "$cmd" ]; then
return
elif grep -q '^|' <<< "$cmd"; then
2011-10-18 20:04:11 -04:00
eval "$( sed 's/^|//' <<< "$cmd" ) $@"
return
2011-10-18 20:04:11 -04:00
fi
# intentionally unquoted
2011-10-18 20:04:11 -04:00
$cmd "$@"
}
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 -- "$@"; }
# load shortmaps from cwd (or provided path) and home dir (if available)
__git_short_maps=$(
2014-08-21 21:58:51 -04:00
sed 's/^\([^ ]\+ [^ ]\+\) :/\1 git /' \
"${1:-./shortmaps}" ~/.git-shortmaps \
2>/dev/null
)
2011-10-19 17:27:49 -04:00
oldifs="$IFS"
2011-10-18 20:04:11 -04:00
# register each shortmap
IFS=$'\n'
for line in $__git_short_maps; do
IFS=$' '
set -- $line
[ -z "$1" ] && continue
2011-10-18 20:04:11 -04:00
__git-short_docomplete "$1" __git-short_shortmap
__git-short_register_alias "$1"
done
2011-10-19 17:27:49 -04:00
IFS="$oldifs"