3 test_description
='git p4 tests'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
10 test_expect_success
'start p4d' '
14 test_expect_success
'add p4 files' '
19 p4 submit -d "file1" &&
26 test_expect_success
'basic git p4 clone' '
27 git p4 clone --dest="$git" //depot &&
28 test_when_finished cleanup_git &&
31 git log --oneline >lines &&
32 test_line_count = 1 lines
36 test_expect_success
'depot typo error' '
37 test_must_fail git p4 clone --dest="$git" /depot 2>errs &&
38 grep "Depot paths must start with" errs
41 test_expect_success
'git p4 clone @all' '
42 git p4 clone --dest="$git" //depot@all &&
43 test_when_finished cleanup_git &&
46 git log --oneline >lines &&
47 test_line_count = 2 lines
51 test_expect_success
'git p4 sync uninitialized repo' '
52 test_create_repo "$git" &&
53 test_when_finished cleanup_git &&
56 test_must_fail git p4 sync 2>errs &&
57 test_grep "Perhaps you never did" errs
62 # Create a git repo by hand. Add a commit so that HEAD is valid.
63 # Test imports a new p4 repository into a new git branch.
65 test_expect_success
'git p4 sync new branch' '
66 test_create_repo "$git" &&
67 test_when_finished cleanup_git &&
71 git p4 sync --branch=refs/remotes/p4/depot //depot@all &&
72 git log --oneline p4/depot >lines &&
73 test_line_count = 2 lines
78 # Setup as before, and then explicitly sync imported branch, using a
79 # different ref format.
81 test_expect_success
'git p4 sync existing branch without changes' '
82 test_create_repo "$git" &&
83 test_when_finished cleanup_git &&
87 git p4 sync --branch=depot //depot@all &&
88 git p4 sync --branch=refs/remotes/p4/depot >out &&
89 test_grep "No changes to import!" out
94 # Same as before, relative branch name.
96 test_expect_success
'git p4 sync existing branch with relative name' '
97 test_create_repo "$git" &&
98 test_when_finished cleanup_git &&
102 git p4 sync --branch=branch1 //depot@all &&
103 git p4 sync --branch=p4/branch1 >out &&
104 test_grep "No changes to import!" out
109 # Same as before, with a nested branch path, referenced different ways.
111 test_expect_success
'git p4 sync existing branch with nested path' '
112 test_create_repo "$git" &&
113 test_when_finished cleanup_git &&
117 git p4 sync --branch=p4/some/path //depot@all &&
118 git p4 sync --branch=some/path >out &&
119 test_grep "No changes to import!" out
124 # Same as before, with a full ref path outside the p4/* namespace.
126 test_expect_success
'git p4 sync branch explicit ref without p4 in path' '
127 test_create_repo "$git" &&
128 test_when_finished cleanup_git &&
132 git p4 sync --branch=refs/remotes/someremote/depot //depot@all &&
133 git p4 sync --branch=refs/remotes/someremote/depot >out &&
134 test_grep "No changes to import!" out
138 test_expect_success
'git p4 sync nonexistent ref' '
139 test_create_repo "$git" &&
140 test_when_finished cleanup_git &&
144 git p4 sync --branch=depot //depot@all &&
145 test_must_fail git p4 sync --branch=depot2 2>errs &&
146 test_grep "Perhaps you never did" errs
150 test_expect_success
'git p4 sync existing non-p4-imported ref' '
151 test_create_repo "$git" &&
152 test_when_finished cleanup_git &&
156 git p4 sync --branch=depot //depot@all &&
157 test_must_fail git p4 sync --branch=refs/heads/master 2>errs &&
158 test_grep "Perhaps you never did" errs
162 test_expect_success
'clone two dirs' '
166 echo sub1/f1 >sub1/f1 &&
167 echo sub2/f2 >sub2/f2 &&
169 p4 submit -d "sub1/f1" &&
171 p4 submit -d "sub2/f2"
173 git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
174 test_when_finished cleanup_git &&
177 git ls-files >lines &&
178 test_line_count = 2 lines &&
179 git log --oneline p4/master >lines &&
180 test_line_count = 1 lines
184 test_expect_success
'clone two dirs, @all' '
187 echo sub1/f3 >sub1/f3 &&
189 p4 submit -d "sub1/f3"
191 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
192 test_when_finished cleanup_git &&
195 git ls-files >lines &&
196 test_line_count = 3 lines &&
197 git log --oneline p4/master >lines &&
198 test_line_count = 3 lines
202 test_expect_success
'clone two dirs, @all, conflicting files' '
205 echo sub2/f3 >sub2/f3 &&
207 p4 submit -d "sub2/f3"
209 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
210 test_when_finished cleanup_git &&
213 git ls-files >lines &&
214 test_line_count = 3 lines &&
215 git log --oneline p4/master >lines &&
216 test_line_count = 4 lines &&
217 echo sub2/f3 >expected &&
222 test_expect_success
'clone two dirs, each edited by submit, single git commit' '
225 echo sub1/f4 >sub1/f4 &&
227 echo sub2/f4 >sub2/f4 &&
229 p4 submit -d "sub1/f4 and sub2/f4"
231 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
232 test_when_finished cleanup_git &&
235 git ls-files >lines &&
236 test_line_count = 4 lines &&
237 git log --oneline p4/master >lines &&
238 test_line_count = 5 lines
242 revision_ranges
="2000/01/01,#head \
244 2000/01/01,2080/01/01 \
248 test_expect_success
'clone using non-numeric revision ranges' '
249 test_when_finished cleanup_git &&
250 for r in $revision_ranges
254 git p4 clone --dest="$git" //depot@$r &&
257 git ls-files >lines &&
258 test_line_count = 8 lines
263 test_expect_success
'clone with date range, excluding some changes' '
264 test_when_finished cleanup_git &&
265 before=$(date +%Y/%m/%d:%H:%M:%S) &&
270 p4 add date_range_test &&
271 p4 submit -d "Adding file"
273 git p4 clone --dest="$git" //depot@1,$before &&
276 test_path_is_missing date_range_test
280 test_expect_success
'exit when p4 fails to produce marshaled output' '
282 test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
283 cat >badp4dir/p4 <<-EOF &&
287 chmod 755 badp4dir/p4 &&
289 PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
291 test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
293 test_grep ! Traceback errs
296 # Hide a file from p4d, make sure we catch its complaint. This won't fail in
297 # p4 changes, files, or describe; just in p4 print. If P4CLIENT is unset, the
298 # message will include "Librarian checkout".
299 test_expect_success
'exit gracefully for p4 server errors' '
300 test_when_finished "mv \"$db\"/depot/file1,v,hidden \"$db\"/depot/file1,v" &&
301 mv "$db"/depot/file1,v "$db"/depot/file1,v,hidden &&
302 test_when_finished cleanup_git &&
303 test_expect_code 1 git p4 clone --dest="$git" //depot@1 >out 2>err &&
304 test_grep "Error from p4 print" err
307 test_expect_success
'clone --bare should make a bare repository' '
309 git p4 clone --dest="$git" --bare //depot &&
310 test_when_finished cleanup_git &&
313 test_path_is_missing .git &&
314 git config --get --bool core.bare true &&
315 git rev-parse --verify refs/remotes/p4/master &&
316 git rev-parse --verify refs/remotes/p4/HEAD &&
317 git rev-parse --verify refs/heads/main &&
318 git rev-parse --verify HEAD
322 # Sleep a bit so that the top-most p4 change did not happen "now". Then
323 # import the repo and make sure that the initial import has the same time
324 # as the top-most change.
325 test_expect_success
'initial import time from top change time' '
326 p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) &&
327 p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) &&
329 git p4 clone --dest="$git" //depot &&
330 test_when_finished cleanup_git &&
333 gittime=$(git show -s --pretty=format:%at HEAD) &&
334 echo $p4time $gittime &&
335 test $p4time = $gittime
339 test_expect_success
'unresolvable host in P4PORT should display error' '
340 test_when_finished cleanup_git &&
341 git p4 clone --dest="$git" //depot &&
344 P4PORT=nosuchhost:65537 &&
346 test_expect_code 1 git p4 sync >out 2>err &&
347 grep "connect to nosuchhost" err
351 # Test following scenarios:
352 # - Without ".git/hooks/p4-pre-submit" , submit should continue
353 # - With the hook returning 0, submit should continue
354 # - With the hook returning 1, submit should abort
355 test_expect_success
'run hook p4-pre-submit before submit' '
356 test_when_finished cleanup_git &&
357 git p4 clone --dest="$git" //depot &&
360 echo "hello world" >hello.txt &&
362 git commit -m "add hello.txt" &&
363 git config git-p4.skipSubmitEdit true &&
364 git p4 submit --dry-run >out &&
365 grep "Would apply" out
367 test_hook -C "$git" p4-pre-submit <<-\EOF &&
372 git p4 submit --dry-run >out &&
373 grep "Would apply" out
375 test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
380 test_must_fail git p4 submit --dry-run >errs 2>&1 &&
381 ! grep "Would apply" errs
385 test_expect_success
'submit from detached head' '
386 test_when_finished cleanup_git &&
387 git p4 clone --dest="$git" //depot &&
390 git checkout p4/master &&
391 >detached_head_test &&
392 git add detached_head_test &&
393 git commit -m "add detached_head" &&
394 git config git-p4.skipSubmitEdit true &&
397 git log p4/master | grep detached_head
401 test_expect_success
'submit from worktree' '
402 test_when_finished cleanup_git &&
403 git p4 clone --dest="$git" //depot &&
406 git worktree add ../worktree-test
409 cd "$git/../worktree-test" &&
410 test_commit "worktree-commit" &&
411 git config git-p4.skipSubmitEdit true &&
417 test_path_is_file worktree-commit.t