#!/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 . # # __git-short_docomplete () { # ignore problem commands grep -q '^-' <<< "$1" && return complete -o bashdefault -o default -o nospace -F $2 $1 2>/dev/null \ || complete -o default -o nospace -F $2 $1 } __git-short_shortmap () { # only perform completion when within a git dir __gitdir >/dev/null || return $? # execute the associated completion function (column two of the shortmaps # file) $( awk "/^$1 / { print \$2 }" <<< "$__git_short_maps" ) } __git-short_register_alias () { # ignore invalid aliases (for which we define functions to handle them # instead) grep -q '^-' <<< "$1" && return alias $1="__git-short_shortalias $1" } __git-short_shortalias () { shortcmd=$1 shift # if we're not within a git dir, fall back to an actual command of this name __gitdir >/dev/null || { " $shortcmd $@" return $? } # execute the command cmd="$( grep "^$shortcmd " <<< "$__git_short_maps" | cut -d' ' -f3- )" if [ -z "$cmd" ]; then return elif [ "$( grep '^|' <<< "$cmd")" ]; then eval "$( sed 's/^|//' <<< "$cmd" ) $@" return $? fi $cmd "$@" } # functions that cannot be aliased - () { __git-short_shortalias - "$@" } -- () { __git-short_shortalias -- "$@" } # load shortmaps from cwd (or provided path) and home dir (if available) __git_short_maps=$( cat ${1:-./shortmaps} ~/.git-shortmaps 2>/dev/null \ | sed 's/^\([^ ]\+ [^ ]\+\) :/\1 git /' ) # register each shortmap IFS=$'\n' for line in $__git_short_maps; do IFS=$' ' set -- $line [ -z "$1" ] && continue __git-short_docomplete "$1" __git-short_shortmap __git-short_docomplete "$1" __git-short_shortmap __git-short_register_alias "$1" done