7401c215626fa95fa36b35f8ee2e247c8439c5ca
   1#!/bin/sh
   2
   3test_description='git p4 tests'
   4
   5. ./lib-git-p4.sh
   6
   7test_expect_success 'start p4d' '
   8        start_p4d
   9'
  10
  11test_expect_success 'add p4 files' '
  12        (
  13                cd "$cli" &&
  14                echo file1 >file1 &&
  15                p4 add file1 &&
  16                p4 submit -d "file1" &&
  17                echo file2 >file2 &&
  18                p4 add file2 &&
  19                p4 submit -d "file2"
  20        )
  21'
  22
  23test_expect_success 'basic git p4 clone' '
  24        git p4 clone --dest="$git" //depot &&
  25        test_when_finished cleanup_git &&
  26        (
  27                cd "$git" &&
  28                git log --oneline >lines &&
  29                test_line_count = 1 lines
  30        )
  31'
  32
  33test_expect_success 'git p4 clone @all' '
  34        git p4 clone --dest="$git" //depot@all &&
  35        test_when_finished cleanup_git &&
  36        (
  37                cd "$git" &&
  38                git log --oneline >lines &&
  39                test_line_count = 2 lines
  40        )
  41'
  42
  43test_expect_success 'git p4 sync uninitialized repo' '
  44        test_create_repo "$git" &&
  45        test_when_finished cleanup_git &&
  46        (
  47                cd "$git" &&
  48                test_must_fail git p4 sync
  49        )
  50'
  51
  52#
  53# Create a git repo by hand.  Add a commit so that HEAD is valid.
  54# Test imports a new p4 repository into a new git branch.
  55#
  56test_expect_success 'git p4 sync new branch' '
  57        test_create_repo "$git" &&
  58        test_when_finished cleanup_git &&
  59        (
  60                cd "$git" &&
  61                test_commit head &&
  62                git p4 sync --branch=refs/remotes/p4/depot //depot@all &&
  63                git log --oneline p4/depot >lines &&
  64                test_line_count = 2 lines
  65        )
  66'
  67
  68test_expect_success 'clone two dirs' '
  69        (
  70                cd "$cli" &&
  71                mkdir sub1 sub2 &&
  72                echo sub1/f1 >sub1/f1 &&
  73                echo sub2/f2 >sub2/f2 &&
  74                p4 add sub1/f1 &&
  75                p4 submit -d "sub1/f1" &&
  76                p4 add sub2/f2 &&
  77                p4 submit -d "sub2/f2"
  78        ) &&
  79        git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
  80        test_when_finished cleanup_git &&
  81        (
  82                cd "$git" &&
  83                git ls-files >lines &&
  84                test_line_count = 2 lines &&
  85                git log --oneline p4/master >lines &&
  86                test_line_count = 1 lines
  87        )
  88'
  89
  90test_expect_success 'clone two dirs, @all' '
  91        (
  92                cd "$cli" &&
  93                echo sub1/f3 >sub1/f3 &&
  94                p4 add sub1/f3 &&
  95                p4 submit -d "sub1/f3"
  96        ) &&
  97        git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
  98        test_when_finished cleanup_git &&
  99        (
 100                cd "$git" &&
 101                git ls-files >lines &&
 102                test_line_count = 3 lines &&
 103                git log --oneline p4/master >lines &&
 104                test_line_count = 3 lines
 105        )
 106'
 107
 108test_expect_success 'clone two dirs, @all, conflicting files' '
 109        (
 110                cd "$cli" &&
 111                echo sub2/f3 >sub2/f3 &&
 112                p4 add sub2/f3 &&
 113                p4 submit -d "sub2/f3"
 114        ) &&
 115        git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
 116        test_when_finished cleanup_git &&
 117        (
 118                cd "$git" &&
 119                git ls-files >lines &&
 120                test_line_count = 3 lines &&
 121                git log --oneline p4/master >lines &&
 122                test_line_count = 4 lines &&
 123                echo sub2/f3 >expected &&
 124                test_cmp expected f3
 125        )
 126'
 127
 128test_expect_success 'exit when p4 fails to produce marshaled output' '
 129        mkdir badp4dir &&
 130        test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
 131        cat >badp4dir/p4 <<-EOF &&
 132        #!$SHELL_PATH
 133        exit 1
 134        EOF
 135        chmod 755 badp4dir/p4 &&
 136        PATH="$TRASH_DIRECTORY/badp4dir:$PATH" git p4 clone --dest="$git" //depot >errs 2>&1 ; retval=$? &&
 137        test $retval -eq 1 &&
 138        test_must_fail grep -q Traceback errs
 139'
 140
 141test_expect_success 'add p4 files with wildcards in the names' '
 142        (
 143                cd "$cli" &&
 144                echo file-wild-hash >file-wild#hash &&
 145                echo file-wild-star >file-wild\*star &&
 146                echo file-wild-at >file-wild@at &&
 147                echo file-wild-percent >file-wild%percent &&
 148                p4 add -f file-wild* &&
 149                p4 submit -d "file wildcards"
 150        )
 151'
 152
 153test_expect_success 'wildcard files git p4 clone' '
 154        git p4 clone --dest="$git" //depot &&
 155        test_when_finished cleanup_git &&
 156        (
 157                cd "$git" &&
 158                test -f file-wild#hash &&
 159                test -f file-wild\*star &&
 160                test -f file-wild@at &&
 161                test -f file-wild%percent
 162        )
 163'
 164
 165test_expect_success 'wildcard files submit back to p4, add' '
 166        test_when_finished cleanup_git &&
 167        git p4 clone --dest="$git" //depot &&
 168        (
 169                cd "$git" &&
 170                echo git-wild-hash >git-wild#hash &&
 171                echo git-wild-star >git-wild\*star &&
 172                echo git-wild-at >git-wild@at &&
 173                echo git-wild-percent >git-wild%percent &&
 174                git add git-wild* &&
 175                git commit -m "add some wildcard filenames" &&
 176                git config git-p4.skipSubmitEdit true &&
 177                git p4 submit
 178        ) &&
 179        (
 180                cd "$cli" &&
 181                test_path_is_file git-wild#hash &&
 182                test_path_is_file git-wild\*star &&
 183                test_path_is_file git-wild@at &&
 184                test_path_is_file git-wild%percent
 185        )
 186'
 187
 188test_expect_success 'wildcard files submit back to p4, modify' '
 189        test_when_finished cleanup_git &&
 190        git p4 clone --dest="$git" //depot &&
 191        (
 192                cd "$git" &&
 193                echo new-line >>git-wild#hash &&
 194                echo new-line >>git-wild\*star &&
 195                echo new-line >>git-wild@at &&
 196                echo new-line >>git-wild%percent &&
 197                git add git-wild* &&
 198                git commit -m "modify the wildcard files" &&
 199                git config git-p4.skipSubmitEdit true &&
 200                git p4 submit
 201        ) &&
 202        (
 203                cd "$cli" &&
 204                test_line_count = 2 git-wild#hash &&
 205                test_line_count = 2 git-wild\*star &&
 206                test_line_count = 2 git-wild@at &&
 207                test_line_count = 2 git-wild%percent
 208        )
 209'
 210
 211test_expect_success 'wildcard files submit back to p4, copy' '
 212        test_when_finished cleanup_git &&
 213        git p4 clone --dest="$git" //depot &&
 214        (
 215                cd "$git" &&
 216                cp file2 git-wild-cp#hash &&
 217                git add git-wild-cp#hash &&
 218                cp git-wild\*star file-wild-3 &&
 219                git add file-wild-3 &&
 220                git commit -m "wildcard copies" &&
 221                git config git-p4.detectCopies true &&
 222                git config git-p4.detectCopiesHarder true &&
 223                git config git-p4.skipSubmitEdit true &&
 224                git p4 submit
 225        ) &&
 226        (
 227                cd "$cli" &&
 228                test_path_is_file git-wild-cp#hash &&
 229                test_path_is_file file-wild-3
 230        )
 231'
 232
 233test_expect_success 'wildcard files submit back to p4, rename' '
 234        test_when_finished cleanup_git &&
 235        git p4 clone --dest="$git" //depot &&
 236        (
 237                cd "$git" &&
 238                git mv git-wild@at file-wild-4 &&
 239                git mv file-wild-3 git-wild-cp%percent &&
 240                git commit -m "wildcard renames" &&
 241                git config git-p4.detectRenames true &&
 242                git config git-p4.skipSubmitEdit true &&
 243                git p4 submit
 244        ) &&
 245        (
 246                cd "$cli" &&
 247                test_path_is_missing git-wild@at &&
 248                test_path_is_file git-wild-cp%percent
 249        )
 250'
 251
 252test_expect_success 'wildcard files submit back to p4, delete' '
 253        test_when_finished cleanup_git &&
 254        git p4 clone --dest="$git" //depot &&
 255        (
 256                cd "$git" &&
 257                git rm git-wild* &&
 258                git commit -m "delete the wildcard files" &&
 259                git config git-p4.skipSubmitEdit true &&
 260                git p4 submit
 261        ) &&
 262        (
 263                cd "$cli" &&
 264                test_path_is_missing git-wild#hash &&
 265                test_path_is_missing git-wild\*star &&
 266                test_path_is_missing git-wild@at &&
 267                test_path_is_missing git-wild%percent
 268        )
 269'
 270
 271test_expect_success 'clone bare' '
 272        git p4 clone --dest="$git" --bare //depot &&
 273        test_when_finished cleanup_git &&
 274        (
 275                cd "$git" &&
 276                test ! -d .git &&
 277                bare=`git config --get core.bare` &&
 278                test "$bare" = true
 279        )
 280'
 281
 282p4_add_user() {
 283        name=$1 fullname=$2 &&
 284        p4 user -f -i <<-EOF &&
 285        User: $name
 286        Email: $name@localhost
 287        FullName: $fullname
 288        EOF
 289        p4 passwd -P secret $name
 290}
 291
 292p4_grant_admin() {
 293        name=$1 &&
 294        {
 295                p4 protect -o &&
 296                echo "    admin user $name * //depot/..."
 297        } | p4 protect -i
 298}
 299
 300p4_check_commit_author() {
 301        file=$1 user=$2 &&
 302        p4 changes -m 1 //depot/$file | grep -q $user
 303}
 304
 305make_change_by_user() {
 306        file=$1 name=$2 email=$3 &&
 307        echo "username: a change by $name" >>"$file" &&
 308        git add "$file" &&
 309        git commit --author "$name <$email>" -m "a change by $name"
 310}
 311
 312# Test username support, submitting as user 'alice'
 313test_expect_success 'preserve users' '
 314        p4_add_user alice Alice &&
 315        p4_add_user bob Bob &&
 316        p4_grant_admin alice &&
 317        git p4 clone --dest="$git" //depot &&
 318        test_when_finished cleanup_git &&
 319        (
 320                cd "$git" &&
 321                echo "username: a change by alice" >>file1 &&
 322                echo "username: a change by bob" >>file2 &&
 323                git commit --author "Alice <alice@localhost>" -m "a change by alice" file1 &&
 324                git commit --author "Bob <bob@localhost>" -m "a change by bob" file2 &&
 325                git config git-p4.skipSubmitEditCheck true &&
 326                P4EDITOR=touch P4USER=alice P4PASSWD=secret git p4 commit --preserve-user &&
 327                p4_check_commit_author file1 alice &&
 328                p4_check_commit_author file2 bob
 329        )
 330'
 331
 332# Test username support, submitting as bob, who lacks admin rights. Should
 333# not submit change to p4 (git diff should show deltas).
 334test_expect_success 'refuse to preserve users without perms' '
 335        git p4 clone --dest="$git" //depot &&
 336        test_when_finished cleanup_git &&
 337        (
 338                cd "$git" &&
 339                git config git-p4.skipSubmitEditCheck true &&
 340                echo "username-noperms: a change by alice" >>file1 &&
 341                git commit --author "Alice <alice@localhost>" -m "perms: a change by alice" file1 &&
 342                P4EDITOR=touch P4USER=bob P4PASSWD=secret &&
 343                export P4EDITOR P4USER P4PASSWD &&
 344                test_must_fail git p4 commit --preserve-user &&
 345                ! git diff --exit-code HEAD..p4/master
 346        )
 347'
 348
 349# What happens with unknown author? Without allowMissingP4Users it should fail.
 350test_expect_success 'preserve user where author is unknown to p4' '
 351        git p4 clone --dest="$git" //depot &&
 352        test_when_finished cleanup_git &&
 353        (
 354                cd "$git" &&
 355                git config git-p4.skipSubmitEditCheck true &&
 356                echo "username-bob: a change by bob" >>file1 &&
 357                git commit --author "Bob <bob@localhost>" -m "preserve: a change by bob" file1 &&
 358                echo "username-unknown: a change by charlie" >>file1 &&
 359                git commit --author "Charlie <charlie@localhost>" -m "preserve: a change by charlie" file1 &&
 360                P4EDITOR=touch P4USER=alice P4PASSWD=secret &&
 361                export P4EDITOR P4USER P4PASSWD &&
 362                test_must_fail git p4 commit --preserve-user &&
 363                ! git diff --exit-code HEAD..p4/master &&
 364
 365                echo "$0: repeat with allowMissingP4Users enabled" &&
 366                git config git-p4.allowMissingP4Users true &&
 367                git config git-p4.preserveUser true &&
 368                git p4 commit &&
 369                git diff --exit-code HEAD..p4/master &&
 370                p4_check_commit_author file1 alice
 371        )
 372'
 373
 374# If we're *not* using --preserve-user, git p4 should warn if we're submitting
 375# changes that are not all ours.
 376# Test: user in p4 and user unknown to p4.
 377# Test: warning disabled and user is the same.
 378test_expect_success 'not preserving user with mixed authorship' '
 379        git p4 clone --dest="$git" //depot &&
 380        test_when_finished cleanup_git &&
 381        (
 382                cd "$git" &&
 383                git config git-p4.skipSubmitEditCheck true &&
 384                p4_add_user derek Derek &&
 385
 386                make_change_by_user usernamefile3 Derek derek@localhost &&
 387                P4EDITOR=cat P4USER=alice P4PASSWD=secret &&
 388                export P4EDITOR P4USER P4PASSWD &&
 389                git p4 commit |\
 390                grep "git author derek@localhost does not match" &&
 391
 392                make_change_by_user usernamefile3 Charlie charlie@localhost &&
 393                git p4 commit |\
 394                grep "git author charlie@localhost does not match" &&
 395
 396                make_change_by_user usernamefile3 alice alice@localhost &&
 397                git p4 commit |\
 398                test_must_fail grep "git author.*does not match" &&
 399
 400                git config git-p4.skipUserNameCheck true &&
 401                make_change_by_user usernamefile3 Charlie charlie@localhost &&
 402                git p4 commit |\
 403                test_must_fail grep "git author.*does not match" &&
 404
 405                p4_check_commit_author usernamefile3 alice
 406        )
 407'
 408
 409marshal_dump() {
 410        what=$1
 411        "$PYTHON_PATH" -c 'import marshal, sys; d = marshal.load(sys.stdin); print d["'$what'"]'
 412}
 413
 414# Sleep a bit so that the top-most p4 change did not happen "now".  Then
 415# import the repo and make sure that the initial import has the same time
 416# as the top-most change.
 417test_expect_success 'initial import time from top change time' '
 418        p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) &&
 419        p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) &&
 420        sleep 3 &&
 421        git p4 clone --dest="$git" //depot &&
 422        test_when_finished cleanup_git &&
 423        (
 424                cd "$git" &&
 425                gittime=$(git show -s --raw --pretty=format:%at HEAD) &&
 426                echo $p4time $gittime &&
 427                test $p4time = $gittime
 428        )
 429'
 430
 431# Rename a file and confirm that rename is not detected in P4.
 432# Rename the new file again with detectRenames option enabled and confirm that
 433# this is detected in P4.
 434# Rename the new file again adding an extra line, configure a big threshold in
 435# detectRenames and confirm that rename is not detected in P4.
 436# Repeat, this time with a smaller threshold and confirm that the rename is
 437# detected in P4.
 438test_expect_success 'detect renames' '
 439        git p4 clone --dest="$git" //depot@all &&
 440        test_when_finished cleanup_git &&
 441        (
 442                cd "$git" &&
 443                git config git-p4.skipSubmitEdit true &&
 444
 445                git mv file1 file4 &&
 446                git commit -a -m "Rename file1 to file4" &&
 447                git diff-tree -r -M HEAD &&
 448                git p4 submit &&
 449                p4 filelog //depot/file4 &&
 450                p4 filelog //depot/file4 | test_must_fail grep -q "branch from" &&
 451
 452                git mv file4 file5 &&
 453                git commit -a -m "Rename file4 to file5" &&
 454                git diff-tree -r -M HEAD &&
 455                git config git-p4.detectRenames true &&
 456                git p4 submit &&
 457                p4 filelog //depot/file5 &&
 458                p4 filelog //depot/file5 | grep -q "branch from //depot/file4" &&
 459
 460                git mv file5 file6 &&
 461                echo update >>file6 &&
 462                git add file6 &&
 463                git commit -a -m "Rename file5 to file6 with changes" &&
 464                git diff-tree -r -M HEAD &&
 465                level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
 466                test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
 467                git config git-p4.detectRenames $(($level + 2)) &&
 468                git p4 submit &&
 469                p4 filelog //depot/file6 &&
 470                p4 filelog //depot/file6 | test_must_fail grep -q "branch from" &&
 471
 472                git mv file6 file7 &&
 473                echo update >>file7 &&
 474                git add file7 &&
 475                git commit -a -m "Rename file6 to file7 with changes" &&
 476                git diff-tree -r -M HEAD &&
 477                level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
 478                test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
 479                git config git-p4.detectRenames $(($level - 2)) &&
 480                git p4 submit &&
 481                p4 filelog //depot/file7 &&
 482                p4 filelog //depot/file7 | grep -q "branch from //depot/file6"
 483        )
 484'
 485
 486# Copy a file and confirm that copy is not detected in P4.
 487# Copy a file with detectCopies option enabled and confirm that copy is not
 488# detected in P4.
 489# Modify and copy a file with detectCopies option enabled and confirm that copy
 490# is detected in P4.
 491# Copy a file with detectCopies and detectCopiesHarder options enabled and
 492# confirm that copy is detected in P4.
 493# Modify and copy a file, configure a bigger threshold in detectCopies and
 494# confirm that copy is not detected in P4.
 495# Modify and copy a file, configure a smaller threshold in detectCopies and
 496# confirm that copy is detected in P4.
 497test_expect_success 'detect copies' '
 498        git p4 clone --dest="$git" //depot@all &&
 499        test_when_finished cleanup_git &&
 500        (
 501                cd "$git" &&
 502                git config git-p4.skipSubmitEdit true &&
 503
 504                cp file2 file8 &&
 505                git add file8 &&
 506                git commit -a -m "Copy file2 to file8" &&
 507                git diff-tree -r -C HEAD &&
 508                git p4 submit &&
 509                p4 filelog //depot/file8 &&
 510                p4 filelog //depot/file8 | test_must_fail grep -q "branch from" &&
 511
 512                cp file2 file9 &&
 513                git add file9 &&
 514                git commit -a -m "Copy file2 to file9" &&
 515                git diff-tree -r -C HEAD &&
 516                git config git-p4.detectCopies true &&
 517                git p4 submit &&
 518                p4 filelog //depot/file9 &&
 519                p4 filelog //depot/file9 | test_must_fail grep -q "branch from" &&
 520
 521                echo "file2" >>file2 &&
 522                cp file2 file10 &&
 523                git add file2 file10 &&
 524                git commit -a -m "Modify and copy file2 to file10" &&
 525                git diff-tree -r -C HEAD &&
 526                git p4 submit &&
 527                p4 filelog //depot/file10 &&
 528                p4 filelog //depot/file10 | grep -q "branch from //depot/file" &&
 529
 530                cp file2 file11 &&
 531                git add file11 &&
 532                git commit -a -m "Copy file2 to file11" &&
 533                git diff-tree -r -C --find-copies-harder HEAD &&
 534                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 535                test "$src" = file10 &&
 536                git config git-p4.detectCopiesHarder true &&
 537                git p4 submit &&
 538                p4 filelog //depot/file11 &&
 539                p4 filelog //depot/file11 | grep -q "branch from //depot/file" &&
 540
 541                cp file2 file12 &&
 542                echo "some text" >>file12 &&
 543                git add file12 &&
 544                git commit -a -m "Copy file2 to file12 with changes" &&
 545                git diff-tree -r -C --find-copies-harder HEAD &&
 546                level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
 547                test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
 548                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 549                test "$src" = file10 &&
 550                git config git-p4.detectCopies $(($level + 2)) &&
 551                git p4 submit &&
 552                p4 filelog //depot/file12 &&
 553                p4 filelog //depot/file12 | test_must_fail grep -q "branch from" &&
 554
 555                cp file2 file13 &&
 556                echo "different text" >>file13 &&
 557                git add file13 &&
 558                git commit -a -m "Copy file2 to file13 with changes" &&
 559                git diff-tree -r -C --find-copies-harder HEAD &&
 560                level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
 561                test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
 562                src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
 563                test "$src" = file10 &&
 564                git config git-p4.detectCopies $(($level - 2)) &&
 565                git p4 submit &&
 566                p4 filelog //depot/file13 &&
 567                p4 filelog //depot/file13 | grep -q "branch from //depot/file"
 568        )
 569'
 570
 571test_expect_success 'kill p4d' '
 572        kill_p4d
 573'
 574
 575test_done