1
0
Fork 0
git-shortmaps/bash_completion

170 lines
3.8 KiB
Bash

#!/bin/bash
#
# Provides short mappings for common Git commands
#
# Copyright (C) 2011, 2012, 2013, 2014, 2015 Mike Gerwitz
#
# 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 <https://www.gnu.org/licenses/>.
# #
declare -r __git_short_path="${1:-$(pwd)/shortmaps}"
__git-short_cmdok()
{
local -r cmd="$1"
test "${cmd:0:1}" != -
}
__git-short_docomplete()
{
local -r cmd="$1" fn="$2"
# ignore problem commands
__git-short_cmdok "$cmd" || return
complete -o bashdefault -o default -o nospace -F"$fn" "$cmd" 1>/dev/null \
|| complete -o default -o nospace -F"$fn" "$cmd"
}
__git-short_shortmap()
{
local -r shortcmd="$1"
# only perform completion when within a git dir
git rev-parse &>/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)
"${__git_short_comp[$shortcmd]}"
}
__git-short_register_alias()
{
local -r cmd="$1"
# ignore invalid aliases (for which we define functions to handle them
# instead)
__git-short_cmdok "$cmd" || return
alias $cmd="__git-short_shortalias '$cmd'"
}
__git-short_shortalias()
{
local -r shortcmd="$1"
shift
if [ "$shortcmd" == --help ]; then
__git-short_help
return
fi
# if we're not within a git dir, fall back to an actual command of this
# name
git rev-parse &>/dev/null || {
command "$shortcmd" "$@"
return
}
local cmd="${__git_short_maps[$shortcmd]}"
test -n "$cmd" || return
# pipe cmd prefix indicates a verbatim command
if [ "${cmd:0:1}" == '|' ]; then
eval "${cmd:1}" '"$@"'
return
fi
# intentionally unquoted
$cmd "$@"
}
__git-short_help()
{
cat <<EOH
Git Shortmaps loaded from:
- $HOME/.git-shortmaps
- $__git_short_path
Defined shortmaps:
$( __git-short_map_out | sed 's/^/ /' )
git-shortmaps Copyright (C) 2011, 2012, 2013, 2014, 2015 Mike Gerwitz
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; see the GNU GPLv3+ for details at
<https://www.gnu.org/licenses/>.
EOH
}
__git-short_map_out()
{
local shortcmd
for shortcmd in "${!__git_short_maps[@]}"; do
echo -e "$shortcmd\t${__git_short_maps[$shortcmd]}"
done \
| sort \
| { type column &>/dev/null && column -t -s$'\t' || cat; }
}
# load shortmaps from cwd (or provided path) and home dir (if available)
__git-short_load_maps()
{
local -r path="$1"
cat "$path" ~/.git-shortmaps 2>/dev/null
}
# functions that cannot be aliased
- () { __git-short_shortalias - "$@"; }
-- () { __git-short_shortalias -- "$@"; }
declare shortcmd comp cmd
declare -gA __git_short_maps __git_short_comp
# register each shortmap
while read shortcmd comp cmd; do
test -n "$shortcmd" || continue
if [ "${cmd:0:1}" == : ]; then
cmd="git ${cmd:1}"
fi
__git_short_maps["$shortcmd"]="$cmd"
__git_short_comp["$shortcmd"]="$comp"
__git-short_docomplete "$shortcmd" __git-short_shortmap
__git-short_register_alias "$shortcmd"
done < <( __git-short_load_maps "$__git_short_path" )
# some distros use completion lazy-loading
type -t _completion_loader &>/dev/null \
&& _completion_loader git