aboutsummaryrefslogtreecommitdiff
path: root/t/t9813-git-p4-preserve-users.sh
blob: bda222aa0270f3a93fa494b308aa174ebc942eca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh

test_description='git p4 preserve users'

. ./lib-git-p4.sh

test_expect_success 'start p4d' '
	start_p4d
'

test_expect_success 'create files' '
	(
		cd "$cli" &&
		p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
		echo file1 >file1 &&
		echo file2 >file2 &&
		p4 add file1 file2 &&
		p4 submit -d "add files"
	)
'

p4_grant_admin() {
	name=$1 &&
	{
		p4 protect -o &&
		echo "    admin user $name * //depot/..."
	} | p4 protect -i
}

p4_check_commit_author() {
	file=$1 user=$2 &&
	p4 changes -m 1 //depot/$file | grep -q $user
}

make_change_by_user() {
	file=$1 name=$2 email=$3 &&
	echo "username: a change by $name" >>"$file" &&
	git add "$file" &&
	git commit --author "$name <$email>" -m "a change by $name"
}

# Test username support, submitting as user 'alice'
test_expect_success 'preserve users' '
	p4_add_user alice &&
	p4_add_user bob &&
	p4_grant_admin alice &&
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		echo "username: a change by alice" >>file1 &&
		echo "username: a change by bob" >>file2 &&
		git commit --author "Alice <alice@example.com>" -m "a change by alice" file1 &&
		git commit --author "Bob <bob@example.com>" -m "a change by bob" file2 &&
		git config git-p4.skipSubmitEditCheck true &&
		P4EDITOR="test-chmtime +5" P4USER=alice P4PASSWD=secret &&
		export P4EDITOR P4USER P4PASSWD &&
		git p4 commit --preserve-user &&
		p4_check_commit_author file1 alice &&
		p4_check_commit_author file2 bob
	)
'

# Test username support, submitting as bob, who lacks admin rights. Should
# not submit change to p4 (git diff should show deltas).
test_expect_success 'refuse to preserve users without perms' '
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git config git-p4.skipSubmitEditCheck true &&
		echo "username-noperms: a change by alice" >>file1 &&
		git commit --author "Alice <alice@example.com>" -m "perms: a change by alice" file1 &&
		P4EDITOR="test-chmtime +5" P4USER=bob P4PASSWD=secret &&
		export P4EDITOR P4USER P4PASSWD &&
		test_must_fail git p4 commit --preserve-user &&
		! git diff --exit-code HEAD..p4/master
	)
'

# What happens with unknown author? Without allowMissingP4Users it should fail.
test_expect_success 'preserve user where author is unknown to p4' '
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git config git-p4.skipSubmitEditCheck true &&
		echo "username-bob: a change by bob" >>file1 &&
		git commit --author "Bob <bob@example.com>" -m "preserve: a change by bob" file1 &&
		echo "username-unknown: a change by charlie" >>file1 &&
		git commit --author "Charlie <charlie@example.com>" -m "preserve: a change by charlie" file1 &&
		P4EDITOR="test-chmtime +5" P4USER=alice P4PASSWD=secret &&
		export P4EDITOR P4USER P4PASSWD &&
		test_must_fail git p4 commit --preserve-user &&
		! git diff --exit-code HEAD..p4/master &&

		echo "$0: repeat with allowMissingP4Users enabled" &&
		git config git-p4.allowMissingP4Users true &&
		git config git-p4.preserveUser true &&
		git p4 commit &&
		git diff --exit-code HEAD..p4/master &&
		p4_check_commit_author file1 alice
	)
'

# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
# changes that are not all ours.
# Test: user in p4 and user unknown to p4.
# Test: warning disabled and user is the same.
test_expect_success 'not preserving user with mixed authorship' '
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git config git-p4.skipSubmitEditCheck true &&
		p4_add_user derek &&

		make_change_by_user usernamefile3 Derek derek@example.com &&
		P4EDITOR=cat P4USER=alice P4PASSWD=secret &&
		export P4EDITOR P4USER P4PASSWD &&
		git p4 commit >actual &&
		grep "git author derek@example.com does not match" actual &&

		make_change_by_user usernamefile3 Charlie charlie@example.com &&
		git p4 commit >actual &&
		grep "git author charlie@example.com does not match" actual &&

		make_change_by_user usernamefile3 alice alice@example.com &&
		git p4 commit >actual &&
		! grep "git author.*does not match" actual &&

		git config git-p4.skipUserNameCheck true &&
		make_change_by_user usernamefile3 Charlie charlie@example.com &&
		git p4 commit >actual &&
		! grep "git author.*does not match" actual &&

		p4_check_commit_author usernamefile3 alice
	)
'

test_expect_success 'kill p4d' '
	kill_p4d
'

test_done