c6a7bfb4480327ec2db69df5cc0a93a0019faed7
   1#!/bin/sh
   2
   3test_description='git remote porcelain-ish'
   4
   5. ./test-lib.sh
   6
   7setup_repository () {
   8        mkdir "$1" && (
   9        cd "$1" &&
  10        git init &&
  11        >file &&
  12        git add file &&
  13        test_tick &&
  14        git commit -m "Initial" &&
  15        git checkout -b side &&
  16        >elif &&
  17        git add elif &&
  18        test_tick &&
  19        git commit -m "Second" &&
  20        git checkout master
  21        )
  22}
  23
  24tokens_match () {
  25        echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
  26        echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
  27        test_cmp expect actual
  28}
  29
  30check_remote_track () {
  31        actual=$(git remote show "$1" | sed -n -e '$p') &&
  32        shift &&
  33        tokens_match "$*" "$actual"
  34}
  35
  36check_tracking_branch () {
  37        f="" &&
  38        r=$(git for-each-ref "--format=%(refname)" |
  39                sed -ne "s|^refs/remotes/$1/||p") &&
  40        shift &&
  41        tokens_match "$*" "$r"
  42}
  43
  44test_expect_success setup '
  45
  46        setup_repository one &&
  47        setup_repository two &&
  48        (
  49                cd two && git branch another
  50        ) &&
  51        git clone one test
  52
  53'
  54
  55test_expect_success 'remote information for the origin' '
  56(
  57        cd test &&
  58        tokens_match origin "$(git remote)" &&
  59        check_remote_track origin master side &&
  60        check_tracking_branch origin HEAD master side
  61)
  62'
  63
  64test_expect_success 'add another remote' '
  65(
  66        cd test &&
  67        git remote add -f second ../two &&
  68        tokens_match "origin second" "$(git remote)" &&
  69        check_remote_track origin master side &&
  70        check_remote_track second master side another &&
  71        check_tracking_branch second master side another &&
  72        git for-each-ref "--format=%(refname)" refs/remotes |
  73        sed -e "/^refs\/remotes\/origin\//d" \
  74            -e "/^refs\/remotes\/second\//d" >actual &&
  75        >expect &&
  76        test_cmp expect actual
  77)
  78'
  79
  80test_expect_success 'remote forces tracking branches' '
  81(
  82        cd test &&
  83        case `git config remote.second.fetch` in
  84        +*) true ;;
  85         *) false ;;
  86        esac
  87)
  88'
  89
  90test_expect_success 'remove remote' '
  91(
  92        cd test &&
  93        git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/master &&
  94        git remote rm second
  95)
  96'
  97
  98test_expect_success 'remove remote' '
  99(
 100        cd test &&
 101        tokens_match origin "$(git remote)" &&
 102        check_remote_track origin master side &&
 103        git for-each-ref "--format=%(refname)" refs/remotes |
 104        sed -e "/^refs\/remotes\/origin\//d" >actual &&
 105        >expect &&
 106        test_cmp expect actual
 107)
 108'
 109
 110cat > test/expect << EOF
 111* remote origin
 112  URL: $(pwd)/one/.git
 113  Remote branch merged with 'git pull' while on branch master
 114    master
 115  New remote branch (next fetch will store in remotes/origin)
 116    master
 117  Tracked remote branches
 118    side master
 119  Local branches pushed with 'git push'
 120    master:upstream +refs/tags/lastbackup
 121EOF
 122
 123test_expect_success 'show' '
 124        (cd test &&
 125         git config --add remote.origin.fetch \
 126                refs/heads/master:refs/heads/upstream &&
 127         git fetch &&
 128         git branch -d -r origin/master &&
 129         (cd ../one &&
 130          echo 1 > file &&
 131          test_tick &&
 132          git commit -m update file) &&
 133         git config remote.origin.push \
 134                refs/heads/master:refs/heads/upstream &&
 135         git config --add remote.origin.push \
 136                +refs/tags/lastbackup &&
 137         git remote show origin > output &&
 138         test_cmp expect output)
 139'
 140
 141cat > test/expect << EOF
 142* remote origin
 143  URL: $(pwd)/one/.git
 144  Remote branch merged with 'git pull' while on branch master
 145    master
 146  Local branches pushed with 'git push'
 147    master:upstream +refs/tags/lastbackup
 148EOF
 149
 150test_expect_success 'show -n' '
 151        (mv one one.unreachable &&
 152         cd test &&
 153         git remote show -n origin > output &&
 154         mv ../one.unreachable ../one &&
 155         test_cmp expect output)
 156'
 157
 158test_expect_success 'prune' '
 159        (cd one &&
 160         git branch -m side side2) &&
 161        (cd test &&
 162         git fetch origin &&
 163         git remote prune origin &&
 164         git rev-parse refs/remotes/origin/side2 &&
 165         ! git rev-parse refs/remotes/origin/side)
 166'
 167
 168test_expect_success 'add --mirror && prune' '
 169        (mkdir mirror &&
 170         cd mirror &&
 171         git init &&
 172         git remote add --mirror -f origin ../one) &&
 173        (cd one &&
 174         git branch -m side2 side) &&
 175        (cd mirror &&
 176         git rev-parse --verify refs/heads/side2 &&
 177         ! git rev-parse --verify refs/heads/side &&
 178         git fetch origin &&
 179         git remote prune origin &&
 180         ! git rev-parse --verify refs/heads/side2 &&
 181         git rev-parse --verify refs/heads/side)
 182'
 183
 184test_expect_success 'add alt && prune' '
 185        (mkdir alttst &&
 186         cd alttst &&
 187         git init &&
 188         git remote add -f origin ../one &&
 189         git config remote.alt.url ../one &&
 190         git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*") &&
 191        (cd one &&
 192         git branch -m side side2) &&
 193        (cd alttst &&
 194         git rev-parse --verify refs/remotes/origin/side &&
 195         ! git rev-parse --verify refs/remotes/origin/side2 &&
 196         git fetch alt &&
 197         git remote prune alt &&
 198         ! git rev-parse --verify refs/remotes/origin/side &&
 199         git rev-parse --verify refs/remotes/origin/side2)
 200'
 201
 202cat > one/expect << EOF
 203  apis/master
 204  apis/side
 205  drosophila/another
 206  drosophila/master
 207  drosophila/side
 208EOF
 209
 210test_expect_success 'update' '
 211
 212        (cd one &&
 213         git remote add drosophila ../two &&
 214         git remote add apis ../mirror &&
 215         git remote update &&
 216         git branch -r > output &&
 217         test_cmp expect output)
 218
 219'
 220
 221cat > one/expect << EOF
 222  drosophila/another
 223  drosophila/master
 224  drosophila/side
 225  manduca/master
 226  manduca/side
 227  megaloprepus/master
 228  megaloprepus/side
 229EOF
 230
 231test_expect_success 'update with arguments' '
 232
 233        (cd one &&
 234         for b in $(git branch -r)
 235         do
 236                git branch -r -d $b || break
 237         done &&
 238         git remote add manduca ../mirror &&
 239         git remote add megaloprepus ../mirror &&
 240         git config remotes.phobaeticus "drosophila megaloprepus" &&
 241         git config remotes.titanus manduca &&
 242         git remote update phobaeticus titanus &&
 243         git branch -r > output &&
 244         test_cmp expect output)
 245
 246'
 247
 248cat > one/expect << EOF
 249  apis/master
 250  apis/side
 251  manduca/master
 252  manduca/side
 253  megaloprepus/master
 254  megaloprepus/side
 255EOF
 256
 257test_expect_success 'update default' '
 258
 259        (cd one &&
 260         for b in $(git branch -r)
 261         do
 262                git branch -r -d $b || break
 263         done &&
 264         git config remote.drosophila.skipDefaultUpdate true &&
 265         git remote update default &&
 266         git branch -r > output &&
 267         test_cmp expect output)
 268
 269'
 270
 271cat > one/expect << EOF
 272  drosophila/another
 273  drosophila/master
 274  drosophila/side
 275EOF
 276
 277test_expect_success 'update default (overridden, with funny whitespace)' '
 278
 279        (cd one &&
 280         for b in $(git branch -r)
 281         do
 282                git branch -r -d $b || break
 283         done &&
 284         git config remotes.default "$(printf "\t drosophila  \n")" &&
 285         git remote update default &&
 286         git branch -r > output &&
 287         test_cmp expect output)
 288
 289'
 290
 291test_expect_success '"remote show" does not show symbolic refs' '
 292
 293        git clone one three &&
 294        (cd three &&
 295         git remote show origin > output &&
 296         ! grep HEAD < output &&
 297         ! grep -i stale < output)
 298
 299'
 300
 301test_expect_success 'reject adding remote with an invalid name' '
 302
 303        ! git remote add some:url desired-name
 304
 305'
 306
 307test_done