Merge branch 'sg/completion-ctags'
authorJunio C Hamano <gitster@pobox.com>
Thu, 30 Mar 2017 21:07:15 +0000 (14:07 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 30 Mar 2017 21:07:15 +0000 (14:07 -0700)
Command line completion updates.

* sg/completion-ctags:
completion: offer ctags symbol names for 'git log -S', '-G' and '-L:'
completion: extract completing ctags symbol names into helper function
completion: put matching ctags symbol names directly into COMPREPLY

1  2 
contrib/completion/git-completion.bash
index 380f755b9fd0a049d5edf5f54210aac39ab48d3e,d6f25a7e0b3f50b6f6f6d5b645fe794c1cbe4788..dd862e018dc29e878191cef7c6a547588f1befbb
@@@ -1102,7 -1102,6 +1102,7 @@@ _git_apply (
                        --apply --no-add --exclude=
                        --ignore-whitespace --ignore-space-change
                        --whitespace= --inaccurate-eof --verbose
 +                      --recount --directory=
                        "
                return
        esac
@@@ -1114,17 -1113,13 +1114,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 ()
        --*)
                __gitcomp "
                        --format= --list --verbose
 -                      --prefix= --remote= --exec=
 +                      --prefix= --remote= --exec= --output
                        "
                return
                ;;
@@@ -1197,7 -1192,6 +1197,7 @@@ _git_branch (
                        --track --no-track --contains --merged --no-merged
                        --set-upstream-to= --edit-description --list
                        --unset-upstream --delete --move --remotes
 +                      --column --no-column --sort= --points-at
                        "
                ;;
        *)
@@@ -1311,8 -1305,6 +1311,8 @@@ _git_clone (
                        --single-branch
                        --branch
                        --recurse-submodules
 +                      --no-single-branch
 +                      --shallow-submodules
                        "
                return
                ;;
@@@ -1354,7 -1346,6 +1354,7 @@@ _git_commit (
                        --reset-author --file= --message= --template=
                        --cleanup= --untracked-files --untracked-files=
                        --verbose --quiet --fixup= --squash=
 +                      --patch --short --date --allow-empty
                        "
                return
        esac
@@@ -1373,8 -1364,7 +1373,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
@@@ -1457,7 -1447,6 +1457,7 @@@ __git_fetch_recurse_submodules="yes on-
  __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 ()
@@@ -1507,7 -1496,7 +1507,7 @@@ _git_fsck (
        --*)
                __gitcomp "
                        --tags --root --unreachable --cache --no-reflogs --full
 -                      --strict --verbose --lost-found
 +                      --strict --verbose --lost-found --name-objects
                        "
                return
                ;;
@@@ -1529,8 -1518,43 +1529,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 ()
                        --max-depth
                        --count
                        --and --or --not --all-match
 +                      --break --heading --show-function --function-context
 +                      --untracked --no-index
                        "
                return
                ;;
  
        case "$cword,$prev" in
        2,*|*,-*)
-               if test -r tags; then
-                       __gitcomp_nl "$(__git_match_ctag "$cur" tags)"
-                       return
-               fi
+               __git_complete_symbol && return
                ;;
        esac
  
@@@ -1625,12 -1644,6 +1657,12 @@@ _git_ls_files (
  
  _git_ls_remote ()
  {
 +      case "$cur" in
 +      --*)
 +              __gitcomp "--heads --tags --refs --get-url --symref"
 +              return
 +              ;;
 +      esac
        __gitcomp_nl "$(__git_remotes)"
  }
  
@@@ -1674,6 -1687,19 +1706,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)
                        "
                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
  }
@@@ -1752,7 -1793,7 +1812,7 @@@ _git_mergetool (
                return
                ;;
        --*)
 -              __gitcomp "--tool="
 +              __gitcomp "--tool= --prompt --no-prompt"
                return
                ;;
        esac
@@@ -1860,7 -1901,7 +1920,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 ()
  {
@@@ -2556,88 -2597,40 +2616,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-branches,--*)
 +              __gitcomp "--add"
                ;;
 -      set-head|set-branches)
 +      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
@@@ -2653,10 -2646,7 +2713,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
@@@ -2684,7 -2674,7 +2744,7 @@@ _git_shortlog (
                __gitcomp "
                        $__git_log_common_options
                        $__git_log_shortlog_options
 -                      --numbered --summary
 +                      --numbered --summary --email
                        "
                return
                ;;
@@@ -2789,11 -2779,10 +2849,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"
                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 ()
                        --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=
@@@ -2962,8 -2924,8 +3022,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 --points-at --merged --no-merged --create-reflog
                        "
                ;;
        esac