| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2010 Sverre Rabbelier |
| 4 | # |
| 5 | |
| 6 | test_description='Test remote-helper import and export commands' |
| 7 | |
| 8 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 9 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 10 | |
| 11 | . ./test-lib.sh |
| 12 | . "$TEST_DIRECTORY"/lib-gpg.sh |
| 13 | |
| 14 | PATH="$TEST_DIRECTORY/t5801:$PATH" |
| 15 | |
| 16 | compare_refs() { |
| 17 | fail= && |
| 18 | if test "x$1" = 'x!' |
| 19 | then |
| 20 | fail='!' && |
| 21 | shift |
| 22 | fi && |
| 23 | git --git-dir="$1/.git" rev-parse --verify $2 >expect && |
| 24 | git --git-dir="$3/.git" rev-parse --verify $4 >actual && |
| 25 | eval $fail test_cmp expect actual |
| 26 | } |
| 27 | |
| 28 | test_expect_success 'setup repository' ' |
| 29 | git init server && |
| 30 | (cd server && |
| 31 | echo content >file && |
| 32 | git add file && |
| 33 | git commit -m one) |
| 34 | ' |
| 35 | |
| 36 | test_expect_success 'cloning from local repo' ' |
| 37 | git clone "testgit::${PWD}/server" local && |
| 38 | test_cmp server/file local/file |
| 39 | ' |
| 40 | |
| 41 | test_expect_success 'clone with remote.*.vcs config' ' |
| 42 | GIT_TRACE=$PWD/vcs-clone.trace \ |
| 43 | git clone --no-local -c remote.origin.vcs=testgit "$PWD/server" vcs-clone && |
| 44 | test_grep remote-testgit vcs-clone.trace |
| 45 | ' |
| 46 | |
| 47 | test_expect_success 'fetch with configured remote.*.vcs' ' |
| 48 | git init vcs-fetch && |
| 49 | git -C vcs-fetch config remote.origin.vcs testgit && |
| 50 | git -C vcs-fetch config remote.origin.url "$PWD/server" && |
| 51 | GIT_TRACE=$PWD/vcs-fetch.trace \ |
| 52 | git -C vcs-fetch fetch origin && |
| 53 | test_grep remote-testgit vcs-fetch.trace |
| 54 | ' |
| 55 | |
| 56 | test_expect_success 'vcs remote with no url' ' |
| 57 | NOURL_UPSTREAM=$PWD/server && |
| 58 | export NOURL_UPSTREAM && |
| 59 | git init vcs-nourl && |
| 60 | git -C vcs-nourl config remote.origin.vcs nourl && |
| 61 | git -C vcs-nourl fetch origin |
| 62 | ' |
| 63 | |
| 64 | test_expect_success 'create new commit on remote' ' |
| 65 | (cd server && |
| 66 | echo content >>file && |
| 67 | git commit -a -m two) |
| 68 | ' |
| 69 | |
| 70 | test_expect_success 'pulling from local repo' ' |
| 71 | (cd local && git pull) && |
| 72 | test_cmp server/file local/file |
| 73 | ' |
| 74 | |
| 75 | test_expect_success 'pushing to local repo' ' |
| 76 | (cd local && |
| 77 | echo content >>file && |
| 78 | git commit -a -m three && |
| 79 | git push) && |
| 80 | compare_refs local HEAD server HEAD |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'fetch new branch' ' |
| 84 | (cd server && |
| 85 | git reset --hard && |
| 86 | git checkout -b new && |
| 87 | echo content >>file && |
| 88 | git commit -a -m five |
| 89 | ) && |
| 90 | (cd local && |
| 91 | git fetch origin new |
| 92 | ) && |
| 93 | compare_refs server HEAD local FETCH_HEAD |
| 94 | ' |
| 95 | |
| 96 | test_expect_success 'fetch multiple branches' ' |
| 97 | (cd local && |
| 98 | git fetch |
| 99 | ) && |
| 100 | compare_refs server main local refs/remotes/origin/main && |
| 101 | compare_refs server new local refs/remotes/origin/new |
| 102 | ' |
| 103 | |
| 104 | test_expect_success 'push when remote has extra refs' ' |
| 105 | (cd local && |
| 106 | git reset --hard origin/main && |
| 107 | echo content >>file && |
| 108 | git commit -a -m six && |
| 109 | git push |
| 110 | ) && |
| 111 | compare_refs local main server main |
| 112 | ' |
| 113 | |
| 114 | test_expect_success 'push new branch by name' ' |
| 115 | (cd local && |
| 116 | git checkout -b new-name && |
| 117 | echo content >>file && |
| 118 | git commit -a -m seven && |
| 119 | git push origin new-name |
| 120 | ) && |
| 121 | compare_refs local HEAD server refs/heads/new-name |
| 122 | ' |
| 123 | |
| 124 | test_expect_success 'push new branch with old:new refspec' ' |
| 125 | (cd local && |
| 126 | git push origin new-name:new-refspec |
| 127 | ) && |
| 128 | compare_refs local HEAD server refs/heads/new-refspec |
| 129 | ' |
| 130 | |
| 131 | test_expect_success 'push new branch with HEAD:new refspec' ' |
| 132 | (cd local && |
| 133 | git checkout new-name && |
| 134 | git push origin HEAD:new-refspec-2 |
| 135 | ) && |
| 136 | compare_refs local HEAD server refs/heads/new-refspec-2 |
| 137 | ' |
| 138 | |
| 139 | test_expect_success 'push delete branch' ' |
| 140 | (cd local && |
| 141 | git push origin :new-name |
| 142 | ) && |
| 143 | test_must_fail git --git-dir="server/.git" \ |
| 144 | rev-parse --verify refs/heads/new-name |
| 145 | ' |
| 146 | |
| 147 | test_expect_success 'forced push' ' |
| 148 | (cd local && |
| 149 | git checkout -b force-test && |
| 150 | echo content >> file && |
| 151 | git commit -a -m eight && |
| 152 | git push origin force-test && |
| 153 | echo content >> file && |
| 154 | git commit -a --amend -m eight-modified && |
| 155 | git push --force origin force-test |
| 156 | ) && |
| 157 | compare_refs local refs/heads/force-test server refs/heads/force-test |
| 158 | ' |
| 159 | |
| 160 | test_expect_success 'cloning without refspec' ' |
| 161 | GIT_REMOTE_TESTGIT_NOREFSPEC=1 \ |
| 162 | git clone "testgit::${PWD}/server" local2 2>error && |
| 163 | test_grep "this remote helper should implement refspec capability" error && |
| 164 | compare_refs local2 HEAD server HEAD |
| 165 | ' |
| 166 | |
| 167 | test_expect_success 'pulling without refspecs' ' |
| 168 | (cd local2 && |
| 169 | git reset --hard && |
| 170 | GIT_REMOTE_TESTGIT_NOREFSPEC=1 git pull 2>../error) && |
| 171 | test_grep "this remote helper should implement refspec capability" error && |
| 172 | compare_refs local2 HEAD server HEAD |
| 173 | ' |
| 174 | |
| 175 | test_expect_success 'pushing without refspecs' ' |
| 176 | test_when_finished "(cd local2 && git reset --hard origin)" && |
| 177 | (cd local2 && |
| 178 | echo content >>file && |
| 179 | git commit -a -m ten && |
| 180 | GIT_REMOTE_TESTGIT_NOREFSPEC=1 && |
| 181 | export GIT_REMOTE_TESTGIT_NOREFSPEC && |
| 182 | test_must_fail git push 2>../error) && |
| 183 | test_grep "remote-helper doesn.t support push; refspec needed" error |
| 184 | ' |
| 185 | |
| 186 | test_expect_success 'pulling without marks' ' |
| 187 | (cd local2 && |
| 188 | GIT_REMOTE_TESTGIT_NO_MARKS=1 git pull) && |
| 189 | compare_refs local2 HEAD server HEAD |
| 190 | ' |
| 191 | |
| 192 | test_expect_failure 'pushing without marks' ' |
| 193 | test_when_finished "(cd local2 && git reset --hard origin)" && |
| 194 | (cd local2 && |
| 195 | echo content >>file && |
| 196 | git commit -a -m twelve && |
| 197 | GIT_REMOTE_TESTGIT_NO_MARKS=1 git push) && |
| 198 | compare_refs local2 HEAD server HEAD |
| 199 | ' |
| 200 | |
| 201 | test_expect_success 'push all with existing object' ' |
| 202 | (cd local && |
| 203 | git branch dup2 main && |
| 204 | git push origin --all |
| 205 | ) && |
| 206 | compare_refs local dup2 server dup2 |
| 207 | ' |
| 208 | |
| 209 | test_expect_success 'push ref with existing object' ' |
| 210 | (cd local && |
| 211 | git branch dup main && |
| 212 | git push origin dup |
| 213 | ) && |
| 214 | compare_refs local dup server dup |
| 215 | ' |
| 216 | |
| 217 | test_expect_success GPG 'push signed tag' ' |
| 218 | (cd local && |
| 219 | git checkout main && |
| 220 | git tag -s -m signed-tag signed-tag && |
| 221 | git push origin signed-tag |
| 222 | ) && |
| 223 | compare_refs local signed-tag^{} server signed-tag^{} && |
| 224 | compare_refs ! local signed-tag server signed-tag |
| 225 | ' |
| 226 | |
| 227 | test_expect_success GPG 'push signed tag with signed-tags capability' ' |
| 228 | (cd local && |
| 229 | git checkout main && |
| 230 | git tag -s -m signed-tag signed-tag-2 && |
| 231 | GIT_REMOTE_TESTGIT_SIGNED_TAGS=1 git push origin signed-tag-2 |
| 232 | ) && |
| 233 | compare_refs local signed-tag-2 server signed-tag-2 |
| 234 | ' |
| 235 | |
| 236 | test_expect_success 'push update refs' ' |
| 237 | (cd local && |
| 238 | git checkout -b update main && |
| 239 | echo update >>file && |
| 240 | git commit -a -m update && |
| 241 | git push origin update && |
| 242 | git rev-parse --verify remotes/origin/update >expect && |
| 243 | git rev-parse --verify testgit/origin/heads/update >actual && |
| 244 | test_cmp expect actual |
| 245 | ) |
| 246 | ' |
| 247 | |
| 248 | test_expect_success 'push update refs disabled by no-private-update' ' |
| 249 | (cd local && |
| 250 | echo more-update >>file && |
| 251 | git commit -a -m more-update && |
| 252 | git rev-parse --verify testgit/origin/heads/update >expect && |
| 253 | GIT_REMOTE_TESTGIT_NO_PRIVATE_UPDATE=t git push origin update && |
| 254 | git rev-parse --verify testgit/origin/heads/update >actual && |
| 255 | test_cmp expect actual |
| 256 | ) |
| 257 | ' |
| 258 | |
| 259 | test_expect_success 'push update refs failure' ' |
| 260 | (cd local && |
| 261 | git checkout update && |
| 262 | echo "update fail" >>file && |
| 263 | git commit -a -m "update fail" && |
| 264 | git rev-parse --verify testgit/origin/heads/update >expect && |
| 265 | test_expect_code 1 env GIT_REMOTE_TESTGIT_FAILURE="non-fast forward" \ |
| 266 | git push origin update && |
| 267 | git rev-parse --verify testgit/origin/heads/update >actual && |
| 268 | test_cmp expect actual |
| 269 | ) |
| 270 | ' |
| 271 | |
| 272 | clean_mark () { |
| 273 | cut -f 2 -d ' ' "$1" | |
| 274 | git cat-file --batch-check | |
| 275 | grep commit | |
| 276 | sort >$(basename "$1") |
| 277 | } |
| 278 | |
| 279 | test_expect_success 'proper failure checks for fetching' ' |
| 280 | (cd local && |
| 281 | test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git fetch 2>error && |
| 282 | test_grep -q "error while running fast-import" error |
| 283 | ) |
| 284 | ' |
| 285 | |
| 286 | test_expect_success 'proper failure checks for pushing' ' |
| 287 | test_when_finished "rm -rf local/git.marks local/testgit.marks" && |
| 288 | (cd local && |
| 289 | git checkout -b crash main && |
| 290 | echo crash >>file && |
| 291 | git commit -a -m crash && |
| 292 | test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git push --all && |
| 293 | clean_mark ".git/testgit/origin/git.marks" && |
| 294 | clean_mark ".git/testgit/origin/testgit.marks" && |
| 295 | test_cmp git.marks testgit.marks |
| 296 | ) |
| 297 | ' |
| 298 | |
| 299 | test_expect_success 'push messages' ' |
| 300 | (cd local && |
| 301 | git checkout -b new_branch main && |
| 302 | echo new >>file && |
| 303 | git commit -a -m new && |
| 304 | git push origin new_branch && |
| 305 | git fetch origin && |
| 306 | echo new >>file && |
| 307 | git commit -a -m new && |
| 308 | git push origin new_branch 2> msg && |
| 309 | ! grep "\[new branch\]" msg |
| 310 | ) |
| 311 | ' |
| 312 | |
| 313 | test_expect_success 'fetch HEAD' ' |
| 314 | (cd server && |
| 315 | git checkout main && |
| 316 | echo more >>file && |
| 317 | git commit -a -m more |
| 318 | ) && |
| 319 | (cd local && |
| 320 | git fetch origin HEAD |
| 321 | ) && |
| 322 | compare_refs server HEAD local FETCH_HEAD |
| 323 | ' |
| 324 | |
| 325 | test_expect_success 'fetch url' ' |
| 326 | (cd server && |
| 327 | git checkout main && |
| 328 | echo more >>file && |
| 329 | git commit -a -m more |
| 330 | ) && |
| 331 | (cd local && |
| 332 | git fetch "testgit::${PWD}/../server" |
| 333 | ) && |
| 334 | compare_refs server HEAD local FETCH_HEAD |
| 335 | ' |
| 336 | |
| 337 | test_expect_success 'fetch tag' ' |
| 338 | (cd server && |
| 339 | git tag v1.0 |
| 340 | ) && |
| 341 | (cd local && |
| 342 | git fetch |
| 343 | ) && |
| 344 | compare_refs local v1.0 server v1.0 |
| 345 | ' |
| 346 | |
| 347 | test_expect_success 'totally broken helper reports failure message' ' |
| 348 | write_script git-remote-broken <<-\EOF && |
| 349 | read cap_cmd |
| 350 | exit 1 |
| 351 | EOF |
| 352 | test_must_fail \ |
| 353 | env PATH="$PWD:$PATH" \ |
| 354 | git clone broken://example.com/foo.git 2>stderr && |
| 355 | grep aborted stderr |
| 356 | ' |
| 357 | |
| 358 | test_done |