3 test_description
='git p4 tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 TEST_PASSES_SANITIZE_LEAK
=true
11 test_expect_success
'start p4d' '
15 test_expect_success
'add p4 files' '
20 p4 submit -d "file1" &&
27 test_expect_success
'basic git p4 clone' '
28 git p4 clone --dest="$git" //depot &&
29 test_when_finished cleanup_git &&
32 git log --oneline >lines &&
33 test_line_count = 1 lines
37 test_expect_success
'depot typo error' '
38 test_must_fail git p4 clone --dest="$git" /depot 2>errs &&
39 grep "Depot paths must start with" errs
42 test_expect_success
'git p4 clone @all' '
43 git p4 clone --dest="$git" //depot@all &&
44 test_when_finished cleanup_git &&
47 git log --oneline >lines &&
48 test_line_count = 2 lines
52 test_expect_success
'git p4 sync uninitialized repo' '
53 test_create_repo "$git" &&
54 test_when_finished cleanup_git &&
57 test_must_fail git p4 sync 2>errs &&
58 test_grep "Perhaps you never did" errs
63 # Create a git repo by hand. Add a commit so that HEAD is valid.
64 # Test imports a new p4 repository into a new git branch.
66 test_expect_success
'git p4 sync new branch' '
67 test_create_repo "$git" &&
68 test_when_finished cleanup_git &&
72 git p4 sync --branch=refs/remotes/p4/depot //depot@all &&
73 git log --oneline p4/depot >lines &&
74 test_line_count = 2 lines
79 # Setup as before, and then explicitly sync imported branch, using a
80 # different ref format.
82 test_expect_success
'git p4 sync existing branch without changes' '
83 test_create_repo "$git" &&
84 test_when_finished cleanup_git &&
88 git p4 sync --branch=depot //depot@all &&
89 git p4 sync --branch=refs/remotes/p4/depot >out &&
90 test_grep "No changes to import!" out
95 # Same as before, relative branch name.
97 test_expect_success
'git p4 sync existing branch with relative name' '
98 test_create_repo "$git" &&
99 test_when_finished cleanup_git &&
103 git p4 sync --branch=branch1 //depot@all &&
104 git p4 sync --branch=p4/branch1 >out &&
105 test_grep "No changes to import!" out
110 # Same as before, with a nested branch path, referenced different ways.
112 test_expect_success
'git p4 sync existing branch with nested path' '
113 test_create_repo "$git" &&
114 test_when_finished cleanup_git &&
118 git p4 sync --branch=p4/some/path //depot@all &&
119 git p4 sync --branch=some/path >out &&
120 test_grep "No changes to import!" out
125 # Same as before, with a full ref path outside the p4/* namespace.
127 test_expect_success
'git p4 sync branch explicit ref without p4 in path' '
128 test_create_repo "$git" &&
129 test_when_finished cleanup_git &&
133 git p4 sync --branch=refs/remotes/someremote/depot //depot@all &&
134 git p4 sync --branch=refs/remotes/someremote/depot >out &&
135 test_grep "No changes to import!" out
139 test_expect_success
'git p4 sync nonexistent ref' '
140 test_create_repo "$git" &&
141 test_when_finished cleanup_git &&
145 git p4 sync --branch=depot //depot@all &&
146 test_must_fail git p4 sync --branch=depot2 2>errs &&
147 test_grep "Perhaps you never did" errs
151 test_expect_success
'git p4 sync existing non-p4-imported ref' '
152 test_create_repo "$git" &&
153 test_when_finished cleanup_git &&
157 git p4 sync --branch=depot //depot@all &&
158 test_must_fail git p4 sync --branch=refs/heads/master 2>errs &&
159 test_grep "Perhaps you never did" errs
163 test_expect_success
'clone two dirs' '
167 echo sub1/f1 >sub1/f1 &&
168 echo sub2/f2 >sub2/f2 &&
170 p4 submit -d "sub1/f1" &&
172 p4 submit -d "sub2/f2"
174 git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
175 test_when_finished cleanup_git &&
178 git ls-files >lines &&
179 test_line_count = 2 lines &&
180 git log --oneline p4/master >lines &&
181 test_line_count = 1 lines
185 test_expect_success
'clone two dirs, @all' '
188 echo sub1/f3 >sub1/f3 &&
190 p4 submit -d "sub1/f3"
192 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
193 test_when_finished cleanup_git &&
196 git ls-files >lines &&
197 test_line_count = 3 lines &&
198 git log --oneline p4/master >lines &&
199 test_line_count = 3 lines
203 test_expect_success
'clone two dirs, @all, conflicting files' '
206 echo sub2/f3 >sub2/f3 &&
208 p4 submit -d "sub2/f3"
210 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
211 test_when_finished cleanup_git &&
214 git ls-files >lines &&
215 test_line_count = 3 lines &&
216 git log --oneline p4/master >lines &&
217 test_line_count = 4 lines &&
218 echo sub2/f3 >expected &&
223 test_expect_success
'clone two dirs, each edited by submit, single git commit' '
226 echo sub1/f4 >sub1/f4 &&
228 echo sub2/f4 >sub2/f4 &&
230 p4 submit -d "sub1/f4 and sub2/f4"
232 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
233 test_when_finished cleanup_git &&
236 git ls-files >lines &&
237 test_line_count = 4 lines &&
238 git log --oneline p4/master >lines &&
239 test_line_count = 5 lines
243 revision_ranges
="2000/01/01,#head \
245 2000/01/01,2080/01/01 \
249 test_expect_success
'clone using non-numeric revision ranges' '
250 test_when_finished cleanup_git &&
251 for r in $revision_ranges
255 git p4 clone --dest="$git" //depot@$r &&
258 git ls-files >lines &&
259 test_line_count = 8 lines
264 test_expect_success
'clone with date range, excluding some changes' '
265 test_when_finished cleanup_git &&
266 before=$(date +%Y/%m/%d:%H:%M:%S) &&
271 p4 add date_range_test &&
272 p4 submit -d "Adding file"
274 git p4 clone --dest="$git" //depot@1,$before &&
277 test_path_is_missing date_range_test
281 test_expect_success
'exit when p4 fails to produce marshaled output' '
283 test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
284 cat >badp4dir/p4 <<-EOF &&
288 chmod 755 badp4dir/p4 &&
290 PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
292 test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
294 test_grep ! Traceback errs
297 # Hide a file from p4d, make sure we catch its complaint. This won't fail in
298 # p4 changes, files, or describe; just in p4 print. If P4CLIENT is unset, the
299 # message will include "Librarian checkout".
300 test_expect_success
'exit gracefully for p4 server errors' '
301 # Note that newer Perforce versions started to store files
302 # compressed in directories. The case statement handles both
303 # old and new layout.
304 case "$(echo "$db"/depot/file1*)" in
306 test_when_finished "mv \"$db\"/depot/file1,v,hidden \"$db\"/depot/file1,v" &&
307 mv "$db"/depot/file1,v "$db"/depot/file1,v,hidden;;
309 path="$(echo "$db"/depot/file1,d/*.gz)" &&
310 test_when_finished "mv \"$path\",hidden \"$path\"" &&
311 mv "$path" "$path",hidden;;
313 BUG "unhandled p4d layout";;
315 test_when_finished cleanup_git &&
316 test_expect_code 1 git p4 clone --dest="$git" //depot@1 >out 2>err &&
317 test_grep "Error from p4 print" err
320 test_expect_success
'clone --bare should make a bare repository' '
322 git p4 clone --dest="$git" --bare //depot &&
323 test_when_finished cleanup_git &&
326 test_path_is_missing .git &&
327 git config --get --bool core.bare true &&
328 git rev-parse --verify refs/remotes/p4/master &&
329 git rev-parse --verify refs/remotes/p4/HEAD &&
330 git rev-parse --verify refs/heads/main &&
331 git rev-parse --verify HEAD
335 # Sleep a bit so that the top-most p4 change did not happen "now". Then
336 # import the repo and make sure that the initial import has the same time
337 # as the top-most change.
338 test_expect_success
'initial import time from top change time' '
339 p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) &&
340 p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) &&
342 git p4 clone --dest="$git" //depot &&
343 test_when_finished cleanup_git &&
346 gittime=$(git show -s --pretty=format:%at HEAD) &&
347 echo $p4time $gittime &&
348 test $p4time = $gittime
352 test_expect_success
'unresolvable host in P4PORT should display error' '
353 test_when_finished cleanup_git &&
354 git p4 clone --dest="$git" //depot &&
357 P4PORT=nosuchhost:65537 &&
359 test_expect_code 1 git p4 sync >out 2>err &&
360 grep "connect to nosuchhost" err
364 # Test following scenarios:
365 # - Without ".git/hooks/p4-pre-submit" , submit should continue
366 # - With the hook returning 0, submit should continue
367 # - With the hook returning 1, submit should abort
368 test_expect_success
'run hook p4-pre-submit before submit' '
369 test_when_finished cleanup_git &&
370 git p4 clone --dest="$git" //depot &&
373 echo "hello world" >hello.txt &&
375 git commit -m "add hello.txt" &&
376 git config git-p4.skipSubmitEdit true &&
377 git p4 submit --dry-run >out &&
378 grep "Would apply" out
380 test_hook -C "$git" p4-pre-submit <<-\EOF &&
385 git p4 submit --dry-run >out &&
386 grep "Would apply" out
388 test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
393 test_must_fail git p4 submit --dry-run >errs 2>&1 &&
394 ! grep "Would apply" errs
398 test_expect_success
'submit from detached head' '
399 test_when_finished cleanup_git &&
400 git p4 clone --dest="$git" //depot &&
403 git checkout p4/master &&
404 >detached_head_test &&
405 git add detached_head_test &&
406 git commit -m "add detached_head" &&
407 git config git-p4.skipSubmitEdit true &&
410 git log p4/master | grep detached_head
414 test_expect_success
'submit from worktree' '
415 test_when_finished cleanup_git &&
416 git p4 clone --dest="$git" //depot &&
419 git worktree add ../worktree-test
422 cd "$git/../worktree-test" &&
423 test_commit "worktree-commit" &&
424 git config git-p4.skipSubmitEdit true &&
430 test_path_is_file worktree-commit.t