From 8dd10a69bb3e9a43b7b8177e5467aa63b39ad75a Mon Sep 17 00:00:00 2001 From: Mike Gerwitz Date: Tue, 18 Oct 2011 17:15:35 -0400 Subject: [PATCH] shortmapped commands will now only be executed while within a git repository --- bash_completion | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/bash_completion b/bash_completion index 0f64585..a432cca 100644 --- a/bash_completion +++ b/bash_completion @@ -32,19 +32,36 @@ __git-short_shortmap () $( awk "/^$1 / { print \$2 }" <<< "$__git_short_maps" ) } +__git-short_shortalias () +{ + cmd=$1 + shift + + # if we're not within a git dir, fall back to an actual command of this name + __gitdir >/dev/null || { + " $cmd $@" + return $? + } + + # execute the command + $( grep "^$cmd" <<< "$__git_short_maps" | cut -d' ' -f3- ) "$@" +} + + # load shortmaps from cwd (or provided path) and home dir (if available) -__git_short_maps=$( cat ${1:-./shortmaps} ~/.git-shortmaps 2>/dev/null ) +__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 - short=$1 [ -z "$1" ] && continue __git-short_docomplete "$1" __git-short_shortmap - shift 2 - alias $short="$( sed 's/^:/git /' <<< "$@" )" + alias $1="__git-short_shortalias $1" done