« Home | Flex Demo: Matrix Math (and an error in the Action... » | Emacs modes for Flex » | Adobe Flex and Custom Namespace / manifest.xml » | How to use exuberant CTAGS with ActionScript and Flex » | Resources for learning Flex » | Claim »

How to make a patch using diff

I always forget the command to use, and google is strangely devoid of helpful/correct advice. Therefore I'm posting this for my own and future generations' reference.

How to Generate a Patch from Standalone Code

For non-(svn|cvs),
  • You should have a directory holding the original source (i.e. "dir") and a directory holding the modified (i.e. "dir-orig").
  • Obviously, don't modify dir-orig (that is, it should match the author's). If you don't trust yourself, do a chmod -R a-w dir-orig to recursively mark the directory read-only.
  • Generate the patch by going to the parent directory (holding dir-orig and dir) and running the command
    diff -Nuwr dir-orig dir > /tmp/my-happy-patch.diff
    • dir and dir-orig should be paths to the dirs in question, obviously.
    • -N creates newly added files (treats absent files as empty files)
    • -u creates a "unified" diff -- it's hunam readable and works well with patch
    • -w ignores whitespace, which is polite if your (clearly superior) formatting policy differs from the original.
    • -r recursively descends the source tree.
  • Other helpful options:
    • -p (applied to C or C++ code) shows the function the new code appears in. Only use this with C or C++ code (i.e. YMMV)
    • -u6 (or any number following the -u) gives that many lines of context (the default is 3, which should be fine for code that isn't changing like the star of a Tootsie stage performance)
    • -x ".??*" ignores .DS_Store, Eclipse and other hidden-file turds, if you have those. Emacsen should add -x "*~".
  • Sanity-check the change less /tmp/my-happy-patch.diff

How to Generate a Patch from Subversion or CVS

To generate a patch from cvs or svn, (advice horked from X-Smiles.org):
  • Make sure you are synchronized with the latest sources: $ cvs update src (or wherever your changes are; use a directory that spans all the changed modules or the trunk directory.)
  • Sanity-check the change: $ cvs diff src
  • Generate the patch (replace -Nuwr with whatever you decided works for you from the options above). $ svn --diff-cmd=diff -x-Nuw src > /tmp/my-happy-patch.diff
  • (Rather than include -x "" args, you should be adding turd files to your ~/.subversion/config, for instance global-ignores = *.o *.lo *.la #*# .*.rej *.rej .*~ *~ .#* .DS_Store or however you pronounce that in a ~/.cvsrc or .cvsignore.)
  • Sanity-check the patch less /tmp/my-happy-patch.diff
(By the way, if your broken OS lacks a command line, you might be able to add one).

How to use a Patch

To apply such a patch,
  • Download the patch and save it somewhere intelligent.
  • If you're not using version control, !!make a copy of the source tree!!
  • Change directories into that new folder --the one holding the unmolested (or svn trunk) code.
  • Sanity check: run the command cat /tmp/my-happy-patch.diff | patch -p1 --dry-run
  • If you see results like patching file foo/bar/my-happy-file.as ... you're good to go: cat /tmp/my-happy-patch.diff | patch -p1
  • Pitfalls:
    • If you get a patch taken from within the modified directory, change -p1 to -p0.
    • If you get a patch with the original and modified dirs reversed, add a --reverse flag.

Labels: , , , ,