root/1.8.3/trunk/utils/fixdiff.scm
| Revision 1032, 2.3 kB (checked in by shawnw, 1 year ago) | |
|---|---|
| |
| Line | |
|---|---|
| 1 | #!/usr/bin/env csi -script |
| 2 | ;;; Convert a diff file made with Windows-style \ directory paths to |
| 3 | ;;; Unix-style / paths. Works with context and unified diffs. Requires |
| 4 | ;;; chicken scheme (http://www.call-with-current-continuation.org). |
| 5 | ;;; |
| 6 | ;;; Written by Shawn Wagner (Raevnos) and placed in public domain. |
| 7 | ;;; No warranty. Use at your own risk. Blah blah blah. |
| 8 | ;;; |
| 9 | ;;; Usage: ./utils/fixdiff.scm < win32.patch > unix.path |
| 10 | ;;; or compile with: csc -O2 -o fixdiff fixdiff.scm |
| 11 | |
| 12 | (cond-expand |
| 13 | ((and chicken compiling) |
| 14 | ; Boilerplate for turning on optimizations |
| 15 | (declare |
| 16 | (block) |
| 17 | (usual-integrations) |
| 18 | (fixnum) |
| 19 | (disable-interrupts) |
| 20 | (always-bound path-regexp) |
| 21 | (uses utils regex))) |
| 22 | ((and chicken csi) |
| 23 | ; Load the appropriate libraries in the interpeter |
| 24 | (use utils regex))) |
| 25 | |
| 26 | ;;; Two ways of doing it. |
| 27 | |
| 28 | ;; Some testing suggests the regular expression approach is a little |
| 29 | ;; bit faster (Especially when run through csi instead of compiled to |
| 30 | ;; a binary) |
| 31 | (define path-regexp (regexp "^(?:\\+\\+\\+|---|\\*\\*\\*|Index:|diff)\\s")) |
| 32 | (define (fix-paths line) |
| 33 | (if (string-search path-regexp line) |
| 34 | (string-translate line #\\ #\/) |
| 35 | line)) |
| 36 | |
| 37 | ;; But this version, using the SRFI-13 string-prefix? function, is a lot |
| 38 | ;; more readable when it comes to seeing what marks a line with a path that needs |
| 39 | ;; to be converted. |
| 40 | ;(define (fix-paths line) |
| 41 | ; (if (or (string-prefix? "+++ " line) |
| 42 | ; (string-prefix? "--- " line) |
| 43 | ; (string-prefix? "*** " line) |
| 44 | ; (string-prefix? "Index: " line) |
| 45 | ; (string-prefix? "diff " line)) |
| 46 | ; (string-translate line #\\ #\/) |
| 47 | ; line)) |
| 48 | |
| 49 | ;; The driver, equivalent to perl's behavior when invoked with -p |
| 50 | (for-each-argv-line (compose print fix-paths)) |
| 51 | |
| 52 | ;;; As an exercise, compare with the following idiomatic perl equivalent: |
| 53 | |
| 54 | ; #!/usr/bin/env perl -p |
| 55 | ; s!\\!/!og if m!^(?:\+\+\+|---|\*\*\*|Index:|diff)\s!o; |
| 56 | ; |
| 57 | ; Without -p, but still using all the things that give perl a bad name |
| 58 | ; (Like relying on $_ instead of explict variables) it'd be more like: |
| 59 | ; |
| 60 | ; while (<>) { |
| 61 | ; s!\\!/!og if m!^(?:\+\+\+|---|\*\*\*|Index:|diff)\s!o; |
| 62 | ; print; |
| 63 | ; } |
| 64 | ; |
| 65 | ; I think the scheme version is far more readable, with only a few |
| 66 | ; more lines of actual code (Ignoring all the boilerplate stuff in the |
| 67 | ; cond-expand. It's just hints to the compiler and loading libraries). |
| 68 |
Note: See TracBrowser for help on using the browser.
