Merge branch 'ph/rerere-doc' into maint
[git] / t / t9809-git-p4-client-view.sh
1 #!/bin/sh
2
3 test_description='git-p4 client view'
4
5 . ./lib-git-p4.sh
6
7 test_expect_success 'start p4d' '
8         start_p4d
9 '
10
11 #
12 # Construct a client with this list of View lines
13 #
14 client_view() {
15         (
16                 cat <<-EOF &&
17                 Client: client
18                 Description: client
19                 Root: $cli
20                 View:
21                 EOF
22                 for arg ; do
23                         printf "\t$arg\n"
24                 done
25         ) | p4 client -i
26 }
27
28 #
29 # Verify these files exist, exactly.  Caller creates
30 # a list of files in file "files".
31 #
32 check_files_exist() {
33         ok=0 &&
34         num=$# &&
35         for arg ; do
36                 test_path_is_file "$arg" &&
37                 ok=$(($ok + 1))
38         done &&
39         test $ok -eq $num &&
40         test_line_count = $num files
41 }
42
43 #
44 # Sync up the p4 client, make sure the given files (and only
45 # those) exist.
46 #
47 client_verify() {
48         (
49                 cd "$cli" &&
50                 p4 sync &&
51                 find . -type f ! -name files >files &&
52                 check_files_exist "$@"
53         )
54 }
55
56 #
57 # Make sure the named files, exactly, exist.
58 #
59 git_verify() {
60         (
61                 cd "$git" &&
62                 git ls-files >files &&
63                 check_files_exist "$@"
64         )
65 }
66
67 # //depot
68 #   - dir1
69 #     - file11
70 #     - file12
71 #   - dir2
72 #     - file21
73 #     - file22
74 init_depot() {
75         for d in 1 2 ; do
76                 mkdir -p dir$d &&
77                 for f in 1 2 ; do
78                         echo dir$d/file$d$f >dir$d/file$d$f &&
79                         p4 add dir$d/file$d$f &&
80                         p4 submit -d "dir$d/file$d$f"
81                 done
82         done &&
83         find . -type f ! -name files >files &&
84         check_files_exist dir1/file11 dir1/file12 \
85                           dir2/file21 dir2/file22
86 }
87
88 test_expect_success 'init depot' '
89         (
90                 cd "$cli" &&
91                 init_depot
92         )
93 '
94
95 # double % for printf
96 test_expect_success 'unsupported view wildcard %%n' '
97         client_view "//depot/%%%%1/sub/... //client/sub/%%%%1/..." &&
98         test_when_finished cleanup_git &&
99         test_must_fail "$GITP4" clone --use-client-spec --dest="$git" //depot
100 '
101
102 test_expect_success 'unsupported view wildcard *' '
103         client_view "//depot/*/bar/... //client/*/bar/..." &&
104         test_when_finished cleanup_git &&
105         test_must_fail "$GITP4" clone --use-client-spec --dest="$git" //depot
106 '
107
108 test_expect_success 'wildcard ... only supported at end of spec' '
109         client_view "//depot/.../file11 //client/.../file11" &&
110         test_when_finished cleanup_git &&
111         test_must_fail "$GITP4" clone --use-client-spec --dest="$git" //depot
112 '
113
114 test_expect_success 'basic map' '
115         client_view "//depot/dir1/... //client/cli1/..." &&
116         files="cli1/file11 cli1/file12" &&
117         client_verify $files &&
118         test_when_finished cleanup_git &&
119         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
120         git_verify $files
121 '
122
123 test_expect_success 'client view with no mappings' '
124         client_view &&
125         client_verify &&
126         test_when_finished cleanup_git &&
127         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
128         git_verify
129 '
130
131 test_expect_success 'single file map' '
132         client_view "//depot/dir1/file11 //client/file11" &&
133         files="file11" &&
134         client_verify $files &&
135         test_when_finished cleanup_git &&
136         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
137         git_verify $files
138 '
139
140 test_expect_success 'later mapping takes precedence (entire repo)' '
141         client_view "//depot/dir1/... //client/cli1/..." \
142                     "//depot/... //client/cli2/..." &&
143         files="cli2/dir1/file11 cli2/dir1/file12
144                cli2/dir2/file21 cli2/dir2/file22" &&
145         client_verify $files &&
146         test_when_finished cleanup_git &&
147         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
148         git_verify $files
149 '
150
151 test_expect_success 'later mapping takes precedence (partial repo)' '
152         client_view "//depot/dir1/... //client/..." \
153                     "//depot/dir2/... //client/..." &&
154         files="file21 file22" &&
155         client_verify $files &&
156         test_when_finished cleanup_git &&
157         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
158         git_verify $files
159 '
160
161 # Reading the view backwards,
162 #   dir2 goes to cli12
163 #   dir1 cannot go to cli12 since it was filled by dir2
164 #   dir1 also does not go to cli3, since the second rule
165 #     noticed that it matched, but was already filled
166 test_expect_success 'depot path matching rejected client path' '
167         client_view "//depot/dir1/... //client/cli3/..." \
168                     "//depot/dir1/... //client/cli12/..." \
169                     "//depot/dir2/... //client/cli12/..." &&
170         files="cli12/file21 cli12/file22" &&
171         client_verify $files &&
172         test_when_finished cleanup_git &&
173         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
174         git_verify $files
175 '
176
177 # since both have the same //client/..., the exclusion
178 # rule keeps everything out
179 test_expect_success 'exclusion wildcard, client rhs same (odd)' '
180         client_view "//depot/... //client/..." \
181                     "-//depot/dir2/... //client/..." &&
182         client_verify &&
183         test_when_finished cleanup_git &&
184         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
185         git_verify
186 '
187
188 test_expect_success 'exclusion wildcard, client rhs different (normal)' '
189         client_view "//depot/... //client/..." \
190                     "-//depot/dir2/... //client/dir2/..." &&
191         files="dir1/file11 dir1/file12" &&
192         client_verify $files &&
193         test_when_finished cleanup_git &&
194         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
195         git_verify $files
196 '
197
198 test_expect_success 'exclusion single file' '
199         client_view "//depot/... //client/..." \
200                     "-//depot/dir2/file22 //client/file22" &&
201         files="dir1/file11 dir1/file12 dir2/file21" &&
202         client_verify $files &&
203         test_when_finished cleanup_git &&
204         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
205         git_verify $files
206 '
207
208 test_expect_success 'overlay wildcard' '
209         client_view "//depot/dir1/... //client/cli/..." \
210                     "+//depot/dir2/... //client/cli/...\n" &&
211         files="cli/file11 cli/file12 cli/file21 cli/file22" &&
212         client_verify $files &&
213         test_when_finished cleanup_git &&
214         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
215         git_verify $files
216 '
217
218 test_expect_success 'overlay single file' '
219         client_view "//depot/dir1/... //client/cli/..." \
220                     "+//depot/dir2/file21 //client/cli/file21" &&
221         files="cli/file11 cli/file12 cli/file21" &&
222         client_verify $files &&
223         test_when_finished cleanup_git &&
224         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
225         git_verify $files
226 '
227
228 test_expect_success 'exclusion with later inclusion' '
229         client_view "//depot/... //client/..." \
230                     "-//depot/dir2/... //client/dir2/..." \
231                     "//depot/dir2/... //client/dir2incl/..." &&
232         files="dir1/file11 dir1/file12 dir2incl/file21 dir2incl/file22" &&
233         client_verify $files &&
234         test_when_finished cleanup_git &&
235         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
236         git_verify $files
237 '
238
239 test_expect_success 'quotes on rhs only' '
240         client_view "//depot/dir1/... \"//client/cdir 1/...\"" &&
241         client_verify "cdir 1/file11" "cdir 1/file12" &&
242         test_when_finished cleanup_git &&
243         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
244         git_verify "cdir 1/file11" "cdir 1/file12"
245 '
246
247 #
248 # Submit tests
249 #
250
251 # clone sets variable
252 test_expect_success 'clone --use-client-spec sets useClientSpec' '
253         client_view "//depot/... //client/..." &&
254         test_when_finished cleanup_git &&
255         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
256         (
257                 cd "$git" &&
258                 git config --bool git-p4.useClientSpec >actual &&
259                 echo true >true &&
260                 test_cmp actual true
261         )
262 '
263
264 # clone just a subdir of the client spec
265 test_expect_success 'subdir clone' '
266         client_view "//depot/... //client/..." &&
267         files="dir1/file11 dir1/file12 dir2/file21 dir2/file22" &&
268         client_verify $files &&
269         test_when_finished cleanup_git &&
270         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
271         git_verify dir1/file11 dir1/file12
272 '
273
274 #
275 # submit back, see what happens:  five cases
276 #
277 test_expect_success 'subdir clone, submit modify' '
278         client_view "//depot/... //client/..." &&
279         test_when_finished cleanup_git &&
280         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
281         (
282                 cd "$git" &&
283                 git config git-p4.skipSubmitEdit true &&
284                 echo line >>dir1/file12 &&
285                 git add dir1/file12 &&
286                 git commit -m dir1/file12 &&
287                 "$GITP4" submit
288         ) &&
289         (
290                 cd "$cli" &&
291                 test_path_is_file dir1/file12 &&
292                 test_line_count = 2 dir1/file12
293         )
294 '
295
296 test_expect_success 'subdir clone, submit add' '
297         client_view "//depot/... //client/..." &&
298         test_when_finished cleanup_git &&
299         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
300         (
301                 cd "$git" &&
302                 git config git-p4.skipSubmitEdit true &&
303                 echo file13 >dir1/file13 &&
304                 git add dir1/file13 &&
305                 git commit -m dir1/file13 &&
306                 "$GITP4" submit
307         ) &&
308         (
309                 cd "$cli" &&
310                 test_path_is_file dir1/file13
311         )
312 '
313
314 test_expect_success 'subdir clone, submit delete' '
315         client_view "//depot/... //client/..." &&
316         test_when_finished cleanup_git &&
317         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
318         (
319                 cd "$git" &&
320                 git config git-p4.skipSubmitEdit true &&
321                 git rm dir1/file12 &&
322                 git commit -m "delete dir1/file12" &&
323                 "$GITP4" submit
324         ) &&
325         (
326                 cd "$cli" &&
327                 test_path_is_missing dir1/file12
328         )
329 '
330
331 test_expect_success 'subdir clone, submit copy' '
332         client_view "//depot/... //client/..." &&
333         test_when_finished cleanup_git &&
334         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
335         (
336                 cd "$git" &&
337                 git config git-p4.skipSubmitEdit true &&
338                 git config git-p4.detectCopies true &&
339                 cp dir1/file11 dir1/file11a &&
340                 git add dir1/file11a &&
341                 git commit -m "copy to dir1/file11a" &&
342                 "$GITP4" submit
343         ) &&
344         (
345                 cd "$cli" &&
346                 test_path_is_file dir1/file11a
347         )
348 '
349
350 test_expect_success 'subdir clone, submit rename' '
351         client_view "//depot/... //client/..." &&
352         test_when_finished cleanup_git &&
353         "$GITP4" clone --use-client-spec --dest="$git" //depot/dir1 &&
354         (
355                 cd "$git" &&
356                 git config git-p4.skipSubmitEdit true &&
357                 git config git-p4.detectRenames true &&
358                 git mv dir1/file13 dir1/file13a &&
359                 git commit -m "rename dir1/file13 to dir1/file13a" &&
360                 "$GITP4" submit
361         ) &&
362         (
363                 cd "$cli" &&
364                 test_path_is_missing dir1/file13 &&
365                 test_path_is_file dir1/file13a
366         )
367 '
368
369 test_expect_success 'reinit depot' '
370         (
371                 cd "$cli" &&
372                 p4 sync -f &&
373                 rm files &&
374                 p4 delete */* &&
375                 p4 submit -d "delete all files" &&
376                 init_depot
377         )
378 '
379
380 #
381 # Rename directories to test quoting in depot-side mappings
382 # //depot
383 #    - "dir 1"
384 #       - file11
385 #       - file12
386 #    - "dir 2"
387 #       - file21
388 #       - file22
389 #
390 test_expect_success 'rename files to introduce spaces' '
391         client_view "//depot/... //client/..." &&
392         client_verify dir1/file11 dir1/file12 \
393                       dir2/file21 dir2/file22 &&
394         (
395                 cd "$cli" &&
396                 p4 open dir1/... &&
397                 p4 move dir1/... "dir 1"/... &&
398                 p4 open dir2/... &&
399                 p4 move dir2/... "dir 2"/... &&
400                 p4 submit -d "rename with spaces"
401         ) &&
402         client_verify "dir 1/file11" "dir 1/file12" \
403                       "dir 2/file21" "dir 2/file22"
404 '
405
406 test_expect_success 'quotes on lhs only' '
407         client_view "\"//depot/dir 1/...\" //client/cdir1/..." &&
408         files="cdir1/file11 cdir1/file12" &&
409         client_verify $files &&
410         test_when_finished cleanup_git &&
411         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
412         client_verify $files
413 '
414
415 test_expect_success 'quotes on both sides' '
416         client_view "\"//depot/dir 1/...\" \"//client/cdir 1/...\"" &&
417         client_verify "cdir 1/file11" "cdir 1/file12" &&
418         test_when_finished cleanup_git &&
419         "$GITP4" clone --use-client-spec --dest="$git" //depot &&
420         git_verify "cdir 1/file11" "cdir 1/file12"
421 '
422
423 test_expect_success 'kill p4d' '
424         kill_p4d
425 '
426
427 test_done