Merge branch 'cl/config-regexp-docfix' into maint-2.46
[git/gitster.git] / t / t9810-git-p4-rcs.sh
blob15e32c9f353a73b47b7e2aab0be3636b3e5bfe5b
1 #!/bin/sh
3 test_description='git p4 rcs keywords'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./lib-git-p4.sh
8 CP1252="\223\224"
10 test_expect_success 'start p4d' '
11 start_p4d
15 # Make one file with keyword lines at the top, and
16 # enough plain text to be able to test modifications
17 # far away from the keywords.
19 test_expect_success 'init depot' '
21 cd "$cli" &&
22 cat <<-\EOF >filek &&
23 $Id$
24 /* $Revision$ */
25 # $Change$
26 line4
27 line5
28 line6
29 line7
30 line8
31 EOF
32 sed "s/Revision/Revision: do not scrub me/" <filek >fileko &&
33 sed "s/Id/Id: do not scrub me/" <fileko >file_text &&
34 p4 add -t text+k filek &&
35 p4 submit -d "filek" &&
36 p4 add -t text+ko fileko &&
37 p4 submit -d "fileko" &&
38 printf "$CP1252" >fileko_cp1252 &&
39 p4 add -t text+ko fileko_cp1252 &&
40 p4 submit -d "fileko_cp1252" &&
41 p4 add -t text file_text &&
42 p4 submit -d "file_text"
47 # Generate these in a function to make it easy to use single quote marks.
49 write_scrub_scripts () {
50 cat >"$TRASH_DIRECTORY/scrub_k.py" <<-\EOF &&
51 import re, sys
52 sys.stdout.write(re.sub(r'(?i)\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', sys.stdin.read()))
53 EOF
54 cat >"$TRASH_DIRECTORY/scrub_ko.py" <<-\EOF
55 import re, sys
56 sys.stdout.write(re.sub(r'(?i)\$(Id|Header):[^$]*\$', r'$\1$', sys.stdin.read()))
57 EOF
60 test_expect_success 'scrub scripts' '
61 write_scrub_scripts
65 # Compare $cli/file to its scrubbed version, should be different.
66 # Compare scrubbed $cli/file to $git/file, should be same.
68 scrub_k_check () {
69 file="$1" &&
70 scrub="$TRASH_DIRECTORY/$file" &&
71 "$PYTHON_PATH" "$TRASH_DIRECTORY/scrub_k.py" <"$git/$file" >"$scrub" &&
72 ! test_cmp "$cli/$file" "$scrub" &&
73 test_cmp "$git/$file" "$scrub" &&
74 rm "$scrub"
76 scrub_ko_check () {
77 file="$1" &&
78 scrub="$TRASH_DIRECTORY/$file" &&
79 "$PYTHON_PATH" "$TRASH_DIRECTORY/scrub_ko.py" <"$git/$file" >"$scrub" &&
80 ! test_cmp "$cli/$file" "$scrub" &&
81 test_cmp "$git/$file" "$scrub" &&
82 rm "$scrub"
86 # Modify far away from keywords. If no RCS lines show up
87 # in the diff, there is no conflict.
89 test_expect_success 'edit far away from RCS lines' '
90 test_when_finished cleanup_git &&
91 git p4 clone --dest="$git" //depot &&
93 cd "$git" &&
94 git config git-p4.skipSubmitEdit true &&
95 sed "s/^line7/line7 edit/" <filek >filek.tmp &&
96 mv -f filek.tmp filek &&
97 git commit -m "filek line7 edit" filek &&
98 git p4 submit &&
99 scrub_k_check filek
104 # Modify near the keywords. This will require RCS scrubbing.
106 test_expect_success 'edit near RCS lines' '
107 test_when_finished cleanup_git &&
108 git p4 clone --dest="$git" //depot &&
110 cd "$git" &&
111 git config git-p4.skipSubmitEdit true &&
112 git config git-p4.attemptRCSCleanup true &&
113 sed "s/^line4/line4 edit/" <filek >filek.tmp &&
114 mv -f filek.tmp filek &&
115 git commit -m "filek line4 edit" filek &&
116 git p4 submit &&
117 scrub_k_check filek
122 # Modify the keywords themselves. This also will require RCS scrubbing.
124 test_expect_success 'edit keyword lines' '
125 test_when_finished cleanup_git &&
126 git p4 clone --dest="$git" //depot &&
128 cd "$git" &&
129 git config git-p4.skipSubmitEdit true &&
130 git config git-p4.attemptRCSCleanup true &&
131 sed "/Revision/d" <filek >filek.tmp &&
132 mv -f filek.tmp filek &&
133 git commit -m "filek remove Revision line" filek &&
134 git p4 submit &&
135 scrub_k_check filek
140 # Scrubbing text+ko files should not alter all keywords, just Id, Header.
142 test_expect_success 'scrub ko files differently' '
143 test_when_finished cleanup_git &&
144 git p4 clone --dest="$git" //depot &&
146 cd "$git" &&
147 git config git-p4.skipSubmitEdit true &&
148 git config git-p4.attemptRCSCleanup true &&
149 sed "s/^line4/line4 edit/" <fileko >fileko.tmp &&
150 mv -f fileko.tmp fileko &&
151 git commit -m "fileko line4 edit" fileko &&
152 git p4 submit &&
153 scrub_ko_check fileko &&
154 ! scrub_k_check fileko
158 # hack; git p4 submit should do it on its own
159 test_expect_success 'cleanup after failure' '
161 cd "$cli" &&
162 p4 revert ...
166 # perl $File:: bug check
167 test_expect_success 'ktext expansion should not expand multi-line $File::' '
169 cd "$cli" &&
170 cat >lv.pm <<-\EOF &&
171 my $wanted = sub { my $f = $File::Find::name;
172 if ( -f && $f =~ /foo/ ) {
174 p4 add -t ktext lv.pm &&
175 p4 submit -d "lv.pm"
176 ) &&
177 test_when_finished cleanup_git &&
178 git p4 clone --dest="$git" //depot &&
180 cd "$git" &&
181 test_cmp "$cli/lv.pm" lv.pm
186 # Do not scrub anything but +k or +ko files. Sneak a change into
187 # the cli file so that submit will get a conflict. Make sure that
188 # scrubbing doesn't make a mess of things.
190 # This might happen only if the git repo is behind the p4 repo at
191 # submit time, and there is a conflict.
193 test_expect_success 'do not scrub plain text' '
194 test_when_finished cleanup_git &&
195 git p4 clone --dest="$git" //depot &&
197 cd "$git" &&
198 git config git-p4.skipSubmitEdit true &&
199 git config git-p4.attemptRCSCleanup true &&
200 sed "s/^line4/line4 edit/" <file_text >file_text.tmp &&
201 mv -f file_text.tmp file_text &&
202 git commit -m "file_text line4 edit" file_text &&
204 cd "$cli" &&
205 p4 open file_text &&
206 sed "s/^line5/line5 p4 edit/" <file_text >file_text.tmp &&
207 mv -f file_text.tmp file_text &&
208 p4 submit -d "file5 p4 edit"
209 ) &&
210 echo s | test_expect_code 1 git p4 submit &&
212 # make sure the file is not left open
213 cd "$cli" &&
214 ! p4 fstat -T action file_text
219 # hack; git p4 submit should do it on its own
220 test_expect_success 'cleanup after failure 2' '
222 cd "$cli" &&
223 p4 revert ...
227 create_kw_file () {
228 cat <<\EOF >"$1"
229 /* A file
230 Id: $Id$
231 Revision: $Revision$
232 File: $File$
234 int main(int argc, const char **argv) {
235 return 0;
240 test_expect_success 'add kwfile' '
242 cd "$cli" &&
243 echo file1 >file1 &&
244 p4 add file1 &&
245 p4 submit -d "file 1" &&
246 create_kw_file kwfile1.c &&
247 p4 add kwfile1.c &&
248 p4 submit -d "Add rcw kw file" kwfile1.c
252 p4_append_to_file () {
253 f="$1" &&
254 p4 edit -t ktext "$f" &&
255 echo "/* $(date) */" >>"$f" &&
256 p4 submit -d "appending a line in p4"
259 # Create some files with RCS keywords. If they get modified
260 # elsewhere then the version number gets bumped which then
261 # results in a merge conflict if we touch the RCS kw lines,
262 # even though the change itself would otherwise apply cleanly.
263 test_expect_success 'cope with rcs keyword expansion damage' '
264 test_when_finished cleanup_git &&
265 git p4 clone --dest="$git" //depot &&
267 cd "$git" &&
268 git config git-p4.skipSubmitEdit true &&
269 git config git-p4.attemptRCSCleanup true &&
270 (cd "$cli" && p4_append_to_file kwfile1.c) &&
271 old_lines=$(wc -l <kwfile1.c) &&
272 perl -n -i -e "print unless m/Revision:/" kwfile1.c &&
273 new_lines=$(wc -l <kwfile1.c) &&
274 test $new_lines = $(($old_lines - 1)) &&
276 git add kwfile1.c &&
277 git commit -m "Zap an RCS kw line" &&
278 git p4 submit &&
279 git p4 rebase &&
280 git diff p4/master &&
281 git p4 commit &&
282 echo "try modifying in both" &&
283 cd "$cli" &&
284 p4 edit kwfile1.c &&
285 echo "line from p4" >>kwfile1.c &&
286 p4 submit -d "add a line in p4" kwfile1.c &&
287 cd "$git" &&
288 echo "line from git at the top" | cat - kwfile1.c >kwfile1.c.new &&
289 mv kwfile1.c.new kwfile1.c &&
290 git commit -m "Add line in git at the top" kwfile1.c &&
291 git p4 rebase &&
292 git p4 submit
296 test_expect_success 'cope with rcs keyword file deletion' '
297 test_when_finished cleanup_git &&
299 cd "$cli" &&
300 echo "\$Revision\$" >kwdelfile.c &&
301 p4 add -t ktext kwdelfile.c &&
302 p4 submit -d "Add file to be deleted" &&
303 grep 1 kwdelfile.c
304 ) &&
305 git p4 clone --dest="$git" //depot &&
307 cd "$git" &&
308 grep Revision kwdelfile.c &&
309 git rm -f kwdelfile.c &&
310 git commit -m "Delete a file containing RCS keywords" &&
311 git config git-p4.skipSubmitEdit true &&
312 git config git-p4.attemptRCSCleanup true &&
313 git p4 submit
314 ) &&
316 cd "$cli" &&
317 p4 sync &&
318 ! test -f kwdelfile.c
322 # If you add keywords in git of the form $Header$ then everything should
323 # work fine without any special handling.
324 test_expect_success 'Add keywords in git which match the default p4 values' '
325 test_when_finished cleanup_git &&
326 git p4 clone --dest="$git" //depot &&
328 cd "$git" &&
329 echo "NewKW: \$Revision\$" >>kwfile1.c &&
330 git add kwfile1.c &&
331 git commit -m "Adding RCS keywords in git" &&
332 git config git-p4.skipSubmitEdit true &&
333 git config git-p4.attemptRCSCleanup true &&
334 git p4 submit
335 ) &&
337 cd "$cli" &&
338 p4 sync &&
339 test -f kwfile1.c &&
340 grep "NewKW.*Revision.*[0-9]" kwfile1.c
345 # If you add keywords in git of the form $Header:#1$ then things will fail
346 # unless git-p4 takes steps to scrub the *git* commit.
348 test_expect_failure 'Add keywords in git which do not match the default p4 values' '
349 test_when_finished cleanup_git &&
350 git p4 clone --dest="$git" //depot &&
352 cd "$git" &&
353 echo "NewKW2: \$Revision:1\$" >>kwfile1.c &&
354 git add kwfile1.c &&
355 git commit -m "Adding RCS keywords in git" &&
356 git config git-p4.skipSubmitEdit true &&
357 git config git-p4.attemptRCSCleanup true &&
358 git p4 submit
359 ) &&
361 cd "$cli" &&
362 p4 sync &&
363 grep "NewKW2.*Revision.*[0-9]" kwfile1.c
368 test_expect_success 'check cp1252 smart quote are preserved through RCS keyword processing' '
369 test_when_finished cleanup_git &&
370 git p4 clone --dest="$git" //depot &&
372 cd "$git" &&
373 printf "$CP1252" >expect &&
374 test_cmp_bin expect fileko_cp1252
378 test_done