Sync with 'maint'
[alt-git.git] / t / t9800-git-p4-basic.sh
blob3e6dfce24833a061acff8babe5135885708aab41
1 #!/bin/sh
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
9 . ./lib-git-p4.sh
11 test_expect_success 'start p4d' '
12 start_p4d
15 test_expect_success 'add p4 files' '
17 cd "$cli" &&
18 echo file1 >file1 &&
19 p4 add file1 &&
20 p4 submit -d "file1" &&
21 echo file2 >file2 &&
22 p4 add file2 &&
23 p4 submit -d "file2"
27 test_expect_success 'basic git p4 clone' '
28 git p4 clone --dest="$git" //depot &&
29 test_when_finished cleanup_git &&
31 cd "$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 &&
46 cd "$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 &&
56 cd "$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 &&
70 cd "$git" &&
71 test_commit head &&
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 &&
86 cd "$git" &&
87 test_commit head &&
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 &&
101 cd "$git" &&
102 test_commit head &&
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 &&
116 cd "$git" &&
117 test_commit head &&
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 &&
131 cd "$git" &&
132 test_commit head &&
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 &&
143 cd "$git" &&
144 test_commit head &&
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 &&
155 cd "$git" &&
156 test_commit head &&
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' '
165 cd "$cli" &&
166 mkdir sub1 sub2 &&
167 echo sub1/f1 >sub1/f1 &&
168 echo sub2/f2 >sub2/f2 &&
169 p4 add sub1/f1 &&
170 p4 submit -d "sub1/f1" &&
171 p4 add sub2/f2 &&
172 p4 submit -d "sub2/f2"
173 ) &&
174 git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
175 test_when_finished cleanup_git &&
177 cd "$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' '
187 cd "$cli" &&
188 echo sub1/f3 >sub1/f3 &&
189 p4 add sub1/f3 &&
190 p4 submit -d "sub1/f3"
191 ) &&
192 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
193 test_when_finished cleanup_git &&
195 cd "$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' '
205 cd "$cli" &&
206 echo sub2/f3 >sub2/f3 &&
207 p4 add sub2/f3 &&
208 p4 submit -d "sub2/f3"
209 ) &&
210 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
211 test_when_finished cleanup_git &&
213 cd "$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 &&
219 test_cmp expected f3
223 test_expect_success 'clone two dirs, each edited by submit, single git commit' '
225 cd "$cli" &&
226 echo sub1/f4 >sub1/f4 &&
227 p4 add sub1/f4 &&
228 echo sub2/f4 >sub2/f4 &&
229 p4 add sub2/f4 &&
230 p4 submit -d "sub1/f4 and sub2/f4"
231 ) &&
232 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
233 test_when_finished cleanup_git &&
235 cd "$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 \
244 1,2080/01/01 \
245 2000/01/01,2080/01/01 \
246 2000/01/01,1000 \
247 1,1000"
249 test_expect_success 'clone using non-numeric revision ranges' '
250 test_when_finished cleanup_git &&
251 for r in $revision_ranges
253 rm -fr "$git" &&
254 test ! -d "$git" &&
255 git p4 clone --dest="$git" //depot@$r &&
257 cd "$git" &&
258 git ls-files >lines &&
259 test_line_count = 8 lines
260 ) || return 1
261 done
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) &&
267 sleep 2 &&
269 cd "$cli" &&
270 :>date_range_test &&
271 p4 add date_range_test &&
272 p4 submit -d "Adding file"
273 ) &&
274 git p4 clone --dest="$git" //depot@1,$before &&
276 cd "$git" &&
277 test_path_is_missing date_range_test
281 test_expect_success 'exit when p4 fails to produce marshaled output' '
282 mkdir badp4dir &&
283 test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
284 cat >badp4dir/p4 <<-EOF &&
285 #!$SHELL_PATH
286 exit 1
288 chmod 755 badp4dir/p4 &&
290 PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
291 export PATH &&
292 test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
293 ) &&
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
305 *,v)
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;;
308 *,d)
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";;
314 esac &&
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' '
321 rm -rf "$git" &&
322 git p4 clone --dest="$git" --bare //depot &&
323 test_when_finished cleanup_git &&
325 cd "$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) &&
341 sleep 3 &&
342 git p4 clone --dest="$git" //depot &&
343 test_when_finished cleanup_git &&
345 cd "$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 &&
356 cd "$git" &&
357 P4PORT=nosuchhost:65537 &&
358 export P4PORT &&
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 &&
372 cd "$git" &&
373 echo "hello world" >hello.txt &&
374 git add 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
379 ) &&
380 test_hook -C "$git" p4-pre-submit <<-\EOF &&
381 exit 0
384 cd "$git" &&
385 git p4 submit --dry-run >out &&
386 grep "Would apply" out
387 ) &&
388 test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
389 exit 1
392 cd "$git" &&
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 &&
402 cd "$git" &&
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 &&
408 git p4 submit &&
409 git p4 rebase &&
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 &&
418 cd "$git" &&
419 git worktree add ../worktree-test
420 ) &&
422 cd "$git/../worktree-test" &&
423 test_commit "worktree-commit" &&
424 git config git-p4.skipSubmitEdit true &&
425 git p4 submit
426 ) &&
428 cd "$cli" &&
429 p4 sync &&
430 test_path_is_file worktree-commit.t
434 test_done