Merge branch 'jc/apply-fix-mismerge'
[gitweb.git] / contrib / completion / git-completion.bash
index d26312899db45180524a394be0d0064c2ab38675..1ed0a09feef51ae606dbc2d7aa1944a49c6c28ee 100644 (file)
 # 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 ; ... '".
+#
+# You can set the following environment variables to influence the behavior of
+# the completion routines:
+#
+#   GIT_COMPLETION_CHECKOUT_NO_GUESS
+#
+#     When set to "1", do not include "DWIM" suggestions in git-checkout
+#     completion (e.g., completing "foo" when "origin/foo" exists).
 
 case "$COMP_WORDBREAKS" in
 *:*) : great ;;
@@ -213,6 +221,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[@]}
@@ -338,14 +360,27 @@ __git_index_files ()
        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 ()
 {
-       __git for-each-ref --format='%(refname:strip=2)' refs/heads
+       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 ()
 {
-       __git for-each-ref --format='%(refname:strip=2)' refs/tags
+       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.
@@ -354,18 +389,21 @@ __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: Currently ignored.
+# 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 track="${2-}"
        local list_refs_from=path remote="${1-}"
-       local format refs pfx
-       local cur_="${4-$cur}"
+       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"
@@ -390,7 +428,8 @@ __git_refs ()
 
        if [ "$list_refs_from" = path ]; then
                if [[ "$cur_" == ^* ]]; then
-                       pfx="^"
+                       pfx="$pfx^"
+                       fer_pfx="$fer_pfx^"
                        cur_=${cur_#^}
                        match=${match#^}
                fi
@@ -405,7 +444,7 @@ __git_refs ()
                                case "$i" in
                                $match*)
                                        if [ -e "$dir/$i" ]; then
-                                               echo $pfx$i
+                                               echo "$pfx$i$sfx"
                                        fi
                                        ;;
                                esac
@@ -416,13 +455,13 @@ __git_refs ()
                                "refs/remotes/$match*" "refs/remotes/$match*/**")
                        ;;
                esac
-               __git_dir="$dir" __git for-each-ref --format="$pfx%($format)" \
+               __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
-                       __git for-each-ref --format="%(refname:strip=3)" \
+                       __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
                                --sort="refname:strip=3" \
                                "refs/remotes/*/$match*" "refs/remotes/*/$match*/**" | \
                        uniq -u
@@ -435,16 +474,16 @@ __git_refs ()
                while read -r hash i; do
                        case "$i" in
                        *^{}) ;;
-                       *) echo "$i" ;;
+                       *) echo "$pfx$i$sfx" ;;
                        esac
                done
                ;;
        *)
                if [ "$list_refs_from" = remote ]; then
                        case "HEAD" in
-                       $match*)        echo "HEAD" ;;
+                       $match*)        echo "${pfx}HEAD$sfx" ;;
                        esac
-                       __git for-each-ref --format="%(refname:strip=3)" \
+                       __git for-each-ref --format="$fer_pfx%(refname:strip=3)$sfx" \
                                "refs/remotes/$remote/$match*" \
                                "refs/remotes/$remote/$match*/**"
                else
@@ -458,8 +497,8 @@ __git_refs ()
                        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
@@ -494,8 +533,7 @@ __git_complete_refs ()
                shift
        done
 
-       __gitcomp_nl "$(__git_refs "$remote" "$track" "" "$cur_")" \
-               "$pfx" "$cur_" "$sfx"
+       __gitcomp_direct "$(__git_refs "$remote" "$track" "$pfx" "$cur_" "$sfx")"
 }
 
 # __git_refs2 requires 1 argument (to pass to __git_refs)
@@ -519,11 +557,11 @@ __git_complete_fetch_refspecs ()
 {
        local i remote="$1" pfx="${2-}" cur_="${3-$cur}" sfx="${4- }"
 
-       __gitcomp_nl "$(
+       __gitcomp_direct "$(
                for i in $(__git_refs "$remote" "" "" "$cur_") ; do
-                       echo "$i:$i"
+                       echo "$pfx$i:$i$sfx"
                done
-               )" "$pfx" "$cur_" "$sfx"
+               )"
 }
 
 # __git_refs_remotes requires 1 argument (to pass to ls-remote)
@@ -679,6 +717,7 @@ __git_complete_remote_or_refspec ()
                i="${words[c]}"
                case "$i" in
                --mirror) [ "$cmd" = "push" ] && no_complete_refspec=1 ;;
+               -d|--delete) [ "$cmd" = "push" ] && lhs=0 ;;
                --all)
                        case "$cmd" in
                        push) no_complete_refspec=1 ;;
@@ -1072,6 +1111,7 @@ _git_apply ()
                        --apply --no-add --exclude=
                        --ignore-whitespace --ignore-space-change
                        --whitespace= --inaccurate-eof --verbose
+                       --recount --directory=
                        "
                return
        esac
@@ -1083,13 +1123,17 @@ _git_add ()
        --*)
                __gitcomp "
                        --interactive --refresh --patch --update --dry-run
-                       --ignore-errors --intent-to-add
+                       --ignore-errors --intent-to-add --force --edit --chmod=
                        "
                return
        esac
 
-       # XXX should we check for --update and --all options ?
-       __git_complete_index_file "--others --modified --directory --no-empty-directory"
+       local complete_opt="--others --modified --directory --no-empty-directory"
+       if test -n "$(__git_find_on_cmdline "-u --update")"
+       then
+               complete_opt="--modified"
+       fi
+       __git_complete_index_file "$complete_opt"
 }
 
 _git_archive ()
@@ -1106,7 +1150,7 @@ _git_archive ()
        --*)
                __gitcomp "
                        --format= --list --verbose
-                       --prefix= --remote= --exec=
+                       --prefix= --remote= --exec= --output
                        "
                return
                ;;
@@ -1159,14 +1203,15 @@ _git_branch ()
        --*)
                __gitcomp "
                        --color --no-color --verbose --abbrev= --no-abbrev
-                       --track --no-track --contains --merged --no-merged
+                       --track --no-track --contains --no-contains --merged --no-merged
                        --set-upstream-to= --edit-description --list
                        --unset-upstream --delete --move --remotes
+                       --column --no-column --sort= --points-at
                        "
                ;;
        *)
                if [ $only_local_ref = "y" -a $has_r = "n" ]; then
-                       __gitcomp_nl "$(__git_heads)"
+                       __gitcomp_direct "$(__git_heads "" "$cur" " ")"
                else
                        __git_complete_refs
                fi
@@ -1212,7 +1257,8 @@ _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_opt="--track"
-               if [ -n "$(__git_find_on_cmdline "$flags")" ]; then
+               if [ "$GIT_COMPLETION_CHECKOUT_NO_GUESS" = "1" ] ||
+                  [ -n "$(__git_find_on_cmdline "$flags")" ]; then
                        track_opt=''
                fi
                __git_complete_refs $track_opt
@@ -1273,8 +1319,11 @@ _git_clone ()
                        --template=
                        --depth
                        --single-branch
+                       --no-tags
                        --branch
                        --recurse-submodules
+                       --no-single-branch
+                       --shallow-submodules
                        "
                return
                ;;
@@ -1316,6 +1365,7 @@ _git_commit ()
                        --reset-author --file= --message= --template=
                        --cleanup= --untracked-files --untracked-files=
                        --verbose --quiet --fixup= --squash=
+                       --patch --short --date --allow-empty
                        "
                return
        esac
@@ -1334,7 +1384,8 @@ _git_describe ()
        --*)
                __gitcomp "
                        --all --tags --contains --abbrev= --candidates=
-                       --exact-match --debug --long --match --always
+                       --exact-match --debug --long --match --always --first-parent
+                       --exclude
                        "
                return
        esac
@@ -1417,6 +1468,7 @@ __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 --recurse-submodules=
+       --unshallow --update-shallow
 "
 
 _git_fetch ()
@@ -1466,7 +1518,7 @@ _git_fsck ()
        --*)
                __gitcomp "
                        --tags --root --unreachable --cache --no-reflogs --full
-                       --strict --verbose --lost-found
+                       --strict --verbose --lost-found --name-objects
                        "
                return
                ;;
@@ -1488,8 +1540,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 ()
@@ -1510,6 +1597,8 @@ _git_grep ()
                        --max-depth
                        --count
                        --and --or --not --all-match
+                       --break --heading --show-function --function-context
+                       --untracked --no-index
                        "
                return
                ;;
@@ -1517,10 +1606,7 @@ _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
 
@@ -1582,6 +1668,12 @@ _git_ls_files ()
 
 _git_ls_remote ()
 {
+       case "$cur" in
+       --*)
+               __gitcomp "--heads --tags --refs --get-url --symref"
+               return
+               ;;
+       esac
        __gitcomp_nl "$(__git_remotes)"
 }
 
@@ -1625,6 +1717,19 @@ _git_log ()
        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)
@@ -1670,6 +1775,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
 }
@@ -1703,7 +1823,7 @@ _git_mergetool ()
                return
                ;;
        --*)
-               __gitcomp "--tool="
+               __gitcomp "--tool= --prompt --no-prompt"
                return
                ;;
        esac
@@ -1811,7 +1931,7 @@ _git_pull ()
        __git_complete_remote_or_refspec
 }
 
-__git_push_recurse_submodules="check on-demand"
+__git_push_recurse_submodules="check on-demand only"
 
 __git_complete_force_with_lease ()
 {
@@ -2161,7 +2281,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
                ;;
@@ -2268,7 +2388,9 @@ _git_config ()
                color.status.added
                color.status.changed
                color.status.header
+               color.status.localBranch
                color.status.nobranch
+               color.status.remoteBranch
                color.status.unmerged
                color.status.untracked
                color.status.updated
@@ -2507,40 +2629,88 @@ _git_config ()
 
 _git_remote ()
 {
-       local subcommands="add rename remove set-head set-branches set-url show prune update"
+       local subcommands="
+               add rename remove set-head set-branches
+               get-url set-url show prune update
+               "
        local subcommand="$(__git_find_on_cmdline "$subcommands")"
        if [ -z "$subcommand" ]; then
-               __gitcomp "$subcommands"
+               case "$cur" in
+               --*)
+                       __gitcomp "--verbose"
+                       ;;
+               *)
+                       __gitcomp "$subcommands"
+                       ;;
+               esac
                return
        fi
 
-       case "$subcommand" in
-       rename|remove|set-url|show|prune)
-               __gitcomp_nl "$(__git_remotes)"
+       case "$subcommand,$cur" in
+       add,--*)
+               __gitcomp "--track --master --fetch --tags --no-tags --mirror="
+               ;;
+       add,*)
+               ;;
+       set-head,--*)
+               __gitcomp "--auto --delete"
                ;;
-       set-head|set-branches)
+       set-branches,--*)
+               __gitcomp "--add"
+               ;;
+       set-head,*|set-branches,*)
                __git_complete_remote_or_refspec
                ;;
-       update)
+       update,--*)
+               __gitcomp "--prune"
+               ;;
+       update,*)
                __gitcomp "$(__git_get_config_variables "remotes")"
                ;;
+       set-url,--*)
+               __gitcomp "--push --add --delete"
+               ;;
+       get-url,--*)
+               __gitcomp "--push --all"
+               ;;
+       prune,--*)
+               __gitcomp "--dry-run"
+               ;;
        *)
+               __gitcomp_nl "$(__git_remotes)"
                ;;
        esac
 }
 
 _git_replace ()
 {
+       case "$cur" in
+       --*)
+               __gitcomp "--edit --graft --format= --list --delete"
+               return
+               ;;
+       esac
        __git_complete_refs
 }
 
+_git_rerere ()
+{
+       local subcommands="clear forget diff remaining status gc"
+       local subcommand="$(__git_find_on_cmdline "$subcommands")"
+       if test -z "$subcommand"
+       then
+               __gitcomp "$subcommands"
+               return
+       fi
+}
+
 _git_reset ()
 {
        __git_has_doubledash && return
 
        case "$cur" in
        --*)
-               __gitcomp "--merge --mixed --hard --soft --patch"
+               __gitcomp "--merge --mixed --hard --soft --patch --keep"
                return
                ;;
        esac
@@ -2556,7 +2726,10 @@ _git_revert ()
        fi
        case "$cur" in
        --*)
-               __gitcomp "--edit --mainline --no-edit --no-commit --signoff"
+               __gitcomp "
+                       --edit --mainline --no-edit --no-commit --signoff
+                       --strategy= --strategy-option=
+                       "
                return
                ;;
        esac
@@ -2584,7 +2757,7 @@ _git_shortlog ()
                __gitcomp "
                        $__git_log_common_options
                        $__git_log_shortlog_options
-                       --numbered --summary
+                       --numbered --summary --email
                        "
                return
                ;;
@@ -2689,10 +2862,11 @@ _git_submodule ()
        __git_has_doubledash && return
 
        local subcommands="add status init deinit update summary foreach sync"
-       if [ -z "$(__git_find_on_cmdline "$subcommands")" ]; then
+       local subcommand="$(__git_find_on_cmdline "$subcommands")"
+       if [ -z "$subcommand" ]; then
                case "$cur" in
                --*)
-                       __gitcomp "--quiet --cached"
+                       __gitcomp "--quiet"
                        ;;
                *)
                        __gitcomp "$subcommands"
@@ -2700,6 +2874,33 @@ _git_submodule ()
                esac
                return
        fi
+
+       case "$subcommand,$cur" in
+       add,--*)
+               __gitcomp "--branch --force --name --reference --depth"
+               ;;
+       status,--*)
+               __gitcomp "--cached --recursive"
+               ;;
+       deinit,--*)
+               __gitcomp "--force --all"
+               ;;
+       update,--*)
+               __gitcomp "
+                       --init --remote --no-fetch
+                       --recommend-shallow --no-recommend-shallow
+                       --force --rebase --merge --reference --depth --recursive --jobs
+               "
+               ;;
+       summary,--*)
+               __gitcomp "--cached --files --summary-limit"
+               ;;
+       foreach,--*|sync,--*)
+               __gitcomp "--recursive"
+               ;;
+       *)
+               ;;
+       esac
 }
 
 _git_svn ()
@@ -2720,14 +2921,14 @@ _git_svn ()
                        --no-metadata --use-svm-props --use-svnsync-props
                        --log-window-size= --no-checkout --quiet
                        --repack-flags --use-log-author --localtime
+                       --add-author-from
                        --ignore-paths= --include-paths= $remote_opts
                        "
                local init_opts="
                        --template= --shared= --trunk= --tags=
                        --branches= --stdlayout --minimize-url
                        --no-metadata --use-svm-props --use-svnsync-props
-                       --rewrite-root= --prefix= --use-log-author
-                       --add-author-from $remote_opts
+                       --rewrite-root= --prefix= $remote_opts
                        "
                local cmt_opts="
                        --edit --rmdir --find-copies-harder --copy-similarity=
@@ -2807,7 +3008,7 @@ _git_tag ()
                i="${words[c]}"
                case "$i" in
                -d|-v)
-                       __gitcomp_nl "$(__git_tags)"
+                       __gitcomp_direct "$(__git_tags "" "$cur" " ")"
                        return
                        ;;
                -f)
@@ -2822,7 +3023,7 @@ _git_tag ()
                ;;
        -*|tag)
                if [ $f = 1 ]; then
-                       __gitcomp_nl "$(__git_tags)"
+                       __gitcomp_direct "$(__git_tags "" "$cur" " ")"
                fi
                ;;
        *)
@@ -2834,8 +3035,8 @@ _git_tag ()
        --*)
                __gitcomp "
                        --list --delete --verify --annotate --message --file
-                       --sign --cleanup --local-user --force --column --sort
-                       --contains --points-at
+                       --sign --cleanup --local-user --force --column --sort=
+                       --contains --no-contains --points-at --merged --no-merged --create-reflog
                        "
                ;;
        esac
@@ -2997,6 +3198,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