t / t9813-git-p4-preserve-users.shon commit clean: replace match_pathspec() with dir_path_match() (05b8502)
   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_grant_admin() {
  23        name=$1 &&
  24        {
  25                p4 protect -o &&
  26                echo "    admin user $name * //depot/..."
  27        } | p4 protect -i
  28}
  29
  30p4_check_commit_author() {
  31        file=$1 user=$2 &&
  32        p4 changes -m 1 //depot/$file | grep -q $user
  33}
  34
  35make_change_by_user() {
  36        file=$1 name=$2 email=$3 &&
  37        echo "username: a change by $name" >>"$file" &&
  38        git add "$file" &&
  39        git commit --author "$name <$email>" -m "a change by $name"
  40}
  41
  42# Test username support, submitting as user 'alice'
  43test_expect_success 'preserve users' '
  44        p4_add_user alice &&
  45        p4_add_user bob &&
  46        p4_grant_admin alice &&
  47        git p4 clone --dest="$git" //depot &&
  48        test_when_finished cleanup_git &&
  49        (
  50                cd "$git" &&
  51                echo "username: a change by alice" >>file1 &&
  52                echo "username: a change by bob" >>file2 &&
  53                git commit --author "Alice <alice@example.com>" -m "a change by alice" file1 &&
  54                git commit --author "Bob <bob@example.com>" -m "a change by bob" file2 &&
  55                git config git-p4.skipSubmitEditCheck true &&
  56                P4EDITOR=touch P4USER=alice P4PASSWD=secret git p4 commit --preserve-user &&
  57                p4_check_commit_author file1 alice &&
  58                p4_check_commit_author file2 bob
  59        )
  60'
  61
  62# Test username support, submitting as bob, who lacks admin rights. Should
  63# not submit change to p4 (git diff should show deltas).
  64test_expect_success 'refuse to preserve users without perms' '
  65        git p4 clone --dest="$git" //depot &&
  66        test_when_finished cleanup_git &&
  67        (
  68                cd "$git" &&
  69                git config git-p4.skipSubmitEditCheck true &&
  70                echo "username-noperms: a change by alice" >>file1 &&
  71                git commit --author "Alice <alice@example.com>" -m "perms: a change by alice" file1 &&
  72                P4EDITOR=touch P4USER=bob P4PASSWD=secret &&
  73                export P4EDITOR P4USER P4PASSWD &&
  74                test_must_fail git p4 commit --preserve-user &&
  75                ! git diff --exit-code HEAD..p4/master
  76        )
  77'
  78
  79# What happens with unknown author? Without allowMissingP4Users it should fail.
  80test_expect_success 'preserve user where author is unknown to p4' '
  81        git p4 clone --dest="$git" //depot &&
  82        test_when_finished cleanup_git &&
  83        (
  84                cd "$git" &&
  85                git config git-p4.skipSubmitEditCheck true &&
  86                echo "username-bob: a change by bob" >>file1 &&
  87                git commit --author "Bob <bob@example.com>" -m "preserve: a change by bob" file1 &&
  88                echo "username-unknown: a change by charlie" >>file1 &&
  89                git commit --author "Charlie <charlie@example.com>" -m "preserve: a change by charlie" file1 &&
  90                P4EDITOR=touch P4USER=alice P4PASSWD=secret &&
  91                export P4EDITOR P4USER P4PASSWD &&
  92                test_must_fail git p4 commit --preserve-user &&
  93                ! git diff --exit-code HEAD..p4/master &&
  94
  95                echo "$0: repeat with allowMissingP4Users enabled" &&
  96                git config git-p4.allowMissingP4Users true &&
  97                git config git-p4.preserveUser true &&
  98                git p4 commit &&
  99                git diff --exit-code HEAD..p4/master &&
 100                p4_check_commit_author file1 alice
 101        )
 102'
 103
 104# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
 105# changes that are not all ours.
 106# Test: user in p4 and user unknown to p4.
 107# Test: warning disabled and user is the same.
 108test_expect_success 'not preserving user with mixed authorship' '
 109        git p4 clone --dest="$git" //depot &&
 110        test_when_finished cleanup_git &&
 111        (
 112                cd "$git" &&
 113                git config git-p4.skipSubmitEditCheck true &&
 114                p4_add_user derek &&
 115
 116                make_change_by_user usernamefile3 Derek derek@example.com &&
 117                P4EDITOR=cat P4USER=alice P4PASSWD=secret &&
 118                export P4EDITOR P4USER P4PASSWD &&
 119                git p4 commit |\
 120                grep "git author derek@example.com does not match" &&
 121
 122                make_change_by_user usernamefile3 Charlie charlie@example.com &&
 123                git p4 commit |\
 124                grep "git author charlie@example.com does not match" &&
 125
 126                make_change_by_user usernamefile3 alice alice@example.com &&
 127                git p4 commit |\
 128                test_must_fail grep "git author.*does not match" &&
 129
 130                git config git-p4.skipUserNameCheck true &&
 131                make_change_by_user usernamefile3 Charlie charlie@example.com &&
 132                git p4 commit |\
 133                test_must_fail grep "git author.*does not match" &&
 134
 135                p4_check_commit_author usernamefile3 alice
 136        )
 137'
 138
 139test_expect_success 'kill p4d' '
 140        kill_p4d
 141'
 142
 143test_done