2011-01-09 15:03:49 -05:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Provides short mappings for common Git commands
|
|
|
|
#
|
2015-01-09 00:07:15 -05:00
|
|
|
# Copyright (C) 2011, 2012, 2013, 2014, 2015 Mike Gerwitz
|
2014-08-21 23:34:10 -04:00
|
|
|
#
|
2011-01-09 15:03:49 -05:00
|
|
|
# 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
|
2014-08-21 23:34:10 -04:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2011-01-09 15:03:49 -05:00
|
|
|
# #
|
|
|
|
|
2014-08-21 23:24:28 -04:00
|
|
|
declare -r __git_short_path="${1:-$(pwd)/shortmaps}"
|
|
|
|
|
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
|
|
|
|
2015-01-09 00:07:15 -05:00
|
|
|
complete -o bashdefault -o default -o nospace -F"$fn" "$cmd" 1>/dev/null \
|
2014-08-21 21:54:18 -04:00
|
|
|
|| 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
|
|
|
{
|
2014-08-21 22:22:10 -04:00
|
|
|
local -r shortcmd="$1"
|
|
|
|
|
2011-01-09 15:03:49 -05:00
|
|
|
# only perform completion when within a git dir
|
2014-11-23 22:43:17 -05:00
|
|
|
git rev-parse &>/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)
|
2014-08-21 22:22:10 -04:00
|
|
|
"${__git_short_comp[$shortcmd]}"
|
2011-01-09 15:03:49 -05:00
|
|
|
}
|
|
|
|
|
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 23:24:28 -04:00
|
|
|
if [ "$shortcmd" == --help ]; then
|
|
|
|
__git-short_help
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
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
|
2014-11-23 22:43:17 -05:00
|
|
|
git rev-parse &>/dev/null || {
|
2014-08-21 21:54:18 -04:00
|
|
|
command "$shortcmd" "$@"
|
|
|
|
return
|
2011-10-18 17:15:35 -04:00
|
|
|
}
|
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
local cmd="${__git_short_maps[$shortcmd]}"
|
|
|
|
test -n "$cmd" || return
|
2014-08-21 21:54:18 -04:00
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
# pipe cmd prefix indicates a verbatim command
|
|
|
|
if [ "${cmd:0:1}" == '|' ]; then
|
|
|
|
eval "${cmd:1}" '"$@"'
|
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
|
|
|
|
2014-08-21 23:24:28 -04:00
|
|
|
__git-short_help()
|
|
|
|
{
|
|
|
|
cat <<EOH
|
|
|
|
Git Shortmaps loaded from:
|
|
|
|
- $HOME/.git-shortmaps
|
|
|
|
- $__git_short_path
|
|
|
|
|
|
|
|
Defined shortmaps:
|
|
|
|
$( __git-short_map_out | sed 's/^/ /' )
|
2014-08-21 23:34:10 -04:00
|
|
|
|
|
|
|
git-shortmaps Copyright (C) 2011, 2012, 2013, 2014 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/>.
|
2014-08-21 23:24:28 -04:00
|
|
|
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; }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
# load shortmaps from cwd (or provided path) and home dir (if available)
|
|
|
|
__git-short_load_maps()
|
|
|
|
{
|
2014-08-21 23:24:28 -04:00
|
|
|
local -r path="$1"
|
2014-08-21 22:31:18 -04:00
|
|
|
cat "$path" ~/.git-shortmaps 2>/dev/null
|
2014-08-21 22:22:10 -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
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
declare shortcmd comp cmd
|
2014-08-27 19:15:32 -04:00
|
|
|
declare -gA __git_short_maps __git_short_comp
|
2011-10-18 20:04:11 -04:00
|
|
|
|
2011-01-09 15:03:49 -05:00
|
|
|
# register each shortmap
|
2014-08-21 22:22:10 -04:00
|
|
|
while read shortcmd comp cmd; do
|
|
|
|
test -n "$shortcmd" || continue
|
2011-01-09 15:03:49 -05:00
|
|
|
|
2014-08-21 22:31:18 -04:00
|
|
|
if [ "${cmd:0:1}" == : ]; then
|
|
|
|
cmd="git ${cmd:1}"
|
|
|
|
fi
|
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
__git_short_maps["$shortcmd"]="$cmd"
|
|
|
|
__git_short_comp["$shortcmd"]="$comp"
|
2011-10-19 17:27:49 -04:00
|
|
|
|
2014-08-21 22:22:10 -04:00
|
|
|
__git-short_docomplete "$shortcmd" __git-short_shortmap
|
|
|
|
__git-short_register_alias "$shortcmd"
|
2014-08-21 23:24:28 -04:00
|
|
|
done < <( __git-short_load_maps "$__git_short_path" )
|
2014-08-21 21:54:18 -04:00
|
|
|
|
2015-01-09 00:33:07 -05:00
|
|
|
|
|
|
|
# some distros use completion lazy-loading
|
|
|
|
type -t _completion_loader &>/dev/null \
|
|
|
|
&& _completion_loader git
|
|
|
|
|