Difference between revisions of "Diff and patch"

From Noah.org
Jump to navigationJump to search
Line 5: Line 5:
 
First, find out if they want Context or Unified diffs.
 
First, find out if they want Context or Unified diffs.
 
That will set the -c or -u option for diff.
 
That will set the -c or -u option for diff.
I usually go with -c when in doubt.
+
I usually go with -c when in doubt. You will use the -r options
 +
to recursively compare the ORIGINAL directory with the NEWER directory
 +
(the one with your changes). You can add many more -x options to
 +
exclude patterns of files that you do not want included in the patch.
 +
Usually you don't want to compare binaries or files generated by
 +
the build process (make).
  
 
<pre>
 
<pre>
diff -r -c -x".svn" -x"*.jpg" -x"*.a" -x"*.so" -x"*.o" -x"*.pyc" -x"CVS" \
+
diff -r -c -x"*.jpg" -x"*.png" -x"*.gif" -x"*.swp" \
 +
-x"*.a" -x"*.so" -x"*.o" -x"*.pyc" -x"*.exe" -x"*.class" \
 +
-x".svn" -x"CVS" -x"core" -x"a.out"
 
"/home/user/project-ORIGINAL" "/home/user/project-NEWER" > changes.patch
 
"/home/user/project-ORIGINAL" "/home/user/project-NEWER" > changes.patch
 
</pre>
 
</pre>
Line 18: Line 25:
 
patch < changes.patch
 
patch < changes.patch
 
</pre>
 
</pre>
 
You can add many more -x options to exclude patterns of files that you
 
do not want included in the patch.
 

Revision as of 11:54, 29 May 2007

If you don't have svn access to a project and want to submit a manual patch, the following notes should help. You made changes to many files in a project and you want to submit a patch to the maintainer of the project. First, find out if they want Context or Unified diffs. That will set the -c or -u option for diff. I usually go with -c when in doubt. You will use the -r options to recursively compare the ORIGINAL directory with the NEWER directory (the one with your changes). You can add many more -x options to exclude patterns of files that you do not want included in the patch. Usually you don't want to compare binaries or files generated by the build process (make).

diff -r -c -x"*.jpg" -x"*.png" -x"*.gif" -x"*.swp" \
-x"*.a" -x"*.so" -x"*.o" -x"*.pyc" -x"*.exe" -x"*.class" \
-x".svn" -x"CVS" -x"core" -x"a.out"
"/home/user/project-ORIGINAL" "/home/user/project-NEWER" > changes.patch

This will generate a patch file that you can mail to the project maintainer. They will then apply the patch as follows:

cd project-ORIGINAL
patch < changes.patch