| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='git p4 tests' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./lib-git-p4.sh |
| 9 | |
| 10 | test_expect_success 'start p4d' ' |
| 11 | start_p4d |
| 12 | ' |
| 13 | |
| 14 | test_expect_success 'add p4 files' ' |
| 15 | ( |
| 16 | cd "$cli" && |
| 17 | echo file1 >file1 && |
| 18 | p4 add file1 && |
| 19 | p4 submit -d "file1" && |
| 20 | echo file2 >file2 && |
| 21 | p4 add file2 && |
| 22 | p4 submit -d "file2" |
| 23 | ) |
| 24 | ' |
| 25 | |
| 26 | test_expect_success 'basic git p4 clone' ' |
| 27 | git p4 clone --dest="$git" //depot && |
| 28 | test_when_finished cleanup_git && |
| 29 | ( |
| 30 | cd "$git" && |
| 31 | git log --oneline >lines && |
| 32 | test_line_count = 1 lines |
| 33 | ) |
| 34 | ' |
| 35 | |
| 36 | test_expect_success 'depot typo error' ' |
| 37 | test_must_fail git p4 clone --dest="$git" /depot 2>errs && |
| 38 | grep "Depot paths must start with" errs |
| 39 | ' |
| 40 | |
| 41 | test_expect_success 'git p4 clone @all' ' |
| 42 | git p4 clone --dest="$git" //depot@all && |
| 43 | test_when_finished cleanup_git && |
| 44 | ( |
| 45 | cd "$git" && |
| 46 | git log --oneline >lines && |
| 47 | test_line_count = 2 lines |
| 48 | ) |
| 49 | ' |
| 50 | |
| 51 | test_expect_success 'git p4 sync uninitialized repo' ' |
| 52 | test_create_repo "$git" && |
| 53 | test_when_finished cleanup_git && |
| 54 | ( |
| 55 | cd "$git" && |
| 56 | test_must_fail git p4 sync 2>errs && |
| 57 | test_grep "Perhaps you never did" errs |
| 58 | ) |
| 59 | ' |
| 60 | |
| 61 | # |
| 62 | # Create a git repo by hand. Add a commit so that HEAD is valid. |
| 63 | # Test imports a new p4 repository into a new git branch. |
| 64 | # |
| 65 | test_expect_success 'git p4 sync new branch' ' |
| 66 | test_create_repo "$git" && |
| 67 | test_when_finished cleanup_git && |
| 68 | ( |
| 69 | cd "$git" && |
| 70 | test_commit head && |
| 71 | git p4 sync --branch=refs/remotes/p4/depot //depot@all && |
| 72 | git log --oneline p4/depot >lines && |
| 73 | test_line_count = 2 lines |
| 74 | ) |
| 75 | ' |
| 76 | |
| 77 | # |
| 78 | # Setup as before, and then explicitly sync imported branch, using a |
| 79 | # different ref format. |
| 80 | # |
| 81 | test_expect_success 'git p4 sync existing branch without changes' ' |
| 82 | test_create_repo "$git" && |
| 83 | test_when_finished cleanup_git && |
| 84 | ( |
| 85 | cd "$git" && |
| 86 | test_commit head && |
| 87 | git p4 sync --branch=depot //depot@all && |
| 88 | git p4 sync --branch=refs/remotes/p4/depot >out && |
| 89 | test_grep "No changes to import!" out |
| 90 | ) |
| 91 | ' |
| 92 | |
| 93 | # |
| 94 | # Same as before, relative branch name. |
| 95 | # |
| 96 | test_expect_success 'git p4 sync existing branch with relative name' ' |
| 97 | test_create_repo "$git" && |
| 98 | test_when_finished cleanup_git && |
| 99 | ( |
| 100 | cd "$git" && |
| 101 | test_commit head && |
| 102 | git p4 sync --branch=branch1 //depot@all && |
| 103 | git p4 sync --branch=p4/branch1 >out && |
| 104 | test_grep "No changes to import!" out |
| 105 | ) |
| 106 | ' |
| 107 | |
| 108 | # |
| 109 | # Same as before, with a nested branch path, referenced different ways. |
| 110 | # |
| 111 | test_expect_success 'git p4 sync existing branch with nested path' ' |
| 112 | test_create_repo "$git" && |
| 113 | test_when_finished cleanup_git && |
| 114 | ( |
| 115 | cd "$git" && |
| 116 | test_commit head && |
| 117 | git p4 sync --branch=p4/some/path //depot@all && |
| 118 | git p4 sync --branch=some/path >out && |
| 119 | test_grep "No changes to import!" out |
| 120 | ) |
| 121 | ' |
| 122 | |
| 123 | # |
| 124 | # Same as before, with a full ref path outside the p4/* namespace. |
| 125 | # |
| 126 | test_expect_success 'git p4 sync branch explicit ref without p4 in path' ' |
| 127 | test_create_repo "$git" && |
| 128 | test_when_finished cleanup_git && |
| 129 | ( |
| 130 | cd "$git" && |
| 131 | test_commit head && |
| 132 | git p4 sync --branch=refs/remotes/someremote/depot //depot@all && |
| 133 | git p4 sync --branch=refs/remotes/someremote/depot >out && |
| 134 | test_grep "No changes to import!" out |
| 135 | ) |
| 136 | ' |
| 137 | |
| 138 | test_expect_success 'git p4 sync nonexistent ref' ' |
| 139 | test_create_repo "$git" && |
| 140 | test_when_finished cleanup_git && |
| 141 | ( |
| 142 | cd "$git" && |
| 143 | test_commit head && |
| 144 | git p4 sync --branch=depot //depot@all && |
| 145 | test_must_fail git p4 sync --branch=depot2 2>errs && |
| 146 | test_grep "Perhaps you never did" errs |
| 147 | ) |
| 148 | ' |
| 149 | |
| 150 | test_expect_success 'git p4 sync existing non-p4-imported ref' ' |
| 151 | test_create_repo "$git" && |
| 152 | test_when_finished cleanup_git && |
| 153 | ( |
| 154 | cd "$git" && |
| 155 | test_commit head && |
| 156 | git p4 sync --branch=depot //depot@all && |
| 157 | test_must_fail git p4 sync --branch=refs/heads/master 2>errs && |
| 158 | test_grep "Perhaps you never did" errs |
| 159 | ) |
| 160 | ' |
| 161 | |
| 162 | test_expect_success 'clone two dirs' ' |
| 163 | ( |
| 164 | cd "$cli" && |
| 165 | mkdir sub1 sub2 && |
| 166 | echo sub1/f1 >sub1/f1 && |
| 167 | echo sub2/f2 >sub2/f2 && |
| 168 | p4 add sub1/f1 && |
| 169 | p4 submit -d "sub1/f1" && |
| 170 | p4 add sub2/f2 && |
| 171 | p4 submit -d "sub2/f2" |
| 172 | ) && |
| 173 | git p4 clone --dest="$git" //depot/sub1 //depot/sub2 && |
| 174 | test_when_finished cleanup_git && |
| 175 | ( |
| 176 | cd "$git" && |
| 177 | git ls-files >lines && |
| 178 | test_line_count = 2 lines && |
| 179 | git log --oneline p4/master >lines && |
| 180 | test_line_count = 1 lines |
| 181 | ) |
| 182 | ' |
| 183 | |
| 184 | test_expect_success 'clone two dirs, @all' ' |
| 185 | ( |
| 186 | cd "$cli" && |
| 187 | echo sub1/f3 >sub1/f3 && |
| 188 | p4 add sub1/f3 && |
| 189 | p4 submit -d "sub1/f3" |
| 190 | ) && |
| 191 | git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all && |
| 192 | test_when_finished cleanup_git && |
| 193 | ( |
| 194 | cd "$git" && |
| 195 | git ls-files >lines && |
| 196 | test_line_count = 3 lines && |
| 197 | git log --oneline p4/master >lines && |
| 198 | test_line_count = 3 lines |
| 199 | ) |
| 200 | ' |
| 201 | |
| 202 | test_expect_success 'clone two dirs, @all, conflicting files' ' |
| 203 | ( |
| 204 | cd "$cli" && |
| 205 | echo sub2/f3 >sub2/f3 && |
| 206 | p4 add sub2/f3 && |
| 207 | p4 submit -d "sub2/f3" |
| 208 | ) && |
| 209 | git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all && |
| 210 | test_when_finished cleanup_git && |
| 211 | ( |
| 212 | cd "$git" && |
| 213 | git ls-files >lines && |
| 214 | test_line_count = 3 lines && |
| 215 | git log --oneline p4/master >lines && |
| 216 | test_line_count = 4 lines && |
| 217 | echo sub2/f3 >expected && |
| 218 | test_cmp expected f3 |
| 219 | ) |
| 220 | ' |
| 221 | |
| 222 | test_expect_success 'clone two dirs, each edited by submit, single git commit' ' |
| 223 | ( |
| 224 | cd "$cli" && |
| 225 | echo sub1/f4 >sub1/f4 && |
| 226 | p4 add sub1/f4 && |
| 227 | echo sub2/f4 >sub2/f4 && |
| 228 | p4 add sub2/f4 && |
| 229 | p4 submit -d "sub1/f4 and sub2/f4" |
| 230 | ) && |
| 231 | git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all && |
| 232 | test_when_finished cleanup_git && |
| 233 | ( |
| 234 | cd "$git" && |
| 235 | git ls-files >lines && |
| 236 | test_line_count = 4 lines && |
| 237 | git log --oneline p4/master >lines && |
| 238 | test_line_count = 5 lines |
| 239 | ) |
| 240 | ' |
| 241 | |
| 242 | revision_ranges="2000/01/01,#head \ |
| 243 | 1,2080/01/01 \ |
| 244 | 2000/01/01,2080/01/01 \ |
| 245 | 2000/01/01,1000 \ |
| 246 | 1,1000" |
| 247 | |
| 248 | test_expect_success 'clone using non-numeric revision ranges' ' |
| 249 | test_when_finished cleanup_git && |
| 250 | for r in $revision_ranges |
| 251 | do |
| 252 | rm -fr "$git" && |
| 253 | test ! -d "$git" && |
| 254 | git p4 clone --dest="$git" //depot@$r && |
| 255 | ( |
| 256 | cd "$git" && |
| 257 | git ls-files >lines && |
| 258 | test_line_count = 8 lines |
| 259 | ) || return 1 |
| 260 | done |
| 261 | ' |
| 262 | |
| 263 | test_expect_success 'clone with date range, excluding some changes' ' |
| 264 | test_when_finished cleanup_git && |
| 265 | before=$(date +%Y/%m/%d:%H:%M:%S) && |
| 266 | sleep 2 && |
| 267 | ( |
| 268 | cd "$cli" && |
| 269 | :>date_range_test && |
| 270 | p4 add date_range_test && |
| 271 | p4 submit -d "Adding file" |
| 272 | ) && |
| 273 | git p4 clone --dest="$git" //depot@1,$before && |
| 274 | ( |
| 275 | cd "$git" && |
| 276 | test_path_is_missing date_range_test |
| 277 | ) |
| 278 | ' |
| 279 | |
| 280 | test_expect_success 'exit when p4 fails to produce marshaled output' ' |
| 281 | mkdir badp4dir && |
| 282 | test_when_finished "rm badp4dir/p4 && rmdir badp4dir" && |
| 283 | cat >badp4dir/p4 <<-EOF && |
| 284 | #!$SHELL_PATH |
| 285 | exit 1 |
| 286 | EOF |
| 287 | chmod 755 badp4dir/p4 && |
| 288 | ( |
| 289 | PATH="$TRASH_DIRECTORY/badp4dir:$PATH" && |
| 290 | export PATH && |
| 291 | test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1 |
| 292 | ) && |
| 293 | test_grep ! Traceback errs |
| 294 | ' |
| 295 | |
| 296 | # Hide a file from p4d, make sure we catch its complaint. This won't fail in |
| 297 | # p4 changes, files, or describe; just in p4 print. If P4CLIENT is unset, the |
| 298 | # message will include "Librarian checkout". |
| 299 | test_expect_success 'exit gracefully for p4 server errors' ' |
| 300 | # Note that newer Perforce versions started to store files |
| 301 | # compressed in directories. The case statement handles both |
| 302 | # old and new layout. |
| 303 | case "$(echo "$db"/depot/file1*)" in |
| 304 | *,v) |
| 305 | test_when_finished "mv \"$db\"/depot/file1,v,hidden \"$db\"/depot/file1,v" && |
| 306 | mv "$db"/depot/file1,v "$db"/depot/file1,v,hidden;; |
| 307 | *,d) |
| 308 | path="$(echo "$db"/depot/file1,d/*.gz)" && |
| 309 | test_when_finished "mv \"$path\",hidden \"$path\"" && |
| 310 | mv "$path" "$path",hidden;; |
| 311 | *) |
| 312 | BUG "unhandled p4d layout";; |
| 313 | esac && |
| 314 | test_when_finished cleanup_git && |
| 315 | test_expect_code 1 git p4 clone --dest="$git" //depot@1 >out 2>err && |
| 316 | test_grep "Error from p4 print" err |
| 317 | ' |
| 318 | |
| 319 | test_expect_success 'clone --bare should make a bare repository' ' |
| 320 | rm -rf "$git" && |
| 321 | git p4 clone --dest="$git" --bare //depot && |
| 322 | test_when_finished cleanup_git && |
| 323 | ( |
| 324 | cd "$git" && |
| 325 | test_path_is_missing .git && |
| 326 | git config --get --bool core.bare true && |
| 327 | git rev-parse --verify refs/remotes/p4/master && |
| 328 | git rev-parse --verify refs/remotes/p4/HEAD && |
| 329 | git rev-parse --verify refs/heads/main && |
| 330 | git rev-parse --verify HEAD |
| 331 | ) |
| 332 | ' |
| 333 | |
| 334 | # Sleep a bit so that the top-most p4 change did not happen "now". Then |
| 335 | # import the repo and make sure that the initial import has the same time |
| 336 | # as the top-most change. |
| 337 | test_expect_success 'initial import time from top change time' ' |
| 338 | p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) && |
| 339 | p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) && |
| 340 | sleep 3 && |
| 341 | git p4 clone --dest="$git" //depot && |
| 342 | test_when_finished cleanup_git && |
| 343 | ( |
| 344 | cd "$git" && |
| 345 | gittime=$(git show -s --pretty=format:%at HEAD) && |
| 346 | echo $p4time $gittime && |
| 347 | test $p4time = $gittime |
| 348 | ) |
| 349 | ' |
| 350 | |
| 351 | test_expect_success 'unresolvable host in P4PORT should display error' ' |
| 352 | test_when_finished cleanup_git && |
| 353 | git p4 clone --dest="$git" //depot && |
| 354 | ( |
| 355 | cd "$git" && |
| 356 | P4PORT=nosuchhost:65537 && |
| 357 | export P4PORT && |
| 358 | test_expect_code 1 git p4 sync >out 2>err && |
| 359 | grep "connect to nosuchhost" err |
| 360 | ) |
| 361 | ' |
| 362 | |
| 363 | # Test following scenarios: |
| 364 | # - Without ".git/hooks/p4-pre-submit" , submit should continue |
| 365 | # - With the hook returning 0, submit should continue |
| 366 | # - With the hook returning 1, submit should abort |
| 367 | test_expect_success 'run hook p4-pre-submit before submit' ' |
| 368 | test_when_finished cleanup_git && |
| 369 | git p4 clone --dest="$git" //depot && |
| 370 | ( |
| 371 | cd "$git" && |
| 372 | echo "hello world" >hello.txt && |
| 373 | git add hello.txt && |
| 374 | git commit -m "add hello.txt" && |
| 375 | git config git-p4.skipSubmitEdit true && |
| 376 | git p4 submit --dry-run >out && |
| 377 | grep "Would apply" out |
| 378 | ) && |
| 379 | test_hook -C "$git" p4-pre-submit <<-\EOF && |
| 380 | exit 0 |
| 381 | EOF |
| 382 | ( |
| 383 | cd "$git" && |
| 384 | git p4 submit --dry-run >out && |
| 385 | grep "Would apply" out |
| 386 | ) && |
| 387 | test_hook -C "$git" --clobber p4-pre-submit <<-\EOF && |
| 388 | exit 1 |
| 389 | EOF |
| 390 | ( |
| 391 | cd "$git" && |
| 392 | test_must_fail git p4 submit --dry-run >errs 2>&1 && |
| 393 | ! grep "Would apply" errs |
| 394 | ) |
| 395 | ' |
| 396 | |
| 397 | test_expect_success 'submit from detached head' ' |
| 398 | test_when_finished cleanup_git && |
| 399 | git p4 clone --dest="$git" //depot && |
| 400 | ( |
| 401 | cd "$git" && |
| 402 | git checkout p4/master && |
| 403 | >detached_head_test && |
| 404 | git add detached_head_test && |
| 405 | git commit -m "add detached_head" && |
| 406 | git config git-p4.skipSubmitEdit true && |
| 407 | git p4 submit && |
| 408 | git p4 rebase && |
| 409 | git log p4/master | grep detached_head |
| 410 | ) |
| 411 | ' |
| 412 | |
| 413 | test_expect_success 'submit from worktree' ' |
| 414 | test_when_finished cleanup_git && |
| 415 | git p4 clone --dest="$git" //depot && |
| 416 | ( |
| 417 | cd "$git" && |
| 418 | git worktree add ../worktree-test |
| 419 | ) && |
| 420 | ( |
| 421 | cd "$git/../worktree-test" && |
| 422 | test_commit "worktree-commit" && |
| 423 | git config git-p4.skipSubmitEdit true && |
| 424 | git p4 submit |
| 425 | ) && |
| 426 | ( |
| 427 | cd "$cli" && |
| 428 | p4 sync && |
| 429 | test_path_is_file worktree-commit.t |
| 430 | ) |
| 431 | ' |
| 432 | |
| 433 | test_done |