summaryrefslogtreecommitdiffstats
path: root/t/t9800-git-p4-basic.sh
blob: 53af8e34ac1c40a31dee6e6f4449f8c52dcf6f59 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#!/bin/sh

test_description='git p4 tests'

GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME

. ./lib-git-p4.sh

test_expect_success 'start p4d' '
	start_p4d
'

test_expect_success 'add p4 files' '
	(
		cd "$cli" &&
		echo file1 >file1 &&
		p4 add file1 &&
		p4 submit -d "file1" &&
		echo file2 >file2 &&
		p4 add file2 &&
		p4 submit -d "file2"
	)
'

test_expect_success 'basic git p4 clone' '
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git log --oneline >lines &&
		test_line_count = 1 lines
	)
'

test_expect_success 'depot typo error' '
	test_must_fail git p4 clone --dest="$git" /depot 2>errs &&
	grep "Depot paths must start with" errs
'

test_expect_success 'git p4 clone @all' '
	git p4 clone --dest="$git" //depot@all &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git log --oneline >lines &&
		test_line_count = 2 lines
	)
'

test_expect_success 'git p4 sync uninitialized repo' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_must_fail git p4 sync 2>errs &&
		test_grep "Perhaps you never did" errs
	)
'

#
# Create a git repo by hand.  Add a commit so that HEAD is valid.
# Test imports a new p4 repository into a new git branch.
#
test_expect_success 'git p4 sync new branch' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=refs/remotes/p4/depot //depot@all &&
		git log --oneline p4/depot >lines &&
		test_line_count = 2 lines
	)
'

#
# Setup as before, and then explicitly sync imported branch, using a
# different ref format.
#
test_expect_success 'git p4 sync existing branch without changes' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=depot //depot@all &&
		git p4 sync --branch=refs/remotes/p4/depot >out &&
		test_grep "No changes to import!" out
	)
'

#
# Same as before, relative branch name.
#
test_expect_success 'git p4 sync existing branch with relative name' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=branch1 //depot@all &&
		git p4 sync --branch=p4/branch1 >out &&
		test_grep "No changes to import!" out
	)
'

#
# Same as before, with a nested branch path, referenced different ways.
#
test_expect_success 'git p4 sync existing branch with nested path' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=p4/some/path //depot@all &&
		git p4 sync --branch=some/path >out &&
		test_grep "No changes to import!" out
	)
'

#
# Same as before, with a full ref path outside the p4/* namespace.
#
test_expect_success 'git p4 sync branch explicit ref without p4 in path' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=refs/remotes/someremote/depot //depot@all &&
		git p4 sync --branch=refs/remotes/someremote/depot >out &&
		test_grep "No changes to import!" out
	)
'

test_expect_success 'git p4 sync nonexistent ref' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=depot //depot@all &&
		test_must_fail git p4 sync --branch=depot2 2>errs &&
		test_grep "Perhaps you never did" errs
	)
'

test_expect_success 'git p4 sync existing non-p4-imported ref' '
	test_create_repo "$git" &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_commit head &&
		git p4 sync --branch=depot //depot@all &&
		test_must_fail git p4 sync --branch=refs/heads/master 2>errs &&
		test_grep "Perhaps you never did" errs
	)
'

test_expect_success 'clone two dirs' '
	(
		cd "$cli" &&
		mkdir sub1 sub2 &&
		echo sub1/f1 >sub1/f1 &&
		echo sub2/f2 >sub2/f2 &&
		p4 add sub1/f1 &&
		p4 submit -d "sub1/f1" &&
		p4 add sub2/f2 &&
		p4 submit -d "sub2/f2"
	) &&
	git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git ls-files >lines &&
		test_line_count = 2 lines &&
		git log --oneline p4/master >lines &&
		test_line_count = 1 lines
	)
'

test_expect_success 'clone two dirs, @all' '
	(
		cd "$cli" &&
		echo sub1/f3 >sub1/f3 &&
		p4 add sub1/f3 &&
		p4 submit -d "sub1/f3"
	) &&
	git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git ls-files >lines &&
		test_line_count = 3 lines &&
		git log --oneline p4/master >lines &&
		test_line_count = 3 lines
	)
'

test_expect_success 'clone two dirs, @all, conflicting files' '
	(
		cd "$cli" &&
		echo sub2/f3 >sub2/f3 &&
		p4 add sub2/f3 &&
		p4 submit -d "sub2/f3"
	) &&
	git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git ls-files >lines &&
		test_line_count = 3 lines &&
		git log --oneline p4/master >lines &&
		test_line_count = 4 lines &&
		echo sub2/f3 >expected &&
		test_cmp expected f3
	)
'

test_expect_success 'clone two dirs, each edited by submit, single git commit' '
	(
		cd "$cli" &&
		echo sub1/f4 >sub1/f4 &&
		p4 add sub1/f4 &&
		echo sub2/f4 >sub2/f4 &&
		p4 add sub2/f4 &&
		p4 submit -d "sub1/f4 and sub2/f4"
	) &&
	git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		git ls-files >lines &&
		test_line_count = 4 lines &&
		git log --oneline p4/master >lines &&
		test_line_count = 5 lines
	)
'

revision_ranges="2000/01/01,#head \
		 1,2080/01/01 \
		 2000/01/01,2080/01/01 \
		 2000/01/01,1000 \
		 1,1000"

test_expect_success 'clone using non-numeric revision ranges' '
	test_when_finished cleanup_git &&
	for r in $revision_ranges
	do
		rm -fr "$git" &&
		test ! -d "$git" &&
		git p4 clone --dest="$git" //depot@$r &&
		(
			cd "$git" &&
			git ls-files >lines &&
			test_line_count = 8 lines
		) || return 1
	done
'

test_expect_success 'clone with date range, excluding some changes' '
	test_when_finished cleanup_git &&
	before=$(date +%Y/%m/%d:%H:%M:%S) &&
	sleep 2 &&
	(
		cd "$cli" &&
		:>date_range_test &&
		p4 add date_range_test &&
		p4 submit -d "Adding file"
	) &&
	git p4 clone --dest="$git" //depot@1,$before &&
	(
		cd "$git" &&
		test_path_is_missing date_range_test
	)
'

test_expect_success 'exit when p4 fails to produce marshaled output' '
	mkdir badp4dir &&
	test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
	cat >badp4dir/p4 <<-EOF &&
	#!$SHELL_PATH
	exit 1
	EOF
	chmod 755 badp4dir/p4 &&
	(
		PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
		export PATH &&
		test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
	) &&
	test_grep ! Traceback errs
'

# Hide a file from p4d, make sure we catch its complaint.  This won't fail in
# p4 changes, files, or describe; just in p4 print.  If P4CLIENT is unset, the
# message will include "Librarian checkout".
test_expect_success 'exit gracefully for p4 server errors' '
	test_when_finished "mv \"$db\"/depot/file1,v,hidden \"$db\"/depot/file1,v" &&
	mv "$db"/depot/file1,v "$db"/depot/file1,v,hidden &&
	test_when_finished cleanup_git &&
	test_expect_code 1 git p4 clone --dest="$git" //depot@1 >out 2>err &&
	test_grep "Error from p4 print" err
'

test_expect_success 'clone --bare should make a bare repository' '
	rm -rf "$git" &&
	git p4 clone --dest="$git" --bare //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		test_path_is_missing .git &&
		git config --get --bool core.bare true &&
		git rev-parse --verify refs/remotes/p4/master &&
		git rev-parse --verify refs/remotes/p4/HEAD &&
		git rev-parse --verify refs/heads/main &&
		git rev-parse --verify HEAD
	)
'

# Sleep a bit so that the top-most p4 change did not happen "now".  Then
# import the repo and make sure that the initial import has the same time
# as the top-most change.
test_expect_success 'initial import time from top change time' '
	p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) &&
	p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) &&
	sleep 3 &&
	git p4 clone --dest="$git" //depot &&
	test_when_finished cleanup_git &&
	(
		cd "$git" &&
		gittime=$(git show -s --pretty=format:%at HEAD) &&
		echo $p4time $gittime &&
		test $p4time = $gittime
	)
'

test_expect_success 'unresolvable host in P4PORT should display error' '
	test_when_finished cleanup_git &&
	git p4 clone --dest="$git" //depot &&
	(
		cd "$git" &&
		P4PORT=nosuchhost:65537 &&
		export P4PORT &&
		test_expect_code 1 git p4 sync >out 2>err &&
		grep "connect to nosuchhost" err
	)
'

# Test following scenarios:
#   - Without ".git/hooks/p4-pre-submit" , submit should continue
#   - With the hook returning 0, submit should continue
#   - With the hook returning 1, submit should abort
test_expect_success 'run hook p4-pre-submit before submit' '
	test_when_finished cleanup_git &&
	git p4 clone --dest="$git" //depot &&
	(
		cd "$git" &&
		echo "hello world" >hello.txt &&
		git add hello.txt &&
		git commit -m "add hello.txt" &&
		git config git-p4.skipSubmitEdit true &&
		git p4 submit --dry-run >out &&
		grep "Would apply" out
	) &&
	test_hook -C "$git" p4-pre-submit <<-\EOF &&
	exit 0
	EOF
	(
		cd "$git" &&
		git p4 submit --dry-run >out &&
		grep "Would apply" out
	) &&
	test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
	exit 1
	EOF
	(
		cd "$git" &&
		test_must_fail git p4 submit --dry-run >errs 2>&1 &&
		! grep "Would apply" errs
	)
'

test_expect_success 'submit from detached head' '
	test_when_finished cleanup_git &&
	git p4 clone --dest="$git" //depot &&
	(
		cd "$git" &&
		git checkout p4/master &&
		>detached_head_test &&
		git add detached_head_test &&
		git commit -m "add detached_head" &&
		git config git-p4.skipSubmitEdit true &&
		git p4 submit &&
		git p4 rebase &&
		git log p4/master | grep detached_head
	)
'

test_expect_success 'submit from worktree' '
	test_when_finished cleanup_git &&
	git p4 clone --dest="$git" //depot &&
	(
		cd "$git" &&
		git worktree add ../worktree-test
	) &&
	(
		cd "$git/../worktree-test" &&
		test_commit "worktree-commit" &&
		git config git-p4.skipSubmitEdit true &&
		git p4 submit
	) &&
	(
		cd "$cli" &&
		p4 sync &&
		test_path_is_file worktree-commit.t
	)
'

test_done