source: cl-darcs/tags/0.1.0/write-patch.lisp

Last change on this file was 49, checked in by Magnus Henoch, 16 years ago

Write newline after { when writing composite patches

File size: 5.3 KB
Line 
1;;; Copyright (C) 2006 Magnus Henoch
2;;;
3;;; This program is free software; you can redistribute it and/or
4;;; modify it under the terms of the GNU General Public License as
5;;; published by the Free Software Foundation; either version 2 of the
6;;; License, or (at your option) any later version.
7;;;
8;;; This program is distributed in the hope that it will be useful,
9;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11;;; General Public License for more details.
12;;;
13;;; You should have received a copy of the GNU General Public License
14;;; along with this program; if not, write to the Free Software
15;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
16
17(in-package :darcs)
18
19(defun write-patch-to-repo (patch repo)
20  "Write the named patch PATCH to REPO, compressed, under correct filename."
21  (let ((filename
22         (upath-subdir repo '("_darcs" "patches")
23                       (patchinfo-make-filename 
24                        (named-patch-patchinfo patch)))))
25    (with-temp-file-name tmp-file
26      (with-open-file (out tmp-file :direction :output :element-type '(unsigned-byte 8)
27                           :if-exists :error)
28        (write-patch patch out))
29      (compress-file tmp-file filename))))
30
31(defgeneric write-patch (patch stream)
32  (:documentation "Write PATCH to STREAM, in darcs patch format.
33STREAM is assumed to have element type (unsigned-byte 8).
34The patch is terminated by a newline character."))
35
36(defun write-as-byte (char stream)
37  "Convert CHAR to a byte, and write it to STREAM."
38  (write-byte (char-code char) stream))
39
40(defun write-as-bytes (string stream)
41  "Convert STRING to bytes, and write it to STREAM."
42  (write-sequence (string-to-bytes string) stream))
43
44(defmethod write-patch ((patch composite-patch) stream)
45  (write-as-byte #\{ stream)
46  (write-byte 10 stream)
47  (dolist (part (patches patch))
48    (write-patch part stream))
49  (write-as-byte #\} stream)
50  (write-byte 10 stream))
51
52(defmethod write-patch ((patch hunk-patch) stream)
53  (write-as-bytes (concatenate
54                   'string
55                   "hunk "
56                   (pathname-to-string (patch-filename patch))
57                   (format nil " ~A" (hunk-line-number patch)))
58                  stream)
59  (write-byte 10 stream)
60  (dolist (line (hunk-old-lines patch))
61    (write-as-byte #\- stream)
62    (write-sequence line stream)
63    (write-byte 10 stream))
64  (dolist (line (hunk-new-lines patch))
65    (write-byte (char-code #\+) stream)
66    (write-sequence line stream)
67    (write-byte 10 stream)))
68
69(defun write-token-and-filename (token filename stream)
70  (write-as-bytes token stream)
71  (write-byte 32 stream)
72  (write-as-bytes (pathname-to-string filename) stream)
73  (write-byte 10 stream))
74
75(defmethod write-patch ((patch add-file-patch) stream)
76  (write-token-and-filename "addfile" (patch-filename patch) stream))
77
78(defmethod write-patch ((patch rm-file-patch) stream)
79  (write-token-and-filename "rmfile" (patch-filename patch) stream))
80
81(defmethod write-patch ((patch add-dir-patch) stream)
82  (write-token-and-filename "adddir" (patch-directory patch) stream))
83
84(defmethod write-patch ((patch rm-dir-patch) stream)
85  (write-token-and-filename "rmdir" (patch-directory patch) stream))
86
87(defmethod write-patch ((patch binary-patch) stream)
88  (write-token-and-filename "binary" (patch-filename patch) stream)
89  (flet ((write-binary-data (bin)
90           ;; Print binary data in hex format, with 78 characters per
91           ;; line.  Each lines starts with *.  A newline is printed
92           ;; at the start, but not at the end.
93           (loop for i from 0 upto (length bin)
94              do (when (zerop (mod i 49))
95                   (write-byte 10 stream)
96                   (write-as-byte #\* stream))
97                (write-as-bytes (string-downcase
98                                 (format nil "~X" (aref bin i)))
99                                stream))))
100    (write-as-bytes "oldhex" stream)
101    (write-binary-data (binary-oldhex patch))
102    (write-as-bytes "newhex" stream)
103    (write-binary-data (binary-newhex patch))
104    (write-byte 10 stream)))
105
106(defmethod write-patch ((patch token-replace-patch) stream)
107  (write-as-bytes (format nil "replace ~A [~A] ~A ~A"
108                          (pathname-to-string (patch-filename patch))
109                          (token-regexp patch)
110                          (old-token patch)
111                          (new-token patch))
112                  stream)
113  (write-byte 10 stream))
114 
115(defmethod write-patch ((patch named-patch) stream)
116  (write-as-bytes
117   (with-output-to-string (strout)
118     (write-patchinfo (named-patch-patchinfo patch) strout))
119   stream)
120  (when (named-patch-dependencies patch)
121    (write-as-byte #\< stream)
122    (write-byte 10 stream)
123    (dolist (d (named-patch-dependencies patch))
124      (write-as-bytes
125       (with-output-to-string (strout)
126         (write-patchinfo d strout))
127       stream)
128      (write-byte 10 stream))
129    (write-as-byte #\> stream)
130    (write-byte 32 stream))
131  (write-patch (named-patch-patch patch) stream))
132
133(defmethod write-patch ((patch change-pref-patch) stream)
134  (write-as-bytes "changepref " stream)
135  (write-as-bytes (change-pref-which patch) stream)
136  (write-byte 10 stream)
137  (write-sequence (change-pref-from patch) stream)
138  (write-byte 10 stream)
139  (write-sequence (change-pref-to patch) stream)
140  (write-byte 10 stream))
141
142(defmethod write-patch ((patch move-patch) stream)
143  (write-sequence (string-to-bytes "move ") stream)
144  (write-sequence (string-to-bytes
145                   (pathname-to-string (patch-move-from patch)))
146                  stream)
147  (write-byte 32 stream)
148  (write-sequence (string-to-bytes
149                   (pathname-to-string
150                    (patch-move-to patch))) stream)
151  (write-byte 10 stream))
152
Note: See TracBrowser for help on using the repository browser.