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