Merge branch 'jc/tree-walk-drop-base-offset'
[git/debian.git] / t / t6040-tracking-info.sh
blob7ddbd96e58e71d0ef5cae0334b0ff005c7a5cb25
1 #!/bin/sh
3 test_description='remote tracking stats'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK=true
9 . ./test-lib.sh
11 advance () {
12 echo "$1" >"$1" &&
13 git add "$1" &&
14 test_tick &&
15 git commit -m "$1"
18 test_expect_success setup '
19 advance a &&
20 advance b &&
21 advance c &&
22 git clone . test &&
24 cd test &&
25 git checkout -b b1 origin &&
26 git reset --hard HEAD^ &&
27 advance d &&
28 git checkout -b b2 origin &&
29 git reset --hard b1 &&
30 git checkout -b b3 origin &&
31 git reset --hard HEAD^ &&
32 git checkout -b b4 origin &&
33 advance e &&
34 advance f &&
35 git checkout -b brokenbase origin &&
36 git checkout -b b5 --track brokenbase &&
37 advance g &&
38 git branch -d brokenbase &&
39 git checkout -b b6 origin
40 ) &&
41 git checkout -b follower --track main &&
42 advance h
45 t6040_script='s/^..\(b.\) *[0-9a-f]* \(.*\)$/\1 \2/p'
46 cat >expect <<\EOF
47 b1 [ahead 1, behind 1] d
48 b2 [ahead 1, behind 1] d
49 b3 [behind 1] b
50 b4 [ahead 2] f
51 b5 [gone] g
52 b6 c
53 EOF
55 test_expect_success 'branch -v' '
57 cd test &&
58 git branch -v
59 ) |
60 sed -n -e "$t6040_script" >actual &&
61 test_cmp expect actual
64 cat >expect <<\EOF
65 b1 [origin/main: ahead 1, behind 1] d
66 b2 [origin/main: ahead 1, behind 1] d
67 b3 [origin/main: behind 1] b
68 b4 [origin/main: ahead 2] f
69 b5 [brokenbase: gone] g
70 b6 [origin/main] c
71 EOF
73 test_expect_success 'branch -vv' '
75 cd test &&
76 git branch -vv
77 ) |
78 sed -n -e "$t6040_script" >actual &&
79 test_cmp expect actual
82 test_expect_success 'checkout (diverged from upstream)' '
84 cd test && git checkout b1
85 ) >actual &&
86 test_i18ngrep "have 1 and 1 different" actual
89 test_expect_success 'checkout with local tracked branch' '
90 git checkout main &&
91 git checkout follower >actual &&
92 test_i18ngrep "is ahead of" actual
95 test_expect_success 'checkout (upstream is gone)' '
97 cd test &&
98 git checkout b5
99 ) >actual &&
100 test_i18ngrep "is based on .*, but the upstream is gone." actual
103 test_expect_success 'checkout (up-to-date with upstream)' '
105 cd test && git checkout b6
106 ) >actual &&
107 test_i18ngrep "Your branch is up to date with .origin/main" actual
110 test_expect_success 'status (diverged from upstream)' '
112 cd test &&
113 git checkout b1 >/dev/null &&
114 # reports nothing to commit
115 test_must_fail git commit --dry-run
116 ) >actual &&
117 test_i18ngrep "have 1 and 1 different" actual
120 test_expect_success 'status (upstream is gone)' '
122 cd test &&
123 git checkout b5 >/dev/null &&
124 # reports nothing to commit
125 test_must_fail git commit --dry-run
126 ) >actual &&
127 test_i18ngrep "is based on .*, but the upstream is gone." actual
130 test_expect_success 'status (up-to-date with upstream)' '
132 cd test &&
133 git checkout b6 >/dev/null &&
134 # reports nothing to commit
135 test_must_fail git commit --dry-run
136 ) >actual &&
137 test_i18ngrep "Your branch is up to date with .origin/main" actual
140 cat >expect <<\EOF
141 ## b1...origin/main [ahead 1, behind 1]
144 test_expect_success 'status -s -b (diverged from upstream)' '
146 cd test &&
147 git checkout b1 >/dev/null &&
148 git status -s -b | head -1
149 ) >actual &&
150 test_cmp expect actual
153 cat >expect <<\EOF
154 ## b1...origin/main [different]
157 test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' '
159 cd test &&
160 git checkout b1 >/dev/null &&
161 git status -s -b --no-ahead-behind | head -1
162 ) >actual &&
163 test_cmp expect actual
166 cat >expect <<\EOF
167 ## b1...origin/main [different]
170 test_expect_success 'status.aheadbehind=false status -s -b (diverged from upstream)' '
172 cd test &&
173 git checkout b1 >/dev/null &&
174 git -c status.aheadbehind=false status -s -b | head -1
175 ) >actual &&
176 test_cmp expect actual
179 cat >expect <<\EOF
180 On branch b1
181 Your branch and 'origin/main' have diverged,
182 and have 1 and 1 different commits each, respectively.
185 test_expect_success 'status --long --branch' '
187 cd test &&
188 git checkout b1 >/dev/null &&
189 git status --long -b | head -3
190 ) >actual &&
191 test_cmp expect actual
194 test_expect_success 'status --long --branch' '
196 cd test &&
197 git checkout b1 >/dev/null &&
198 git -c status.aheadbehind=true status --long -b | head -3
199 ) >actual &&
200 test_cmp expect actual
203 cat >expect <<\EOF
204 On branch b1
205 Your branch and 'origin/main' refer to different commits.
208 test_expect_success 'status --long --branch --no-ahead-behind' '
210 cd test &&
211 git checkout b1 >/dev/null &&
212 git status --long -b --no-ahead-behind | head -2
213 ) >actual &&
214 test_cmp expect actual
217 test_expect_success 'status.aheadbehind=false status --long --branch' '
219 cd test &&
220 git checkout b1 >/dev/null &&
221 git -c status.aheadbehind=false status --long -b | head -2
222 ) >actual &&
223 test_cmp expect actual
226 cat >expect <<\EOF
227 ## b5...brokenbase [gone]
230 test_expect_success 'status -s -b (upstream is gone)' '
232 cd test &&
233 git checkout b5 >/dev/null &&
234 git status -s -b | head -1
235 ) >actual &&
236 test_cmp expect actual
239 cat >expect <<\EOF
240 ## b6...origin/main
243 test_expect_success 'status -s -b (up-to-date with upstream)' '
245 cd test &&
246 git checkout b6 >/dev/null &&
247 git status -s -b | head -1
248 ) >actual &&
249 test_cmp expect actual
252 test_expect_success 'fail to track lightweight tags' '
253 git checkout main &&
254 git tag light &&
255 test_must_fail git branch --track lighttrack light >actual &&
256 test_i18ngrep ! "set up to track" actual &&
257 test_must_fail git checkout lighttrack
260 test_expect_success 'fail to track annotated tags' '
261 git checkout main &&
262 git tag -m heavy heavy &&
263 test_must_fail git branch --track heavytrack heavy >actual &&
264 test_i18ngrep ! "set up to track" actual &&
265 test_must_fail git checkout heavytrack
268 test_expect_success '--set-upstream-to does not change branch' '
269 git branch from-main main &&
270 git branch --set-upstream-to main from-main &&
271 git branch from-topic_2 main &&
272 test_must_fail git config branch.from-topic_2.merge > actual &&
273 git rev-list from-topic_2 &&
274 git update-ref refs/heads/from-topic_2 from-topic_2^ &&
275 git rev-parse from-topic_2 >expect2 &&
276 git branch --set-upstream-to main from-topic_2 &&
277 git config branch.from-main.merge > actual &&
278 git rev-parse from-topic_2 >actual2 &&
279 grep -q "^refs/heads/main$" actual &&
280 cmp expect2 actual2
283 test_expect_success '--set-upstream-to @{-1}' '
284 git checkout follower &&
285 git checkout from-topic_2 &&
286 git config branch.from-topic_2.merge > expect2 &&
287 git branch --set-upstream-to @{-1} from-main &&
288 git config branch.from-main.merge > actual &&
289 git config branch.from-topic_2.merge > actual2 &&
290 git branch --set-upstream-to follower from-main &&
291 git config branch.from-main.merge > expect &&
292 test_cmp expect2 actual2 &&
293 test_cmp expect actual
296 test_done