The situation ( file/directory tree )
Foo is the original directory (CGI.pm-3.59)
Foo-old is the backup pristine copy (CGI.pm-3.59-old)
Foo-new is thing you've edited that you generate the patch from (CGI.pm-3.59-new)
Creating a new patch ( Foo-new.patch, CGI.pm-3.59-new.patch )
$ diff -Npurd Foo-old Foo-new > Foo-new.patch
What the switches mean
- -N => --new-file Treat absent files as empty.
- -p => --show-c-function Show which C function each change is in.
- -u => -U NUM --unified=NUM Output NUM (default 3) lines of unified context.
- -r => --recursive Recursively compare any subdirectories found.
- -d => --minimal Try hard to find a smaller set of changes.
NOTE: If you're making a patch from a directory like CGI.pm-3.59, where you've used perl Makefile.PL && make, esp if using -N, you should clean up before making a patch, you should make realclean
What a patch looks like
Applying your new patch to Foo (CGI.pm-3.59), what the recipient of that patch would do, is first change working directory, then call patch
$ cd Foo
$ patch -p1 -i../Foo-new.patch
-p1 means ignore the first path part, ignore the bold parts
--- Foo-old/two.txt 1969-12-31 16:00:00.000000000 -0800
+++ Foo-new/two.txt 2012-08-16 08:19:25.500000000 -0700
It also means don't create a Foo-new directory :)
If you used -p0 (ignore no path parts) the patching would fail in two ways, it wouldn't find one.txt, and it would create a Foo-new directory
For best patching experience
Always use relative paths for diff -Npurd diffolddir newdir
Always use the p1 or p0 option, otherwise patch will ignore all directories/patch the wrong file
To re-create my original situation, to create files and directories out of a patch, first produce the patch
diff -Npurd emptyDir tmpFoo > tmpFoo.patch
The new patch tmpFoo.patch looks like
Then apply the patch in your new working directory tmp2Foo
Oh noes, a mistake! I forgot to include the -p1 switch, and no directories were created! Trying again with -p1
Hooray! Hmm, I'll try p0 in the same directory
Whoa, that worked also and it didn't interfere :) but I don't really need that extra tmpFoo, now to get rid of it without rm -rfv -- from the same working directory
-Reversing a patch (just add -R to the command you used to apply the patch)
See also http://perldoc.perl.org/5.14.0/perlhack.html#Patching, http://perldoc.perl.org/5.10.0/perlhack.html#Patching |