Some escapes were mentioned twice, although they're not qr//-specific
[p5sagit/p5-mst-13.2.git] / Porting / patching.pod
CommitLineData
55d729e4 1=head1 Name
2
3patching.pod - Appropriate format for patches to the perl source tree
4
55d729e4 5=head2 How to contribute to this document
6
1266ad8f 7You may mail corrections, additions, and suggestions by following the
8instructions set forth in this document and submitting a patch :).
55d729e4 9
10=head1 Description
11
12=head2 Why this document exists
13
14As an open source project Perl relies on patches and contributions from
15its users to continue functioning properly and to root out the inevitable
16bugs. But, some users are unsure as to the I<right> way to prepare a patch
17and end up submitting seriously malformed patches. This makes it very
18difficult for the current maintainer to integrate said patches into their
19distribution. This document sets out usage guidelines for patches in an
20attempt to make everybody's life easier.
21
22=head2 Common problems
23
24The most common problems appear to be patches being mangled by certain
25mailers (I won't name names, but most of these seem to be originating on
54aff467 26boxes running a certain popular commercial operating system). Other problems
55d729e4 27include patches not rooted in the appropriate place in the directory structure,
28and patches not produced using standard utilities (such as diff).
29
30=head1 Proper Patch Guidelines
31
f556e4ac 32=head2 What to patch
33
34Generally speaking you should patch the latest development release
35of perl. The maintainers of the individual branches will see to it
36that patches are picked up and applied as appropriate.
37
55d729e4 38=head2 How to prepare your patch
39
40=over 4
41
42=item Creating your patch
43
44First, back up the original files. This can't be stressed enough,
45back everything up _first_.
46
47Also, please create patches against a clean distribution of the perl source.
54aff467 48This ensures that everyone else can apply your patch without clobbering their
55d729e4 49source tree.
50
51=item diff
52
53While individual tastes vary (and are not the point here) patches should
54be created using either C<-u> or C<-c> arguments to diff. These produce,
55respectively, unified diffs (where the changed line appears immediately next
56to the original) and context diffs (where several lines surrounding the changes
57are included). See the manpage for diff for more details.
58
4a92fa57 59When GNU diff is available, the pumpkins would prefer you use C<-u -p>
60(--unified --show-c-function) as arguments for optimal control. The
61examples below will only use -u.
62
54aff467 63The preferred method for creating a unified diff suitable for feeding
64to the patch program is:
55d729e4 65
54aff467 66 diff -u old-file new-file > patch-file
55d729e4 67
54aff467 68Note the order of files. See below for how to create a patch from
69two directory trees.
55d729e4 70
54aff467 71If your patch is for wider consumption, it may be better to create it as
72a context diff as some machines have broken patch utilities that choke on
73unified diffs. A context diff is made using C<diff -c> rather than
74C<diff -u>.
55d729e4 75
9e52009c 76GNU diff has many desirable features not provided by most vendor-supplied
77diffs. Some examples using GNU diff:
78
79 # generate a patch for a newly added file
80 % diff -u /dev/null new/file
1266ad8f 81
9e52009c 82 # generate a patch to remove a file (patch > v2.4 will remove it cleanly)
83 % diff -u old/goner /dev/null
1266ad8f 84
9e52009c 85 # get additions, deletions along with everything else, recursively
86 % diff -ruN olddir newdir
1266ad8f 87
9e52009c 88 # ignore whitespace
89 % diff -bu a/file b/file
1266ad8f 90
9e52009c 91 # show function name in every hunk (safer, more informative)
4a92fa57 92 % diff -u -p old/file new/file
9e52009c 93 % diff -u -F '^[_a-zA-Z0-9]+ *(' old/file new/file
94
4a92fa57 95 # show sub name in perl files and modules
96 % diff -u -F '^sub' old/file.pm new/file.pm
97
98 # show header in doc patches
99 % diff -u -F '^=head' old/file.pod new/file.pod
100
39f9fc43 101=item Derived Files
54aff467 102
103Many files in the distribution are derivative--avoid patching them.
104Patch the originals instead. Most utilities (like perldoc) are in
105this category, i.e. patch utils/perldoc.PL rather than utils/perldoc.
106Similarly, don't create patches for files under $src_root/ext from
107their copies found in $install_root/lib. If you are unsure about the
108proper location of a file that may have gotten copied while building
109the source distribution, consult the C<MANIFEST>.
55d729e4 110
111=item Filenames
112
113The most usual convention when submitting patches for a single file is to make
114your changes to a copy of the file with the same name as the original. Rename
54aff467 115the original file in such a way that it is obvious what is being patched
116($file.dist or $file.old seem to be popular).
117
118If you are submitting patches that affect multiple files then you should
119backup the entire directory tree (to $source_root.old/ for example). This
120will allow C<diff -ruN old-dir new-dir> to create all the patches at once.
55d729e4 121
39f9fc43 122=item Directories
123
124IMPORTANT: Patches should be generated from the source root directory, not
125from the directory that the patched file resides in. This ensures that the
126maintainer patches the proper file.
127
128For larger patches that are dealing with multiple files or
129directories, Johan Vromans has written a powerful utility: makepatch.
130See the JV directory on CPAN for the current version. If you have this
131program available, it is recommended to create a duplicate of the perl
132directory tree against which you are intending to provide a patch and
133let makepatch figure out all the changes you made to your copy of the
134sources. As perl comes with a MANIFEST file, you need not delete
135object files and other derivative files from the two directory trees,
136makepatch is smart about them.
137
138Say, you have created a directory perl-5.7.1@8685/ for the perl you
139are taking as the base and a directory perl-5.7.1@8685-withfoo/ where
140you have your changes, you would run makepatch as follows:
141
142 makepatch -oldman perl-5.7.1@8685/MANIFEST \
143 -newman perl-5.7.1@8685-withfoo/MANIFEST \
144 -diff "diff -u" \
145 perl-5.7.1@8685 perl-5.7.1@8685-withfoo
146
687d3573 147=item Binary Files
148
21f7822e 149Since the patch(1) utility cannot deal with binary files, it's important
150that you either avoid the use of binary files in your patch, generate the
151files dynamically, or that you encode any binary files using the
1266ad8f 152F<uupacktool.pl> utility.
687d3573 153
21f7822e 154Assuming you needed to include a gzip-encoded file for a module's test
1266ad8f 155suite, you might do this as follows using the F<uupacktool.pl> utility:
687d3573 156
1266ad8f 157 $ perl uupacktool.pl -v -p -D lib/Some/Module/t/src/t.gz
687d3573 158 Writing lib/Some/Module/t/src/t.gz into lib/Some/Module/t/src/t.gz.packed
159
21f7822e 160This will replace the C<t.gz> file with an encoded counterpart. During
161C<make test>, before any tests are run, perl's Makefile will restore all
162the C<.packed> files mentioned in the MANIFEST to their original name.
163This means that the test suite does not need to be aware of this packing
164scheme and will not need to be altered.
687d3573 165
54aff467 166=item Try it yourself
167
168Just to make sure your patch "works", be sure to apply it to the Perl
169distribution, rebuild everything, and make sure the testsuite runs
170without incident.
55d729e4 171
172=back
173
174=head2 What to include in your patch
175
176=over 4
177
178=item Description of problem
179
180The first thing you should include is a description of the problem that
181the patch corrects. If it is a code patch (rather than a documentation
182patch) you should also include a small test case that illustrates the
183bug.
184
54aff467 185=item Directions for application
55d729e4 186
187You should include instructions on how to properly apply your patch.
188These should include the files affected, any shell scripts or commands
189that need to be run before or after application of the patch, and
190the command line necessary for application.
191
192=item If you have a code patch
193
194If you are submitting a code patch there are several other things that
195you need to do.
196
197=over 4
198
199=item Comments, Comments, Comments
200
201Be sure to adequately comment your code. While commenting every
202line is unnecessary, anything that takes advantage of side effects of
203operators, that creates changes that will be felt outside of the
204function being patched, or that others may find confusing should
205be documented. If you are going to err, it is better to err on the
206side of adding too many comments than too few.
207
208=item Style
209
54aff467 210In general, please follow the particular style of the code you are patching.
211
212In particular, follow these general guidelines for patching Perl sources:
213
214 8-wide tabs (no exceptions!)
215 4-wide indents for code, 2-wide indents for nested CPP #defines
216 try hard not to exceed 79-columns
217 ANSI C prototypes
218 uncuddled elses and "K&R" style for indenting control constructs
219 no C++ style (//) comments, most C compilers will choke on them
220 mark places that need to be revisited with XXX (and revisit often!)
221 opening brace lines up with "if" when conditional spans multiple
222 lines; should be at end-of-line otherwise
223 in function definitions, name starts in column 0 (return value is on
224 previous line)
225 single space after keywords that are followed by parens, no space
226 between function name and following paren
227 avoid assignments in conditionals, but if they're unavoidable, use
228 extra paren, e.g. "if (a && (b = c)) ..."
229 "return foo;" rather than "return(foo);"
230 "if (!foo) ..." rather than "if (foo == FALSE) ..." etc.
231
55d729e4 232
233=item Testsuite
234
f4dad39e 235When submitting a patch you should make every effort to also include
236an addition to perl's regression tests to properly exercise your
237patch. Your testsuite additions should generally follow these
54aff467 238guidelines (courtesy of Gurusamy Sarathy <gsar@activestate.com>):
f4dad39e 239
240 Know what you're testing. Read the docs, and the source.
241 Tend to fail, not succeed.
242 Interpret results strictly.
243 Use unrelated features (this will flush out bizarre interactions).
244 Use non-standard idioms (otherwise you are not testing TIMTOWTDI).
f556e4ac 245 Avoid using hardcoded test numbers whenever possible (the
246 EXPECTED/GOT found in t/op/tie.t is much more maintainable,
247 and gives better failure reports).
f4dad39e 248 Give meaningful error messages when a test fails.
249 Avoid using qx// and system() unless you are testing for them. If you
250 do use them, make sure that you cover _all_ perl platforms.
251 Unlink any temporary files you create.
252 Promote unforeseen warnings to errors with $SIG{__WARN__}.
54aff467 253 Be sure to use the libraries and modules shipped with the version
f556e4ac 254 being tested, not those that were already installed.
f4dad39e 255 Add comments to the code explaining what you are testing for.
f556e4ac 256 Make updating the '1..42' string unnecessary. Or make sure that
257 you update it.
54aff467 258 Test _all_ behaviors of a given operator, library, or function:
259 - All optional arguments
260 - Return values in various contexts (boolean, scalar, list, lvalue)
261 - Use both global and lexical variables
262 - Don't forget the exceptional, pathological cases.
55d729e4 263
264=back
265
266=item Test your patch
267
268Apply your patch to a clean distribution, compile, and run the
269regression test suite (you did remember to add one for your
270patch, didn't you).
271
272=back
273
274=head2 An example patch creation
275
54aff467 276This should work for most patches:
55d729e4 277
278 cp MANIFEST MANIFEST.old
279 emacs MANIFEST
280 (make changes)
281 cd ..
535aafb8 282 diff -c perl5.7.42/MANIFEST.old perl5.7.42/MANIFEST > mypatch
55d729e4 283 (testing the patch:)
535aafb8 284 mv perl5.7.42/MANIFEST perl5.7.42/MANIFEST.new
285 cp perl5.7.42/MANIFEST.old perl5.7.42/MANIFEST
55d729e4 286 patch -p < mypatch
287 (should succeed)
535aafb8 288 diff perl5.7.42/MANIFEST perl5.7.42/MANIFEST.new
55d729e4 289 (should produce no output)
290
291=head2 Submitting your patch
292
293=over 4
294
295=item Mailers
296
297Please, please, please (get the point? 8-) don't use a mailer that
69c646ef 298word wraps your patch. This leaves the patch essentially worthless
299to the maintainers.
55d729e4 300
69c646ef 301Unfortunately many mailers word wrap the main text of messages, but
302luckily you can usually send your patches as email attachments without
303them getting "helpfully" word wrapped.
304
305If you have no choice in mailers and no way to get your hands on
306a better one, there is, of course, a Perl solution. Just do this:
55d729e4 307
308 perl -ne 'print pack("u*",$_)' patch > patch.uue
309
310and post patch.uue with a note saying to unpack it using
311
312 perl -ne 'print unpack("u*",$_)' patch.uue > patch
313
314=item Subject lines for patches
315
316The subject line on your patch should read
317
535aafb8 318 [PATCH 5.x.x AREA] Description
55d729e4 319
54aff467 320where the x's are replaced by the appropriate version number.
321The description should be a very brief but accurate summary of the
55d729e4 322problem (don't forget this is an email header).
323
54aff467 324Examples:
55d729e4 325
535aafb8 326 [PATCH 5.6.4 DOC] fix minor typos
55d729e4 327
535aafb8 328 [PATCH 5.7.9 CORE] New warning for foo() when frobbing
55d729e4 329
535aafb8 330 [PATCH 5.7.16 CONFIG] Added support for fribnatz 1.5
54aff467 331
332The name of the file being patched makes for a poor subject line if
333no other descriptive text accompanies it.
55d729e4 334
335=item Where to send your patch
336
54aff467 337If your patch is for a specific bug in the Perl core, it should be sent
338using the perlbug utility. Don't forget to describe the problem and the
339fix adequately.
340
55d729e4 341If it is a patch to a module that you downloaded from CPAN you should
342submit your patch to that module's author.
343
54aff467 344If your patch addresses one of the items described in perltodo.pod,
345please discuss your approach B<before> you make the patch at
346<perl5-porters@perl.org>. Be sure to browse the archives of past
347discussions (see perltodo.pod for archive locations).
348
55d729e4 349=back
350
351=head2 Applying a patch
352
353=over 4
354
355=item General notes on applying patches
356
357The following are some general notes on applying a patch
358to your perl distribution.
359
360=over 4
361
362=item patch C<-p>
363
54aff467 364It is generally easier to apply patches with the C<-p N> argument to
365patch (where N is the number of path components to skip in the files
366found in the headers). This helps reconcile differing paths between
367the machine the patch was created on and the machine on which it is
368being applied.
55d729e4 369
4a92fa57 370Be sure to use the Larry Wall version of patch. Some Operating Systems
371(HP-UX amongst those) have a patch command that does something completely
372different. The correct version of patch will show Larry's name several
373times when invoked as patch --version.
374
55d729e4 375=item Cut and paste
376
54aff467 377B<Never> cut and paste a patch into your editor. This usually clobbers
55d729e4 378the tabs and confuses patch.
379
380=item Hand editing patches
381
54aff467 382Avoid hand editing patches as this almost always screws up the line
383numbers and offsets in the patch, making it useless.
55d729e4 384
385=back
386
387=back
388
389=head2 Final notes
390
391If you follow these guidelines it will make everybody's life a little
392easier. You'll have the satisfaction of having contributed to perl,
393others will have an easy time using your work, and it should be easier
1266ad8f 394for the maintainers to coordinate the occasionally large numbers of
55d729e4 395patches received.
396
f556e4ac 397Also, just because you're not a brilliant coder doesn't mean that you
398can't contribute. As valuable as code patches are there is always a
399need for better documentation (especially considering the general
400level of joy that most programmers feel when forced to sit down and
401write docs). If all you do is patch the documentation you have still
402contributed more than the person who sent in an amazing new feature
403that no one can use because no one understands the code (what I'm
404getting at is that documentation is both the hardest part to do
405(because everyone hates doing it) and the most valuable).
406
407Mostly, when contributing patches, imagine that it is B<you> receiving
408hundreds of patches and that it is B<your> responsibility to integrate
409them into the source. Obviously you'd want the patches to be as easy
410to apply as possible. Keep that in mind. 8-)
55d729e4 411
55d729e4 412=head1 Author and Copyright Information
413
4a92fa57 414Copyright (c) 1998-2002 Daniel Grisinger
55d729e4 415
416Adapted from a posting to perl5-porters by Tim Bunce (Tim.Bunce@ig.co.uk).
417
418I'd like to thank the perl5-porters for their suggestions.