t / t6040-tracking-info.shon commit Merge branch 'ks/branch-set-upstream' (4229478)
   1#!/bin/sh
   2
   3test_description='remote tracking stats'
   4
   5. ./test-lib.sh
   6
   7advance () {
   8        echo "$1" >"$1" &&
   9        git add "$1" &&
  10        test_tick &&
  11        git commit -m "$1"
  12}
  13
  14test_expect_success setup '
  15        advance a &&
  16        advance b &&
  17        advance c &&
  18        git clone . test &&
  19        (
  20                cd test &&
  21                git checkout -b b1 origin &&
  22                git reset --hard HEAD^ &&
  23                advance d &&
  24                git checkout -b b2 origin &&
  25                git reset --hard b1 &&
  26                git checkout -b b3 origin &&
  27                git reset --hard HEAD^ &&
  28                git checkout -b b4 origin &&
  29                advance e &&
  30                advance f &&
  31                git checkout -b brokenbase origin &&
  32                git checkout -b b5 --track brokenbase &&
  33                advance g &&
  34                git branch -d brokenbase &&
  35                git checkout -b b6 origin
  36        ) &&
  37        git checkout -b follower --track master &&
  38        advance h
  39'
  40
  41script='s/^..\(b.\) *[0-9a-f]* \(.*\)$/\1 \2/p'
  42cat >expect <<\EOF
  43b1 [ahead 1, behind 1] d
  44b2 [ahead 1, behind 1] d
  45b3 [behind 1] b
  46b4 [ahead 2] f
  47b5 [gone] g
  48b6 c
  49EOF
  50
  51test_expect_success 'branch -v' '
  52        (
  53                cd test &&
  54                git branch -v
  55        ) |
  56        sed -n -e "$script" >actual &&
  57        test_i18ncmp expect actual
  58'
  59
  60cat >expect <<\EOF
  61b1 [origin/master: ahead 1, behind 1] d
  62b2 [origin/master: ahead 1, behind 1] d
  63b3 [origin/master: behind 1] b
  64b4 [origin/master: ahead 2] f
  65b5 [brokenbase: gone] g
  66b6 [origin/master] c
  67EOF
  68
  69test_expect_success 'branch -vv' '
  70        (
  71                cd test &&
  72                git branch -vv
  73        ) |
  74        sed -n -e "$script" >actual &&
  75        test_i18ncmp expect actual
  76'
  77
  78test_expect_success 'checkout (diverged from upstream)' '
  79        (
  80                cd test && git checkout b1
  81        ) >actual &&
  82        test_i18ngrep "have 1 and 1 different" actual
  83'
  84
  85test_expect_success 'checkout with local tracked branch' '
  86        git checkout master &&
  87        git checkout follower >actual &&
  88        test_i18ngrep "is ahead of" actual
  89'
  90
  91test_expect_success 'checkout (upstream is gone)' '
  92        (
  93                cd test &&
  94                git checkout b5
  95        ) >actual &&
  96        test_i18ngrep "is based on .*, but the upstream is gone." actual
  97'
  98
  99test_expect_success 'checkout (up-to-date with upstream)' '
 100        (
 101                cd test && git checkout b6
 102        ) >actual &&
 103        test_i18ngrep "Your branch is up to date with .origin/master" actual
 104'
 105
 106test_expect_success 'status (diverged from upstream)' '
 107        (
 108                cd test &&
 109                git checkout b1 >/dev/null &&
 110                # reports nothing to commit
 111                test_must_fail git commit --dry-run
 112        ) >actual &&
 113        test_i18ngrep "have 1 and 1 different" actual
 114'
 115
 116test_expect_success 'status (upstream is gone)' '
 117        (
 118                cd test &&
 119                git checkout b5 >/dev/null &&
 120                # reports nothing to commit
 121                test_must_fail git commit --dry-run
 122        ) >actual &&
 123        test_i18ngrep "is based on .*, but the upstream is gone." actual
 124'
 125
 126test_expect_success 'status (up-to-date with upstream)' '
 127        (
 128                cd test &&
 129                git checkout b6 >/dev/null &&
 130                # reports nothing to commit
 131                test_must_fail git commit --dry-run
 132        ) >actual &&
 133        test_i18ngrep "Your branch is up to date with .origin/master" actual
 134'
 135
 136cat >expect <<\EOF
 137## b1...origin/master [ahead 1, behind 1]
 138EOF
 139
 140test_expect_success 'status -s -b (diverged from upstream)' '
 141        (
 142                cd test &&
 143                git checkout b1 >/dev/null &&
 144                git status -s -b | head -1
 145        ) >actual &&
 146        test_i18ncmp expect actual
 147'
 148
 149cat >expect <<\EOF
 150## b1...origin/master [different]
 151EOF
 152
 153test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' '
 154        (
 155                cd test &&
 156                git checkout b1 >/dev/null &&
 157                git status -s -b --no-ahead-behind | head -1
 158        ) >actual &&
 159        test_i18ncmp expect actual
 160'
 161
 162cat >expect <<\EOF
 163On branch b1
 164Your branch and 'origin/master' have diverged,
 165and have 1 and 1 different commits each, respectively.
 166EOF
 167
 168test_expect_success 'status --long --branch' '
 169        (
 170                cd test &&
 171                git checkout b1 >/dev/null &&
 172                git status --long -b | head -3
 173        ) >actual &&
 174        test_i18ncmp expect actual
 175'
 176
 177cat >expect <<\EOF
 178On branch b1
 179Your branch and 'origin/master' refer to different commits.
 180EOF
 181
 182test_expect_success 'status --long --branch --no-ahead-behind' '
 183        (
 184                cd test &&
 185                git checkout b1 >/dev/null &&
 186                git status --long -b --no-ahead-behind | head -2
 187        ) >actual &&
 188        test_i18ncmp expect actual
 189'
 190
 191cat >expect <<\EOF
 192## b5...brokenbase [gone]
 193EOF
 194
 195test_expect_success 'status -s -b (upstream is gone)' '
 196        (
 197                cd test &&
 198                git checkout b5 >/dev/null &&
 199                git status -s -b | head -1
 200        ) >actual &&
 201        test_i18ncmp expect actual
 202'
 203
 204cat >expect <<\EOF
 205## b6...origin/master
 206EOF
 207
 208test_expect_success 'status -s -b (up-to-date with upstream)' '
 209        (
 210                cd test &&
 211                git checkout b6 >/dev/null &&
 212                git status -s -b | head -1
 213        ) >actual &&
 214        test_i18ncmp expect actual
 215'
 216
 217test_expect_success 'fail to track lightweight tags' '
 218        git checkout master &&
 219        git tag light &&
 220        test_must_fail git branch --track lighttrack light >actual &&
 221        test_i18ngrep ! "set up to track" actual &&
 222        test_must_fail git checkout lighttrack
 223'
 224
 225test_expect_success 'fail to track annotated tags' '
 226        git checkout master &&
 227        git tag -m heavy heavy &&
 228        test_must_fail git branch --track heavytrack heavy >actual &&
 229        test_i18ngrep ! "set up to track" actual &&
 230        test_must_fail git checkout heavytrack
 231'
 232
 233test_expect_success '--set-upstream-to does not change branch' '
 234        git branch from-master master &&
 235        git branch --set-upstream-to master from-master &&
 236        git branch from-master2 master &&
 237        test_must_fail git config branch.from-master2.merge > actual &&
 238        git rev-list from-master2 &&
 239        git update-ref refs/heads/from-master2 from-master2^ &&
 240        git rev-parse from-master2 >expect2 &&
 241        git branch --set-upstream-to master from-master2 &&
 242        git config branch.from-master.merge > actual &&
 243        git rev-parse from-master2 >actual2 &&
 244        grep -q "^refs/heads/master$" actual &&
 245        cmp expect2 actual2
 246'
 247
 248test_expect_success '--set-upstream-to @{-1}' '
 249        git checkout follower &&
 250        git checkout from-master2 &&
 251        git config branch.from-master2.merge > expect2 &&
 252        git branch --set-upstream-to @{-1} from-master &&
 253        git config branch.from-master.merge > actual &&
 254        git config branch.from-master2.merge > actual2 &&
 255        git branch --set-upstream-to follower from-master &&
 256        git config branch.from-master.merge > expect &&
 257        test_cmp expect2 actual2 &&
 258        test_cmp expect actual
 259'
 260
 261test_done