f641d99ecf1b73432ed0b26cea8ddf3610290f1f
   1#!/bin/sh
   2#
   3# Copyright (c) 2012 Felipe Contreras
   4#
   5
   6test_description='test bash completion'
   7
   8. ./lib-bash.sh
   9
  10complete ()
  11{
  12        # do nothing
  13        return 0
  14}
  15
  16# Be careful when updating this list:
  17#
  18# (1) The build tree may have build artifact from different branch, or
  19#     the user's $PATH may have a random executable that may begin
  20#     with "git-check" that are not part of the subcommands this build
  21#     will ship, e.g.  "check-ignore".  The tests for completion for
  22#     subcommand names tests how "check" is expanded; we limit the
  23#     possible candidates to "checkout" and "check-attr" to make sure
  24#     "check-attr", which is known by the filter function as a
  25#     subcommand to be thrown out, while excluding other random files
  26#     that happen to begin with "check" to avoid letting them get in
  27#     the way.
  28#
  29# (2) A test makes sure that common subcommands are included in the
  30#     completion for "git <TAB>", and a plumbing is excluded.  "add",
  31#     "filter-branch" and "ls-files" are listed for this.
  32
  33GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
  34
  35. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
  36
  37# We don't need this function to actually join words or do anything special.
  38# Also, it's cleaner to avoid touching bash's internal completion variables.
  39# So let's override it with a minimal version for testing purposes.
  40_get_comp_words_by_ref ()
  41{
  42        while [ $# -gt 0 ]; do
  43                case "$1" in
  44                cur)
  45                        cur=${_words[_cword]}
  46                        ;;
  47                prev)
  48                        prev=${_words[_cword-1]}
  49                        ;;
  50                words)
  51                        words=("${_words[@]}")
  52                        ;;
  53                cword)
  54                        cword=$_cword
  55                        ;;
  56                esac
  57                shift
  58        done
  59}
  60
  61print_comp ()
  62{
  63        local IFS=$'\n'
  64        echo "${COMPREPLY[*]}" > out
  65}
  66
  67run_completion ()
  68{
  69        local -a COMPREPLY _words
  70        local _cword
  71        _words=( $1 )
  72        test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
  73        (( _cword = ${#_words[@]} - 1 ))
  74        __git_wrap__git_main && print_comp
  75}
  76
  77# Test high-level completion
  78# Arguments are:
  79# 1: typed text so far (cur)
  80# 2: expected completion
  81test_completion ()
  82{
  83        if test $# -gt 1
  84        then
  85                printf '%s\n' "$2" >expected
  86        else
  87                sed -e 's/Z$//' >expected
  88        fi &&
  89        run_completion "$1" &&
  90        test_cmp expected out
  91}
  92
  93# Test __gitcomp.
  94# The first argument is the typed text so far (cur); the rest are
  95# passed to __gitcomp.  Expected output comes is read from the
  96# standard input, like test_completion().
  97test_gitcomp ()
  98{
  99        local -a COMPREPLY &&
 100        sed -e 's/Z$//' >expected &&
 101        local cur="$1" &&
 102        shift &&
 103        __gitcomp "$@" &&
 104        print_comp &&
 105        test_cmp expected out
 106}
 107
 108# Test __gitcomp_nl
 109# Arguments are:
 110# 1: current word (cur)
 111# -: the rest are passed to __gitcomp_nl
 112test_gitcomp_nl ()
 113{
 114        local -a COMPREPLY &&
 115        sed -e 's/Z$//' >expected &&
 116        local cur="$1" &&
 117        shift &&
 118        __gitcomp_nl "$@" &&
 119        print_comp &&
 120        test_cmp expected out
 121}
 122
 123invalid_variable_name='${foo.bar}'
 124
 125actual="$TRASH_DIRECTORY/actual"
 126
 127if test_have_prereq MINGW
 128then
 129        ROOT="$(pwd -W)"
 130else
 131        ROOT="$(pwd)"
 132fi
 133
 134test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
 135        mkdir -p subdir/subsubdir &&
 136        mkdir -p non-repo &&
 137        git init otherrepo
 138'
 139
 140test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
 141        echo "$ROOT/otherrepo/.git" >expected &&
 142        (
 143                __git_dir="$ROOT/otherrepo/.git" &&
 144                __git_find_repo_path &&
 145                echo "$__git_repo_path" >"$actual"
 146        ) &&
 147        test_cmp expected "$actual"
 148'
 149
 150test_expect_success '__git_find_repo_path - .git directory in cwd' '
 151        echo ".git" >expected &&
 152        (
 153                __git_find_repo_path &&
 154                echo "$__git_repo_path" >"$actual"
 155        ) &&
 156        test_cmp expected "$actual"
 157'
 158
 159test_expect_success '__git_find_repo_path - .git directory in parent' '
 160        echo "$ROOT/.git" >expected &&
 161        (
 162                cd subdir/subsubdir &&
 163                __git_find_repo_path &&
 164                echo "$__git_repo_path" >"$actual"
 165        ) &&
 166        test_cmp expected "$actual"
 167'
 168
 169test_expect_success '__git_find_repo_path - cwd is a .git directory' '
 170        echo "." >expected &&
 171        (
 172                cd .git &&
 173                __git_find_repo_path &&
 174                echo "$__git_repo_path" >"$actual"
 175        ) &&
 176        test_cmp expected "$actual"
 177'
 178
 179test_expect_success '__git_find_repo_path - parent is a .git directory' '
 180        echo "$ROOT/.git" >expected &&
 181        (
 182                cd .git/refs/heads &&
 183                __git_find_repo_path &&
 184                echo "$__git_repo_path" >"$actual"
 185        ) &&
 186        test_cmp expected "$actual"
 187'
 188
 189test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
 190        echo "$ROOT/otherrepo/.git" >expected &&
 191        (
 192                GIT_DIR="$ROOT/otherrepo/.git" &&
 193                export GIT_DIR &&
 194                __git_find_repo_path &&
 195                echo "$__git_repo_path" >"$actual"
 196        ) &&
 197        test_cmp expected "$actual"
 198'
 199
 200test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
 201        echo "$ROOT/otherrepo/.git" >expected &&
 202        (
 203                GIT_DIR="$ROOT/otherrepo/.git" &&
 204                export GIT_DIR &&
 205                cd subdir &&
 206                __git_find_repo_path &&
 207                echo "$__git_repo_path" >"$actual"
 208        ) &&
 209        test_cmp expected "$actual"
 210'
 211
 212test_expect_success '__git_find_repo_path - from command line while "git -C"' '
 213        echo "$ROOT/.git" >expected &&
 214        (
 215                __git_dir="$ROOT/.git" &&
 216                __git_C_args=(-C otherrepo) &&
 217                __git_find_repo_path &&
 218                echo "$__git_repo_path" >"$actual"
 219        ) &&
 220        test_cmp expected "$actual"
 221'
 222
 223test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
 224        echo "$ROOT/otherrepo/.git" >expected &&
 225        (
 226                cd subdir &&
 227                __git_dir="otherrepo/.git" &&
 228                __git_C_args=(-C ..) &&
 229                __git_find_repo_path &&
 230                echo "$__git_repo_path" >"$actual"
 231        ) &&
 232        test_cmp expected "$actual"
 233'
 234
 235test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
 236        echo "$ROOT/.git" >expected &&
 237        (
 238                GIT_DIR="$ROOT/.git" &&
 239                export GIT_DIR &&
 240                __git_C_args=(-C otherrepo) &&
 241                __git_find_repo_path &&
 242                echo "$__git_repo_path" >"$actual"
 243        ) &&
 244        test_cmp expected "$actual"
 245'
 246
 247test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
 248        echo "$ROOT/otherrepo/.git" >expected &&
 249        (
 250                cd subdir &&
 251                GIT_DIR="otherrepo/.git" &&
 252                export GIT_DIR &&
 253                __git_C_args=(-C ..) &&
 254                __git_find_repo_path &&
 255                echo "$__git_repo_path" >"$actual"
 256        ) &&
 257        test_cmp expected "$actual"
 258'
 259
 260test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
 261        echo "$ROOT/otherrepo/.git" >expected &&
 262        (
 263                __git_C_args=(-C otherrepo) &&
 264                __git_find_repo_path &&
 265                echo "$__git_repo_path" >"$actual"
 266        ) &&
 267        test_cmp expected "$actual"
 268'
 269
 270test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
 271        echo "$ROOT/otherrepo/.git" >expected &&
 272        (
 273                cd .git &&
 274                __git_C_args=(-C .. -C otherrepo) &&
 275                __git_find_repo_path &&
 276                echo "$__git_repo_path" >"$actual"
 277        ) &&
 278        test_cmp expected "$actual"
 279'
 280
 281test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
 282        echo "$ROOT/otherrepo/.git" >expected &&
 283        (
 284                cd subdir &&
 285                __git_C_args=(-C .. -C otherrepo) &&
 286                __git_find_repo_path &&
 287                echo "$__git_repo_path" >"$actual"
 288        ) &&
 289        test_cmp expected "$actual"
 290'
 291
 292test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
 293        (
 294                __git_C_args=(-C non-existing) &&
 295                test_must_fail __git_find_repo_path &&
 296                printf "$__git_repo_path" >"$actual"
 297        ) &&
 298        test_must_be_empty "$actual"
 299'
 300
 301test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
 302        (
 303                __git_dir="non-existing" &&
 304                test_must_fail __git_find_repo_path &&
 305                printf "$__git_repo_path" >"$actual"
 306        ) &&
 307        test_must_be_empty "$actual"
 308'
 309
 310test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
 311        (
 312                GIT_DIR="$ROOT/non-existing" &&
 313                export GIT_DIR &&
 314                test_must_fail __git_find_repo_path &&
 315                printf "$__git_repo_path" >"$actual"
 316        ) &&
 317        test_must_be_empty "$actual"
 318'
 319
 320test_expect_success '__git_find_repo_path - gitfile in cwd' '
 321        echo "$ROOT/otherrepo/.git" >expected &&
 322        echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
 323        test_when_finished "rm -f subdir/.git" &&
 324        (
 325                cd subdir &&
 326                __git_find_repo_path &&
 327                echo "$__git_repo_path" >"$actual"
 328        ) &&
 329        test_cmp expected "$actual"
 330'
 331
 332test_expect_success '__git_find_repo_path - gitfile in parent' '
 333        echo "$ROOT/otherrepo/.git" >expected &&
 334        echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
 335        test_when_finished "rm -f subdir/.git" &&
 336        (
 337                cd subdir/subsubdir &&
 338                __git_find_repo_path &&
 339                echo "$__git_repo_path" >"$actual"
 340        ) &&
 341        test_cmp expected "$actual"
 342'
 343
 344test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
 345        echo "$ROOT/otherrepo/.git" >expected &&
 346        mkdir otherrepo/dir &&
 347        test_when_finished "rm -rf otherrepo/dir" &&
 348        ln -s otherrepo/dir link &&
 349        test_when_finished "rm -f link" &&
 350        (
 351                cd link &&
 352                __git_find_repo_path &&
 353                echo "$__git_repo_path" >"$actual"
 354        ) &&
 355        test_cmp expected "$actual"
 356'
 357
 358test_expect_success '__git_find_repo_path - not a git repository' '
 359        (
 360                cd non-repo &&
 361                GIT_CEILING_DIRECTORIES="$ROOT" &&
 362                export GIT_CEILING_DIRECTORIES &&
 363                test_must_fail __git_find_repo_path &&
 364                printf "$__git_repo_path" >"$actual"
 365        ) &&
 366        test_must_be_empty "$actual"
 367'
 368
 369test_expect_success '__gitdir - finds repo' '
 370        echo "$ROOT/.git" >expected &&
 371        (
 372                cd subdir/subsubdir &&
 373                __gitdir >"$actual"
 374        ) &&
 375        test_cmp expected "$actual"
 376'
 377
 378
 379test_expect_success '__gitdir - returns error when cant find repo' '
 380        (
 381                __git_dir="non-existing" &&
 382                test_must_fail __gitdir >"$actual"
 383        ) &&
 384        test_must_be_empty "$actual"
 385'
 386
 387test_expect_success '__gitdir - repo as argument' '
 388        echo "otherrepo/.git" >expected &&
 389        (
 390                __gitdir "otherrepo" >"$actual"
 391        ) &&
 392        test_cmp expected "$actual"
 393'
 394
 395test_expect_success '__gitdir - remote as argument' '
 396        echo "remote" >expected &&
 397        (
 398                __gitdir "remote" >"$actual"
 399        ) &&
 400        test_cmp expected "$actual"
 401'
 402
 403test_expect_success '__gitcomp - trailing space - options' '
 404        test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
 405                --reset-author" <<-EOF
 406        --reuse-message=Z
 407        --reedit-message=Z
 408        --reset-author Z
 409        EOF
 410'
 411
 412test_expect_success '__gitcomp - trailing space - config keys' '
 413        test_gitcomp "br" "branch. branch.autosetupmerge
 414                branch.autosetuprebase browser." <<-\EOF
 415        branch.Z
 416        branch.autosetupmerge Z
 417        branch.autosetuprebase Z
 418        browser.Z
 419        EOF
 420'
 421
 422test_expect_success '__gitcomp - option parameter' '
 423        test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
 424                "" "re" <<-\EOF
 425        recursive Z
 426        resolve Z
 427        EOF
 428'
 429
 430test_expect_success '__gitcomp - prefix' '
 431        test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
 432                "branch.maint." "me" <<-\EOF
 433        branch.maint.merge Z
 434        branch.maint.mergeoptions Z
 435        EOF
 436'
 437
 438test_expect_success '__gitcomp - suffix' '
 439        test_gitcomp "branch.me" "master maint next pu" "branch." \
 440                "ma" "." <<-\EOF
 441        branch.master.Z
 442        branch.maint.Z
 443        EOF
 444'
 445
 446test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
 447        __gitcomp "$invalid_variable_name"
 448'
 449
 450read -r -d "" refs <<-\EOF
 451maint
 452master
 453next
 454pu
 455EOF
 456
 457test_expect_success '__gitcomp_nl - trailing space' '
 458        test_gitcomp_nl "m" "$refs" <<-EOF
 459        maint Z
 460        master Z
 461        EOF
 462'
 463
 464test_expect_success '__gitcomp_nl - prefix' '
 465        test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
 466        --fixup=maint Z
 467        --fixup=master Z
 468        EOF
 469'
 470
 471test_expect_success '__gitcomp_nl - suffix' '
 472        test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
 473        branch.maint.Z
 474        branch.master.Z
 475        EOF
 476'
 477
 478test_expect_success '__gitcomp_nl - no suffix' '
 479        test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
 480        maintZ
 481        masterZ
 482        EOF
 483'
 484
 485test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
 486        __gitcomp_nl "$invalid_variable_name"
 487'
 488
 489test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
 490        cat >expect <<-EOF &&
 491        remote_from_file_1
 492        remote_from_file_2
 493        remote_in_config_1
 494        remote_in_config_2
 495        EOF
 496        test_when_finished "rm -rf .git/remotes" &&
 497        mkdir -p .git/remotes &&
 498        >.git/remotes/remote_from_file_1 &&
 499        >.git/remotes/remote_from_file_2 &&
 500        test_when_finished "git remote remove remote_in_config_1" &&
 501        git remote add remote_in_config_1 git://remote_1 &&
 502        test_when_finished "git remote remove remote_in_config_2" &&
 503        git remote add remote_in_config_2 git://remote_2 &&
 504        (
 505                __git_remotes >actual
 506        ) &&
 507        test_cmp expect actual
 508'
 509
 510test_expect_success '__git_is_configured_remote' '
 511        test_when_finished "git remote remove remote_1" &&
 512        git remote add remote_1 git://remote_1 &&
 513        test_when_finished "git remote remove remote_2" &&
 514        git remote add remote_2 git://remote_2 &&
 515        (
 516                verbose __git_is_configured_remote remote_2 &&
 517                test_must_fail __git_is_configured_remote non-existent
 518        )
 519'
 520
 521test_expect_success 'setup for ref completion' '
 522        git commit --allow-empty -m initial &&
 523        git branch matching-branch &&
 524        git tag matching-tag &&
 525        (
 526                cd otherrepo &&
 527                git commit --allow-empty -m initial &&
 528                git branch -m master master-in-other &&
 529                git branch branch-in-other &&
 530                git tag tag-in-other
 531        ) &&
 532        git remote add other "$ROOT/otherrepo/.git" &&
 533        git fetch --no-tags other &&
 534        rm -f .git/FETCH_HEAD &&
 535        git init thirdrepo
 536'
 537
 538test_expect_success '__git_refs - simple' '
 539        cat >expected <<-EOF &&
 540        HEAD
 541        master
 542        matching-branch
 543        other/branch-in-other
 544        other/master-in-other
 545        matching-tag
 546        EOF
 547        (
 548                cur= &&
 549                __git_refs >"$actual"
 550        ) &&
 551        test_cmp expected "$actual"
 552'
 553
 554test_expect_success '__git_refs - full refs' '
 555        cat >expected <<-EOF &&
 556        refs/heads/master
 557        refs/heads/matching-branch
 558        EOF
 559        (
 560                cur=refs/heads/ &&
 561                __git_refs >"$actual"
 562        ) &&
 563        test_cmp expected "$actual"
 564'
 565
 566test_expect_success '__git_refs - repo given on the command line' '
 567        cat >expected <<-EOF &&
 568        HEAD
 569        branch-in-other
 570        master-in-other
 571        tag-in-other
 572        EOF
 573        (
 574                __git_dir="$ROOT/otherrepo/.git" &&
 575                cur= &&
 576                __git_refs >"$actual"
 577        ) &&
 578        test_cmp expected "$actual"
 579'
 580
 581test_expect_success '__git_refs - remote on local file system' '
 582        cat >expected <<-EOF &&
 583        HEAD
 584        branch-in-other
 585        master-in-other
 586        tag-in-other
 587        EOF
 588        (
 589                cur= &&
 590                __git_refs otherrepo >"$actual"
 591        ) &&
 592        test_cmp expected "$actual"
 593'
 594
 595test_expect_success '__git_refs - remote on local file system - full refs' '
 596        cat >expected <<-EOF &&
 597        refs/heads/branch-in-other
 598        refs/heads/master-in-other
 599        refs/tags/tag-in-other
 600        EOF
 601        (
 602                cur=refs/ &&
 603                __git_refs otherrepo >"$actual"
 604        ) &&
 605        test_cmp expected "$actual"
 606'
 607
 608test_expect_success '__git_refs - configured remote' '
 609        cat >expected <<-EOF &&
 610        HEAD
 611        branch-in-other
 612        master-in-other
 613        EOF
 614        (
 615                cur= &&
 616                __git_refs other >"$actual"
 617        ) &&
 618        test_cmp expected "$actual"
 619'
 620
 621test_expect_success '__git_refs - configured remote - full refs' '
 622        cat >expected <<-EOF &&
 623        refs/heads/branch-in-other
 624        refs/heads/master-in-other
 625        refs/tags/tag-in-other
 626        EOF
 627        (
 628                cur=refs/ &&
 629                __git_refs other >"$actual"
 630        ) &&
 631        test_cmp expected "$actual"
 632'
 633
 634test_expect_success '__git_refs - configured remote - repo given on the command line' '
 635        cat >expected <<-EOF &&
 636        HEAD
 637        branch-in-other
 638        master-in-other
 639        EOF
 640        (
 641                cd thirdrepo &&
 642                __git_dir="$ROOT/.git" &&
 643                cur= &&
 644                __git_refs other >"$actual"
 645        ) &&
 646        test_cmp expected "$actual"
 647'
 648
 649test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
 650        cat >expected <<-EOF &&
 651        refs/heads/branch-in-other
 652        refs/heads/master-in-other
 653        refs/tags/tag-in-other
 654        EOF
 655        (
 656                cd thirdrepo &&
 657                __git_dir="$ROOT/.git" &&
 658                cur=refs/ &&
 659                __git_refs other >"$actual"
 660        ) &&
 661        test_cmp expected "$actual"
 662'
 663
 664test_expect_success '__git_refs - configured remote - remote name matches a directory' '
 665        cat >expected <<-EOF &&
 666        HEAD
 667        branch-in-other
 668        master-in-other
 669        EOF
 670        mkdir other &&
 671        test_when_finished "rm -rf other" &&
 672        (
 673                cur= &&
 674                __git_refs other >"$actual"
 675        ) &&
 676        test_cmp expected "$actual"
 677'
 678
 679test_expect_success '__git_refs - URL remote' '
 680        cat >expected <<-EOF &&
 681        HEAD
 682        branch-in-other
 683        master-in-other
 684        tag-in-other
 685        EOF
 686        (
 687                cur= &&
 688                __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
 689        ) &&
 690        test_cmp expected "$actual"
 691'
 692
 693test_expect_success '__git_refs - URL remote - full refs' '
 694        cat >expected <<-EOF &&
 695        refs/heads/branch-in-other
 696        refs/heads/master-in-other
 697        refs/tags/tag-in-other
 698        EOF
 699        (
 700                cur=refs/ &&
 701                __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
 702        ) &&
 703        test_cmp expected "$actual"
 704'
 705
 706test_expect_success '__git_refs - non-existing remote' '
 707        (
 708                cur= &&
 709                __git_refs non-existing >"$actual"
 710        ) &&
 711        test_must_be_empty "$actual"
 712'
 713
 714test_expect_success '__git_refs - non-existing remote - full refs' '
 715        (
 716                cur=refs/ &&
 717                __git_refs non-existing >"$actual"
 718        ) &&
 719        test_must_be_empty "$actual"
 720'
 721
 722test_expect_success '__git_refs - non-existing URL remote' '
 723        (
 724                cur= &&
 725                __git_refs "file://$ROOT/non-existing" >"$actual"
 726        ) &&
 727        test_must_be_empty "$actual"
 728'
 729
 730test_expect_success '__git_refs - non-existing URL remote - full refs' '
 731        (
 732                cur=refs/ &&
 733                __git_refs "file://$ROOT/non-existing" >"$actual"
 734        ) &&
 735        test_must_be_empty "$actual"
 736'
 737
 738test_expect_success '__git_refs - not in a git repository' '
 739        (
 740                GIT_CEILING_DIRECTORIES="$ROOT" &&
 741                export GIT_CEILING_DIRECTORIES &&
 742                cd subdir &&
 743                cur= &&
 744                __git_refs >"$actual"
 745        ) &&
 746        test_must_be_empty "$actual"
 747'
 748
 749test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
 750        cat >expected <<-EOF &&
 751        HEAD
 752        master
 753        matching-branch
 754        other/ambiguous
 755        other/branch-in-other
 756        other/master-in-other
 757        remote/ambiguous
 758        remote/branch-in-remote
 759        matching-tag
 760        branch-in-other
 761        branch-in-remote
 762        master-in-other
 763        EOF
 764        for remote_ref in refs/remotes/other/ambiguous \
 765                refs/remotes/remote/ambiguous \
 766                refs/remotes/remote/branch-in-remote
 767        do
 768                git update-ref $remote_ref master &&
 769                test_when_finished "git update-ref -d $remote_ref"
 770        done &&
 771        (
 772                cur= &&
 773                __git_refs "" 1 >"$actual"
 774        ) &&
 775        test_cmp expected "$actual"
 776'
 777
 778test_expect_success '__git_refs - after --opt=' '
 779        cat >expected <<-EOF &&
 780        HEAD
 781        master
 782        matching-branch
 783        other/branch-in-other
 784        other/master-in-other
 785        matching-tag
 786        EOF
 787        (
 788                cur="--opt=" &&
 789                __git_refs "" "" "" "" >"$actual"
 790        ) &&
 791        test_cmp expected "$actual"
 792'
 793
 794test_expect_success '__git_refs - after --opt= - full refs' '
 795        cat >expected <<-EOF &&
 796        refs/heads/master
 797        refs/heads/matching-branch
 798        refs/remotes/other/branch-in-other
 799        refs/remotes/other/master-in-other
 800        refs/tags/matching-tag
 801        EOF
 802        (
 803                cur="--opt=refs/" &&
 804                __git_refs "" "" "" refs/ >"$actual"
 805        ) &&
 806        test_cmp expected "$actual"
 807'
 808
 809test_expect_success '__git_complete_refs - simple' '
 810        sed -e "s/Z$//" >expected <<-EOF &&
 811        HEAD Z
 812        master Z
 813        matching-branch Z
 814        other/branch-in-other Z
 815        other/master-in-other Z
 816        matching-tag Z
 817        EOF
 818        (
 819                cur= &&
 820                __git_complete_refs &&
 821                print_comp
 822        ) &&
 823        test_cmp expected out
 824'
 825
 826test_expect_success '__git_complete_refs - matching' '
 827        sed -e "s/Z$//" >expected <<-EOF &&
 828        matching-branch Z
 829        matching-tag Z
 830        EOF
 831        (
 832                cur=mat &&
 833                __git_complete_refs &&
 834                print_comp
 835        ) &&
 836        test_cmp expected out
 837'
 838
 839test_expect_success '__git_complete_refs - remote' '
 840        sed -e "s/Z$//" >expected <<-EOF &&
 841        HEAD Z
 842        branch-in-other Z
 843        master-in-other Z
 844        EOF
 845        (
 846                cur=
 847                __git_complete_refs --remote=other &&
 848                print_comp
 849        ) &&
 850        test_cmp expected out
 851'
 852
 853test_expect_success '__git_complete_refs - track' '
 854        sed -e "s/Z$//" >expected <<-EOF &&
 855        HEAD Z
 856        master Z
 857        matching-branch Z
 858        other/branch-in-other Z
 859        other/master-in-other Z
 860        matching-tag Z
 861        branch-in-other Z
 862        master-in-other Z
 863        EOF
 864        (
 865                cur=
 866                __git_complete_refs --track &&
 867                print_comp
 868        ) &&
 869        test_cmp expected out
 870'
 871
 872test_expect_success '__git_complete_refs - current word' '
 873        sed -e "s/Z$//" >expected <<-EOF &&
 874        matching-branch Z
 875        matching-tag Z
 876        EOF
 877        (
 878                cur="--option=mat" &&
 879                __git_complete_refs --cur="${cur#*=}" &&
 880                print_comp
 881        ) &&
 882        test_cmp expected out
 883'
 884
 885test_expect_success '__git_complete_refs - prefix' '
 886        sed -e "s/Z$//" >expected <<-EOF &&
 887        v1.0..matching-branch Z
 888        v1.0..matching-tag Z
 889        EOF
 890        (
 891                cur=v1.0..mat &&
 892                __git_complete_refs --pfx=v1.0.. --cur=mat &&
 893                print_comp
 894        ) &&
 895        test_cmp expected out
 896'
 897
 898test_expect_success '__git_complete_refs - suffix' '
 899        cat >expected <<-EOF &&
 900        HEAD.
 901        master.
 902        matching-branch.
 903        other/branch-in-other.
 904        other/master-in-other.
 905        matching-tag.
 906        EOF
 907        (
 908                cur= &&
 909                __git_complete_refs --sfx=. &&
 910                print_comp
 911        ) &&
 912        test_cmp expected out
 913'
 914
 915test_expect_success '__git_complete_fetch_refspecs - simple' '
 916        sed -e "s/Z$//" >expected <<-EOF &&
 917        HEAD:HEAD Z
 918        branch-in-other:branch-in-other Z
 919        master-in-other:master-in-other Z
 920        EOF
 921        (
 922                cur= &&
 923                __git_complete_fetch_refspecs other &&
 924                print_comp
 925        ) &&
 926        test_cmp expected out
 927'
 928
 929test_expect_success '__git_complete_fetch_refspecs - matching' '
 930        sed -e "s/Z$//" >expected <<-EOF &&
 931        branch-in-other:branch-in-other Z
 932        EOF
 933        (
 934                cur=br &&
 935                __git_complete_fetch_refspecs other "" br &&
 936                print_comp
 937        ) &&
 938        test_cmp expected out
 939'
 940
 941test_expect_success '__git_complete_fetch_refspecs - prefix' '
 942        sed -e "s/Z$//" >expected <<-EOF &&
 943        +HEAD:HEAD Z
 944        +branch-in-other:branch-in-other Z
 945        +master-in-other:master-in-other Z
 946        EOF
 947        (
 948                cur="+" &&
 949                __git_complete_fetch_refspecs other "+" ""  &&
 950                print_comp
 951        ) &&
 952        test_cmp expected out
 953'
 954
 955test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
 956        sed -e "s/Z$//" >expected <<-EOF &&
 957        refs/heads/branch-in-other:refs/heads/branch-in-other Z
 958        refs/heads/master-in-other:refs/heads/master-in-other Z
 959        refs/tags/tag-in-other:refs/tags/tag-in-other Z
 960        EOF
 961        (
 962                cur=refs/ &&
 963                __git_complete_fetch_refspecs other "" refs/ &&
 964                print_comp
 965        ) &&
 966        test_cmp expected out
 967'
 968
 969test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
 970        sed -e "s/Z$//" >expected <<-EOF &&
 971        +refs/heads/branch-in-other:refs/heads/branch-in-other Z
 972        +refs/heads/master-in-other:refs/heads/master-in-other Z
 973        +refs/tags/tag-in-other:refs/tags/tag-in-other Z
 974        EOF
 975        (
 976                cur=+refs/ &&
 977                __git_complete_fetch_refspecs other + refs/ &&
 978                print_comp
 979        ) &&
 980        test_cmp expected out
 981'
 982
 983test_expect_success 'teardown after ref completion' '
 984        git branch -d matching-branch &&
 985        git tag -d matching-tag &&
 986        git remote remove other
 987'
 988
 989test_expect_success '__git_get_config_variables' '
 990        cat >expect <<-EOF &&
 991        name-1
 992        name-2
 993        EOF
 994        test_config interesting.name-1 good &&
 995        test_config interesting.name-2 good &&
 996        test_config subsection.interesting.name-3 bad &&
 997        __git_get_config_variables interesting >actual &&
 998        test_cmp expect actual
 999'
1000
1001test_expect_success '__git_pretty_aliases' '
1002        cat >expect <<-EOF &&
1003        author
1004        hash
1005        EOF
1006        test_config pretty.author "%an %ae" &&
1007        test_config pretty.hash %H &&
1008        __git_pretty_aliases >actual &&
1009        test_cmp expect actual
1010'
1011
1012test_expect_success '__git_aliases' '
1013        cat >expect <<-EOF &&
1014        ci
1015        co
1016        EOF
1017        test_config alias.ci commit &&
1018        test_config alias.co checkout &&
1019        __git_aliases >actual &&
1020        test_cmp expect actual
1021'
1022
1023test_expect_success 'basic' '
1024        run_completion "git " &&
1025        # built-in
1026        grep -q "^add \$" out &&
1027        # script
1028        grep -q "^filter-branch \$" out &&
1029        # plumbing
1030        ! grep -q "^ls-files \$" out &&
1031
1032        run_completion "git f" &&
1033        ! grep -q -v "^f" out
1034'
1035
1036test_expect_success 'double dash "git" itself' '
1037        test_completion "git --" <<-\EOF
1038        --paginate Z
1039        --no-pager Z
1040        --git-dir=
1041        --bare Z
1042        --version Z
1043        --exec-path Z
1044        --exec-path=
1045        --html-path Z
1046        --man-path Z
1047        --info-path Z
1048        --work-tree=
1049        --namespace=
1050        --no-replace-objects Z
1051        --help Z
1052        EOF
1053'
1054
1055test_expect_success 'double dash "git checkout"' '
1056        test_completion "git checkout --" <<-\EOF
1057        --quiet Z
1058        --ours Z
1059        --theirs Z
1060        --track Z
1061        --no-track Z
1062        --merge Z
1063        --conflict=
1064        --orphan Z
1065        --patch Z
1066        EOF
1067'
1068
1069test_expect_success 'general options' '
1070        test_completion "git --ver" "--version " &&
1071        test_completion "git --hel" "--help " &&
1072        test_completion "git --exe" <<-\EOF &&
1073        --exec-path Z
1074        --exec-path=
1075        EOF
1076        test_completion "git --htm" "--html-path " &&
1077        test_completion "git --pag" "--paginate " &&
1078        test_completion "git --no-p" "--no-pager " &&
1079        test_completion "git --git" "--git-dir=" &&
1080        test_completion "git --wor" "--work-tree=" &&
1081        test_completion "git --nam" "--namespace=" &&
1082        test_completion "git --bar" "--bare " &&
1083        test_completion "git --inf" "--info-path " &&
1084        test_completion "git --no-r" "--no-replace-objects "
1085'
1086
1087test_expect_success 'general options plus command' '
1088        test_completion "git --version check" "checkout " &&
1089        test_completion "git --paginate check" "checkout " &&
1090        test_completion "git --git-dir=foo check" "checkout " &&
1091        test_completion "git --bare check" "checkout " &&
1092        test_completion "git --exec-path=foo check" "checkout " &&
1093        test_completion "git --html-path check" "checkout " &&
1094        test_completion "git --no-pager check" "checkout " &&
1095        test_completion "git --work-tree=foo check" "checkout " &&
1096        test_completion "git --namespace=foo check" "checkout " &&
1097        test_completion "git --paginate check" "checkout " &&
1098        test_completion "git --info-path check" "checkout " &&
1099        test_completion "git --no-replace-objects check" "checkout " &&
1100        test_completion "git --git-dir some/path check" "checkout " &&
1101        test_completion "git -c conf.var=value check" "checkout " &&
1102        test_completion "git -C some/path check" "checkout " &&
1103        test_completion "git --work-tree some/path check" "checkout " &&
1104        test_completion "git --namespace name/space check" "checkout "
1105'
1106
1107test_expect_success 'git --help completion' '
1108        test_completion "git --help ad" "add " &&
1109        test_completion "git --help core" "core-tutorial "
1110'
1111
1112test_expect_success 'setup for integration tests' '
1113        echo content >file1 &&
1114        echo more >file2 &&
1115        git add file1 file2 &&
1116        git commit -m one &&
1117        git branch mybranch &&
1118        git tag mytag
1119'
1120
1121test_expect_success 'checkout completes ref names' '
1122        test_completion "git checkout m" <<-\EOF
1123        master Z
1124        mybranch Z
1125        mytag Z
1126        EOF
1127'
1128
1129test_expect_success 'git -C <path> checkout uses the right repo' '
1130        test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
1131        branch-in-other Z
1132        EOF
1133'
1134
1135test_expect_success 'show completes all refs' '
1136        test_completion "git show m" <<-\EOF
1137        master Z
1138        mybranch Z
1139        mytag Z
1140        EOF
1141'
1142
1143test_expect_success '<ref>: completes paths' '
1144        test_completion "git show mytag:f" <<-\EOF
1145        file1 Z
1146        file2 Z
1147        EOF
1148'
1149
1150test_expect_success 'complete tree filename with spaces' '
1151        echo content >"name with spaces" &&
1152        git add "name with spaces" &&
1153        git commit -m spaces &&
1154        test_completion "git show HEAD:nam" <<-\EOF
1155        name with spaces Z
1156        EOF
1157'
1158
1159test_expect_success 'complete tree filename with metacharacters' '
1160        echo content >"name with \${meta}" &&
1161        git add "name with \${meta}" &&
1162        git commit -m meta &&
1163        test_completion "git show HEAD:nam" <<-\EOF
1164        name with ${meta} Z
1165        name with spaces Z
1166        EOF
1167'
1168
1169test_expect_success 'send-email' '
1170        test_completion "git send-email --cov" "--cover-letter " &&
1171        test_completion "git send-email ma" "master "
1172'
1173
1174test_expect_success 'complete files' '
1175        git init tmp && cd tmp &&
1176        test_when_finished "cd .. && rm -rf tmp" &&
1177
1178        echo "expected" > .gitignore &&
1179        echo "out" >> .gitignore &&
1180
1181        git add .gitignore &&
1182        test_completion "git commit " ".gitignore" &&
1183
1184        git commit -m ignore &&
1185
1186        touch new &&
1187        test_completion "git add " "new" &&
1188
1189        git add new &&
1190        git commit -a -m new &&
1191        test_completion "git add " "" &&
1192
1193        git mv new modified &&
1194        echo modify > modified &&
1195        test_completion "git add " "modified" &&
1196
1197        touch untracked &&
1198
1199        : TODO .gitignore should not be here &&
1200        test_completion "git rm " <<-\EOF &&
1201        .gitignore
1202        modified
1203        EOF
1204
1205        test_completion "git clean " "untracked" &&
1206
1207        : TODO .gitignore should not be here &&
1208        test_completion "git mv " <<-\EOF &&
1209        .gitignore
1210        modified
1211        EOF
1212
1213        mkdir dir &&
1214        touch dir/file-in-dir &&
1215        git add dir/file-in-dir &&
1216        git commit -m dir &&
1217
1218        mkdir untracked-dir &&
1219
1220        : TODO .gitignore should not be here &&
1221        test_completion "git mv modified " <<-\EOF &&
1222        .gitignore
1223        dir
1224        modified
1225        untracked
1226        untracked-dir
1227        EOF
1228
1229        test_completion "git commit " "modified" &&
1230
1231        : TODO .gitignore should not be here &&
1232        test_completion "git ls-files " <<-\EOF &&
1233        .gitignore
1234        dir
1235        modified
1236        EOF
1237
1238        touch momified &&
1239        test_completion "git add mom" "momified"
1240'
1241
1242test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1243        test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1244        test_completion "git co m" <<-\EOF
1245        master Z
1246        mybranch Z
1247        mytag Z
1248        EOF
1249'
1250
1251test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1252        test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1253        test_completion "git co m" <<-\EOF
1254        master Z
1255        mybranch Z
1256        mytag Z
1257        EOF
1258'
1259
1260test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1261        test_config alias.co "!f() { : git checkout ; if ... } f" &&
1262        test_completion "git co m" <<-\EOF
1263        master Z
1264        mybranch Z
1265        mytag Z
1266        EOF
1267'
1268
1269test_expect_failure 'complete with tilde expansion' '
1270        git init tmp && cd tmp &&
1271        test_when_finished "cd .. && rm -rf tmp" &&
1272
1273        touch ~/tmp/file &&
1274
1275        test_completion "git add ~/tmp/" "~/tmp/file"
1276'
1277
1278test_done