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