completion: respect 'git -C <path>'
[gitweb.git] / contrib / completion / git-completion.bash
index 6721ff80fb1324ec1cf450f7b69cbf8266880035..f2d939d358dff9820912e774b9f5c2eb17bd684b 100644 (file)
@@ -39,7 +39,12 @@ esac
 __gitdir ()
 {
        if [ -z "${1-}" ]; then
-               if [ -n "${__git_dir-}" ]; then
+               if [ -n "${__git_C_args-}" ]; then
+                       git "${__git_C_args[@]}" \
+                               ${__git_dir:+--git-dir="$__git_dir"} \
+                               rev-parse --absolute-git-dir 2>/dev/null
+               elif [ -n "${__git_dir-}" ]; then
+                       test -d "$__git_dir" || return 1
                        echo "$__git_dir"
                elif [ -n "${GIT_DIR-}" ]; then
                        test -d "${GIT_DIR-}" || return 1
@@ -282,11 +287,13 @@ __gitcomp_file ()
 # argument, and using the options specified in the second argument.
 __git_ls_files_helper ()
 {
+       local dir="$(__gitdir)"
+
        if [ "$2" == "--committable" ]; then
-               git -C "$1" diff-index --name-only --relative HEAD
+               git ${__git_C_args:+"${__git_C_args[@]}"} --git-dir="$dir" -C "$1" diff-index --name-only --relative HEAD
        else
                # NOTE: $2 is not quoted in order to support multiple options
-               git -C "$1" ls-files --exclude-standard $2
+               git ${__git_C_args:+"${__git_C_args[@]}"} --git-dir="$dir" -C "$1" ls-files --exclude-standard $2
        fi 2>/dev/null
 }
 
@@ -332,14 +339,37 @@ __git_tags ()
        fi
 }
 
-# __git_refs accepts 0, 1 (to pass to __gitdir), or 2 arguments
-# presence of 2nd argument means use the guess heuristic employed
-# by checkout for tracking branches
+# Lists refs from the local (by default) or from a remote repository.
+# It accepts 0, 1 or 2 arguments:
+# 1: The remote to list refs from (optional; ignored, if set but empty).
+#    Can be the name of a configured remote, a path, or a URL.
+# 2: In addition to local refs, list unique branches from refs/remotes/ for
+#    'git checkout's tracking DWIMery (optional; ignored, if set but empty).
 __git_refs ()
 {
-       local i hash dir="$(__gitdir "${1-}")" track="${2-}"
+       local i hash dir="$(__gitdir)" track="${2-}"
+       local list_refs_from=path remote="${1-}"
        local format refs pfx
-       if [ -d "$dir" ]; then
+
+       if [ -z "$remote" ]; then
+               if [ -z "$dir" ]; then
+                       return
+               fi
+       else
+               if __git_is_configured_remote "$remote"; then
+                       # configured remote takes precedence over a
+                       # local directory with the same name
+                       list_refs_from=remote
+               elif [ -d "$remote/.git" ]; then
+                       dir="$remote/.git"
+               elif [ -d "$remote" ]; then
+                       dir="$remote"
+               else
+                       list_refs_from=url
+               fi
+       fi
+
+       if [ "$list_refs_from" = path ]; then
                case "$cur" in
                refs|refs/*)
                        format="refname"
@@ -376,7 +406,7 @@ __git_refs ()
        fi
        case "$cur" in
        refs|refs/*)
-               git ls-remote "$dir" "$cur*" 2>/dev/null | \
+               git --git-dir="$dir" ls-remote "$remote" "$cur*" 2>/dev/null | \
                while read -r hash i; do
                        case "$i" in
                        *^{}) ;;
@@ -385,9 +415,21 @@ __git_refs ()
                done
                ;;
        *)
-               echo "HEAD"
-               git for-each-ref --format="%(refname:short)" -- \
-                       "refs/remotes/$dir/" 2>/dev/null | sed -e "s#^$dir/##"
+               if [ "$list_refs_from" = remote ]; then
+                       echo "HEAD"
+                       git --git-dir="$dir" for-each-ref --format="%(refname:short)" \
+                               "refs/remotes/$remote/" 2>/dev/null | sed -e "s#^$remote/##"
+               else
+                       git --git-dir="$dir" ls-remote "$remote" HEAD \
+                               "refs/tags/*" "refs/heads/*" "refs/remotes/*" 2>/dev/null |
+                       while read -r hash i; do
+                               case "$i" in
+                               *^{})   ;;
+                               refs/*) echo "${i#refs/*/}" ;;
+                               *)      echo "$i" ;;  # symbolic refs
+                               esac
+                       done
+               fi
                ;;
        esac
 }
@@ -405,7 +447,7 @@ __git_refs2 ()
 __git_refs_remotes ()
 {
        local i hash
-       git ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
+       git --git-dir="$(__gitdir)" ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
        while read -r hash i; do
                echo "$i:refs/remotes/$1/${i#refs/heads/}"
        done
@@ -418,6 +460,18 @@ __git_remotes ()
        git --git-dir="$d" remote
 }
 
+# Returns true if $1 matches the name of a configured remote, false otherwise.
+__git_is_configured_remote ()
+{
+       local remote
+       for remote in $(__git_remotes); do
+               if [ "$remote" = "$1" ]; then
+                       return 0
+               fi
+       done
+       return 1
+}
+
 __git_list_merge_strategies ()
 {
        git merge -s help 2>&1 |
@@ -469,7 +523,7 @@ __git_complete_revlist_file ()
                *)   pfx="$ref:$pfx" ;;
                esac
 
-               __gitcomp_nl "$(git --git-dir="$(__gitdir)" ls-tree "$ls" 2>/dev/null \
+               __gitcomp_nl "$(git ${__git_C_args:+"${__git_C_args[@]}"} --git-dir="$(__gitdir)" ls-tree "$ls" 2>/dev/null \
                                | sed '/^100... blob /{
                                           s,^.*        ,,
                                           s,$, ,
@@ -766,7 +820,7 @@ __git_aliases ()
 __git_aliased_command ()
 {
        local word cmdline=$(git --git-dir="$(__gitdir)" \
-               config --get "alias.$1")
+               config --get "alias.$1" 2>/dev/null)
        for word in $cmdline; do
                case "$word" in
                \!gitk|gitk)
@@ -1183,7 +1237,7 @@ _git_commit ()
                return
        esac
 
-       if git rev-parse --verify --quiet HEAD >/dev/null; then
+       if git --git-dir="$(__gitdir)" rev-parse --verify --quiet HEAD >/dev/null; then
                __git_complete_index_file "--committable"
        else
                # This is the first commit
@@ -1206,7 +1260,7 @@ _git_describe ()
 
 __git_diff_algorithms="myers minimal patience histogram"
 
-__git_diff_submodule_formats="log short"
+__git_diff_submodule_formats="diff log short"
 
 __git_diff_common_options="--stat --numstat --shortstat --summary
                        --patch-with-stat --name-only --name-status --color
@@ -1483,7 +1537,7 @@ _git_log ()
 {
        __git_has_doubledash && return
 
-       local g="$(git rev-parse --git-dir 2>/dev/null)"
+       local g="$(__gitdir)"
        local merge=""
        if [ -f "$g/MERGE_HEAD" ]; then
                merge="--merge"
@@ -2742,6 +2796,7 @@ _git_worktree ()
 __git_main ()
 {
        local i c=1 command __git_dir
+       local __git_C_args C_args_count=0
 
        while [ $c -lt $cword ]; do
                i="${words[c]}"
@@ -2751,6 +2806,10 @@ __git_main ()
                --bare)      __git_dir="." ;;
                --help) command="help"; break ;;
                -c|--work-tree|--namespace) ((c++)) ;;
+               -C)     __git_C_args[C_args_count++]=-C
+                       ((c++))
+                       __git_C_args[C_args_count++]="${words[c]}"
+                       ;;
                -*) ;;
                *) command="$i"; break ;;
                esac
@@ -2758,6 +2817,17 @@ __git_main ()
        done
 
        if [ -z "$command" ]; then
+               case "$prev" in
+               --git-dir|-C|--work-tree)
+                       # these need a path argument, let's fall back to
+                       # Bash filename completion
+                       return
+                       ;;
+               -c|--namespace)
+                       # we don't support completing these options' arguments
+                       return
+                       ;;
+               esac
                case "$cur" in
                --*)   __gitcomp "
                        --paginate
@@ -2783,13 +2853,13 @@ __git_main ()
        fi
 
        local completion_func="_git_${command//-/_}"
-       declare -f $completion_func >/dev/null && $completion_func && return
+       declare -f $completion_func >/dev/null 2>/dev/null && $completion_func && return
 
        local expansion=$(__git_aliased_command "$command")
        if [ -n "$expansion" ]; then
                words[1]=$expansion
                completion_func="_git_${expansion//-/_}"
-               declare -f $completion_func >/dev/null && $completion_func
+               declare -f $completion_func >/dev/null 2>/dev/null && $completion_func
        fi
 }