]>
Commit | Line | Data |
---|---|---|
24bc1a12 CB |
1 | #!/bin/sh |
2 | ||
3 | test_description='"git fetch/pull --set-upstream" basic tests.' | |
028cb644 | 4 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
5 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
6 | ||
24bc1a12 CB |
7 | . ./test-lib.sh |
8 | ||
9 | check_config () { | |
10 | printf "%s\n" "$2" "$3" >"expect.$1" && | |
11 | { | |
12 | git config "branch.$1.remote" && git config "branch.$1.merge" | |
13 | } >"actual.$1" && | |
14 | test_cmp "expect.$1" "actual.$1" | |
15 | } | |
16 | ||
17 | check_config_missing () { | |
18 | test_expect_code 1 git config "branch.$1.remote" && | |
19 | test_expect_code 1 git config "branch.$1.merge" | |
20 | } | |
21 | ||
22 | clear_config () { | |
23 | for branch in "$@"; do | |
24 | test_might_fail git config --unset-all "branch.$branch.remote" | |
25 | test_might_fail git config --unset-all "branch.$branch.merge" | |
26 | done | |
27 | } | |
28 | ||
29 | ensure_fresh_upstream () { | |
30 | rm -rf parent && git init --bare parent | |
31 | } | |
32 | ||
33 | test_expect_success 'setup bare parent fetch' ' | |
34 | ensure_fresh_upstream && | |
35 | git remote add upstream parent | |
36 | ' | |
37 | ||
028cb644 | 38 | test_expect_success 'setup commit on main and other fetch' ' |
24bc1a12 | 39 | test_commit one && |
028cb644 | 40 | git push upstream main && |
24bc1a12 CB |
41 | git checkout -b other && |
42 | test_commit two && | |
43 | git push upstream other | |
44 | ' | |
45 | ||
46 | # tests for fetch --set-upstream | |
47 | ||
48 | test_expect_success 'fetch --set-upstream does not set upstream w/o branch' ' | |
028cb644 JS |
49 | clear_config main other && |
50 | git checkout main && | |
24bc1a12 | 51 | git fetch --set-upstream upstream && |
028cb644 | 52 | check_config_missing main && |
24bc1a12 CB |
53 | check_config_missing other |
54 | ' | |
55 | ||
028cb644 JS |
56 | test_expect_success 'fetch --set-upstream upstream main sets branch main but not other' ' |
57 | clear_config main other && | |
58 | git fetch --set-upstream upstream main && | |
59 | check_config main upstream refs/heads/main && | |
24bc1a12 CB |
60 | check_config_missing other |
61 | ' | |
62 | ||
63 | test_expect_success 'fetch --set-upstream upstream other sets branch other' ' | |
028cb644 | 64 | clear_config main other && |
24bc1a12 | 65 | git fetch --set-upstream upstream other && |
028cb644 | 66 | check_config main upstream refs/heads/other && |
24bc1a12 CB |
67 | check_config_missing other |
68 | ' | |
69 | ||
028cb644 | 70 | test_expect_success 'fetch --set-upstream main:other does not set the branch other2' ' |
24bc1a12 | 71 | clear_config other2 && |
028cb644 | 72 | git fetch --set-upstream upstream main:other2 && |
24bc1a12 CB |
73 | check_config_missing other2 |
74 | ' | |
75 | ||
76 | test_expect_success 'fetch --set-upstream http://nosuchdomain.example.com fails with invalid url' ' | |
028cb644 | 77 | # main explicitly not cleared, we check that it is not touched from previous value |
24bc1a12 CB |
78 | clear_config other other2 && |
79 | test_must_fail git fetch --set-upstream http://nosuchdomain.example.com && | |
028cb644 | 80 | check_config main upstream refs/heads/other && |
24bc1a12 CB |
81 | check_config_missing other && |
82 | check_config_missing other2 | |
83 | ' | |
84 | ||
85 | test_expect_success 'fetch --set-upstream with valid URL sets upstream to URL' ' | |
86 | clear_config other other2 && | |
c76b84a1 | 87 | url="file://$PWD" && |
24bc1a12 | 88 | git fetch --set-upstream "$url" && |
028cb644 | 89 | check_config main "$url" HEAD && |
24bc1a12 CB |
90 | check_config_missing other && |
91 | check_config_missing other2 | |
92 | ' | |
93 | ||
17baeaf8 ÆAB |
94 | test_expect_success 'fetch --set-upstream with a detached HEAD' ' |
95 | git checkout HEAD^0 && | |
96 | test_when_finished "git checkout -" && | |
97 | cat >expect <<-\EOF && | |
98 | warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch. | |
99 | EOF | |
100 | git fetch --set-upstream upstream main 2>actual.raw && | |
101 | grep ^warning: actual.raw >actual && | |
102 | test_cmp expect actual | |
103 | ' | |
104 | ||
24bc1a12 CB |
105 | # tests for pull --set-upstream |
106 | ||
107 | test_expect_success 'setup bare parent pull' ' | |
108 | git remote rm upstream && | |
109 | ensure_fresh_upstream && | |
110 | git remote add upstream parent | |
111 | ' | |
112 | ||
028cb644 | 113 | test_expect_success 'setup commit on main and other pull' ' |
24bc1a12 | 114 | test_commit three && |
028cb644 | 115 | git push --tags upstream main && |
24bc1a12 CB |
116 | test_commit four && |
117 | git push upstream other | |
118 | ' | |
119 | ||
028cb644 JS |
120 | test_expect_success 'pull --set-upstream upstream main sets branch main but not other' ' |
121 | clear_config main other && | |
031e2f7a | 122 | git pull --no-rebase --set-upstream upstream main && |
028cb644 | 123 | check_config main upstream refs/heads/main && |
24bc1a12 CB |
124 | check_config_missing other |
125 | ' | |
126 | ||
028cb644 | 127 | test_expect_success 'pull --set-upstream main:other2 does not set the branch other2' ' |
24bc1a12 | 128 | clear_config other2 && |
031e2f7a | 129 | git pull --no-rebase --set-upstream upstream main:other2 && |
24bc1a12 CB |
130 | check_config_missing other2 |
131 | ' | |
132 | ||
028cb644 JS |
133 | test_expect_success 'pull --set-upstream upstream other sets branch main' ' |
134 | clear_config main other && | |
031e2f7a | 135 | git pull --no-rebase --set-upstream upstream other && |
028cb644 | 136 | check_config main upstream refs/heads/other && |
24bc1a12 CB |
137 | check_config_missing other |
138 | ' | |
139 | ||
140 | test_expect_success 'pull --set-upstream upstream tag does not set the tag' ' | |
141 | clear_config three && | |
031e2f7a | 142 | git pull --no-rebase --tags --set-upstream upstream three && |
24bc1a12 CB |
143 | check_config_missing three |
144 | ' | |
145 | ||
146 | test_expect_success 'pull --set-upstream http://nosuchdomain.example.com fails with invalid url' ' | |
028cb644 | 147 | # main explicitly not cleared, we check that it is not touched from previous value |
24bc1a12 CB |
148 | clear_config other other2 three && |
149 | test_must_fail git pull --set-upstream http://nosuchdomain.example.com && | |
028cb644 | 150 | check_config main upstream refs/heads/other && |
24bc1a12 CB |
151 | check_config_missing other && |
152 | check_config_missing other2 && | |
153 | check_config_missing three | |
154 | ' | |
155 | ||
156 | test_expect_success 'pull --set-upstream upstream HEAD sets branch HEAD' ' | |
028cb644 | 157 | clear_config main other && |
031e2f7a | 158 | git pull --no-rebase --set-upstream upstream HEAD && |
028cb644 | 159 | check_config main upstream HEAD && |
24bc1a12 | 160 | git checkout other && |
031e2f7a | 161 | git pull --no-rebase --set-upstream upstream HEAD && |
24bc1a12 CB |
162 | check_config other upstream HEAD |
163 | ' | |
164 | ||
165 | test_expect_success 'pull --set-upstream upstream with more than one branch does nothing' ' | |
028cb644 | 166 | clear_config main three && |
031e2f7a | 167 | git pull --no-rebase --set-upstream upstream main three && |
028cb644 | 168 | check_config_missing main && |
24bc1a12 CB |
169 | check_config_missing three |
170 | ' | |
171 | ||
172 | test_expect_success 'pull --set-upstream with valid URL sets upstream to URL' ' | |
028cb644 JS |
173 | clear_config main other other2 && |
174 | git checkout main && | |
c76b84a1 | 175 | url="file://$PWD" && |
24bc1a12 | 176 | git pull --set-upstream "$url" && |
028cb644 | 177 | check_config main "$url" HEAD && |
24bc1a12 CB |
178 | check_config_missing other && |
179 | check_config_missing other2 | |
180 | ' | |
181 | ||
182 | test_expect_success 'pull --set-upstream with valid URL and branch sets branch' ' | |
028cb644 JS |
183 | clear_config main other other2 && |
184 | git checkout main && | |
c76b84a1 | 185 | url="file://$PWD" && |
028cb644 JS |
186 | git pull --set-upstream "$url" main && |
187 | check_config main "$url" refs/heads/main && | |
24bc1a12 CB |
188 | check_config_missing other && |
189 | check_config_missing other2 | |
190 | ' | |
191 | ||
17baeaf8 ÆAB |
192 | test_expect_success 'pull --set-upstream with a detached HEAD' ' |
193 | git checkout HEAD^0 && | |
194 | test_when_finished "git checkout -" && | |
195 | cat >expect <<-\EOF && | |
196 | warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch. | |
197 | EOF | |
198 | git pull --no-rebase --set-upstream upstream main 2>actual.raw && | |
199 | grep ^warning: actual.raw >actual && | |
200 | test_cmp expect actual | |
201 | ' | |
202 | ||
24bc1a12 | 203 | test_done |