completion: offer ctags symbol names for 'git log -S', '-G' and '-L:'
[gitweb.git] / contrib / completion / git-completion.bash
index 262760f8c00ae27e26f72e9ada98d994115a4947..d6f25a7e0b3f50b6f6f6d5b645fe794c1cbe4788 100644 (file)
@@ -34,22 +34,41 @@ case "$COMP_WORDBREAKS" in
 *)   COMP_WORDBREAKS="$COMP_WORDBREAKS:"
 esac
 
+# Discovers the path to the git repository taking any '--git-dir=<path>' and
+# '-C <path>' options into account and stores it in the $__git_repo_path
+# variable.
+__git_find_repo_path ()
+{
+       if [ -n "$__git_repo_path" ]; then
+               # we already know where it is
+               return
+       fi
+
+       if [ -n "${__git_C_args-}" ]; then
+               __git_repo_path="$(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" &&
+               __git_repo_path="$__git_dir"
+       elif [ -n "${GIT_DIR-}" ]; then
+               test -d "${GIT_DIR-}" &&
+               __git_repo_path="$GIT_DIR"
+       elif [ -d .git ]; then
+               __git_repo_path=.git
+       else
+               __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)"
+       fi
+}
+
+# Deprecated: use __git_find_repo_path() and $__git_repo_path instead
 # __gitdir accepts 0 or 1 arguments (i.e., location)
 # returns location of .git repo
 __gitdir ()
 {
        if [ -z "${1-}" ]; then
-               if [ -n "${__git_dir-}" ]; then
-                       test -d "$__git_dir" || return 1
-                       echo "$__git_dir"
-               elif [ -n "${GIT_DIR-}" ]; then
-                       test -d "${GIT_DIR-}" || return 1
-                       echo "$GIT_DIR"
-               elif [ -d .git ]; then
-                       echo .git
-               else
-                       git rev-parse --git-dir 2>/dev/null
-               fi
+               __git_find_repo_path || return 1
+               echo "$__git_repo_path"
        elif [ -d "$1/.git" ]; then
                echo "$1/.git"
        else
@@ -57,6 +76,14 @@ __gitdir ()
        fi
 }
 
+# Runs git with all the options given as argument, respecting any
+# '--git-dir=<path>' and '-C <path>' options present on the command line
+__git ()
+{
+       git ${__git_C_args:+"${__git_C_args[@]}"} \
+               ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null
+}
+
 # The following function is based on code from:
 #
 #   bash_completion - programmable completion functions for bash 3.2+
@@ -186,6 +213,20 @@ _get_comp_words_by_ref ()
 }
 fi
 
+# Fills the COMPREPLY array with prefiltered words without any additional
+# processing.
+# Callers must take care of providing only words that match the current word
+# to be completed and adding any prefix and/or suffix (trailing space!), if
+# necessary.
+# 1: List of newline-separated matching completion words, complete with
+#    prefix and suffix.
+__gitcomp_direct ()
+{
+       local IFS=$'\n'
+
+       COMPREPLY=($1)
+}
+
 __gitcompappend ()
 {
        local x i=${#COMPREPLY[@]}
@@ -283,14 +324,12 @@ __gitcomp_file ()
 # argument, and using the options specified in the second argument.
 __git_ls_files_helper ()
 {
-       local dir="$(__gitdir)"
-
        if [ "$2" == "--committable" ]; then
-               git --git-dir="$dir" -C "$1" diff-index --name-only --relative HEAD
+               __git -C "$1" diff-index --name-only --relative HEAD
        else
                # NOTE: $2 is not quoted in order to support multiple options
-               git --git-dir="$dir" -C "$1" ls-files --exclude-standard $2
-       fi 2>/dev/null
+               __git -C "$1" ls-files --exclude-standard $2
+       fi
 }
 
 
@@ -302,37 +341,38 @@ __git_ls_files_helper ()
 #    slash.
 __git_index_files ()
 {
-       local dir="$(__gitdir)" root="${2-.}" file
+       local root="${2-.}" file
 
-       if [ -d "$dir" ]; then
-               __git_ls_files_helper "$root" "$1" |
-               while read -r file; do
-                       case "$file" in
-                       ?*/*) echo "${file%%/*}" ;;
-                       *) echo "$file" ;;
-                       esac
-               done | sort | uniq
-       fi
+       __git_ls_files_helper "$root" "$1" |
+       while read -r file; do
+               case "$file" in
+               ?*/*) echo "${file%%/*}" ;;
+               *) echo "$file" ;;
+               esac
+       done | sort | uniq
 }
 
+# Lists branches from the local repository.
+# 1: A prefix to be added to each listed branch (optional).
+# 2: List only branches matching this word (optional; list all branches if
+#    unset or empty).
+# 3: A suffix to be appended to each listed branch (optional).
 __git_heads ()
 {
-       local dir="$(__gitdir)"
-       if [ -d "$dir" ]; then
-               git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-                       refs/heads
-               return
-       fi
+       local pfx="${1-}" cur_="${2-}" sfx="${3-}"
+
+       __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
+                       "refs/heads/$cur_*" "refs/heads/$cur_*/**"
 }
 
+# Lists tags from the local repository.
+# Accepts the same positional parameters as __git_heads() above.
 __git_tags ()
 {
-       local dir="$(__gitdir)"
-       if [ -d "$dir" ]; then
-               git --git-dir="$dir" for-each-ref --format='%(refname:short)' \
-                       refs/tags
-               return
-       fi
+       local pfx="${1-}" cur_="${2-}" sfx="${3-}"
+
+       __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \
+                       "refs/tags/$cur_*" "refs/tags/$cur_*/**"
 }
 
 # Lists refs from the local (by default) or from a remote repository.
@@ -341,11 +381,24 @@ __git_tags ()
 #    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).
+# 3: A prefix to be added to each listed ref (optional).
+# 4: List only refs matching this word (optional; list all refs if unset or
+#    empty).
+# 5: A suffix to be appended to each listed ref (optional; ignored, if set
+#    but empty).
+#
+# Use __git_complete_refs() instead.
 __git_refs ()
 {
-       local i hash dir="$(__gitdir)" track="${2-}"
+       local i hash dir track="${2-}"
        local list_refs_from=path remote="${1-}"
-       local format refs pfx
+       local format refs
+       local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}"
+       local match="${4-}"
+       local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers
+
+       __git_find_repo_path
+       dir="$__git_repo_path"
 
        if [ -z "$remote" ]; then
                if [ -z "$dir" ]; then
@@ -366,63 +419,78 @@ __git_refs ()
        fi
 
        if [ "$list_refs_from" = path ]; then
-               case "$cur" in
+               if [[ "$cur_" == ^* ]]; then
+                       pfx="$pfx^"
+                       fer_pfx="$fer_pfx^"
+                       cur_=${cur_#^}
+                       match=${match#^}
+               fi
+               case "$cur_" in
                refs|refs/*)
                        format="refname"
-                       refs="${cur%/*}"
+                       refs=("$match*" "$match*/**")
                        track=""
                        ;;
                *)
-                       [[ "$cur" == ^* ]] && pfx="^"
                        for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD; do
-                               if [ -e "$dir/$i" ]; then echo $pfx$i; fi
+                               case "$i" in
+                               $match*)
+                                       if [ -e "$dir/$i" ]; then
+                                               echo "$pfx$i$sfx"
+                                       fi
+                                       ;;
+                               esac
                        done
-                       format="refname:short"
-                       refs="refs/tags refs/heads refs/remotes"
+                       format="refname:strip=2"
+                       refs=("refs/tags/$match*" "refs/tags/$match*/**"
+                               "refs/heads/$match*" "refs/heads/$match*/**"
+                               "refs/remotes/$match*" "refs/remotes/$match*/**")
                        ;;
                esac
-               git --git-dir="$dir" for-each-ref --format="$pfx%($format)" \
-                       $refs
+               __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \
+                       "${refs[@]}"
                if [ -n "$track" ]; then
                        # employ the heuristic used by git checkout
                        # Try to find a remote branch that matches the completion word
                        # but only output if the branch name is unique
-                       local ref entry
-                       git --git-dir="$dir" for-each-ref --shell --format="ref=%(refname:short)" \
-                               "refs/remotes/" | \
-                       while read -r entry; do
-                               eval "$entry"
-                               ref="${ref#*/}"
-                               if [[ "$ref" == "$cur"* ]]; then
-                                       echo "$ref"
-                               fi
-                       done | sort | uniq -u
+                       __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
+                               --sort="refname:strip=3" \
+                               "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
+                       uniq -u
                fi
                return
        fi
-       case "$cur" in
+       case "$cur_" in
        refs|refs/*)
-               git --git-dir="$dir" ls-remote "$remote" "$cur*" 2>/dev/null | \
+               __git ls-remote "$remote" "$match*" | \
                while read -r hash i; do
                        case "$i" in
                        *^{}) ;;
-                       *) echo "$i" ;;
+                       *) echo "$pfx$i$sfx" ;;
                        esac
                done
                ;;
        *)
                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/##"
+                       case "HEAD" in
+                       $match*)        echo "${pfx}HEAD$sfx" ;;
+                       esac
+                       __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
+                               "refs/remotes/$remote/$match*" \
+                               "refs/remotes/$remote/$match*/**"
                else
-                       git --git-dir="$dir" ls-remote "$remote" HEAD \
-                               "refs/tags/*" "refs/heads/*" "refs/remotes/*" 2>/dev/null |
+                       local query_symref
+                       case "HEAD" in
+                       $match*)        query_symref="HEAD" ;;
+                       esac
+                       __git ls-remote "$remote" $query_symref \
+                               "refs/tags/$match*" "refs/heads/$match*" \
+                               "refs/remotes/$match*" |
                        while read -r hash i; do
                                case "$i" in
                                *^{})   ;;
-                               refs/*) echo "${i#refs/*/}" ;;
-                               *)      echo "$i" ;;  # symbolic refs
+                               refs/*) echo "$pfx${i#refs/*/}$sfx" ;;
+                               *)      echo "$pfx$i$sfx" ;;  # symbolic refs
                                esac
                        done
                fi
@@ -430,7 +498,38 @@ __git_refs ()
        esac
 }
 
+# Completes refs, short and long, local and remote, symbolic and pseudo.
+#
+# Usage: __git_complete_refs [<option>]...
+# --remote=<remote>: The remote to list refs from, can be the name of a
+#                    configured remote, a path, or a URL.
+# --track: List unique remote branches for 'git checkout's tracking DWIMery.
+# --pfx=<prefix>: A prefix to be added to each ref.
+# --cur=<word>: The current ref to be completed.  Defaults to the current
+#               word to be completed.
+# --sfx=<suffix>: A suffix to be appended to each ref instead of the default
+#                 space.
+__git_complete_refs ()
+{
+       local remote track pfx cur_="$cur" sfx=" "
+
+       while test $# != 0; do
+               case "$1" in
+               --remote=*)     remote="${1##--remote=}" ;;
+               --track)        track="yes" ;;
+               --pfx=*)        pfx="${1##--pfx=}" ;;
+               --cur=*)        cur_="${1##--cur=}" ;;
+               --sfx=*)        sfx="${1##--sfx=}" ;;
+               *)              return 1 ;;
+               esac
+               shift
+       done
+
+       __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
+}
+
 # __git_refs2 requires 1 argument (to pass to __git_refs)
+# Deprecated: use __git_complete_fetch_refspecs() instead.
 __git_refs2 ()
 {
        local i
@@ -439,11 +538,29 @@ __git_refs2 ()
        done
 }
 
+# Completes refspecs for fetching from a remote repository.
+# 1: The remote repository.
+# 2: A prefix to be added to each listed refspec (optional).
+# 3: The ref to be completed as a refspec instead of the current word to be
+#    completed (optional)
+# 4: A suffix to be appended to each listed refspec instead of the default
+#    space (optional).
+__git_complete_fetch_refspecs ()
+{
+       local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
+
+       __gitcomp_direct "$(
+               for i in $(__git_refs "$remote" "" "" "$cur_") ; do
+                       echo "$pfx$i:$i$sfx"
+               done
+               )"
+}
+
 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
 __git_refs_remotes ()
 {
        local i hash
-       git --git-dir="$(__gitdir)" ls-remote "$1" 'refs/heads/*' 2>/dev/null | \
+       __git ls-remote "$1" 'refs/heads/*' | \
        while read -r hash i; do
                echo "$i:refs/remotes/$1/${i#refs/heads/}"
        done
@@ -451,9 +568,9 @@ __git_refs_remotes ()
 
 __git_remotes ()
 {
-       local d="$(__gitdir)"
-       test -d "$d/remotes" && ls -1 "$d/remotes"
-       git --git-dir="$d" remote
+       __git_find_repo_path
+       test -d "$__git_repo_path/remotes" && ls -1 "$__git_repo_path/remotes"
+       __git remote
 }
 
 # Returns true if $1 matches the name of a configured remote, false otherwise.
@@ -519,7 +636,7 @@ __git_complete_revlist_file ()
                *)   pfx="$ref:$pfx" ;;
                esac
 
-               __gitcomp_nl "$(git --git-dir="$(__gitdir)" ls-tree "$ls" 2>/dev/null \
+               __gitcomp_nl "$(__git ls-tree "$ls" \
                                | sed '/^100... blob /{
                                           s,^.*        ,,
                                           s,$, ,
@@ -538,15 +655,15 @@ __git_complete_revlist_file ()
        *...*)
                pfx="${cur_%...*}..."
                cur_="${cur_#*...}"
-               __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
+               __git_complete_refs --pfx="$pfx" --cur="$cur_"
                ;;
        *..*)
                pfx="${cur_%..*}.."
                cur_="${cur_#*..}"
-               __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
+               __git_complete_refs --pfx="$pfx" --cur="$cur_"
                ;;
        *)
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
                ;;
        esac
 }
@@ -631,23 +748,23 @@ __git_complete_remote_or_refspec ()
        case "$cmd" in
        fetch)
                if [ $lhs = 1 ]; then
-                       __gitcomp_nl "$(__git_refs2 "$remote")" "$pfx" "$cur_"
+                       __git_complete_fetch_refspecs "$remote" "$pfx" "$cur_"
                else
-                       __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
+                       __git_complete_refs --pfx="$pfx" --cur="$cur_"
                fi
                ;;
        pull|remote)
                if [ $lhs = 1 ]; then
-                       __gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
+                       __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
                else
-                       __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
+                       __git_complete_refs --pfx="$pfx" --cur="$cur_"
                fi
                ;;
        push)
                if [ $lhs = 1 ]; then
-                       __gitcomp_nl "$(__git_refs)" "$pfx" "$cur_"
+                       __git_complete_refs --pfx="$pfx" --cur="$cur_"
                else
-                       __gitcomp_nl "$(__git_refs "$remote")" "$pfx" "$cur_"
+                       __git_complete_refs --remote="$remote" --pfx="$pfx" --cur="$cur_"
                fi
                ;;
        esac
@@ -797,7 +914,7 @@ __git_compute_porcelain_commands ()
 __git_get_config_variables ()
 {
        local section="$1" i IFS=$'\n'
-       for i in $(git --git-dir="$(__gitdir)" config --name-only --get-regexp "^$section\..*" 2>/dev/null); do
+       for i in $(__git config --name-only --get-regexp "^$section\..*"); do
                echo "${i#$section.}"
        done
 }
@@ -815,8 +932,7 @@ __git_aliases ()
 # __git_aliased_command requires 1 argument
 __git_aliased_command ()
 {
-       local word cmdline=$(git --git-dir="$(__gitdir)" \
-               config --get "alias.$1" 2>/dev/null)
+       local word cmdline=$(__git config --get "alias.$1")
        for word in $cmdline; do
                case "$word" in
                \!gitk|gitk)
@@ -892,7 +1008,7 @@ __git_get_option_value ()
        done
 
        if [ -n "$config_key" ] && [ -z "$result" ]; then
-               result="$(git --git-dir="$(__gitdir)" config "$config_key")"
+               result="$(__git config "$config_key")"
        fi
 
        echo "$result"
@@ -951,8 +1067,8 @@ __git_whitespacelist="nowarn warn error error-all fix"
 
 _git_am ()
 {
-       local dir="$(__gitdir)"
-       if [ -d "$dir"/rebase-apply ]; then
+       __git_find_repo_path
+       if [ -d "$__git_repo_path"/rebase-apply ]; then
                __gitcomp "--skip --continue --resolved --abort"
                return
        fi
@@ -1035,7 +1151,8 @@ _git_bisect ()
        local subcommands="start bad good skip reset visualize replay log run"
        local subcommand="$(__git_find_on_cmdline "$subcommands")"
        if [ -z "$subcommand" ]; then
-               if [ -f "$(__gitdir)"/BISECT_START ]; then
+               __git_find_repo_path
+               if [ -f "$__git_repo_path"/BISECT_START ]; then
                        __gitcomp "$subcommands"
                else
                        __gitcomp "replay start"
@@ -1045,7 +1162,7 @@ _git_bisect ()
 
        case "$subcommand" in
        bad|good|reset|skip|start)
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
                ;;
        *)
                ;;
@@ -1067,7 +1184,7 @@ _git_branch ()
 
        case "$cur" in
        --set-upstream-to=*)
-               __gitcomp_nl "$(__git_refs)" "" "${cur##--set-upstream-to=}"
+               __git_complete_refs --cur="${cur##--set-upstream-to=}"
                ;;
        --*)
                __gitcomp "
@@ -1079,9 +1196,9 @@ _git_branch ()
                ;;
        *)
                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
-                       __gitcomp_nl "$(__git_heads)"
+                       __gitcomp_direct "$(__git_heads "" "$cur" " ")"
                else
-                       __gitcomp_nl "$(__git_refs)"
+                       __git_complete_refs
                fi
                ;;
        esac
@@ -1124,24 +1241,24 @@ _git_checkout ()
        *)
                # check if --track, --no-track, or --no-guess was specified
                # if so, disable DWIM mode
-               local flags="--track --no-track --no-guess" track=1
+               local flags="--track --no-track --no-guess" track_opt="--track"
                if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
-                       track=''
+                       track_opt=''
                fi
-               __gitcomp_nl "$(__git_refs '' $track)"
+               __git_complete_refs $track_opt
                ;;
        esac
 }
 
 _git_cherry ()
 {
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_cherry_pick ()
 {
-       local dir="$(__gitdir)"
-       if [ -f "$dir"/CHERRY_PICK_HEAD ]; then
+       __git_find_repo_path
+       if [ -f "$__git_repo_path"/CHERRY_PICK_HEAD ]; then
                __gitcomp "--continue --quit --abort"
                return
        fi
@@ -1150,7 +1267,7 @@ _git_cherry_pick ()
                __gitcomp "--edit --no-commit --signoff --strategy= --mainline"
                ;;
        *)
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
                ;;
        esac
 }
@@ -1200,7 +1317,7 @@ _git_commit ()
 {
        case "$prev" in
        -c|-C)
-               __gitcomp_nl "$(__git_refs)" "" "${cur}"
+               __git_complete_refs
                return
                ;;
        esac
@@ -1213,7 +1330,7 @@ _git_commit ()
                ;;
        --reuse-message=*|--reedit-message=*|\
        --fixup=*|--squash=*)
-               __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
+               __git_complete_refs --cur="${cur#*=}"
                return
                ;;
        --untracked-files=*)
@@ -1233,7 +1350,7 @@ _git_commit ()
                return
        esac
 
-       if git --git-dir="$(__gitdir)" rev-parse --verify --quiet HEAD >/dev/null; then
+       if __git rev-parse --verify --quiet HEAD >/dev/null; then
                __git_complete_index_file "--committable"
        else
                # This is the first commit
@@ -1251,7 +1368,7 @@ _git_describe ()
                        "
                return
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 __git_diff_algorithms="myers minimal patience histogram"
@@ -1401,8 +1518,43 @@ _git_gitk ()
        _gitk
 }
 
-__git_match_ctag() {
-       awk "/^${1//\//\\/}/ { print \$1 }" "$2"
+# Lists matching symbol names from a tag (as in ctags) file.
+# 1: List symbol names matching this word.
+# 2: The tag file to list symbol names from.
+# 3: A prefix to be added to each listed symbol name (optional).
+# 4: A suffix to be appended to each listed symbol name (optional).
+__git_match_ctag () {
+       awk -v pfx="${3-}" -v sfx="${4-}" "
+               /^${1//\//\\/}/ { print pfx \$1 sfx }
+               " "$2"
+}
+
+# Complete symbol names from a tag file.
+# Usage: __git_complete_symbol [<option>]...
+# --tags=<file>: The tag file to list symbol names from instead of the
+#                default "tags".
+# --pfx=<prefix>: A prefix to be added to each symbol name.
+# --cur=<word>: The current symbol name to be completed.  Defaults to
+#               the current word to be completed.
+# --sfx=<suffix>: A suffix to be appended to each symbol name instead
+#                 of the default space.
+__git_complete_symbol () {
+       local tags=tags pfx="" cur_="${cur-}" sfx=" "
+
+       while test $# != 0; do
+               case "$1" in
+               --tags=*)       tags="${1##--tags=}" ;;
+               --pfx=*)        pfx="${1##--pfx=}" ;;
+               --cur=*)        cur_="${1##--cur=}" ;;
+               --sfx=*)        sfx="${1##--sfx=}" ;;
+               *)              return 1 ;;
+               esac
+               shift
+       done
+
+       if test -r "$tags"; then
+               __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
+       fi
 }
 
 _git_grep ()
@@ -1430,14 +1582,11 @@ _git_grep ()
 
        case "$cword,$prev" in
        2,*|*,-*)
-               if test -r tags; then
-                       __gitcomp_nl "$(__git_match_ctag "$cur" tags)"
-                       return
-               fi
+               __git_complete_symbol && return
                ;;
        esac
 
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_help ()
@@ -1532,12 +1681,25 @@ __git_log_date_formats="relative iso8601 rfc2822 short local default raw"
 _git_log ()
 {
        __git_has_doubledash && return
+       __git_find_repo_path
 
-       local g="$(__gitdir)"
        local merge=""
-       if [ -f "$g/MERGE_HEAD" ]; then
+       if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
                merge="--merge"
        fi
+       case "$prev,$cur" in
+       -L,:*:*)
+               return  # fall back to Bash filename completion
+               ;;
+       -L,:*)
+               __git_complete_symbol --cur="${cur#:}" --sfx=":"
+               return
+               ;;
+       -G,*|-S,*)
+               __git_complete_symbol
+               return
+               ;;
+       esac
        case "$cur" in
        --pretty=*|--format=*)
                __gitcomp "$__git_log_pretty_formats $(__git_pretty_aliases)
@@ -1583,6 +1745,21 @@ _git_log ()
                        "
                return
                ;;
+       -L:*:*)
+               return  # fall back to Bash filename completion
+               ;;
+       -L:*)
+               __git_complete_symbol --cur="${cur#-L:}" --sfx=":"
+               return
+               ;;
+       -G*)
+               __git_complete_symbol --pfx="-G" --cur="${cur#-G}"
+               return
+               ;;
+       -S*)
+               __git_complete_symbol --pfx="-S" --cur="${cur#-S}"
+               return
+               ;;
        esac
        __git_complete_revlist
 }
@@ -1605,7 +1782,7 @@ _git_merge ()
                        --rerere-autoupdate --no-rerere-autoupdate --abort --continue"
                return
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_mergetool ()
@@ -1630,7 +1807,7 @@ _git_merge_base ()
                return
                ;;
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_mv ()
@@ -1668,7 +1845,7 @@ _git_notes ()
        ,*)
                case "$prev" in
                --ref)
-                       __gitcomp_nl "$(__git_refs)"
+                       __git_complete_refs
                        ;;
                *)
                        __gitcomp "$subcommands --ref"
@@ -1677,7 +1854,7 @@ _git_notes ()
                ;;
        add,--reuse-message=*|append,--reuse-message=*|\
        add,--reedit-message=*|append,--reedit-message=*)
-               __gitcomp_nl "$(__git_refs)" "" "${cur#*=}"
+               __git_complete_refs --cur="${cur#*=}"
                ;;
        add,--*|append,--*)
                __gitcomp '--file= --message= --reedit-message=
@@ -1696,7 +1873,7 @@ _git_notes ()
                -m|-F)
                        ;;
                *)
-                       __gitcomp_nl "$(__git_refs)"
+                       __git_complete_refs
                        ;;
                esac
                ;;
@@ -1734,10 +1911,10 @@ __git_complete_force_with_lease ()
        --*=)
                ;;
        *:*)
-               __gitcomp_nl "$(__git_refs)" "" "${cur_#*:}"
+               __git_complete_refs --cur="${cur_#*:}"
                ;;
        *)
-               __gitcomp_nl "$(__git_refs)" "" "$cur_"
+               __git_complete_refs --cur="$cur_"
                ;;
        esac
 }
@@ -1782,11 +1959,12 @@ _git_push ()
 
 _git_rebase ()
 {
-       local dir="$(__gitdir)"
-       if [ -f "$dir"/rebase-merge/interactive ]; then
+       __git_find_repo_path
+       if [ -f "$__git_repo_path"/rebase-merge/interactive ]; then
                __gitcomp "--continue --skip --abort --quit --edit-todo"
                return
-       elif [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
+       elif [ -d "$__git_repo_path"/rebase-apply ] || \
+            [ -d "$__git_repo_path"/rebase-merge ]; then
                __gitcomp "--continue --skip --abort --quit"
                return
        fi
@@ -1812,7 +1990,7 @@ _git_rebase ()
 
                return
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_reflog ()
@@ -1823,7 +2001,7 @@ _git_reflog ()
        if [ -z "$subcommand" ]; then
                __gitcomp "$subcommands"
        else
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
        fi
 }
 
@@ -1834,9 +2012,7 @@ _git_send_email ()
 {
        case "$prev" in
        --to|--cc|--bcc|--from)
-               __gitcomp "
-               $(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
-               "
+               __gitcomp "$(__git send-email --dump-aliases)"
                return
                ;;
        esac
@@ -1866,9 +2042,7 @@ _git_send_email ()
                return
                ;;
        --to=*|--cc=*|--bcc=*|--from=*)
-               __gitcomp "
-               $(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
-               " "" "${cur#--*=}"
+               __gitcomp "$(__git send-email --dump-aliases)" "" "${cur#--*=}"
                return
                ;;
        --*)
@@ -1962,7 +2136,7 @@ __git_config_get_set_variables ()
                c=$((--c))
        done
 
-       git --git-dir="$(__gitdir)" config $config_file --name-only --list 2>/dev/null
+       __git config $config_file --name-only --list
 }
 
 _git_config ()
@@ -1973,7 +2147,7 @@ _git_config ()
                return
                ;;
        branch.*.merge)
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
                return
                ;;
        branch.*.rebase)
@@ -1997,9 +2171,8 @@ _git_config ()
        remote.*.push)
                local remote="${prev#remote.}"
                remote="${remote%.push}"
-               __gitcomp_nl "$(git --git-dir="$(__gitdir)" \
-                       for-each-ref --format='%(refname):%(refname)' \
-                       refs/heads)"
+               __gitcomp_nl "$(__git for-each-ref \
+                       --format='%(refname):%(refname)' refs/heads)"
                return
                ;;
        pull.twohead|pull.octopus)
@@ -2078,7 +2251,7 @@ _git_config ()
                ;;
        branch.*)
                local pfx="${cur%.*}." cur_="${cur#*.}"
-               __gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
+               __gitcomp_direct "$(__git_heads "$pfx" "$cur_" ".")"
                __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
                return
                ;;
@@ -2448,7 +2621,7 @@ _git_remote ()
 
 _git_replace ()
 {
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_reset ()
@@ -2461,13 +2634,13 @@ _git_reset ()
                return
                ;;
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_revert ()
 {
-       local dir="$(__gitdir)"
-       if [ -f "$dir"/REVERT_HEAD ]; then
+       __git_find_repo_path
+       if [ -f "$__git_repo_path"/REVERT_HEAD ]; then
                __gitcomp "--continue --quit --abort"
                return
        fi
@@ -2477,7 +2650,7 @@ _git_revert ()
                return
                ;;
        esac
-       __gitcomp_nl "$(__git_refs)"
+       __git_complete_refs
 }
 
 _git_rm ()
@@ -2585,14 +2758,14 @@ _git_stash ()
                        ;;
                branch,*)
                        if [ $cword -eq 3 ]; then
-                               __gitcomp_nl "$(__git_refs)";
+                               __git_complete_refs
                        else
-                               __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
+                               __gitcomp_nl "$(__git stash list \
                                                | sed -n -e 's/:.*//p')"
                        fi
                        ;;
                show,*|apply,*|drop,*|pop,*)
-                       __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
+                       __gitcomp_nl "$(__git stash list \
                                        | sed -n -e 's/:.*//p')"
                        ;;
                *)
@@ -2724,7 +2897,7 @@ _git_tag ()
                i="${words[c]}"
                case "$i" in
                -d|-v)
-                       __gitcomp_nl "$(__git_tags)"
+                       __gitcomp_direct "$(__git_tags "" "$cur" " ")"
                        return
                        ;;
                -f)
@@ -2739,11 +2912,11 @@ _git_tag ()
                ;;
        -*|tag)
                if [ $f = 1 ]; then
-                       __gitcomp_nl "$(__git_tags)"
+                       __gitcomp_direct "$(__git_tags "" "$cur" " ")"
                fi
                ;;
        *)
-               __gitcomp_nl "$(__git_refs)"
+               __git_complete_refs
                ;;
        esac
 
@@ -2791,7 +2964,8 @@ _git_worktree ()
 
 __git_main ()
 {
-       local i c=1 command __git_dir
+       local i c=1 command __git_dir __git_repo_path
+       local __git_C_args C_args_count=0
 
        while [ $c -lt $cword ]; do
                i="${words[c]}"
@@ -2800,7 +2974,11 @@ __git_main ()
                --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
                --bare)      __git_dir="." ;;
                --help) command="help"; break ;;
-               -c|-C|--work-tree|--namespace) ((c++)) ;;
+               -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
@@ -2858,9 +3036,11 @@ __gitk_main ()
 {
        __git_has_doubledash && return
 
-       local g="$(__gitdir)"
+       local __git_repo_path
+       __git_find_repo_path
+
        local merge=""
-       if [ -f "$g/MERGE_HEAD" ]; then
+       if [ -f "$__git_repo_path/MERGE_HEAD" ]; then
                merge="--merge"
        fi
        case "$cur" in
@@ -2907,6 +3087,15 @@ if [[ -n ${ZSH_VERSION-} ]]; then
                esac
        }
 
+       __gitcomp_direct ()
+       {
+               emulate -L zsh
+
+               local IFS=$'\n'
+               compset -P '*[=:]'
+               compadd -Q -- ${=1} && _ret=0
+       }
+
        __gitcomp_nl ()
        {
                emulate -L zsh