Simple source patch utility.
This is a simple source patch utility to enable production and application of patches to source files on any platform that supports Chicken.
The procedure make-patch takes a GNU-style diff file on (current-input-port) and produces a corresponding s-expression that can be used by the procedure apply-patch.
In its turn, the apply-patch procedure takes a patch definition and applies it to the input stream on (current-input-port), writing the result to (current-output-port).
The procedure reverse-patch transforms a patch definition into its reverse, so that the original patch can be undone.
It is recommended that make-patch be used to produce patches for distribution. These patches can then be interpreted and applied by the apply-patch procedure, which has no machine or system dependencies beyond the posix package. The third example above also illustrates operation similar to the Larry Wall/Paul Eggert patch program, accepting as input a patch in the form of raw GNU diff file output.
The patch definition is composed of a list of forms of the following format:
([c|a|d] start-line finish-line new-start-line new-finish-line (lines to be deleted) (lines to be inserted))
The forms c, a and d correspond to the change, append and delete commands in default GNU diff output. Where one of the line number parameters is omitted in the diff file, this is translated to #f in the corresponding form in the patch definition. The lists lines to be deleted and lines to be inserted are ordered lists of strings.
Reversing the patch is a straightforward swap: swap the a and d commands, swap the start-line, finish-line pair with new-start-line and new-finish-line, and swap the lines to be deleted and lines to be inserted lists.
Read GNU diff output on (current-input-port) and return a patch definition.
- apply-patch PATCH-DEFINITIONprocedure
Apply a patch definition to the text stream on (standard-input-port) and write the result to (current-output-port).
- A patch definition produced by make-patch
- reverse-patch PATCH-DEFINITIONprocedure
Produce a patch definition that undoes the patch definition given.
; To write a patch from GNU diff output on (current-input-port): (use patch) (write (make-patch))(newline) ; To apply a patch in a filename argument to (current-input-port) ; and write the result to (current-output-port): (use patch) (apply-patch (with-input-from-file (cadr (argv)) read)) ; To apply a patch in a GNU diff file in a filename argument (use patch) (apply-patch (with-input-from-file (cadr (argv)) make-patch)) ; To undo a patch in a filename argument, reading the result of applying ; the patch on (current-input-port) and writing the restored original to ; (current-output-port) (use patch) (apply-patch (reverse-patch (with-input-from-file (cadr (argv)) read)))
Bugs and limitations
This simple utility cannot handle files that have no line terminator at the end of the last line. make-patch will barf on the diff input in such cases. It shouldn't be too difficult to write a special case for this, but an absent line terminator in a text file is usually an error or an oversight, so a conscious decision has been made not to cater for it. Feel free to fix this bug.
No effort has been made to duplicate the extreme versatility of the Wall/Eggert patch utility. In particular, any noise in the raw diff input will cause make-patch to barf.
About this egg
This egg is hosted on the CHICKEN Subversion repository:
If you want to check out the source code repository of this egg and you are not familiar with Subversion, see this page.
- Ported to CHICKEN 5 [Ivan Raikov]
- Fix for botched conversion to regex egg [Ivan Raikov]
- Added regex as a dependency; ported eggdoc documentation to wiki format [Ivan Raikov]
- Ported to Chicken 4 [Ivan Raikov]
- Bug 308: does not require '(needs posix)'
- Initial release
Copyright (c) Tony Sidaway <email@example.com> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.