# source ~/.git-completion.sh
#
+__gitdir ()
+{
+ echo "${__git_dir:-$(git rev-parse --git-dir 2>/dev/null)}"
+}
+
__git_refs ()
{
- local cmd i is_hash=y
- if [ -d "$1" ]; then
+ local cmd i is_hash=y dir="${1:-$(__gitdir)}"
+ if [ -d "$dir" ]; then
cmd=git-peek-remote
else
cmd=git-ls-remote
fi
- for i in $($cmd "$1" 2>/dev/null); do
+ for i in $($cmd "$dir" 2>/dev/null); do
case "$is_hash,$i" in
y,*) is_hash=n ;;
n,*^{}) is_hash=y ;;
__git_refs2 ()
{
- local cmd i is_hash=y
- if [ -d "$1" ]; then
+ local cmd i is_hash=y dir="${1:-$(__gitdir)}"
+ if [ -d "$dir" ]; then
cmd=git-peek-remote
else
cmd=git-ls-remote
fi
- for i in $($cmd "$1" 2>/dev/null); do
+ for i in $($cmd "$dir" 2>/dev/null); do
case "$is_hash,$i" in
y,*) is_hash=n ;;
n,*^{}) is_hash=y ;;
__git_remotes ()
{
- local i ngoff IFS=$'\n'
+ local i ngoff IFS=$'\n' d="$(__gitdir)"
shopt -q nullglob || ngoff=1
shopt -s nullglob
- for i in .git/remotes/*; do
- echo ${i#.git/remotes/}
+ for i in "$d/remotes"/*; do
+ echo ${i#$d/remotes/}
done
[ "$ngoff" ] && shopt -u nullglob
- for i in $(git repo-config --list); do
+ for i in $(git --git-dir="$d" repo-config --list); do
case "$i" in
remote.*.url=*)
i="${i#remote.}"
;;
esac
COMPREPLY=($(compgen -P "$pfx" \
- -W "$(git-ls-tree "$ls" \
+ -W "$(git --git-dir="$(__gitdir)" ls-tree "$ls" \
| sed '/^100... blob /s,^.* ,,
/^040000 tree /{
s,^.* ,,
-- "$cur"))
;;
*)
- COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
;;
esac
}
__git_aliases ()
{
local i IFS=$'\n'
- for i in $(git repo-config --list); do
+ for i in $(git --git-dir="$(__gitdir)" repo-config --list); do
case "$i" in
alias.*)
i="${i#alias.}"
__git_aliased_command ()
{
- local word cmdline=$(git repo-config --get "alias.$1")
+ local word cmdline=$(git --git-dir="$(__gitdir)" \
+ repo-config --get "alias.$1")
for word in $cmdline; do
if [ "${word##-*}" ]; then
echo $word
_git_branch ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "-l -f -d -D $(__git_refs)" -- "$cur"))
}
_git_cat_file ()
_git_checkout ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "-l -b $(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "-l -b $(__git_refs)" -- "$cur"))
}
_git_diff ()
_git_diff_tree ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "-r -p -M $(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "-r -p -M $(__git_refs)" -- "$cur"))
}
_git_fetch ()
case "$cur" in
*:*)
cur=$(echo "$cur" | sed 's/^.*://')
- COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
;;
*)
local remote
*..*)
local pfx=$(echo "$cur" | sed 's/\.\..*$/../')
cur=$(echo "$cur" | sed 's/^.*\.\.//')
- COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -P "$pfx" -W "$(__git_refs)" -- "$cur"))
;;
*)
- COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
;;
esac
}
_git_merge_base ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
}
_git_pull ()
COMPREPLY=($(compgen -W "$(__git_refs "$remote")" -- "$cur"))
;;
*)
- COMPREPLY=($(compgen -W "$(__git_refs2 .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs2)" -- "$cur"))
;;
esac
;;
{
local cur="${COMP_WORDS[COMP_CWORD]}"
local opt="--mixed --hard --soft"
- COMPREPLY=($(compgen -W "$opt $(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$opt $(__git_refs)" -- "$cur"))
}
_git_show ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "$(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "$(__git_refs)" -- "$cur"))
}
_git ()
{
- if [ $COMP_CWORD = 1 ]; then
+ local i c=1 command __git_dir
+
+ while [ $c -lt $COMP_CWORD ]; do
+ i="${COMP_WORDS[c]}"
+ case "$i" in
+ --git-dir=*) __git_dir="${i#--git-dir=}" ;;
+ --bare) __git_dir="." ;;
+ --version|--help|-p|--paginate) ;;
+ *) command="$i"; break ;;
+ esac
+ c=$((++c))
+ done
+
+ if [ $c -eq $COMP_CWORD -a -z "$command" ]; then
COMPREPLY=($(compgen \
- -W "--version $(git help -a|egrep '^ ') \
+ -W "--git-dir= --version \
+ $(git help -a|egrep '^ ') \
$(__git_aliases)" \
-- "${COMP_WORDS[COMP_CWORD]}"))
- else
- local command="${COMP_WORDS[1]}"
- local expansion=$(__git_aliased_command "$command")
+ return;
+ fi
- if [ "$expansion" ]; then
- command="$expansion"
- fi
+ local expansion=$(__git_aliased_command "$command")
+ [ "$expansion" ] && command="$expansion"
- case "$command" in
- branch) _git_branch ;;
- cat-file) _git_cat_file ;;
- checkout) _git_checkout ;;
- diff) _git_diff ;;
- diff-tree) _git_diff_tree ;;
- fetch) _git_fetch ;;
- log) _git_log ;;
- ls-remote) _git_ls_remote ;;
- ls-tree) _git_ls_tree ;;
- merge-base) _git_merge_base ;;
- pull) _git_pull ;;
- push) _git_push ;;
- reset) _git_reset ;;
- show) _git_show ;;
- show-branch) _git_log ;;
- whatchanged) _git_log ;;
- *) COMPREPLY=() ;;
- esac
- fi
+ case "$command" in
+ branch) _git_branch ;;
+ cat-file) _git_cat_file ;;
+ checkout) _git_checkout ;;
+ diff) _git_diff ;;
+ diff-tree) _git_diff_tree ;;
+ fetch) _git_fetch ;;
+ log) _git_log ;;
+ ls-remote) _git_ls_remote ;;
+ ls-tree) _git_ls_tree ;;
+ merge-base) _git_merge_base ;;
+ pull) _git_pull ;;
+ push) _git_push ;;
+ reset) _git_reset ;;
+ show) _git_show ;;
+ show-branch) _git_log ;;
+ whatchanged) _git_log ;;
+ *) COMPREPLY=() ;;
+ esac
}
_gitk ()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
- COMPREPLY=($(compgen -W "--all $(__git_refs .)" -- "$cur"))
+ COMPREPLY=($(compgen -W "--all $(__git_refs)" -- "$cur"))
}
complete -o default -o nospace -F _git git