t / t5512-ls-remote.shon commit Merge branch 'js/vscode' (30cf191)
   1#!/bin/sh
   2
   3test_description='git ls-remote'
   4
   5. ./test-lib.sh
   6
   7test_expect_success setup '
   8        >file &&
   9        git add file &&
  10        test_tick &&
  11        git commit -m initial &&
  12        git tag mark &&
  13        git tag mark1.1 &&
  14        git tag mark1.2 &&
  15        git tag mark1.10 &&
  16        git show-ref --tags -d | sed -e "s/ /   /" >expected.tag &&
  17        (
  18                echo "$(git rev-parse HEAD)     HEAD" &&
  19                git show-ref -d | sed -e "s/ /  /"
  20        ) >expected.all &&
  21
  22        git remote add self "$(pwd)/.git"
  23'
  24
  25test_expect_success 'ls-remote --tags .git' '
  26        git ls-remote --tags .git >actual &&
  27        test_cmp expected.tag actual
  28'
  29
  30test_expect_success 'ls-remote .git' '
  31        git ls-remote .git >actual &&
  32        test_cmp expected.all actual
  33'
  34
  35test_expect_success 'ls-remote --tags self' '
  36        git ls-remote --tags self >actual &&
  37        test_cmp expected.tag actual
  38'
  39
  40test_expect_success 'ls-remote self' '
  41        git ls-remote self >actual &&
  42        test_cmp expected.all actual
  43'
  44
  45test_expect_success 'ls-remote --sort="version:refname" --tags self' '
  46        cat >expect <<-EOF &&
  47        $(git rev-parse mark)   refs/tags/mark
  48        $(git rev-parse mark1.1)        refs/tags/mark1.1
  49        $(git rev-parse mark1.2)        refs/tags/mark1.2
  50        $(git rev-parse mark1.10)       refs/tags/mark1.10
  51        EOF
  52        git ls-remote --sort="version:refname" --tags self >actual &&
  53        test_cmp expect actual
  54'
  55
  56test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
  57        cat >expect <<-EOF &&
  58        $(git rev-parse mark1.10)       refs/tags/mark1.10
  59        $(git rev-parse mark1.2)        refs/tags/mark1.2
  60        $(git rev-parse mark1.1)        refs/tags/mark1.1
  61        $(git rev-parse mark)   refs/tags/mark
  62        EOF
  63        git ls-remote --sort="-version:refname" --tags self >actual &&
  64        test_cmp expect actual
  65'
  66
  67test_expect_success 'ls-remote --sort="-refname" --tags self' '
  68        cat >expect <<-EOF &&
  69        $(git rev-parse mark1.2)        refs/tags/mark1.2
  70        $(git rev-parse mark1.10)       refs/tags/mark1.10
  71        $(git rev-parse mark1.1)        refs/tags/mark1.1
  72        $(git rev-parse mark)   refs/tags/mark
  73        EOF
  74        git ls-remote --sort="-refname" --tags self >actual &&
  75        test_cmp expect actual
  76'
  77
  78test_expect_success 'dies when no remote specified and no default remotes found' '
  79        test_must_fail git ls-remote
  80'
  81
  82test_expect_success 'use "origin" when no remote specified' '
  83        URL="$(pwd)/.git" &&
  84        echo "From $URL" >exp_err &&
  85
  86        git remote add origin "$URL" &&
  87        git ls-remote 2>actual_err >actual &&
  88
  89        test_cmp exp_err actual_err &&
  90        test_cmp expected.all actual
  91'
  92
  93test_expect_success 'suppress "From <url>" with -q' '
  94        git ls-remote -q 2>actual_err &&
  95        test_must_fail test_cmp exp_err actual_err
  96'
  97
  98test_expect_success 'use branch.<name>.remote if possible' '
  99        #
 100        # Test that we are indeed using branch.<name>.remote, not "origin", even
 101        # though the "origin" remote has been set.
 102        #
 103
 104        # setup a new remote to differentiate from "origin"
 105        git clone . other.git &&
 106        (
 107                cd other.git &&
 108                echo "$(git rev-parse HEAD)     HEAD" &&
 109                git show-ref    | sed -e "s/ /  /"
 110        ) >exp &&
 111
 112        URL="other.git" &&
 113        echo "From $URL" >exp_err &&
 114
 115        git remote add other $URL &&
 116        git config branch.master.remote other &&
 117
 118        git ls-remote 2>actual_err >actual &&
 119        test_cmp exp_err actual_err &&
 120        test_cmp exp actual
 121'
 122
 123test_expect_success 'confuses pattern as remote when no remote specified' '
 124        if test_have_prereq MINGW
 125        then
 126                # Windows does not like asterisks in pathname
 127                does_not_exist=master
 128        else
 129                does_not_exist="refs*master"
 130        fi &&
 131        cat >exp <<-EOF &&
 132        fatal: '\''$does_not_exist'\'' does not appear to be a git repository
 133        fatal: Could not read from remote repository.
 134
 135        Please make sure you have the correct access rights
 136        and the repository exists.
 137        EOF
 138        #
 139        # Do not expect "git ls-remote <pattern>" to work; ls-remote needs
 140        # <remote> if you want to feed <pattern>, just like you cannot say
 141        # fetch <branch>.
 142        # We could just as easily have used "master"; the "*" emphasizes its
 143        # role as a pattern.
 144        test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
 145        test_i18ncmp exp actual
 146'
 147
 148test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
 149        {
 150                git ls-remote --exit-code ./no-such-repository
 151                status=$?
 152        } &&
 153        test $status != 2 && test $status != 0
 154'
 155
 156test_expect_success 'Report success even when nothing matches' '
 157        git ls-remote other.git "refs/nsn/*" >actual &&
 158        >expect &&
 159        test_cmp expect actual
 160'
 161
 162test_expect_success 'Report no-match with --exit-code' '
 163        test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
 164        >expect &&
 165        test_cmp expect actual
 166'
 167
 168test_expect_success 'Report match with --exit-code' '
 169        git ls-remote --exit-code other.git "refs/tags/*" >actual &&
 170        git ls-remote . tags/mark* >expect &&
 171        test_cmp expect actual
 172'
 173
 174test_expect_success 'set up some extra tags for ref hiding' '
 175        git tag magic/one &&
 176        git tag magic/two
 177'
 178
 179for configsection in transfer uploadpack
 180do
 181        test_expect_success "Hide some refs with $configsection.hiderefs" '
 182                test_config $configsection.hiderefs refs/tags &&
 183                git ls-remote . >actual &&
 184                test_unconfig $configsection.hiderefs &&
 185                git ls-remote . |
 186                sed -e "/       refs\/tags\//d" >expect &&
 187                test_cmp expect actual
 188        '
 189
 190        test_expect_success "Override hiding of $configsection.hiderefs" '
 191                test_when_finished "test_unconfig $configsection.hiderefs" &&
 192                git config --add $configsection.hiderefs refs/tags &&
 193                git config --add $configsection.hiderefs "!refs/tags/magic" &&
 194                git config --add $configsection.hiderefs refs/tags/magic/one &&
 195                git ls-remote . >actual &&
 196                grep refs/tags/magic/two actual &&
 197                ! grep refs/tags/magic/one actual
 198        '
 199
 200done
 201
 202test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs' '
 203        test_config uploadpack.hiderefs refs/tags &&
 204        test_config transfer.hiderefs "!refs/tags/magic" &&
 205        git ls-remote . >actual &&
 206        grep refs/tags/magic actual
 207'
 208
 209test_expect_success 'ls-remote --symref' '
 210        git fetch origin &&
 211        cat >expect <<-EOF &&
 212        ref: refs/heads/master  HEAD
 213        $(git rev-parse HEAD)   HEAD
 214        $(git rev-parse refs/heads/master)      refs/heads/master
 215        $(git rev-parse HEAD)   refs/remotes/origin/HEAD
 216        $(git rev-parse refs/remotes/origin/master)     refs/remotes/origin/master
 217        $(git rev-parse refs/tags/mark) refs/tags/mark
 218        $(git rev-parse refs/tags/mark1.1)      refs/tags/mark1.1
 219        $(git rev-parse refs/tags/mark1.10)     refs/tags/mark1.10
 220        $(git rev-parse refs/tags/mark1.2)      refs/tags/mark1.2
 221        EOF
 222        git ls-remote --symref >actual &&
 223        test_cmp expect actual
 224'
 225
 226test_expect_success 'ls-remote with filtered symref (refname)' '
 227        cat >expect <<-\EOF &&
 228        ref: refs/heads/master  HEAD
 229        1bd44cb9d13204b0fe1958db0082f5028a16eb3a        HEAD
 230        EOF
 231        git ls-remote --symref . HEAD >actual &&
 232        test_cmp expect actual
 233'
 234
 235test_expect_failure 'ls-remote with filtered symref (--heads)' '
 236        git symbolic-ref refs/heads/foo refs/tags/mark &&
 237        cat >expect <<-\EOF &&
 238        ref: refs/tags/mark     refs/heads/foo
 239        1bd44cb9d13204b0fe1958db0082f5028a16eb3a        refs/heads/foo
 240        1bd44cb9d13204b0fe1958db0082f5028a16eb3a        refs/heads/master
 241        EOF
 242        git ls-remote --symref --heads . >actual &&
 243        test_cmp expect actual
 244'
 245
 246test_expect_success 'ls-remote --symref omits filtered-out matches' '
 247        cat >expect <<-\EOF &&
 248        1bd44cb9d13204b0fe1958db0082f5028a16eb3a        refs/heads/foo
 249        1bd44cb9d13204b0fe1958db0082f5028a16eb3a        refs/heads/master
 250        EOF
 251        git ls-remote --symref --heads . >actual &&
 252        test_cmp expect actual &&
 253        git ls-remote --symref . "refs/heads/*" >actual &&
 254        test_cmp expect actual
 255'
 256
 257test_lazy_prereq GIT_DAEMON '
 258        test_tristate GIT_TEST_GIT_DAEMON &&
 259        test "$GIT_TEST_GIT_DAEMON" != false
 260'
 261
 262# This test spawns a daemon, so run it only if the user would be OK with
 263# testing with git-daemon.
 264test_expect_success PIPE,JGIT,GIT_DAEMON 'indicate no refs in standards-compliant empty remote' '
 265        JGIT_DAEMON_PORT=${JGIT_DAEMON_PORT-${this_test#t}} &&
 266        JGIT_DAEMON_PID= &&
 267        git init --bare empty.git &&
 268        >empty.git/git-daemon-export-ok &&
 269        mkfifo jgit_daemon_output &&
 270        {
 271                jgit daemon --port="$JGIT_DAEMON_PORT" . >jgit_daemon_output &
 272                JGIT_DAEMON_PID=$!
 273        } &&
 274        test_when_finished kill "$JGIT_DAEMON_PID" &&
 275        {
 276                read line &&
 277                case $line in
 278                Exporting*)
 279                        ;;
 280                *)
 281                        echo "Expected: Exporting" &&
 282                        false;;
 283                esac &&
 284                read line &&
 285                case $line in
 286                "Listening on"*)
 287                        ;;
 288                *)
 289                        echo "Expected: Listening on" &&
 290                        false;;
 291                esac
 292        } <jgit_daemon_output &&
 293        # --exit-code asks the command to exit with 2 when no
 294        # matching refs are found.
 295        test_expect_code 2 git ls-remote --exit-code git://localhost:$JGIT_DAEMON_PORT/empty.git
 296'
 297
 298test_expect_success 'ls-remote works outside repository' '
 299        # It is important for this repo to be inside the nongit
 300        # area, as we want a repo name that does not include
 301        # slashes (because those inhibit some of our configuration
 302        # lookups).
 303        nongit git init --bare dst.git &&
 304        nongit git ls-remote dst.git
 305'
 306
 307test_done