t / t9813-git-p4-preserve-users.shon commit reset: optionally setup worktree and refresh index on --mixed (b7756d4)
   1#!/bin/sh
   2
   3test_description='git p4 preserve users'
   4
   5. ./lib-git-p4.sh
   6
   7test_expect_success 'start p4d' '
   8        start_p4d
   9'
  10
  11test_expect_success 'create files' '
  12        (
  13                cd "$cli" &&
  14                p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
  15                echo file1 >file1 &&
  16                echo file2 >file2 &&
  17                p4 add file1 file2 &&
  18                p4 submit -d "add files"
  19        )
  20'
  21
  22p4_add_user() {
  23        name=$1 fullname=$2 &&
  24        p4 user -f -i <<-EOF &&
  25        User: $name
  26        Email: $name@localhost
  27        FullName: $fullname
  28        EOF
  29        p4 passwd -P secret $name
  30}
  31
  32p4_grant_admin() {
  33        name=$1 &&
  34        {
  35                p4 protect -o &&
  36                echo "    admin user $name * //depot/..."
  37        } | p4 protect -i
  38}
  39
  40p4_check_commit_author() {
  41        file=$1 user=$2 &&
  42        p4 changes -m 1 //depot/$file | grep -q $user
  43}
  44
  45make_change_by_user() {
  46        file=$1 name=$2 email=$3 &&
  47        echo "username: a change by $name" >>"$file" &&
  48        git add "$file" &&
  49        git commit --author "$name <$email>" -m "a change by $name"
  50}
  51
  52# Test username support, submitting as user 'alice'
  53test_expect_success 'preserve users' '
  54        p4_add_user alice Alice &&
  55        p4_add_user bob Bob &&
  56        p4_grant_admin alice &&
  57        git p4 clone --dest="$git" //depot &&
  58        test_when_finished cleanup_git &&
  59        (
  60                cd "$git" &&
  61                echo "username: a change by alice" >>file1 &&
  62                echo "username: a change by bob" >>file2 &&
  63                git commit --author "Alice <alice@localhost>" -m "a change by alice" file1 &&
  64                git commit --author "Bob <bob@localhost>" -m "a change by bob" file2 &&
  65                git config git-p4.skipSubmitEditCheck true &&
  66                P4EDITOR=touch P4USER=alice P4PASSWD=secret git p4 commit --preserve-user &&
  67                p4_check_commit_author file1 alice &&
  68                p4_check_commit_author file2 bob
  69        )
  70'
  71
  72# Test username support, submitting as bob, who lacks admin rights. Should
  73# not submit change to p4 (git diff should show deltas).
  74test_expect_success 'refuse to preserve users without perms' '
  75        git p4 clone --dest="$git" //depot &&
  76        test_when_finished cleanup_git &&
  77        (
  78                cd "$git" &&
  79                git config git-p4.skipSubmitEditCheck true &&
  80                echo "username-noperms: a change by alice" >>file1 &&
  81                git commit --author "Alice <alice@localhost>" -m "perms: a change by alice" file1 &&
  82                P4EDITOR=touch P4USER=bob P4PASSWD=secret &&
  83                export P4EDITOR P4USER P4PASSWD &&
  84                test_must_fail git p4 commit --preserve-user &&
  85                ! git diff --exit-code HEAD..p4/master
  86        )
  87'
  88
  89# What happens with unknown author? Without allowMissingP4Users it should fail.
  90test_expect_success 'preserve user where author is unknown to p4' '
  91        git p4 clone --dest="$git" //depot &&
  92        test_when_finished cleanup_git &&
  93        (
  94                cd "$git" &&
  95                git config git-p4.skipSubmitEditCheck true &&
  96                echo "username-bob: a change by bob" >>file1 &&
  97                git commit --author "Bob <bob@localhost>" -m "preserve: a change by bob" file1 &&
  98                echo "username-unknown: a change by charlie" >>file1 &&
  99                git commit --author "Charlie <charlie@localhost>" -m "preserve: a change by charlie" file1 &&
 100                P4EDITOR=touch P4USER=alice P4PASSWD=secret &&
 101                export P4EDITOR P4USER P4PASSWD &&
 102                test_must_fail git p4 commit --preserve-user &&
 103                ! git diff --exit-code HEAD..p4/master &&
 104
 105                echo "$0: repeat with allowMissingP4Users enabled" &&
 106                git config git-p4.allowMissingP4Users true &&
 107                git config git-p4.preserveUser true &&
 108                git p4 commit &&
 109                git diff --exit-code HEAD..p4/master &&
 110                p4_check_commit_author file1 alice
 111        )
 112'
 113
 114# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
 115# changes that are not all ours.
 116# Test: user in p4 and user unknown to p4.
 117# Test: warning disabled and user is the same.
 118test_expect_success 'not preserving user with mixed authorship' '
 119        git p4 clone --dest="$git" //depot &&
 120        test_when_finished cleanup_git &&
 121        (
 122                cd "$git" &&
 123                git config git-p4.skipSubmitEditCheck true &&
 124                p4_add_user derek Derek &&
 125
 126                make_change_by_user usernamefile3 Derek derek@localhost &&
 127                P4EDITOR=cat P4USER=alice P4PASSWD=secret &&
 128                export P4EDITOR P4USER P4PASSWD &&
 129                git p4 commit |\
 130                grep "git author derek@localhost does not match" &&
 131
 132                make_change_by_user usernamefile3 Charlie charlie@localhost &&
 133                git p4 commit |\
 134                grep "git author charlie@localhost does not match" &&
 135
 136                make_change_by_user usernamefile3 alice alice@localhost &&
 137                git p4 commit |\
 138                test_must_fail grep "git author.*does not match" &&
 139
 140                git config git-p4.skipUserNameCheck true &&
 141                make_change_by_user usernamefile3 Charlie charlie@localhost &&
 142                git p4 commit |\
 143                test_must_fail grep "git author.*does not match" &&
 144
 145                p4_check_commit_author usernamefile3 alice
 146        )
 147'
 148
 149test_expect_success 'kill p4d' '
 150        kill_p4d
 151'
 152
 153test_done