6f678a4dc27f2983d8613411c3de00068370ddd6
   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        for i in a b c;
  16        do
  17                advance $i || break
  18        done &&
  19        git clone . test &&
  20        (
  21                cd test &&
  22                git checkout -b b1 origin &&
  23                git reset --hard HEAD^ &&
  24                advance d &&
  25                git checkout -b b2 origin &&
  26                git reset --hard b1 &&
  27                git checkout -b b3 origin &&
  28                git reset --hard HEAD^ &&
  29                git checkout -b b4 origin &&
  30                advance e &&
  31                advance f &&
  32                git checkout -b brokenbase origin &&
  33                git checkout -b b5 --track brokenbase &&
  34                advance g &&
  35                git branch -d brokenbase
  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
  44b2 ahead 1, behind 1
  45b3 behind 1
  46b4 ahead 2
  47EOF
  48
  49test_expect_success 'branch -v' '
  50        (
  51                cd test &&
  52                git branch -v
  53        ) |
  54        sed -n -e "$script" >actual &&
  55        test_i18ncmp expect actual
  56'
  57
  58cat >expect <<\EOF
  59b1 origin/master: ahead 1, behind 1
  60b2 origin/master: ahead 1, behind 1
  61b3 origin/master: behind 1
  62b4 origin/master: ahead 2
  63b5 brokenbase: gone
  64EOF
  65
  66test_expect_success 'branch -vv' '
  67        (
  68                cd test &&
  69                git branch -vv
  70        ) |
  71        sed -n -e "$script" >actual &&
  72        test_i18ncmp expect actual
  73'
  74
  75test_expect_success 'checkout (diverged from upstream)' '
  76        (
  77                cd test && git checkout b1
  78        ) >actual &&
  79        test_i18ngrep "have 1 and 1 different" actual
  80'
  81
  82test_expect_success 'checkout with local tracked branch' '
  83        git checkout master &&
  84        git checkout follower >actual &&
  85        test_i18ngrep "is ahead of" actual
  86'
  87
  88test_expect_success 'checkout (upstream is gone)' '
  89        (
  90                cd test &&
  91                git checkout b5
  92        ) >actual &&
  93        test_i18ngrep "is based on .*, but the upstream is gone." actual
  94'
  95
  96test_expect_success 'status (diverged from upstream)' '
  97        (
  98                cd test &&
  99                git checkout b1 >/dev/null &&
 100                # reports nothing to commit
 101                test_must_fail git commit --dry-run
 102        ) >actual &&
 103        test_i18ngrep "have 1 and 1 different" actual
 104'
 105
 106test_expect_success 'status (upstream is gone)' '
 107        (
 108                cd test &&
 109                git checkout b5 >/dev/null &&
 110                # reports nothing to commit
 111                test_must_fail git commit --dry-run
 112        ) >actual &&
 113        test_i18ngrep "is based on .*, but the upstream is gone." actual
 114'
 115
 116cat >expect <<\EOF
 117## b1...origin/master [ahead 1, behind 1]
 118EOF
 119
 120test_expect_success 'status -s -b (diverged from upstream)' '
 121        (
 122                cd test &&
 123                git checkout b1 >/dev/null &&
 124                git status -s -b | head -1
 125        ) >actual &&
 126        test_i18ncmp expect actual
 127'
 128
 129cat >expect <<\EOF
 130## b5...brokenbase [gone]
 131EOF
 132
 133test_expect_success 'status -s -b (upstream is gone)' '
 134        (
 135                cd test &&
 136                git checkout b5 >/dev/null &&
 137                git status -s -b | head -1
 138        ) >actual &&
 139        test_i18ncmp expect actual
 140'
 141
 142test_expect_success 'fail to track lightweight tags' '
 143        git checkout master &&
 144        git tag light &&
 145        test_must_fail git branch --track lighttrack light >actual &&
 146        test_i18ngrep ! "set up to track" actual &&
 147        test_must_fail git checkout lighttrack
 148'
 149
 150test_expect_success 'fail to track annotated tags' '
 151        git checkout master &&
 152        git tag -m heavy heavy &&
 153        test_must_fail git branch --track heavytrack heavy >actual &&
 154        test_i18ngrep ! "set up to track" actual &&
 155        test_must_fail git checkout heavytrack
 156'
 157
 158test_expect_success 'setup tracking with branch --set-upstream on existing branch' '
 159        git branch from-master master &&
 160        test_must_fail git config branch.from-master.merge > actual &&
 161        git branch --set-upstream from-master master &&
 162        git config branch.from-master.merge > actual &&
 163        grep -q "^refs/heads/master$" actual
 164'
 165
 166test_expect_success '--set-upstream does not change branch' '
 167        git branch from-master2 master &&
 168        test_must_fail git config branch.from-master2.merge > actual &&
 169        git rev-list from-master2 &&
 170        git update-ref refs/heads/from-master2 from-master2^ &&
 171        git rev-parse from-master2 >expect2 &&
 172        git branch --set-upstream from-master2 master &&
 173        git config branch.from-master.merge > actual &&
 174        git rev-parse from-master2 >actual2 &&
 175        grep -q "^refs/heads/master$" actual &&
 176        cmp expect2 actual2
 177'
 178
 179test_expect_success '--set-upstream @{-1}' '
 180        git checkout from-master &&
 181        git checkout from-master2 &&
 182        git config branch.from-master2.merge > expect2 &&
 183        git branch --set-upstream @{-1} follower &&
 184        git config branch.from-master.merge > actual &&
 185        git config branch.from-master2.merge > actual2 &&
 186        git branch --set-upstream from-master follower &&
 187        git config branch.from-master.merge > expect &&
 188        test_cmp expect2 actual2 &&
 189        test_cmp expect actual
 190'
 191
 192test_done