-
Notifications
You must be signed in to change notification settings - Fork 9
/
patch.lisp
354 lines (314 loc) · 13.2 KB
/
patch.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
;;;; patch.lisp -- reading patch files and applying them
(in-package :diff)
;;; Some people, when confronted with a problem think, "I know, I'll
;;; use regular expressions." Now they have two problems.
;;;
;;; --Jamie Zawinski
(defparameter *number-regex* '(:greedy-repetition 1 nil :digit-class))
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun anchored-line (regex)
(if (consp regex)
`(:sequence :start-anchor ,@regex :end-anchor)
`(:sequence :start-anchor ,regex :end-anchor)))
)
(defparameter *unified-diff-window-header*
(cl-ppcre:create-scanner
(anchored-line
`("@@ -"
(:register ,*number-regex*)
(:greedy-repetition 0 1 (:sequence #\, (:register ,*number-regex*)))
" +"
(:register ,*number-regex*)
(:greedy-repetition 0 1 (:sequence #\, (:register ,*number-regex*)))
" @@"))))
(defparameter *context-diff-window-header*
(cl-ppcre:create-scanner (anchored-line "***************")))
(defparameter *context-diff-window-original-line*
(cl-ppcre:create-scanner
(anchored-line
`("*** "
(:register ,*number-regex*)
#\,
(:register ,*number-regex*)
" ****"))))
(defparameter *context-diff-window-modified-line*
(cl-ppcre:create-scanner
(anchored-line
`("--- "
(:register ,*number-regex*)
#\,
(:register ,*number-regex*)
" ----"))))
(defparameter *index-header-line*
(cl-ppcre:create-scanner
(anchored-line
'("Index: " (:register (:greedy-repetition 0 nil :everything))))))
(defparameter *prereq-header-line*
(cl-ppcre:create-scanner
(anchored-line
'("Prereq: " (:register (:greedy-repetition 0 nil :everything))))))
(defparameter *unified-diff-line*
(cl-ppcre:create-scanner
(anchored-line
`((:register (:alternation #\+ #\- #\Space))
(:register (:greedy-repetition 0 nil :everything))))))
(defparameter *context-diff-line*
(cl-ppcre:create-scanner
(anchored-line
`((:register (:alternation #\+ #\- #\! #\Space))
(:register (:greedy-repetition 0 nil :everything))))))
(defparameter *unified-diff-header-original-line*
(cl-ppcre:create-scanner
'(:sequence :start-anchor
"--- " (:register (:greedy-repetition 1 nil :non-whitespace-char-class)))))
(defparameter *unified-diff-header-modified-line*
(cl-ppcre:create-scanner
'(:sequence :start-anchor
"+++ " (:register (:greedy-repetition 1 nil :non-whitespace-char-class)))))
(defparameter *context-diff-header-original-line*
(cl-ppcre:create-scanner
'(:sequence :start-anchor
"*** " (:register (:greedy-repetition 1 nil :non-whitespace-char-class)))))
(defparameter *context-diff-header-modified-line*
(cl-ppcre:create-scanner
'(:sequence :start-anchor
"--- " (:register (:greedy-repetition 1 nil :non-whitespace-char-class)))))
(defun collect-window-lines (stream test)
(loop for line = (read-line stream)
while (funcall test line)
collect line into lines
finally (return (values lines line))))
;;; abstraction for reading straight lines from a stream
(defclass line-generator ()
((peeked-line :accessor peeked-line :initform nil)
(stream :accessor line-stream :initarg :stream)))
(defun make-line-generator (stream)
(make-instance 'line-generator :stream stream))
(defun yield-line (line-generator)
(cond
((peeked-line line-generator)
(prog1 (peeked-line line-generator)
(setf (peeked-line line-generator) nil)))
(t
(read-line (line-stream line-generator) nil nil))))
(defun peek-line (line-generator)
(cond
((peeked-line line-generator)
(peeked-line line-generator))
(t
(setf (peeked-line line-generator)
(read-line (line-stream line-generator) nil nil)))))
;;; reading diff windows ("hunks")
(defparameter *default-lead-char-alist* '((#\Space . :common)
(#\+ . :create)
(#\- . :delete)))
(defparameter *context-original-lead-char-alist*
(acons #\! :replace *default-lead-char-alist*))
(defparameter *context-modified-lead-char-alist*
(acons #\! :insert *default-lead-char-alist*))
(defun line-to-chunk (line &optional (table *default-lead-char-alist*))
(let* ((char (aref line 0))
(text-line (subseq line 1))
(chunk-kind (cdr (assoc char table :test #'char=))))
(make-instance 'chunk
:kind chunk-kind
:lines (list text-line))))
(defgeneric read-diff-window (stream diff))
(defmethod read-diff-window (linegen (diff unified-diff))
(let ((window (create-window-for-diff diff))
(line (peek-line linegen)))
(cond
((cl-ppcre:register-groups-bind ((#'parse-integer original-start
original-length
modified-start
modified-length))
(*unified-diff-window-header* line)
(setf (original-start-line window) (1- original-start)
(original-length window) (if original-length original-length 1)
(modified-start-line window) (1- modified-start)
(modified-length window) (if modified-length modified-length 1))
(yield-line linegen)))
(t
(return-from read-diff-window nil)))
(loop for line = (peek-line linegen)
while (and line (cl-ppcre:scan *unified-diff-line* line))
collect (line-to-chunk (yield-line linegen)) into chunks
finally (setf (window-chunks window) (consolidate-chunks chunks)))
window))
;;; We want to convert sequences of chunks with the same kind to a single
;;; chunk containing the lines of the sequence.
(defun consolidate-chunks (chunk-list)
(declare (type list chunk-list))
(do ((list chunk-list)
(consolidated nil))
((null list) (nreverse consolidated))
;; consolidate chunks which match the kind of the head chunk
(let* ((head-chunk (first list))
(kind (chunk-kind head-chunk)))
(loop for rest on list
for candidate = (car rest)
while (eq kind (chunk-kind candidate))
collect (first (chunk-lines candidate)) into lines
finally (let ((new-chunk (make-instance 'chunk
:kind kind
:lines lines)))
(push new-chunk consolidated)
(setf list rest))))))
(defmethod read-diff-window (linegen (diff context-diff))
(let ((window (create-window-for-diff diff))
(original-chunks nil)
(modified-chunks nil)
(line (yield-line linegen)))
;; read the "original" part
(unless line
(return-from read-diff-window nil))
(and (cl-ppcre:scan *context-diff-window-header* line)
(setf line (yield-line linegen)))
(unless (cl-ppcre:register-groups-bind ((#'parse-integer original-start
original-end))
(*context-diff-window-original-line* line)
(setf (original-start-line window) (1- original-start)
(original-length window) (1+ (- original-end
original-start))))
(error "Non-matching original window header ~A" line))
(loop for i from 0 below (original-length window)
until (cl-ppcre:scan *context-diff-window-modified-line* (peek-line linegen))
collect (line-to-chunk (yield-line linegen)
*context-original-lead-char-alist*) into chunks
finally (setf original-chunks chunks))
;; read the "modified" part
(setf line (yield-line linegen))
(unless (cl-ppcre:register-groups-bind ((#'parse-integer modified-start
modified-end))
(*context-diff-window-modified-line* line)
(setf (modified-start-line window) (1- modified-start)
(modified-length window) (1+ (- modified-end
modified-start))))
(error "Non-matching modified window header ~A" line))
(loop for i from 0 below (modified-length window)
until (let ((maybe-line (peek-line linegen)))
(or (not maybe-line) ; EOF
(cl-ppcre:scan *context-diff-window-header* maybe-line)))
collect (line-to-chunk (yield-line linegen)
*context-modified-lead-char-alist*) into chunks
finally (setf modified-chunks chunks))
(setf (window-chunks window)
(merge-context-chunks (consolidate-chunks original-chunks)
(consolidate-chunks modified-chunks)))
window))
(defun merge-context-chunks (original modified)
(do ((original original)
(modified modified)
(merged-chunks nil))
((and (null original) (null modified))
(nreverse merged-chunks))
(cond
((null original)
(push (pop modified) merged-chunks))
((null modified)
(push (pop original) merged-chunks))
(t
(let ((orig-kind (chunk-kind (first original)))
(mod-kind (chunk-kind (first modified))))
(cond
((and (eq orig-kind :common) (eq mod-kind :common))
(push (pop original) merged-chunks)
(pop modified))
((eq orig-kind :common)
(push (pop modified) merged-chunks))
((eq mod-kind :common)
(push (pop original) merged-chunks))
(t
(push (pop original) merged-chunks)
(push (pop modified) merged-chunks))))))))
;;; reading patches
(defclass patch ()
((diff :initarg :diff :reader diff)
(index-file :initarg :index-file :reader index-file)
(prereq-string :initarg :prereq-string :reader prereq-string)))
(defun read-patch (linegen)
(let ((diff nil)
(index-filename nil)
(prereq-string nil)
(line ""))
(tagbody
UNKNOWN-STATE
(setf line (yield-line linegen))
(unless line
(go RETURN-VALUE))
(cond
((cl-ppcre:register-groups-bind (original-pathname)
(*unified-diff-header-original-line* line)
(setf diff (make-instance 'unified-diff))
(setf (original-pathname diff) original-pathname))
(go UNIFIED-MODIFIED-LINE))
((cl-ppcre:register-groups-bind (original-pathname)
(*context-diff-header-original-line* line)
(setf diff (make-instance 'context-diff))
(setf (original-pathname diff) original-pathname))
(go CONTEXT-MODIFIED-LINE))
((cl-ppcre:register-groups-bind (index-pathname)
(*index-header-line* line)
(setf index-filename index-pathname))
(go UNKNOWN-STATE))
((cl-ppcre:register-groups-bind (prereq)
(*prereq-header-line* line)
(setf prereq-string prereq))
(go UNKNOWN-STATE))
(t
(go UNKNOWN-STATE)))
UNIFIED-MODIFIED-LINE
(setf line (yield-line linegen))
(unless line
(go RETURN-VALUE))
(cond
((cl-ppcre:register-groups-bind (modified-pathname)
(*unified-diff-header-modified-line* line)
(setf (modified-pathname diff) modified-pathname))
(go WINDOW-STATE))
(t
(error "Could not read unified modified-line")))
CONTEXT-MODIFIED-LINE
(setf line (yield-line linegen))
(unless line
(go RETURN-VALUE))
(cond
((cl-ppcre:register-groups-bind (modified-pathname)
(*context-diff-header-modified-line* line)
(setf (modified-pathname diff) modified-pathname))
(go WINDOW-STATE))
(t
(error "Could not read context modified-line")))
WINDOW-STATE
(loop for window = (read-diff-window linegen diff)
while window
collect window into windows
finally (progn
(setf (diff-windows diff) windows)
(go RETURN-VALUE)))
RETURN-VALUE
(return-from read-patch (values diff index-filename prereq-string)))))
(defun read-patches-from-file (pathname)
(let ((patches nil))
(with-open-file (stream pathname :direction :input
:if-does-not-exist :error)
(let ((linegen (make-line-generator stream)))
(loop
(multiple-value-bind (diff index prereq)
(read-patch linegen)
(unless diff
(return-from read-patches-from-file (nreverse patches)))
(push (make-instance 'patch
:diff diff
:index-file (and index (pathname index))
:prereq-string prereq)
patches)))))))
(defun apply-seq-patch (original-seq patch)
"Apply PATCH to the sequence ORIGINAL-SEQ."
(apply-seq-diff original-seq (diff patch)))
(defun apply-patch (patch &aux original)
"Apply PATCH."
(do-file-lines (line (original-pathname (diff patch))) (push line original))
(with-open-file (out (original-pathname (diff patch))
:direction :output :if-exists :supersede)
(format out "~{~a~^~%~}~%" (apply-seq-patch (nreverse original) patch))))