LinuxDevCenter.com

oreilly.comSafari Books Online.Conferences.
Sign In/My Account | View Cart   

We've expanded our Linux news coverage and improved our search! Search for all things Linux across O'Reilly!

Search
Search Tips

advertisement


Listen Print Subscribe to Linux Subscribe to Newsletters

Unix Power Tools
patch: Generalized Updating of Files that Differ

by Tim O'Reilly
02/24/2000

Like all of Larry Wall's widely used programs (including perl, a software configuration script called Configure, and the rn news reader), patch betrays a whimsical intelligence of its own. Feed it any kind of diff listing (not just an editing script produced with the - e option). patch figures out what it needs to do to apply the diff, and updates the file, supplying all the while a breezy commentary on what it's doing:

% patch < testfile.diff
Hmm...  Looks like a normal diff to me...
File to patch: testfile
Patching file testfile using Plan A...
Hunk #1 succeeded at 2.
done

As Larry once noted, patch has done an awful lot to "change the culture of computing." Almost all free software is now updated by means of patches rather than complete new releases. patch is smart enough to discard any leading or trailing garbage (such as mail headers or signatures) so that a program source file can be updated by piping a mail message containing a diff listing between old and new versions directly to patch.

Here are a few of the other things patch is smart enough to do:

  • Figure out the name of the file to be updated and do it without asking (usually only if the diff file is a context diff produced with the - c option).

  • Look for a suitable SCCS or RCS file and check it out, if the filename itself can't be found.

  • Handle diff listings that don't quite match. This makes it possible for patch to update a file that the recipient has already changed from the one that the diff was based on.

  • Save any pieces of the diff file that don't end up being used, in a file named by adding the suffix .rej (reject) to the name of the file being patched.

  • Back up the file that is being patched, appending the suffix .orig to the name of the file being patched.

  • Recognize that its input may actually apply to several files, and patch each of them separately. So, for example, a whole directory might be updated by a "patch" file that contained diff listings for each of the files in the directory. (By the way, the - d option to patch tells it to cd to a specified directory before starting work.)

  • Recognize (or at least speculate) that a patch might have been created incorrectly, with the old and new files swapped. Larry says: "Yes, I'm afraid that does happen occasionally, human nature being what it is." patch's - R option will force patch to reverse the sense of the patch; what's really amusing is to see patch suggest that this might be the thing to do, when things seem out of sync.

If you are a programmer, patch is worth studying just to see how much a program can do to anticipate errors, deal with fuzzy input, and in general "make the computer do the dirty work." But if you're a programmer, you doubtless already know about patch....


Back More Unix Power Tools

 




Tagged Articles

Be the first to post this article to del.icio.us

Sponsored Resources

  • Inside Lightroom
Advertisement
O'Reilly Media
© 2008, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Privacy Policy
Contacts
Authors
Press Room
Jobs
User Groups
Academic Solutions
Newsletters
Writing for O'Reilly
RSS Feeds
Other O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
O'Reilly FYI
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com
Sponsored Sites
Inside Aperture
Inside Lightroom
Inside Port 25
InsideRIA
java.net