Imported Upstream version 2.9.3
[platform/upstream/git.git] / contrib / completion / git-completion.bash
index 2186b4b..bd25b0a 100644 (file)
@@ -1,5 +1,3 @@
-#!bash
-#
 # bash/zsh completion support for core Git.
 #
 # Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
 #    *) local and remote tag names
 #    *) .git/remotes file names
 #    *) git 'subcommands'
+#    *) git email aliases for git-send-email
 #    *) tree paths within 'ref:path/to/file' expressions
+#    *) file paths within current working directory and index
 #    *) common --long-options
 #
 # To use these routines:
 #
-#    1) Copy this file to somewhere (e.g. ~/.git-completion.sh).
+#    1) Copy this file to somewhere (e.g. ~/.git-completion.bash).
 #    2) Add the following line to your .bashrc/.zshrc:
-#        source ~/.git-completion.sh
+#        source ~/.git-completion.bash
 #    3) Consider changing your PS1 to also show the current branch,
 #       see git-prompt.sh for details.
+#
+# If you use complex aliases of form '!f() { ... }; f', you can use the null
+# command ':' as the first command in the function body to declare the desired
+# completion style.  For example '!f() { : git commit ; ... }; f' will
+# tell the completion to use commit completion.  This also works with aliases
+# of form "!sh -c '...'".  For example, "!sh -c ': git commit ; ... '".
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -32,8 +38,6 @@ esac
 # returns location of .git repo
 __gitdir ()
 {
-       # Note: this function is duplicated in git-prompt.sh
-       # When updating it, make sure you update the other one to match.
        if [ -z "${1-}" ]; then
                if [ -n "${__git_dir-}" ]; then
                        echo "$__git_dir"
@@ -52,19 +56,6 @@ __gitdir ()
        fi
 }
 
-__gitcomp_1 ()
-{
-       local c IFS=$' \t\n'
-       for c in $1; do
-               c="$c$2"
-               case $c in
-               --*=*|*.) ;;
-               *) c="$c " ;;
-               esac
-               printf '%s\n' "$c"
-       done
-}
-
 # The following function is based on code from:
 #
 #   bash_completion - programmable completion functions for bash 3.2+
@@ -194,8 +185,24 @@ _get_comp_words_by_ref ()
 }
 fi
 
-# Generates completion reply with compgen, appending a space to possible
-# completion words, if necessary.
+__gitcompappend ()
+{
+       local x i=${#COMPREPLY[@]}
+       for x in $1; do
+               if [[ "$x" == "$3"* ]]; then
+                       COMPREPLY[i++]="$2$x$4"
+               fi
+       done
+}
+
+__gitcompadd ()
+{
+       COMPREPLY=()
+       __gitcompappend "$@"
+}
+
+# Generates completion reply, appending a space to possible completion words,
+# if necessary.
 # It accepts 1 to 4 arguments:
 # 1: List of possible completion words.
 # 2: A prefix to be added to each possible completion word (optional).
@@ -207,19 +214,33 @@ __gitcomp ()
 
        case "$cur_" in
        --*=)
-               COMPREPLY=()
                ;;
        *)
-               local IFS=$'\n'
-               COMPREPLY=($(compgen -P "${2-}" \
-                       -W "$(__gitcomp_1 "${1-}" "${4-}")" \
-                       -- "$cur_"))
+               local c i=0 IFS=$' \t\n'
+               for c in $1; do
+                       c="$c${4-}"
+                       if [[ $c == "$cur_"* ]]; then
+                               case $c in
+                               --*=*|*.) ;;
+                               *) c="$c " ;;
+                               esac
+                               COMPREPLY[i++]="${2-}$c"
+                       fi
+               done
                ;;
        esac
 }
 
-# Generates completion reply with compgen from newline-separated possible
-# completion words by appending a space to all of them.
+# Variation of __gitcomp_nl () that appends to the existing list of
+# completion candidates, COMPREPLY.
+__gitcomp_nl_append ()
+{
+       local IFS=$'\n'
+       __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
+}
+
+# Generates completion reply from newline-separated possible completion words
+# by appending a space to all of them.
 # It accepts 1 to 4 arguments:
 # 1: List of possible completion words, separated by a single newline.
 # 2: A prefix to be added to each possible completion word (optional).
@@ -229,8 +250,66 @@ __gitcomp ()
 #    appended.
 __gitcomp_nl ()
 {
+       COMPREPLY=()
+       __gitcomp_nl_append "$@"
+}
+
+# Generates completion reply with compgen from newline-separated possible
+# completion filenames.
+# It accepts 1 to 3 arguments:
+# 1: List of possible completion filenames, separated by a single newline.
+# 2: A directory prefix to be added to each possible completion filename
+#    (optional).
+# 3: Generate possible completion matches for this word (optional).
+__gitcomp_file ()
+{
        local IFS=$'\n'
-       COMPREPLY=($(compgen -P "${2-}" -S "${4- }" -W "$1" -- "${3-$cur}"))
+
+       # XXX does not work when the directory prefix contains a tilde,
+       # since tilde expansion is not applied.
+       # This means that COMPREPLY will be empty and Bash default
+       # completion will be used.
+       __gitcompadd "$1" "${2-}" "${3-$cur}" ""
+
+       # use a hack to enable file mode in bash < 4
+       compopt -o filenames +o nospace 2>/dev/null ||
+       compgen -f /non-existing-dir/ > /dev/null
+}
+
+# Execute 'git ls-files', unless the --committable option is specified, in
+# which case it runs 'git diff-index' to find out the files that can be
+# committed.  It return paths relative to the directory specified in the first
+# argument, and using the options specified in the second argument.
+__git_ls_files_helper ()
+{
+       if [ "$2" == "--committable" ]; then
+               git -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
+       fi 2>/dev/null
+}
+
+
+# __git_index_files accepts 1 or 2 arguments:
+# 1: Options to pass to ls-files (required).
+# 2: A directory path (optional).
+#    If provided, only files within the specified directory are listed.
+#    Sub directories are never recursed.  Path must have a trailing
+#    slash.
+__git_index_files ()
+{
+       local dir="$(__gitdir)" 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_heads ()
@@ -305,14 +384,9 @@ __git_refs ()
                done
                ;;
        *)
-               git ls-remote "$dir" HEAD ORIG_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" ;;
-                       esac
-               done
+               echo "HEAD"
+               git for-each-ref --format="%(refname:short)" -- \
+                       "refs/remotes/$dir/" 2>/dev/null | sed -e "s#^$dir/##"
                ;;
        esac
 }
@@ -338,12 +412,9 @@ __git_refs_remotes ()
 
 __git_remotes ()
 {
-       local i IFS=$'\n' d="$(__gitdir)"
+       local d="$(__gitdir)"
        test -d "$d/remotes" && ls -1 "$d/remotes"
-       for i in $(git --git-dir="$d" config --get-regexp 'remote\..*\.url' 2>/dev/null); do
-               i="${i#remote.}"
-               echo "${i/.url*/}"
-       done
+       git --git-dir="$d" remote
 }
 
 __git_list_merge_strategies ()
@@ -430,6 +501,25 @@ __git_complete_revlist_file ()
 }
 
 
+# __git_complete_index_file requires 1 argument:
+# 1: the options to pass to ls-file
+#
+# The exception is --committable, which finds the files appropriate commit.
+__git_complete_index_file ()
+{
+       local pfx="" cur_="$cur"
+
+       case "$cur_" in
+       ?*/*)
+               pfx="${cur_%/*}"
+               cur_="${cur_##*/}"
+               pfx="${pfx}/"
+               ;;
+       esac
+
+       __gitcomp_file "$(__git_index_files "$1" ${pfx:+"$pfx"})" "$pfx" "$cur_"
+}
+
 __git_complete_file ()
 {
        __git_complete_revlist_file
@@ -455,7 +545,6 @@ __git_complete_remote_or_refspec ()
                        case "$cmd" in
                        push) no_complete_refspec=1 ;;
                        fetch)
-                               COMPREPLY=()
                                return
                                ;;
                        *) ;;
@@ -471,7 +560,6 @@ __git_complete_remote_or_refspec ()
                return
        fi
        if [ $no_complete_refspec = 1 ]; then
-               COMPREPLY=()
                return
        fi
        [ "$remote" = "." ] && remote=
@@ -572,12 +660,15 @@ __git_list_porcelain_commands ()
                archimport)       : import;;
                cat-file)         : plumbing;;
                check-attr)       : plumbing;;
+               check-ignore)     : plumbing;;
+               check-mailmap)    : plumbing;;
                check-ref-format) : plumbing;;
                checkout-index)   : plumbing;;
+               column)           : internal helper;;
                commit-tree)      : plumbing;;
                count-objects)    : infrequent;;
-               credential-cache) : credentials helper;;
-               credential-store) : credentials helper;;
+               credential)       : credentials;;
+               credential-*)     : credentials helper;;
                cvsexportcommit)  : export;;
                cvsimport)        : import;;
                cvsserver)        : daemon;;
@@ -596,7 +687,6 @@ __git_list_porcelain_commands ()
                index-pack)       : plumbing;;
                init-db)          : deprecated;;
                local-fetch)      : plumbing;;
-               lost-found)       : infrequent;;
                ls-files)         : plumbing;;
                ls-remote)        : plumbing;;
                ls-tree)          : plumbing;;
@@ -610,14 +700,12 @@ __git_list_porcelain_commands ()
                pack-refs)        : plumbing;;
                parse-remote)     : plumbing;;
                patch-id)         : plumbing;;
-               peek-remote)      : plumbing;;
                prune)            : plumbing;;
                prune-packed)     : plumbing;;
                quiltimport)      : import;;
                read-tree)        : plumbing;;
                receive-pack)     : plumbing;;
                remote-*)         : transport;;
-               repo-config)      : deprecated;;
                rerere)           : plumbing;;
                rev-list)         : plumbing;;
                rev-parse)        : plumbing;;
@@ -630,7 +718,6 @@ __git_list_porcelain_commands ()
                ssh-*)            : transport;;
                stripspace)       : plumbing;;
                symbolic-ref)     : plumbing;;
-               tar-tree)         : deprecated;;
                unpack-file)      : plumbing;;
                unpack-objects)   : plumbing;;
                update-index)     : plumbing;;
@@ -650,35 +737,28 @@ __git_list_porcelain_commands ()
 __git_porcelain_commands=
 __git_compute_porcelain_commands ()
 {
-       __git_compute_all_commands
        test -n "$__git_porcelain_commands" ||
        __git_porcelain_commands=$(__git_list_porcelain_commands)
 }
 
-__git_pretty_aliases ()
+# Lists all set config variables starting with the given section prefix,
+# with the prefix removed.
+__git_get_config_variables ()
 {
-       local i IFS=$'\n'
-       for i in $(git --git-dir="$(__gitdir)" config --get-regexp "pretty\..*" 2>/dev/null); do
-               case "$i" in
-               pretty.*)
-                       i="${i#pretty.}"
-                       echo "${i/ */}"
-                       ;;
-               esac
+       local section="$1" i IFS=$'\n'
+       for i in $(git --git-dir="$(__gitdir)" config --name-only --get-regexp "^$section\..*" 2>/dev/null); do
+               echo "${i#$section.}"
        done
 }
 
+__git_pretty_aliases ()
+{
+       __git_get_config_variables "pretty"
+}
+
 __git_aliases ()
 {
-       local i IFS=$'\n'
-       for i in $(git --git-dir="$(__gitdir)" config --get-regexp "alias\..*" 2>/dev/null); do
-               case "$i" in
-               alias.*)
-                       i="${i#alias.}"
-                       echo "${i/ */}"
-                       ;;
-               esac
-       done
+       __git_get_config_variables "alias"
 }
 
 # __git_aliased_command requires 1 argument
@@ -696,6 +776,10 @@ __git_aliased_command ()
                -*)     : option ;;
                *=*)    : setting env ;;
                git)    : git itself ;;
+               \(\))   : skip parens of shell function definition ;;
+               {)      : skip start of shell helper function ;;
+               :)      : skip null command ;;
+               \'*)    : skip opening quote after sh -c ;;
                *)
                        echo "$word"
                        return
@@ -731,6 +815,43 @@ __git_has_doubledash ()
        return 1
 }
 
+# Try to count non option arguments passed on the command line for the
+# specified git command.
+# When options are used, it is necessary to use the special -- option to
+# tell the implementation were non option arguments begin.
+# XXX this can not be improved, since options can appear everywhere, as
+# an example:
+#      git mv x -n y
+#
+# __git_count_arguments requires 1 argument: the git command executed.
+__git_count_arguments ()
+{
+       local word i c=0
+
+       # Skip "git" (first argument)
+       for ((i=1; i < ${#words[@]}; i++)); do
+               word="${words[i]}"
+
+               case "$word" in
+                       --)
+                               # Good; we can assume that the following are only non
+                               # option arguments.
+                               ((c = 0))
+                               ;;
+                       "$1")
+                               # Skip the specified git command and discard git
+                               # main options
+                               ((c = 0))
+                               ;;
+                       ?*)
+                               ((c++))
+                               ;;
+               esac
+       done
+
+       printf "%d" $c
+}
+
 __git_whitespacelist="nowarn warn error error-all fix"
 
 _git_am ()
@@ -754,7 +875,6 @@ _git_am ()
                        "
                return
        esac
-       COMPREPLY=()
 }
 
 _git_apply ()
@@ -774,13 +894,10 @@ _git_apply ()
                        "
                return
        esac
-       COMPREPLY=()
 }
 
 _git_add ()
 {
-       __git_has_doubledash && return
-
        case "$cur" in
        --*)
                __gitcomp "
@@ -789,7 +906,9 @@ _git_add ()
                        "
                return
        esac
-       COMPREPLY=()
+
+       # XXX should we check for --update and --all options ?
+       __git_complete_index_file "--others --modified --directory --no-empty-directory"
 }
 
 _git_archive ()
@@ -834,7 +953,6 @@ _git_bisect ()
                __gitcomp_nl "$(__git_refs)"
                ;;
        *)
-               COMPREPLY=()
                ;;
        esac
 }
@@ -854,7 +972,7 @@ _git_branch ()
 
        case "$cur" in
        --set-upstream-to=*)
-               __gitcomp "$(__git_refs)" "" "${cur##--set-upstream-to=}"
+               __gitcomp_nl "$(__git_refs)" "" "${cur##--set-upstream-to=}"
                ;;
        --*)
                __gitcomp "
@@ -922,14 +1040,19 @@ _git_checkout ()
 
 _git_cherry ()
 {
-       __gitcomp "$(__git_refs)"
+       __gitcomp_nl "$(__git_refs)"
 }
 
 _git_cherry_pick ()
 {
+       local dir="$(__gitdir)"
+       if [ -f "$dir"/CHERRY_PICK_HEAD ]; then
+               __gitcomp "--continue --quit --abort"
+               return
+       fi
        case "$cur" in
        --*)
-               __gitcomp "--edit --no-commit"
+               __gitcomp "--edit --no-commit --signoff --strategy= --mainline"
                ;;
        *)
                __gitcomp_nl "$(__git_refs)"
@@ -939,15 +1062,15 @@ _git_cherry_pick ()
 
 _git_clean ()
 {
-       __git_has_doubledash && return
-
        case "$cur" in
        --*)
                __gitcomp "--dry-run --quiet"
                return
                ;;
        esac
-       COMPREPLY=()
+
+       # XXX should we check for -x option ?
+       __git_complete_index_file "--others --directory"
 }
 
 _git_clone ()
@@ -969,17 +1092,15 @@ _git_clone ()
                        --depth
                        --single-branch
                        --branch
+                       --recurse-submodules
                        "
                return
                ;;
        esac
-       COMPREPLY=()
 }
 
 _git_commit ()
 {
-       __git_has_doubledash && return
-
        case "$prev" in
        -c|-C)
                __gitcomp_nl "$(__git_refs)" "" "${cur}"
@@ -989,7 +1110,7 @@ _git_commit ()
 
        case "$cur" in
        --cleanup=*)
-               __gitcomp "default strip verbatim whitespace
+               __gitcomp "default scissors strip verbatim whitespace
                        " "" "${cur##--cleanup=}"
                return
                ;;
@@ -1014,7 +1135,13 @@ _git_commit ()
                        "
                return
        esac
-       COMPREPLY=()
+
+       if git rev-parse --verify --quiet HEAD >/dev/null; then
+               __git_complete_index_file "--committable"
+       else
+               # This is the first commit
+               __git_complete_index_file "--cached"
+       fi
 }
 
 _git_describe ()
@@ -1030,20 +1157,23 @@ _git_describe ()
        __gitcomp_nl "$(__git_refs)"
 }
 
+__git_diff_algorithms="myers minimal patience histogram"
+
 __git_diff_common_options="--stat --numstat --shortstat --summary
                        --patch-with-stat --name-only --name-status --color
                        --no-color --color-words --no-renames --check
                        --full-index --binary --abbrev --diff-filter=
                        --find-copies-harder
                        --text --ignore-space-at-eol --ignore-space-change
-                       --ignore-all-space --exit-code --quiet --ext-diff
-                       --no-ext-diff
+                       --ignore-all-space --ignore-blank-lines --exit-code
+                       --quiet --ext-diff --no-ext-diff
                        --no-prefix --src-prefix= --dst-prefix=
                        --inter-hunk-context=
-                       --patience
-                       --raw
+                       --patience --histogram --minimal
+                       --raw --word-diff --word-diff-regex=
                        --dirstat --dirstat= --dirstat-by-file
                        --dirstat-by-file= --cumulative
+                       --diff-algorithm=
 "
 
 _git_diff ()
@@ -1051,6 +1181,10 @@ _git_diff ()
        __git_has_doubledash && return
 
        case "$cur" in
+       --diff-algorithm=*)
+               __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
+               return
+               ;;
        --*)
                __gitcomp "--cached --staged --pickaxe-all --pickaxe-regex
                        --base --ours --theirs --no-index
@@ -1062,8 +1196,8 @@ _git_diff ()
        __git_complete_revlist_file
 }
 
-__git_mergetools_common="diffuse ecmerge emerge kdiff3 meld opendiff
-                       tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc3 codecompare
+__git_mergetools_common="diffuse diffmerge ecmerge emerge kdiff3 meld opendiff
+                       tkdiff vimdiff gvimdiff xxdiff araxis p4merge bc codecompare
 "
 
 _git_difftool ()
@@ -1084,17 +1218,23 @@ _git_difftool ()
                return
                ;;
        esac
-       __git_complete_file
+       __git_complete_revlist_file
 }
 
+__git_fetch_recurse_submodules="yes on-demand no"
+
 __git_fetch_options="
        --quiet --verbose --append --upload-pack --force --keep --depth=
-       --tags --no-tags --all --prune --dry-run
+       --tags --no-tags --all --prune --dry-run --recurse-submodules=
 "
 
 _git_fetch ()
 {
        case "$cur" in
+       --recurse-submodules=*)
+               __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
+               return
+               ;;
        --*)
                __gitcomp "$__git_fetch_options"
                return
@@ -1104,11 +1244,12 @@ _git_fetch ()
 }
 
 __git_format_patch_options="
-       --stdout --attach --no-attach --thread --thread= --output-directory
+       --stdout --attach --no-attach --thread --thread= --no-thread
        --numbered --start-number --numbered-files --keep-subject --signoff
        --signature --no-signature --in-reply-to= --cc= --full-index --binary
        --not --all --cover-letter --no-prefix --src-prefix= --dst-prefix=
        --inline --suffix= --ignore-if-in-upstream --subject-prefix=
+       --output-directory --reroll-count --to= --quiet --notes
 "
 
 _git_format_patch ()
@@ -1139,7 +1280,6 @@ _git_fsck ()
                return
                ;;
        esac
-       COMPREPLY=()
 }
 
 _git_gc ()
@@ -1150,7 +1290,6 @@ _git_gc ()
                return
                ;;
        esac
-       COMPREPLY=()
 }
 
 _git_gitk ()
@@ -1159,7 +1298,7 @@ _git_gitk ()
 }
 
 __git_match_ctag() {
-       awk "/^${1////\\/}/ { print \$1 }" "$2"
+       awk "/^${1//\//\\/}/ { print \$1 }" "$2"
 }
 
 _git_grep ()
@@ -1174,6 +1313,7 @@ _git_grep ()
                        --full-name --line-number
                        --extended-regexp --basic-regexp --fixed-strings
                        --perl-regexp
+                       --threads
                        --files-with-matches --name-only
                        --files-without-match
                        --max-depth
@@ -1200,15 +1340,15 @@ _git_help ()
 {
        case "$cur" in
        --*)
-               __gitcomp "--all --info --man --web"
+               __gitcomp "--all --guides --info --man --web"
                return
                ;;
        esac
        __git_compute_all_commands
        __gitcomp "$__git_all_commands $(__git_aliases)
                attributes cli core-tutorial cvs-migration
-               diffcore gitk glossary hooks ignore modules
-               namespaces repository-layout tutorial tutorial-2
+               diffcore everyday gitk glossary hooks ignore modules
+               namespaces repository-layout revisions tutorial tutorial-2
                workflows
                "
 }
@@ -1227,13 +1367,10 @@ _git_init ()
                return
                ;;
        esac
-       COMPREPLY=()
 }
 
 _git_ls_files ()
 {
-       __git_has_doubledash && return
-
        case "$cur" in
        --*)
                __gitcomp "--cached --deleted --modified --others --ignored
@@ -1246,7 +1383,10 @@ _git_ls_files ()
                return
                ;;
        esac
-       COMPREPLY=()
+
+       # XXX ignore options like --modified and always suggest all cached
+       # files.
+       __git_complete_index_file "--cached"
 }
 
 _git_ls_remote ()
@@ -1279,7 +1419,7 @@ __git_log_gitk_options="
 # Options that go well for log and shortlog (not gitk)
 __git_log_shortlog_options="
        --author= --committer= --grep=
-       --all-match
+       --all-match --invert-grep
 "
 
 __git_log_pretty_formats="oneline short medium full fuller email raw format:"
@@ -1305,7 +1445,7 @@ _git_log ()
                return
                ;;
        --decorate=*)
-               __gitcomp "long short" "" "${cur##--decorate=}"
+               __gitcomp "full short no" "" "${cur##--decorate=}"
                return
                ;;
        --*)
@@ -1318,6 +1458,8 @@ _git_log ()
                        --abbrev-commit --abbrev=
                        --relative-date --date=
                        --pretty= --format= --oneline
+                       --show-signature
+                       --cherry-mark
                        --cherry-pick
                        --graph
                        --decorate --decorate=
@@ -1333,9 +1475,12 @@ _git_log ()
        __git_complete_revlist
 }
 
+# Common merge options shared by git-merge(1) and git-pull(1).
 __git_merge_options="
        --no-commit --no-stat --log --no-log --squash --strategy
        --commit --stat --no-squash --ff --no-ff --ff-only --edit --no-edit
+       --verify-signatures --no-verify-signatures --gpg-sign
+       --quiet --verbose --progress --no-progress
 "
 
 _git_merge ()
@@ -1344,7 +1489,8 @@ _git_merge ()
 
        case "$cur" in
        --*)
-               __gitcomp "$__git_merge_options"
+               __gitcomp "$__git_merge_options
+                       --rerere-autoupdate --no-rerere-autoupdate --abort"
                return
        esac
        __gitcomp_nl "$(__git_refs)"
@@ -1362,11 +1508,16 @@ _git_mergetool ()
                return
                ;;
        esac
-       COMPREPLY=()
 }
 
 _git_merge_base ()
 {
+       case "$cur" in
+       --*)
+               __gitcomp "--octopus --independent --is-ancestor --fork-point"
+               return
+               ;;
+       esac
        __gitcomp_nl "$(__git_refs)"
 }
 
@@ -1378,7 +1529,14 @@ _git_mv ()
                return
                ;;
        esac
-       COMPREPLY=()
+
+       if [ $(__git_count_arguments "mv") -gt 0 ]; then
+               # We need to show both cached and untracked files (including
+               # empty directories) since this may not be the last argument.
+               __git_complete_index_file "--cached --others --directory"
+       else
+               __git_complete_index_file "--cached"
+       fi
 }
 
 _git_name_rev ()
@@ -1438,6 +1596,10 @@ _git_pull ()
        __git_complete_strategy && return
 
        case "$cur" in
+       --recurse-submodules=*)
+               __gitcomp "$__git_fetch_recurse_submodules" "" "${cur##--recurse-submodules=}"
+               return
+               ;;
        --*)
                __gitcomp "
                        --rebase --no-rebase
@@ -1450,22 +1612,55 @@ _git_pull ()
        __git_complete_remote_or_refspec
 }
 
+__git_push_recurse_submodules="check on-demand"
+
+__git_complete_force_with_lease ()
+{
+       local cur_=$1
+
+       case "$cur_" in
+       --*=)
+               ;;
+       *:*)
+               __gitcomp_nl "$(__git_refs)" "" "${cur_#*:}"
+               ;;
+       *)
+               __gitcomp_nl "$(__git_refs)" "" "$cur_"
+               ;;
+       esac
+}
+
 _git_push ()
 {
        case "$prev" in
        --repo)
                __gitcomp_nl "$(__git_remotes)"
                return
+               ;;
+       --recurse-submodules)
+               __gitcomp "$__git_push_recurse_submodules"
+               return
+               ;;
        esac
        case "$cur" in
        --repo=*)
                __gitcomp_nl "$(__git_remotes)" "" "${cur##--repo=}"
                return
                ;;
+       --recurse-submodules=*)
+               __gitcomp "$__git_push_recurse_submodules" "" "${cur##--recurse-submodules=}"
+               return
+               ;;
+       --force-with-lease=*)
+               __git_complete_force_with_lease "${cur##--force-with-lease=}"
+               return
+               ;;
        --*)
                __gitcomp "
                        --all --mirror --tags --dry-run --force --verbose
+                       --quiet --prune --delete --follow-tags
                        --receive-pack= --repo= --set-upstream
+                       --force-with-lease --force-with-lease= --recurse-submodules=
                "
                return
                ;;
@@ -1476,7 +1671,10 @@ _git_push ()
 _git_rebase ()
 {
        local dir="$(__gitdir)"
-       if [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
+       if [ -f "$dir"/rebase-merge/interactive ]; then
+               __gitcomp "--continue --skip --abort --edit-todo"
+               return
+       elif [ -d "$dir"/rebase-apply ] || [ -d "$dir"/rebase-merge ]; then
                __gitcomp "--continue --skip --abort"
                return
        fi
@@ -1492,7 +1690,12 @@ _git_rebase ()
                        --preserve-merges --stat --no-stat
                        --committer-date-is-author-date --ignore-date
                        --ignore-whitespace --whitespace=
-                       --autosquash
+                       --autosquash --no-autosquash
+                       --fork-point --no-fork-point
+                       --autostash --no-autostash
+                       --verify --no-verify
+                       --keep-empty --root --force-rebase --no-ff
+                       --exec
                        "
 
                return
@@ -1517,6 +1720,15 @@ __git_send_email_suppresscc_options="author self cc bodycc sob cccmd body all"
 
 _git_send_email ()
 {
+       case "$prev" in
+       --to|--cc|--bcc|--from)
+               __gitcomp "
+               $(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
+               "
+               return
+               ;;
+       esac
+
        case "$cur" in
        --confirm=*)
                __gitcomp "
@@ -1541,6 +1753,12 @@ _git_send_email ()
                        " "" "${cur##--thread=}"
                return
                ;;
+       --to=*|--cc=*|--bcc=*|--from=*)
+               __gitcomp "
+               $(git --git-dir="$(__gitdir)" send-email --dump-aliases 2>/dev/null)
+               " "" "${cur#--*=}"
+               return
+               ;;
        --*)
                __gitcomp "--annotate --bcc --cc --cc-cmd --chain-reply-to
                        --compose --confirm= --dry-run --envelope-sender
@@ -1569,7 +1787,7 @@ __git_config_get_set_variables ()
        while [ $c -gt 1 ]; do
                word="${words[c]}"
                case "$word" in
-               --global|--system|--file=*)
+               --system|--global|--local|--file=*)
                        config_file="$word"
                        break
                        ;;
@@ -1582,21 +1800,13 @@ __git_config_get_set_variables ()
                c=$((--c))
        done
 
-       git --git-dir="$(__gitdir)" config $config_file --list 2>/dev/null |
-       while read -r line
-       do
-               case "$line" in
-               *.*=*)
-                       echo "${line/=*/}"
-                       ;;
-               esac
-       done
+       git --git-dir="$(__gitdir)" config $config_file --name-only --list 2>/dev/null
 }
 
 _git_config ()
 {
        case "$prev" in
-       branch.*.remote)
+       branch.*.remote|branch.*.pushremote)
                __gitcomp_nl "$(__git_remotes)"
                return
                ;;
@@ -1604,11 +1814,19 @@ _git_config ()
                __gitcomp_nl "$(__git_refs)"
                return
                ;;
+       branch.*.rebase)
+               __gitcomp "false true preserve interactive"
+               return
+               ;;
+       remote.pushdefault)
+               __gitcomp_nl "$(__git_remotes)"
+               return
+               ;;
        remote.*.fetch)
                local remote="${prev#remote.}"
                remote="${remote%.fetch}"
                if [ -z "$cur" ]; then
-                       COMPREPLY=("refs/heads/")
+                       __gitcomp_nl "refs/heads/" "" "" ""
                        return
                fi
                __gitcomp_nl "$(__git_refs_remotes "$remote")"
@@ -1643,6 +1861,10 @@ _git_config ()
                        "
                return
                ;;
+       diff.submodule)
+               __gitcomp "log short"
+               return
+               ;;
        help.format)
                __gitcomp "man info web html"
                return
@@ -1663,34 +1885,39 @@ _git_config ()
                __gitcomp "$__git_send_email_suppresscc_options"
                return
                ;;
+       sendemail.transferencoding)
+               __gitcomp "7bit 8bit quoted-printable base64"
+               return
+               ;;
        --get|--get-all|--unset|--unset-all)
                __gitcomp_nl "$(__git_config_get_set_variables)"
                return
                ;;
        *.*)
-               COMPREPLY=()
                return
                ;;
        esac
        case "$cur" in
        --*)
                __gitcomp "
-                       --global --system --file=
+                       --system --global --local --file=
                        --list --replace-all
                        --get --get-all --get-regexp
                        --add --unset --unset-all
                        --remove-section --rename-section
+                       --name-only
                        "
                return
                ;;
        branch.*.*)
                local pfx="${cur%.*}." cur_="${cur##*.}"
-               __gitcomp "remote merge mergeoptions rebase" "$pfx" "$cur_"
+               __gitcomp "remote pushremote merge mergeoptions rebase" "$pfx" "$cur_"
                return
                ;;
        branch.*)
                local pfx="${cur%.*}." cur_="${cur#*.}"
                __gitcomp_nl "$(__git_heads)" "$pfx" "$cur_" "."
+               __gitcomp_nl_append $'autosetupmerge\nautosetuprebase\n' "$pfx" "$cur_"
                return
                ;;
        guitool.*.*)
@@ -1733,6 +1960,7 @@ _git_config ()
        remote.*)
                local pfx="${cur%.*}." cur_="${cur#*.}"
                __gitcomp_nl "$(__git_remotes)" "$pfx" "$cur_" "."
+               __gitcomp_nl_append "pushdefault" "$pfx" "$cur_"
                return
                ;;
        url.*.*)
@@ -1796,6 +2024,7 @@ _git_config ()
                color.status.changed
                color.status.header
                color.status.nobranch
+               color.status.unmerged
                color.status.untracked
                color.status.updated
                color.ui
@@ -1816,7 +2045,6 @@ _git_config ()
                core.fileMode
                core.fsyncobjectfiles
                core.gitProxy
-               core.ignoreCygwinFSTricks
                core.ignoreStat
                core.ignorecase
                core.logAllRefUpdates
@@ -1834,26 +2062,30 @@ _git_config ()
                core.sparseCheckout
                core.symlinks
                core.trustctime
+               core.untrackedCache
                core.warnAmbiguousRefs
                core.whitespace
                core.worktree
                diff.autorefreshindex
-               diff.statGraphWidth
                diff.external
                diff.ignoreSubmodules
                diff.mnemonicprefix
                diff.noprefix
                diff.renameLimit
                diff.renames
+               diff.statGraphWidth
+               diff.submodule
                diff.suppressBlankEmpty
                diff.tool
                diff.wordRegex
+               diff.algorithm
                difftool.
                difftool.prompt
                fetch.recurseSubmodules
                fetch.unpackLimit
                format.attach
                format.cc
+               format.coverLetter
                format.headers
                format.numbered
                format.pretty
@@ -1905,6 +2137,8 @@ _git_config ()
                http.noEPSV
                http.postBuffer
                http.proxy
+               http.sslCipherList
+               http.sslVersion
                http.sslCAInfo
                http.sslCAPath
                http.sslCert
@@ -1968,6 +2202,7 @@ _git_config ()
                pull.octopus
                pull.twohead
                push.default
+               push.followTags
                rebase.autosquash
                rebase.stat
                receive.autogc
@@ -1978,6 +2213,7 @@ _git_config ()
                receive.fsckObjects
                receive.unpackLimit
                receive.updateserverinfo
+               remote.pushdefault
                remotes.
                repack.usedeltabaseoffset
                rerere.autoupdate
@@ -2040,15 +2276,9 @@ _git_remote ()
                __git_complete_remote_or_refspec
                ;;
        update)
-               local i c='' IFS=$'\n'
-               for i in $(git --git-dir="$(__gitdir)" config --get-regexp "remotes\..*" 2>/dev/null); do
-                       i="${i#remotes.}"
-                       c="$c ${i/ */}"
-               done
-               __gitcomp "$c"
+               __gitcomp "$(__git_get_config_variables "remotes")"
                ;;
        *)
-               COMPREPLY=()
                ;;
        esac
 }
@@ -2073,6 +2303,11 @@ _git_reset ()
 
 _git_revert ()
 {
+       local dir="$(__gitdir)"
+       if [ -f "$dir"/REVERT_HEAD ]; then
+               __gitcomp "--continue --quit --abort"
+               return
+       fi
        case "$cur" in
        --*)
                __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
@@ -2084,15 +2319,14 @@ _git_revert ()
 
 _git_rm ()
 {
-       __git_has_doubledash && return
-
        case "$cur" in
        --*)
                __gitcomp "--cached --dry-run --ignore-unmatch --quiet"
                return
                ;;
        esac
-       COMPREPLY=()
+
+       __git_complete_index_file "--cached"
 }
 
 _git_shortlog ()
@@ -2122,14 +2356,19 @@ _git_show ()
                        " "" "${cur#*=}"
                return
                ;;
+       --diff-algorithm=*)
+               __gitcomp "$__git_diff_algorithms" "" "${cur##--diff-algorithm=}"
+               return
+               ;;
        --*)
                __gitcomp "--pretty= --format= --abbrev-commit --oneline
+                       --show-signature
                        $__git_diff_common_options
                        "
                return
                ;;
        esac
-       __git_complete_file
+       __git_complete_revlist_file
 }
 
 _git_show_branch ()
@@ -2137,7 +2376,7 @@ _git_show_branch ()
        case "$cur" in
        --*)
                __gitcomp "
-                       --all --remotes --topo-order --current --more=
+                       --all --remotes --topo-order --date-order --current --more=
                        --list --independent --merge-base --no-name
                        --color --no-color
                        --sha1-name --sparse --topics --reflog
@@ -2150,7 +2389,7 @@ _git_show_branch ()
 
 _git_stash ()
 {
-       local save_opts='--keep-index --no-keep-index --quiet --patch'
+       local save_opts='--all --keep-index --no-keep-index --quiet --patch --include-untracked'
        local subcommands='save list show apply clear drop pop create branch'
        local subcommand="$(__git_find_on_cmdline "$subcommands")"
        if [ -z "$subcommand" ]; then
@@ -2161,8 +2400,6 @@ _git_stash ()
                *)
                        if [ -z "$(__git_find_on_cmdline "$save_opts")" ]; then
                                __gitcomp "$subcommands"
-                       else
-                               COMPREPLY=()
                        fi
                        ;;
                esac
@@ -2174,15 +2411,24 @@ _git_stash ()
                apply,--*|pop,--*)
                        __gitcomp "--index --quiet"
                        ;;
-               show,--*|drop,--*|branch,--*)
-                       COMPREPLY=()
+               drop,--*)
+                       __gitcomp "--quiet"
+                       ;;
+               show,--*|branch,--*)
                        ;;
-               show,*|apply,*|drop,*|pop,*|branch,*)
+               branch,*)
+                       if [ $cword -eq 3 ]; then
+                               __gitcomp_nl "$(__git_refs)";
+                       else
+                               __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
+                                               | sed -n -e 's/:.*//p')"
+                       fi
+                       ;;
+               show,*|apply,*|drop,*|pop,*)
                        __gitcomp_nl "$(git --git-dir="$(__gitdir)" stash list \
                                        | sed -n -e 's/:.*//p')"
                        ;;
                *)
-                       COMPREPLY=()
                        ;;
                esac
        fi
@@ -2192,7 +2438,7 @@ _git_submodule ()
 {
        __git_has_doubledash && return
 
-       local subcommands="add status init update summary foreach sync"
+       local subcommands="add status init deinit update summary foreach sync"
        if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
                case "$cur" in
                --*)
@@ -2224,7 +2470,7 @@ _git_svn ()
                        --no-metadata --use-svm-props --use-svnsync-props
                        --log-window-size= --no-checkout --quiet
                        --repack-flags --use-log-author --localtime
-                       --ignore-paths= $remote_opts
+                       --ignore-paths= --include-paths= $remote_opts
                        "
                local init_opts="
                        --template= --shared= --trunk= --tags=
@@ -2299,7 +2545,6 @@ _git_svn ()
                        __gitcomp "--revision= --parent"
                        ;;
                *)
-                       COMPREPLY=()
                        ;;
                esac
        fi
@@ -2324,19 +2569,26 @@ _git_tag ()
 
        case "$prev" in
        -m|-F)
-               COMPREPLY=()
                ;;
        -*|tag)
                if [ $f = 1 ]; then
                        __gitcomp_nl "$(__git_tags)"
-               else
-                       COMPREPLY=()
                fi
                ;;
        *)
                __gitcomp_nl "$(__git_refs)"
                ;;
        esac
+
+       case "$cur" in
+       --*)
+               __gitcomp "
+                       --list --delete --verify --annotate --message --file
+                       --sign --cleanup --local-user --force --column --sort
+                       --contains --points-at
+                       "
+               ;;
+       esac
 }
 
 _git_whatchanged ()
@@ -2352,9 +2604,10 @@ __git_main ()
                i="${words[c]}"
                case "$i" in
                --git-dir=*) __git_dir="${i#--git-dir=}" ;;
+               --git-dir)   ((c++)) ; __git_dir="${words[c]}" ;;
                --bare)      __git_dir="." ;;
                --help) command="help"; break ;;
-               -c) c=$((++c)) ;;
+               -c|--work-tree|--namespace) ((c++)) ;;
                -*) ;;
                *) command="$i"; break ;;
                esac
@@ -2372,6 +2625,7 @@ __git_main ()
                        --exec-path
                        --exec-path=
                        --html-path
+                       --man-path
                        --info-path
                        --work-tree=
                        --namespace=
@@ -2390,6 +2644,7 @@ __git_main ()
 
        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
        fi
@@ -2440,7 +2695,7 @@ if [[ -n ${ZSH_VERSION-} ]]; then
                                --*=*|*.) ;;
                                *) c="$c " ;;
                                esac
-                               array[$#array+1]="$c"
+                               array[${#array[@]}+1]="$c"
                        done
                        compset -P '*[=:]'
                        compadd -Q -S '' -p "${2-}" -a -- array && _ret=0
@@ -2457,22 +2712,23 @@ if [[ -n ${ZSH_VERSION-} ]]; then
                compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
        }
 
-       __git_zsh_helper ()
+       __gitcomp_file ()
        {
-               emulate -L ksh
-               local cur cword prev
-               cur=${words[CURRENT-1]}
-               prev=${words[CURRENT-2]}
-               let cword=CURRENT-1
-               __${service}_main
+               emulate -L zsh
+
+               local IFS=$'\n'
+               compset -P '*[=:]'
+               compadd -Q -p "${2-}" -f -- ${=1} && _ret=0
        }
 
        _git ()
        {
-               emulate -L zsh
-               local _ret=1
-               __git_zsh_helper
-               let _ret && _default -S '' && _ret=0
+               local _ret=1 cur cword prev
+               cur=${words[CURRENT]}
+               prev=${words[CURRENT-1]}
+               let cword=CURRENT-1
+               emulate ksh -c __${service}_main
+               let _ret && _default && _ret=0
                return _ret
        }