Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/contrib/completion/git-completion.bash # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / contrib / completion / git-completion.bash` 4119 lines · 96256 bytes Raw Blame History 1 # bash/zsh completion support for core Git. 2 # 3 # Copyright (C) 2006,2007 Shawn O. Pearce 4 # Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/). 5 # Distributed under the GNU General Public License, version 2.0. 6 # 7 # The contained completion routines provide support for completing: 8 # 9 # *) local and remote branch names 10 # *) local and remote tag names 11 # *) .git/remotes file names 12 # *) git 'subcommands' 13 # *) git email aliases for git-send-email 14 # *) tree paths within 'ref:path/to/file' expressions 15 # *) file paths within current working directory and index 16 # *) common --long-options but not single-letter options 17 # *) arguments to long and single-letter options 18 # 19 # To use these routines: 20 # 21 # 1) Copy this file to somewhere (e.g. ~/.git-completion.bash). 22 # 2) Add the following line to your .bashrc/.zshrc: 23 # source ~/.git-completion.bash 24 # 3) Consider changing your PS1 to also show the current branch, 25 # see git-prompt.sh for details. 26 # 27 # If you use complex aliases of form '!f() { ... }; f', you can use the null 28 # command ':' as the first command in the function body to declare the desired 29 # completion style. For example '!f() { : git commit ; ... }; f' will 30 # tell the completion to use commit completion. This also works with aliases 31 # of form "!sh -c '...'". For example, "!sh -c ': git commit ; ... '". 32 # Note that "git" is optional --- '!f() { : commit; ...}; f' would complete 33 # just like the 'git commit' command. 34 # 35 # To add completion for git subcommands that are implemented in external 36 # scripts, define a function of the form '_git_${subcommand}' while replacing 37 # all dashes with underscores, and the main git completion will make use of it. 38 # For example, to add completion for 'git do-stuff' (which could e.g. live 39 # in /usr/bin/git-do-stuff), name the completion function '_git_do_stuff'. 40 # See _git_show, _git_bisect etc. below for more examples. 41 # 42 # If you have a shell command that is not part of git (and is not called as a 43 # git subcommand), but you would still like git-style completion for it, use 44 # __git_complete. For example, to use the same completion as for 'git log' also 45 # for the 'gl' command: 46 # 47 # __git_complete gl git_log 48 # 49 # Or if the 'gk' command should be completed the same as 'gitk': 50 # 51 # __git_complete gk gitk 52 # 53 # The second parameter of __git_complete gives the completion function; it is 54 # resolved as a function named "$2", or "__$2_main", or "_$2" in that order. 55 # In the examples above, the actual functions used for completion will be 56 # _git_log and __gitk_main. 57 # 58 # Compatible with bash 3.2.57. 59 # 60 # You can set the following environment variables to influence the behavior of 61 # the completion routines: 62 # 63 # GIT_COMPLETION_CHECKOUT_NO_GUESS 64 # 65 # When set to "1", do not include "DWIM" suggestions in git-checkout 66 # and git-switch completion (e.g., completing "foo" when "origin/foo" 67 # exists). 68 # 69 # GIT_COMPLETION_SHOW_ALL_COMMANDS 70 # 71 # When set to "1" suggest all commands, including plumbing commands 72 # which are hidden by default (e.g. "cat-file" on "git ca"). 73 # 74 # GIT_COMPLETION_SHOW_ALL 75 # 76 # When set to "1" suggest all options, including options which are 77 # typically hidden (e.g. '--allow-empty' for 'git commit'). 78 # 79 # GIT_COMPLETION_IGNORE_CASE 80 # 81 # When set, uses for-each-ref '--ignore-case' to find refs that match 82 # case insensitively, even on systems with case sensitive file systems 83 # (e.g., completing tag name "FOO" on "git checkout f"). 84 85 case "$COMP_WORDBREAKS" in 86 *:*) : great ;; 87 *) COMP_WORDBREAKS="$COMP_WORDBREAKS:" 88 esac 89 90 # Discovers the path to the git repository taking any '--git-dir=' and 91 # '-C ' options into account and stores it in the $__git_repo_path 92 # variable. 93 __git_find_repo_path () 94 { 95 if [ -n "${__git_repo_path-}" ]; then 96 # we already know where it is 97 return 98 fi 99 100 if [ -n "${__git_C_args-}" ]; then 101 __git_repo_path="$(git "${__git_C_args[@]}" \ 102 ${__git_dir:+--git-dir="$__git_dir"} \ 103 rev-parse --absolute-git-dir 2>/dev/null)" 104 elif [ -n "${__git_dir-}" ]; then 105 test -d "$__git_dir" && 106 __git_repo_path="$__git_dir" 107 elif [ -n "${GIT_DIR-}" ]; then 108 test -d "$GIT_DIR" && 109 __git_repo_path="$GIT_DIR" 110 elif [ -d .git ]; then 111 __git_repo_path=.git 112 else 113 __git_repo_path="$(git rev-parse --git-dir 2>/dev/null)" 114 fi 115 } 116 117 # Deprecated: use __git_find_repo_path() and $__git_repo_path instead 118 # __gitdir accepts 0 or 1 arguments (i.e., location) 119 # returns location of .git repo 120 __gitdir () 121 { 122 if [ -z "${1-}" ]; then 123 __git_find_repo_path || return 1 124 echo "$__git_repo_path" 125 elif [ -d "$1/.git" ]; then 126 echo "$1/.git" 127 else 128 echo "$1" 129 fi 130 } 131 132 # Runs git with all the options given as argument, respecting any 133 # '--git-dir=' and '-C ' options present on the command line 134 __git () 135 { 136 git ${__git_C_args:+"${__git_C_args[@]}"} \ 137 ${__git_dir:+--git-dir="$__git_dir"} "$@" 2>/dev/null 138 } 139 140 # Helper function to read the first line of a file into a variable. 141 # __git_eread requires 2 arguments, the file path and the name of the 142 # variable, in that order. 143 # 144 # This is taken from git-prompt.sh. 145 __git_eread () 146 { 147 test -r "$1" && IFS=$'\r\n' read -r "$2" <"$1" 148 } 149 150 # Runs git in $__git_repo_path to determine whether a pseudoref exists. 151 # 1: The pseudo-ref to search 152 __git_pseudoref_exists () 153 { 154 local ref=$1 155 local head 156 157 __git_find_repo_path 158 159 # If the reftable is in use, we have to shell out to 'git rev-parse' 160 # to determine whether the ref exists instead of looking directly in 161 # the filesystem to determine whether the ref exists. Otherwise, use 162 # Bash builtins since executing Git commands are expensive on some 163 # platforms. 164 if __git_eread "$__git_repo_path/HEAD" head; then 165 if [ "$head" == "ref: refs/heads/.invalid" ]; then 166 __git show-ref --exists "$ref" 167 return $? 168 fi 169 fi 170 171 [ -f "$__git_repo_path/$ref" ] 172 } 173 174 # Removes backslash escaping, single quotes and double quotes from a word, 175 # stores the result in the variable $dequoted_word. 176 # 1: The word to dequote. 177 __git_dequote () 178 { 179 local rest="$1" len ch 180 181 dequoted_word="" 182 183 while test -n "$rest"; do 184 len=${#dequoted_word} 185 dequoted_word="$dequoted_word${rest%%[\\\'\"]*}" 186 rest="${rest:$((${#dequoted_word}-$len))}" 187 188 case "${rest:0:1}" in 189 \\) 190 ch="${rest:1:1}" 191 case "$ch" in 192 $'\n') 193 ;; 194 *) 195 dequoted_word="$dequoted_word$ch" 196 ;; 197 esac 198 rest="${rest:2}" 199 ;; 200 \') 201 rest="${rest:1}" 202 len=${#dequoted_word} 203 dequoted_word="$dequoted_word${rest%%\'*}" 204 rest="${rest:$((${#dequoted_word}-$len+1))}" 205 ;; 206 \") 207 rest="${rest:1}" 208 while test -n "$rest" ; do 209 len=${#dequoted_word} 210 dequoted_word="$dequoted_word${rest%%[\\\"]*}" 211 rest="${rest:$((${#dequoted_word}-$len))}" 212 case "${rest:0:1}" in 213 \\) 214 ch="${rest:1:1}" 215 case "$ch" in 216 \"|\\|\$|\`) 217 dequoted_word="$dequoted_word$ch" 218 ;; 219 $'\n') 220 ;; 221 *) 222 dequoted_word="$dequoted_word\\$ch" 223 ;; 224 esac 225 rest="${rest:2}" 226 ;; 227 \") 228 rest="${rest:1}" 229 break 230 ;; 231 esac 232 done 233 ;; 234 esac 235 done 236 } 237 238 # Prints the number of slash-separated components in a path. 239 # 1: Path to count components of. 240 __git_count_path_components () 241 { 242 local path="$1" 243 local relative="${path#/}" 244 relative="${relative%/}" 245 local slashes="/${relative//[^\/]}" 246 echo "${#slashes}" 247 } 248 249 # The following function is based on code from: 250 # 251 # bash_completion - programmable completion functions for bash 3.2+ 252 # 253 # Copyright © 2006-2008, Ian Macdonald 254 # © 2009-2010, Bash Completion Maintainers 255 # 256 # 257 # This program is free software; you can redistribute it and/or modify 258 # it under the terms of the GNU General Public License as published by 259 # the Free Software Foundation; either version 2, or (at your option) 260 # any later version. 261 # 262 # This program is distributed in the hope that it will be useful, 263 # but WITHOUT ANY WARRANTY; without even the implied warranty of 264 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 265 # GNU General Public License for more details. 266 # 267 # You should have received a copy of the GNU General Public License 268 # along with this program; if not, see . 269 # 270 # The latest version of this software can be obtained here: 271 # 272 # http://bash-completion.alioth.debian.org/ 273 # 274 # RELEASE: 2.x 275 276 # This function can be used to access a tokenized list of words 277 # on the command line: 278 # 279 # __git_reassemble_comp_words_by_ref '=:' 280 # if test "${words_[cword_-1]}" = -w 281 # then 282 # ... 283 # fi 284 # 285 # The argument should be a collection of characters from the list of 286 # word completion separators (COMP_WORDBREAKS) to treat as ordinary 287 # characters. 288 # 289 # This is roughly equivalent to going back in time and setting 290 # COMP_WORDBREAKS to exclude those characters. The intent is to 291 # make option types like --date= and : easy to 292 # recognize by treating each shell word as a single token. 293 # 294 # It is best not to set COMP_WORDBREAKS directly because the value is 295 # shared with other completion scripts. By the time the completion 296 # function gets called, COMP_WORDS has already been populated so local 297 # changes to COMP_WORDBREAKS have no effect. 298 # 299 # Output: words_, cword_, cur_. 300 301 __git_reassemble_comp_words_by_ref() 302 { 303 local exclude i j first 304 # Which word separators to exclude? 305 exclude="${1//[^$COMP_WORDBREAKS]}" 306 cword_=$COMP_CWORD 307 if [ -z "$exclude" ]; then 308 words_=("${COMP_WORDS[@]}") 309 return 310 fi 311 # List of word completion separators has shrunk; 312 # re-assemble words to complete. 313 for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do 314 # Append each nonempty word consisting of just 315 # word separator characters to the current word. 316 first=t 317 while 318 [ $i -gt 0 ] && 319 [ -n "${COMP_WORDS[$i]}" ] && 320 # word consists of excluded word separators 321 [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ] 322 do 323 # Attach to the previous token, 324 # unless the previous token is the command name. 325 if [ $j -ge 2 ] && [ -n "$first" ]; then 326 ((j--)) 327 fi 328 first= 329 words_[$j]=${words_[j]}${COMP_WORDS[i]} 330 if [ $i = $COMP_CWORD ]; then 331 cword_=$j 332 fi 333 if (($i < ${#COMP_WORDS[@]} - 1)); then 334 ((i++)) 335 else 336 # Done. 337 return 338 fi 339 done 340 words_[$j]=${words_[j]}${COMP_WORDS[i]} 341 if [ $i = $COMP_CWORD ]; then 342 cword_=$j 343 fi 344 done 345 } 346 347 if ! type _get_comp_words_by_ref >/dev/null 2>&1; then 348 _get_comp_words_by_ref () 349 { 350 local exclude cur_ words_ cword_ 351 if [ "$1" = "-n" ]; then 352 exclude=$2 353 shift 2 354 fi 355 __git_reassemble_comp_words_by_ref "$exclude" 356 cur_=${words_[cword_]} 357 while [ $# -gt 0 ]; do 358 case "$1" in 359 cur) 360 cur=$cur_ 361 ;; 362 prev) 363 prev=${words_[$cword_-1]} 364 ;; 365 words) 366 words=("${words_[@]}") 367 ;; 368 cword) 369 cword=$cword_ 370 ;; 371 esac 372 shift 373 done 374 } 375 fi 376 377 # Fills the COMPREPLY array with prefiltered words without any additional 378 # processing. 379 # Callers must take care of providing only words that match the current word 380 # to be completed and adding any prefix and/or suffix (trailing space!), if 381 # necessary. 382 # 1: List of newline-separated matching completion words, complete with 383 # prefix and suffix. 384 __gitcomp_direct () 385 { 386 local IFS=$'\n' 387 388 COMPREPLY=($1) 389 } 390 391 # Similar to __gitcomp_direct, but appends to COMPREPLY instead. 392 # Callers must take care of providing only words that match the current word 393 # to be completed and adding any prefix and/or suffix (trailing space!), if 394 # necessary. 395 # 1: List of newline-separated matching completion words, complete with 396 # prefix and suffix. 397 __gitcomp_direct_append () 398 { 399 local IFS=$'\n' 400 401 COMPREPLY+=($1) 402 } 403 404 __gitcompappend () 405 { 406 local x i=${#COMPREPLY[@]} 407 for x in $1; do 408 if [[ "$x" == "$3"* ]]; then 409 COMPREPLY[i++]="$2$x$4" 410 fi 411 done 412 } 413 414 __gitcompadd () 415 { 416 COMPREPLY=() 417 __gitcompappend "$@" 418 } 419 420 # Generates completion reply, appending a space to possible completion words, 421 # if necessary. 422 # It accepts 1 to 4 arguments: 423 # 1: List of possible completion words. 424 # 2: A prefix to be added to each possible completion word (optional). 425 # 3: Generate possible completion matches for this word (optional). 426 # 4: A suffix to be appended to each possible completion word (optional). 427 __gitcomp () 428 { 429 local cur_="${3-$cur}" 430 431 case "$cur_" in 432 *=) 433 ;; 434 --no-*) 435 local c i=0 IFS=$' \t\n' 436 for c in $1; do 437 if [[ $c == "--" ]]; then 438 continue 439 fi 440 c="$c${4-}" 441 if [[ $c == "$cur_"* ]]; then 442 case $c in 443 --*=|*.) ;; 444 *) c="$c " ;; 445 esac 446 COMPREPLY[i++]="${2-}$c" 447 fi 448 done 449 ;; 450 *) 451 local c i=0 IFS=$' \t\n' 452 for c in $1; do 453 if [[ $c == "--" ]]; then 454 c="--no-...${4-}" 455 if [[ $c == "$cur_"* ]]; then 456 COMPREPLY[i++]="${2-}$c " 457 fi 458 break 459 fi 460 c="$c${4-}" 461 if [[ $c == "$cur_"* ]]; then 462 case $c in 463 *=|*.) ;; 464 *) c="$c " ;; 465 esac 466 COMPREPLY[i++]="${2-}$c" 467 fi 468 done 469 ;; 470 esac 471 } 472 473 # Clear the variables caching builtins' options when (re-)sourcing 474 # the completion script. 475 if [[ -n ${ZSH_VERSION-} ]]; then 476 unset ${(M)${(k)parameters[@]}:#__gitcomp_builtin_*} 2>/dev/null 477 else 478 unset $(compgen -v __gitcomp_builtin_) 479 fi 480 481 # This function is equivalent to 482 # 483 # ___git_resolved_builtins=$(git xxx --git-completion-helper) 484 # 485 # except that the result of the execution is cached. 486 # 487 # Accept 1-3 arguments: 488 # 1: the git command to execute, this is also the cache key 489 # (use "_" when the command contains spaces, e.g. "remote add" 490 # becomes "remote_add") 491 # 2: extra options to be added on top (e.g. negative forms) 492 # 3: options to be excluded 493 __git_resolve_builtins () 494 { 495 local cmd="$1" 496 local incl="${2-}" 497 local excl="${3-}" 498 499 local var=__gitcomp_builtin_"${cmd//-/_}" 500 local options 501 eval "options=\${$var-}" 502 503 if [ -z "$options" ]; then 504 local completion_helper 505 if [ "${GIT_COMPLETION_SHOW_ALL-}" = "1" ]; then 506 completion_helper="--git-completion-helper-all" 507 else 508 completion_helper="--git-completion-helper" 509 fi 510 # leading and trailing spaces are significant to make 511 # option removal work correctly. 512 options=" $incl $(__git ${cmd/_/ } $completion_helper) " || return 513 514 for i in $excl; do 515 options="${options/ $i / }" 516 done 517 eval "$var=\"$options\"" 518 fi 519 520 ___git_resolved_builtins="$options" 521 } 522 523 # This function is equivalent to 524 # 525 # __gitcomp "$(git xxx --git-completion-helper) ..." 526 # 527 # except that the output is cached. Accept 1-3 arguments: 528 # 1: the git command to execute, this is also the cache key 529 # (use "_" when the command contains spaces, e.g. "remote add" 530 # becomes "remote_add") 531 # 2: extra options to be added on top (e.g. negative forms) 532 # 3: options to be excluded 533 __gitcomp_builtin () 534 { 535 __git_resolve_builtins "$1" "$2" "$3" 536 537 __gitcomp "$___git_resolved_builtins" 538 } 539 540 # Variation of __gitcomp_nl () that appends to the existing list of 541 # completion candidates, COMPREPLY. 542 __gitcomp_nl_append () 543 { 544 local IFS=$'\n' 545 __gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }" 546 } 547 548 # Generates completion reply from newline-separated possible completion words 549 # by appending a space to all of them. 550 # It accepts 1 to 4 arguments: 551 # 1: List of possible completion words, separated by a single newline. 552 # 2: A prefix to be added to each possible completion word (optional). 553 # 3: Generate possible completion matches for this word (optional). 554 # 4: A suffix to be appended to each possible completion word instead of 555 # the default space (optional). If specified but empty, nothing is 556 # appended. 557 __gitcomp_nl () 558 { 559 COMPREPLY=() 560 __gitcomp_nl_append "$@" 561 } 562 563 # Fills the COMPREPLY array with prefiltered paths without any additional 564 # processing. 565 # Callers must take care of providing only paths that match the current path 566 # to be completed and adding any prefix path components, if necessary. 567 # 1: List of newline-separated matching paths, complete with all prefix 568 # path components. 569 __gitcomp_file_direct () 570 { 571 local IFS=$'\n' 572 573 COMPREPLY=($1) 574 575 # use a hack to enable file mode in bash < 4 576 compopt -o filenames +o nospace 2>/dev/null || 577 compgen -f /non-existing-dir/ >/dev/null || 578 true 579 } 580 581 # Generates completion reply with compgen from newline-separated possible 582 # completion filenames. 583 # It accepts 1 to 3 arguments: 584 # 1: List of possible completion filenames, separated by a single newline. 585 # 2: A directory prefix to be added to each possible completion filename 586 # (optional). 587 # 3: Generate possible completion matches for this word (optional). 588 __gitcomp_file () 589 { 590 local IFS=$'\n' 591 592 # XXX does not work when the directory prefix contains a tilde, 593 # since tilde expansion is not applied. 594 # This means that COMPREPLY will be empty and Bash default 595 # completion will be used. 596 __gitcompadd "$1" "${2-}" "${3-$cur}" "" 597 598 # use a hack to enable file mode in bash < 4 599 compopt -o filenames +o nospace 2>/dev/null || 600 compgen -f /non-existing-dir/ >/dev/null || 601 true 602 } 603 604 # Find the current subcommand for commands that follow the syntax: 605 # 606 # git 607 # 608 # 1: List of possible subcommands. 609 # 2: Optional subcommand to return when none is found. 610 __git_find_subcommand () 611 { 612 local subcommand subcommands="$1" default_subcommand="$2" 613 614 for subcommand in $subcommands; do 615 if [ "$subcommand" = "${words[__git_cmd_idx+1]}" ]; then 616 echo $subcommand 617 return 618 fi 619 done 620 621 echo $default_subcommand 622 } 623 624 # Execute 'git ls-files', unless the --committable option is specified, in 625 # which case it runs 'git diff-index' to find out the files that can be 626 # committed. It return paths relative to the directory specified in the first 627 # argument, and using the options specified in the second argument. 628 __git_ls_files_helper () 629 { 630 if [ "$2" = "--committable" ]; then 631 __git -C "$1" -c core.quotePath=false diff-index \ 632 --name-only --relative HEAD -- "${3//\\/\\\\}*" 633 else 634 # NOTE: $2 is not quoted in order to support multiple options 635 __git -C "$1" -c core.quotePath=false ls-files \ 636 --exclude-standard $2 -- "${3//\\/\\\\}*" 637 fi 638 } 639 640 641 # __git_index_files accepts 1 or 2 arguments: 642 # 1: Options to pass to ls-files (required). 643 # 2: A directory path (optional). 644 # If provided, only files within the specified directory are listed. 645 # Sub directories are never recursed. Path must have a trailing 646 # slash. 647 # 3: List only paths matching this path component (optional). 648 __git_index_files () 649 { 650 local root="$2" match="$3" 651 652 __git_ls_files_helper "$root" "$1" "${match:-?}" | 653 awk -F / -v pfx="${2//\\/\\\\}" '{ 654 paths[$1] = 1 655 } 656 END { 657 for (p in paths) { 658 if (substr(p, 1, 1) != "\"") { 659 # No special characters, easy! 660 print pfx p 661 continue 662 } 663 664 # The path is quoted. 665 p = dequote(p) 666 if (p == "") 667 continue 668 669 # Even when a directory name itself does not contain 670 # any special characters, it will still be quoted if 671 # any of its (stripped) trailing path components do. 672 # Because of this we may have seen the same directory 673 # both quoted and unquoted. 674 if (p in paths) 675 # We have seen the same directory unquoted, 676 # skip it. 677 continue 678 else 679 print pfx p 680 } 681 } 682 function dequote(p, bs_idx, out, esc, esc_idx, dec) { 683 # Skip opening double quote. 684 p = substr(p, 2) 685 686 # Interpret backslash escape sequences. 687 while ((bs_idx = index(p, "\\")) != 0) { 688 out = out substr(p, 1, bs_idx - 1) 689 esc = substr(p, bs_idx + 1, 1) 690 p = substr(p, bs_idx + 2) 691 692 if ((esc_idx = index("abtvfr\"\\", esc)) != 0) { 693 # C-style one-character escape sequence. 694 out = out substr("\a\b\t\v\f\r\"\\", 695 esc_idx, 1) 696 } else if (esc == "n") { 697 # Uh-oh, a newline character. 698 # We cannot reliably put a pathname 699 # containing a newline into COMPREPLY, 700 # and the newline would create a mess. 701 # Skip this path. 702 return "" 703 } else { 704 # Must be a \nnn octal value, then. 705 dec = esc * 64 + \ 706 substr(p, 1, 1) * 8 + \ 707 substr(p, 2, 1) 708 out = out sprintf("%c", dec) 709 p = substr(p, 3) 710 } 711 } 712 # Drop closing double quote, if there is one. 713 # (There is not any if this is a directory, as it was 714 # already stripped with the trailing path components.) 715 if (substr(p, length(p), 1) == "\"") 716 out = out substr(p, 1, length(p) - 1) 717 else 718 out = out p 719 720 return out 721 }' 722 } 723 724 # __git_complete_index_file requires 1 argument: 725 # 1: the options to pass to ls-file 726 # 727 # The exception is --committable, which finds the files appropriate commit. 728 __git_complete_index_file () 729 { 730 local dequoted_word pfx="" cur_ 731 732 __git_dequote "$cur" 733 734 case "$dequoted_word" in 735 ?*/*) 736 pfx="${dequoted_word%/*}/" 737 cur_="${dequoted_word##*/}" 738 ;; 739 *) 740 cur_="$dequoted_word" 741 esac 742 743 __gitcomp_file_direct "$(__git_index_files "$1" "$pfx" "$cur_")" 744 } 745 746 # Lists branches from the local repository. 747 # 1: A prefix to be added to each listed branch (optional). 748 # 2: List only branches matching this word (optional; list all branches if 749 # unset or empty). 750 # 3: A suffix to be appended to each listed branch (optional). 751 __git_heads () 752 { 753 local pfx="${1-}" cur_="${2-}" sfx="${3-}" 754 755 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \ 756 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \ 757 "refs/heads/$cur_*" "refs/heads/$cur_*/**" 758 } 759 760 # Lists branches from remote repositories. 761 # 1: A prefix to be added to each listed branch (optional). 762 # 2: List only branches matching this word (optional; list all branches if 763 # unset or empty). 764 # 3: A suffix to be appended to each listed branch (optional). 765 __git_remote_heads () 766 { 767 local pfx="${1-}" cur_="${2-}" sfx="${3-}" 768 769 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \ 770 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \ 771 "refs/remotes/$cur_*" "refs/remotes/$cur_*/**" 772 } 773 774 # Lists tags from the local repository. 775 # Accepts the same positional parameters as __git_heads() above. 776 __git_tags () 777 { 778 local pfx="${1-}" cur_="${2-}" sfx="${3-}" 779 780 __git for-each-ref --format="${pfx//\%/%%}%(refname:strip=2)$sfx" \ 781 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \ 782 "refs/tags/$cur_*" "refs/tags/$cur_*/**" 783 } 784 785 # List unique branches from refs/remotes used for 'git checkout' and 'git 786 # switch' tracking DWIMery. 787 # 1: A prefix to be added to each listed branch (optional) 788 # 2: List only branches matching this word (optional; list all branches if 789 # unset or empty). 790 # 3: A suffix to be appended to each listed branch (optional). 791 __git_dwim_remote_heads () 792 { 793 local pfx="${1-}" cur_="${2-}" sfx="${3-}" 794 795 # employ the heuristic used by git checkout and git switch 796 # Try to find a remote branch that cur_es the completion word 797 # but only output if the branch name is unique 798 local awk_script=' 799 function casemap(s) { 800 if (ENVIRON["IGNORE_CASE"]) 801 return tolower(s) 802 else 803 return s 804 } 805 BEGIN { 806 split(ENVIRON["REMOTES"], remotes, /\n/) 807 for (i in remotes) 808 remotes[i] = "refs/remotes/" casemap(remotes[i]) 809 cur_ = casemap(ENVIRON["CUR_"]) 810 } 811 { 812 ref_case = casemap($0) 813 for (i in remotes) { 814 if (index(ref_case, remotes[i] "/" cur_) == 1) { 815 branch = substr($0, length(remotes[i] "/") + 1) 816 print ENVIRON["PFX"] branch ENVIRON["SFX"] 817 break 818 } 819 } 820 } 821 ' 822 __git for-each-ref --format='%(refname)' refs/remotes/ | 823 PFX="$pfx" SFX="$sfx" CUR_="$cur_" \ 824 IGNORE_CASE=${GIT_COMPLETION_IGNORE_CASE+1} \ 825 REMOTES="$(__git_remotes | sort -r)" awk "$awk_script" | 826 sort | uniq -u 827 } 828 829 # Lists refs from the local (by default) or from a remote repository. 830 # It accepts 0, 1 or 2 arguments: 831 # 1: The remote to list refs from (optional; ignored, if set but empty). 832 # Can be the name of a configured remote, a path, or a URL. 833 # 2: In addition to local refs, list unique branches from refs/remotes/ for 834 # 'git checkout's tracking DWIMery (optional; ignored, if set but empty). 835 # 3: A prefix to be added to each listed ref (optional). 836 # 4: List only refs matching this word (optional; list all refs if unset or 837 # empty). 838 # 5: A suffix to be appended to each listed ref (optional; ignored, if set 839 # but empty). 840 # 841 # Use __git_complete_refs() instead. 842 __git_refs () 843 { 844 local i hash dir track="${2-}" 845 local list_refs_from=path remote="${1-}" 846 local format refs 847 local pfx="${3-}" cur_="${4-$cur}" sfx="${5-}" 848 local match="${4-}" 849 local umatch="${4-}" 850 local fer_pfx="${pfx//\%/%%}" # "escape" for-each-ref format specifiers 851 852 __git_find_repo_path 853 dir="$__git_repo_path" 854 855 if [ -z "$remote" ]; then 856 if [ -z "$dir" ]; then 857 return 858 fi 859 else 860 if __git_is_configured_remote "$remote"; then 861 # configured remote takes precedence over a 862 # local directory with the same name 863 list_refs_from=remote 864 elif [ -d "$remote/.git" ]; then 865 dir="$remote/.git" 866 elif [ -d "$remote" ]; then 867 dir="$remote" 868 else 869 list_refs_from=url 870 fi 871 fi 872 873 if test "${GIT_COMPLETION_IGNORE_CASE:+1}" = "1" 874 then 875 # uppercase with tr instead of ${match,^^} for bash 3.2 compatibility 876 umatch=$(echo "$match" | tr a-z A-Z 2>/dev/null || echo "$match") 877 fi 878 879 if [ "$list_refs_from" = path ]; then 880 if [[ "$cur_" == ^* ]]; then 881 pfx="$pfx^" 882 fer_pfx="$fer_pfx^" 883 cur_=${cur_#^} 884 match=${match#^} 885 umatch=${umatch#^} 886 fi 887 case "$cur_" in 888 refs|refs/*) 889 format="refname" 890 refs=("$match*" "$match*/**") 891 track="" 892 ;; 893 *) 894 for i in HEAD FETCH_HEAD ORIG_HEAD MERGE_HEAD REBASE_HEAD CHERRY_PICK_HEAD REVERT_HEAD BISECT_HEAD AUTO_MERGE; do 895 case "$i" in 896 $match*|$umatch*) 897 if [ -e "$dir/$i" ]; then 898 echo "$pfx$i$sfx" 899 fi 900 ;; 901 esac 902 done 903 format="refname:strip=2" 904 refs=("refs/tags/$match*" "refs/tags/$match*/**" 905 "refs/heads/$match*" "refs/heads/$match*/**" 906 "refs/remotes/$match*" "refs/remotes/$match*/**") 907 ;; 908 esac 909 __git_dir="$dir" __git for-each-ref --format="$fer_pfx%($format)$sfx" \ 910 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \ 911 "${refs[@]}" 912 if [ -n "$track" ]; then 913 __git_dwim_remote_heads "$pfx" "$match" "$sfx" 914 fi 915 return 916 fi 917 case "$cur_" in 918 refs|refs/*) 919 __git ls-remote "$remote" "$match*" | \ 920 while read -r hash i; do 921 case "$i" in 922 *^{}) ;; 923 *) echo "$pfx$i$sfx" ;; 924 esac 925 done 926 ;; 927 *) 928 if [ "$list_refs_from" = remote ]; then 929 case "HEAD" in 930 $match*|$umatch*) echo "${pfx}HEAD$sfx" ;; 931 esac 932 local strip="$(__git_count_path_components "refs/remotes/$remote")" 933 __git for-each-ref --format="$fer_pfx%(refname:strip=$strip)$sfx" \ 934 ${GIT_COMPLETION_IGNORE_CASE+--ignore-case} \ 935 "refs/remotes/$remote/$match*" \ 936 "refs/remotes/$remote/$match*/**" 937 else 938 local query_symref 939 case "HEAD" in 940 $match*|$umatch*) query_symref="HEAD" ;; 941 esac 942 __git ls-remote "$remote" $query_symref \ 943 "refs/tags/$match*" "refs/heads/$match*" \ 944 "refs/remotes/$match*" | 945 while read -r hash i; do 946 case "$i" in 947 *^{}) ;; 948 refs/*) echo "$pfx${i#refs/*/}$sfx" ;; 949 *) echo "$pfx$i$sfx" ;; # symbolic refs 950 esac 951 done 952 fi 953 ;; 954 esac 955 } 956 957 # Completes refs, short and long, local and remote, symbolic and pseudo. 958 # 959 # Usage: __git_complete_refs [