apply: don't leak fd on fdopen() error
[git.git] / t / t9800-git-p4-basic.sh
blob53af8e34ac1c40a31dee6e6f4449f8c52dcf6f59
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 . ./lib-git-p4.sh
10 test_expect_success 'start p4d' '
11 start_p4d
14 test_expect_success 'add p4 files' '
16 cd "$cli" &&
17 echo file1 >file1 &&
18 p4 add file1 &&
19 p4 submit -d "file1" &&
20 echo file2 >file2 &&
21 p4 add file2 &&
22 p4 submit -d "file2"
26 test_expect_success 'basic git p4 clone' '
27 git p4 clone --dest="$git" //depot &&
28 test_when_finished cleanup_git &&
30 cd "$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 &&
45 cd "$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 &&
55 cd "$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 &&
69 cd "$git" &&
70 test_commit head &&
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 &&
85 cd "$git" &&
86 test_commit head &&
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 &&
100 cd "$git" &&
101 test_commit head &&
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 &&
115 cd "$git" &&
116 test_commit head &&
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 &&
130 cd "$git" &&
131 test_commit head &&
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 &&
142 cd "$git" &&
143 test_commit head &&
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 &&
154 cd "$git" &&
155 test_commit head &&
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' '
164 cd "$cli" &&
165 mkdir sub1 sub2 &&
166 echo sub1/f1 >sub1/f1 &&
167 echo sub2/f2 >sub2/f2 &&
168 p4 add sub1/f1 &&
169 p4 submit -d "sub1/f1" &&
170 p4 add sub2/f2 &&
171 p4 submit -d "sub2/f2"
172 ) &&
173 git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
174 test_when_finished cleanup_git &&
176 cd "$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' '
186 cd "$cli" &&
187 echo sub1/f3 >sub1/f3 &&
188 p4 add sub1/f3 &&
189 p4 submit -d "sub1/f3"
190 ) &&
191 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
192 test_when_finished cleanup_git &&
194 cd "$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' '
204 cd "$cli" &&
205 echo sub2/f3 >sub2/f3 &&
206 p4 add sub2/f3 &&
207 p4 submit -d "sub2/f3"
208 ) &&
209 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
210 test_when_finished cleanup_git &&
212 cd "$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 &&
218 test_cmp expected f3
222 test_expect_success 'clone two dirs, each edited by submit, single git commit' '
224 cd "$cli" &&
225 echo sub1/f4 >sub1/f4 &&
226 p4 add sub1/f4 &&
227 echo sub2/f4 >sub2/f4 &&
228 p4 add sub2/f4 &&
229 p4 submit -d "sub1/f4 and sub2/f4"
230 ) &&
231 git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
232 test_when_finished cleanup_git &&
234 cd "$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 \
243 1,2080/01/01 \
244 2000/01/01,2080/01/01 \
245 2000/01/01,1000 \
246 1,1000"
248 test_expect_success 'clone using non-numeric revision ranges' '
249 test_when_finished cleanup_git &&
250 for r in $revision_ranges
252 rm -fr "$git" &&
253 test ! -d "$git" &&
254 git p4 clone --dest="$git" //depot@$r &&
256 cd "$git" &&
257 git ls-files >lines &&
258 test_line_count = 8 lines
259 ) || return 1
260 done
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) &&
266 sleep 2 &&
268 cd "$cli" &&
269 :>date_range_test &&
270 p4 add date_range_test &&
271 p4 submit -d "Adding file"
272 ) &&
273 git p4 clone --dest="$git" //depot@1,$before &&
275 cd "$git" &&
276 test_path_is_missing date_range_test
280 test_expect_success 'exit when p4 fails to produce marshaled output' '
281 mkdir badp4dir &&
282 test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
283 cat >badp4dir/p4 <<-EOF &&
284 #!$SHELL_PATH
285 exit 1
287 chmod 755 badp4dir/p4 &&
289 PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
290 export PATH &&
291 test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
292 ) &&
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' '
308 rm -rf "$git" &&
309 git p4 clone --dest="$git" --bare //depot &&
310 test_when_finished cleanup_git &&
312 cd "$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) &&
328 sleep 3 &&
329 git p4 clone --dest="$git" //depot &&
330 test_when_finished cleanup_git &&
332 cd "$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 &&
343 cd "$git" &&
344 P4PORT=nosuchhost:65537 &&
345 export P4PORT &&
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 &&
359 cd "$git" &&
360 echo "hello world" >hello.txt &&
361 git add 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
366 ) &&
367 test_hook -C "$git" p4-pre-submit <<-\EOF &&
368 exit 0
371 cd "$git" &&
372 git p4 submit --dry-run >out &&
373 grep "Would apply" out
374 ) &&
375 test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
376 exit 1
379 cd "$git" &&
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 &&
389 cd "$git" &&
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 &&
395 git p4 submit &&
396 git p4 rebase &&
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 &&
405 cd "$git" &&
406 git worktree add ../worktree-test
407 ) &&
409 cd "$git/../worktree-test" &&
410 test_commit "worktree-commit" &&
411 git config git-p4.skipSubmitEdit true &&
412 git p4 submit
413 ) &&
415 cd "$cli" &&
416 p4 sync &&
417 test_path_is_file worktree-commit.t
421 test_done