0
|
1 ;;; ediff-ptch.el --- Ediff's patch support
|
|
2
|
16
|
3 ;; Copyright (C) 1996, 1997 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
|
|
6
|
|
7 ;; This file is part of GNU Emacs.
|
|
8
|
|
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
10 ;; it under the terms of the GNU General Public License as published by
|
|
11 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
12 ;; any later version.
|
|
13
|
|
14 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
17 ;; GNU General Public License for more details.
|
|
18
|
|
19 ;; You should have received a copy of the GNU General Public License
|
|
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
22 ;; Boston, MA 02111-1307, USA.
|
|
23
|
|
24
|
|
25 ;;; Code:
|
14
|
26
|
|
27 (provide 'ediff-ptch)
|
|
28
|
|
29 ;; compiler pacifier
|
|
30 (defvar ediff-window-A)
|
|
31 (defvar ediff-window-B)
|
|
32 (defvar ediff-window-C)
|
|
33 (defvar ediff-use-last-dir)
|
|
34 (defvar ediff-shell)
|
|
35
|
|
36 (eval-when-compile
|
16
|
37 (let ((load-path (cons (expand-file-name ".") load-path)))
|
14
|
38 (or (featurep 'ediff-init)
|
|
39 (load "ediff-init.el" nil nil 'nosuffix))
|
|
40 (or (featurep 'ediff)
|
|
41 (load "ediff.el" nil nil 'nosuffix))
|
|
42 ))
|
|
43 ;; end pacifier
|
0
|
44
|
12
|
45 (require 'ediff-init)
|
|
46
|
0
|
47 (defvar ediff-last-dir-patch nil
|
|
48 "Last directory used by an Ediff command for file to patch.")
|
|
49
|
|
50 (defvar ediff-backup-extension
|
|
51 (if (memq system-type '(vax-vms axp-vms emx ms-dos windows-nt windows-95))
|
|
52 "_orig" ".orig")
|
12
|
53 "Backup extension used by the patch program.
|
|
54 See also `ediff-backup-specs'.")
|
|
55
|
|
56 (defvar ediff-backup-specs (format "-b %s" ediff-backup-extension)
|
|
57 "*Backup directives to pass to the patch program.
|
|
58 Ediff requires that the old version of the file \(before applying the patch\)
|
|
59 is saved in a file named `the-patch-file.extension'. Usually `extension' is
|
|
60 `.orig', but this can be changed by the user and may depend on the system.
|
|
61 Therefore, Ediff needs to know the backup extension used by the patch program.
|
|
62
|
|
63 Some versions of the patch program let you specify `-b backup-extension'.
|
|
64 Other versions only permit `-b', which assumes some canned extension
|
|
65 \(usually `.orig'\).
|
|
66
|
|
67 Note that both `ediff-backup-extension' and `ediff-backup-specs'
|
|
68 must be properly set. If your patch program takes the option `-b',
|
|
69 but not `-b extension', the variable `ediff-backup-extension' must
|
|
70 still be set so Ediff will know which extension to use.")
|
|
71
|
0
|
72
|
|
73 (defvar ediff-patch-default-directory nil
|
|
74 "*Default directory to look for patches.")
|
|
75
|
|
76 (defvar ediff-context-diff-label-regexp
|
|
77 (concat "\\(" ; context diff 2-liner
|
|
78 "^\\*\\*\\* \\([^ \t]+\\)[^*]+[\t ]*\n--- \\([^ \t]+\\)"
|
|
79 "\\|" ; GNU unified format diff 2-liner
|
12
|
80 "^--- \\([^ \t]+\\)[\t ]+.*\n\\+\\+\\+ \\([^ \t]+\\)"
|
0
|
81 "\\)")
|
|
82 "*Regexp matching filename 2-liners at the start of each context diff.")
|
|
83
|
|
84 (defvar ediff-patch-program "patch"
|
12
|
85 "*Name of the program that applies patches.
|
|
86 It is recommended to use GNU-compatible versions.")
|
|
87 (defvar ediff-patch-options "-f"
|
|
88 "*Options to pass to ediff-patch-program.
|
|
89
|
|
90 Note: the `-b' option should be specified in `ediff-backup-specs'.
|
|
91
|
|
92 It is recommended to pass the `-f' option to the patch program, so it won't ask
|
|
93 questions. However, some implementations don't accept this option, in which
|
|
94 case the default value for this variable should be changed.")
|
0
|
95
|
|
96 ;; The buffer of the patch file. Local to control buffer.
|
|
97 (ediff-defvar-local ediff-patchbufer nil "")
|
|
98
|
|
99 ;; The buffer where patch displays its diagnostics.
|
|
100 (ediff-defvar-local ediff-patch-diagnostics nil "")
|
|
101
|
|
102 ;; Map of patch buffer. Has the form:
|
|
103 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
|
|
104 ;; where filenames are files to which patch would have applied the patch;
|
|
105 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
|
|
106 ;; it for the end.
|
|
107 (ediff-defvar-local ediff-patch-map nil "")
|
|
108
|
|
109 ;; strip prefix from filename
|
|
110 ;; returns /dev/null, if can't strip prefix
|
|
111 (defsubst ediff-file-name-sans-prefix (filename prefix)
|
|
112 (save-match-data
|
|
113 (if (string-match (concat "^" prefix) filename)
|
|
114 (substring filename (match-end 0))
|
|
115 (concat "/null/" filename))))
|
|
116
|
|
117
|
|
118
|
|
119 ;; no longer used
|
|
120 ;; return the number of matches of regexp in buf starting from the beginning
|
|
121 (defun ediff-count-matches (regexp buf)
|
|
122 (ediff-eval-in-buffer buf
|
|
123 (let ((count 0) opoint)
|
|
124 (save-excursion
|
|
125 (goto-char (point-min))
|
|
126 (while (and (not (eobp))
|
|
127 (progn (setq opoint (point))
|
|
128 (re-search-forward regexp nil t)))
|
|
129 (if (= opoint (point))
|
|
130 (forward-char 1)
|
|
131 (setq count (1+ count)))))
|
|
132 count)))
|
|
133
|
|
134 ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
|
|
135 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
|
|
136 ;; where filenames are files to which patch would have applied the patch;
|
|
137 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
|
|
138 ;; it for the end. This list is then assigned to ediff-patch-map.
|
|
139 ;; Returns the number of elements in the list ediff-patch-map
|
|
140 (defun ediff-map-patch-buffer (buf)
|
|
141 (ediff-eval-in-buffer buf
|
|
142 (let ((count 0)
|
|
143 (mark1 (move-marker (make-marker) (point-min)))
|
|
144 (mark1-end (point-min))
|
|
145 (possible-file-names '("/dev/null" . "/dev/null"))
|
|
146 mark2-end mark2 filenames
|
|
147 beg1 beg2 end1 end2
|
|
148 patch-map opoint)
|
|
149 (save-excursion
|
|
150 (goto-char (point-min))
|
|
151 (setq opoint (point))
|
|
152 (while (and (not (eobp))
|
|
153 (re-search-forward ediff-context-diff-label-regexp nil t))
|
|
154 (if (= opoint (point))
|
|
155 (forward-char 1) ; ensure progress towards the end
|
|
156 (setq mark2 (move-marker (make-marker) (match-beginning 0))
|
|
157 mark2-end (match-end 0)
|
12
|
158 beg1 (or (match-beginning 2) (match-beginning 4))
|
|
159 end1 (or (match-end 2) (match-end 4))
|
|
160 beg2 (or (match-beginning 3) (match-beginning 5))
|
|
161 end2 (or (match-end 3) (match-end 5)))
|
0
|
162 ;; possible-file-names is holding the new file names until we
|
|
163 ;; insert the old file name in the patch map
|
|
164 ;; It is a pair (filename from 1st header line . fn from 2nd line)
|
|
165 (setq possible-file-names
|
|
166 (cons (if (and beg1 end1)
|
|
167 (buffer-substring beg1 end1)
|
|
168 "/dev/null")
|
|
169 (if (and beg2 end2)
|
|
170 (buffer-substring beg2 end2)
|
|
171 "/dev/null")))
|
|
172 ;; check for any `Index:' or `Prereq:' lines, but don't use them
|
|
173 (if (re-search-backward "^Index:" mark1-end 'noerror)
|
|
174 (move-marker mark2 (match-beginning 0)))
|
|
175 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
|
|
176 (move-marker mark2 (match-beginning 0)))
|
|
177
|
|
178 (goto-char mark2-end)
|
|
179
|
|
180 (if filenames
|
|
181 (setq patch-map (cons (list filenames mark1 mark2) patch-map)))
|
|
182 (setq mark1 mark2
|
|
183 mark1-end mark2-end
|
|
184 filenames possible-file-names))
|
|
185 (setq opoint (point)
|
|
186 count (1+ count))))
|
|
187 (setq mark2 (point-max-marker)
|
|
188 patch-map (cons (list possible-file-names mark1 mark2) patch-map))
|
|
189 (setq ediff-patch-map (nreverse patch-map))
|
|
190 count)))
|
|
191
|
|
192 ;; Fix up the file names in the list using the argument FILENAME
|
|
193 ;; Algorithm: find the first file's directory and cut it out from each file
|
|
194 ;; name in the patch. Prepend the directory of FILENAME to each file in the
|
|
195 ;; patch. In addition, the first file in the patch is replaced by FILENAME.
|
|
196 ;; Each file is actually a file-pair of files found in the context diff header
|
|
197 ;; In the end, for each pair, we select the shortest existing file.
|
|
198 ;; Note: Ediff doesn't recognize multi-file patches that are separated
|
|
199 ;; with the `Index:' line. It treats them as a single-file patch.
|
|
200 ;;
|
|
201 ;; Executes inside the patch buffer
|
|
202 (defun ediff-fixup-patch-map (filename)
|
|
203 (setq filename (expand-file-name filename))
|
|
204 (let ((actual-dir (if (file-directory-p filename)
|
|
205 ;; directory part of filename
|
|
206 (file-name-as-directory filename)
|
|
207 (file-name-directory filename)))
|
|
208 ;; directory part of the first file in the patch
|
|
209 (base-dir1 (file-name-directory (car (car (car ediff-patch-map)))))
|
|
210 (base-dir2 (file-name-directory (cdr (car (car ediff-patch-map)))))
|
|
211 )
|
|
212
|
|
213 ;; chop off base-dirs
|
|
214 (mapcar (function (lambda (triple)
|
|
215 (or (string= (car (car triple)) "/dev/null")
|
|
216 (setcar (car triple)
|
|
217 (ediff-file-name-sans-prefix
|
|
218 (car (car triple)) base-dir1)))
|
|
219 (or (string= (cdr (car triple)) "/dev/null")
|
|
220 (setcdr (car triple)
|
|
221 (ediff-file-name-sans-prefix
|
|
222 (cdr (car triple)) base-dir2)))
|
|
223 ))
|
|
224 ediff-patch-map)
|
|
225
|
|
226 ;; take the given file name into account
|
|
227 (or (file-directory-p filename)
|
|
228 (string= "/dev/null" filename)
|
|
229 (progn
|
|
230 (setcar (car ediff-patch-map)
|
|
231 (cons (file-name-nondirectory filename)
|
|
232 (file-name-nondirectory filename)))))
|
|
233
|
|
234 ;; prepend actual-dir
|
|
235 (mapcar (function (lambda (triple)
|
|
236 (if (and (string-match "^/null/" (car (car triple)))
|
|
237 (string-match "^/null/" (cdr (car triple))))
|
|
238 ;; couldn't strip base-dir1 and base-dir2
|
|
239 ;; hence, something wrong
|
|
240 (progn
|
|
241 (with-output-to-temp-buffer ediff-msg-buffer
|
|
242 (princ
|
|
243 (format "
|
|
244 The patch file contains a context diff for
|
12
|
245
|
0
|
246 %s
|
|
247 %s
|
|
248
|
|
249 However, Ediff cannot infer the name of the actual file
|
|
250 to be patched on your system. If you know the correct file name,
|
|
251 please enter it now.
|
|
252
|
|
253 If you don't know and still would like to apply patches to
|
|
254 other files, enter /dev/null
|
|
255 "
|
|
256 (substring (car (car triple)) 6)
|
|
257 (substring (cdr (car triple)) 6))))
|
|
258 (let ((directory t)
|
|
259 user-file)
|
|
260 (while directory
|
|
261 (setq user-file
|
|
262 (read-file-name
|
|
263 "Please enter file name: "
|
|
264 actual-dir actual-dir t))
|
|
265 (if (not (file-directory-p user-file))
|
|
266 (setq directory nil)
|
|
267 (setq directory t)
|
|
268 (beep)
|
|
269 (message "%s is a directory" user-file)
|
|
270 (sit-for 2)))
|
|
271 (setcar triple (cons user-file user-file))))
|
|
272 (setcar (car triple)
|
|
273 (expand-file-name
|
|
274 (concat actual-dir (car (car triple)))))
|
|
275 (setcdr (car triple)
|
|
276 (expand-file-name
|
|
277 (concat actual-dir (cdr (car triple))))))
|
|
278 ))
|
|
279 ediff-patch-map)
|
|
280 ;; check for the shorter existing file in each pair and discard the other
|
|
281 ;; one
|
|
282 (mapcar (function (lambda (triple)
|
|
283 (let* ((file1 (car (car triple)))
|
|
284 (file2 (cdr (car triple)))
|
|
285 (f1-exists (file-exists-p file1))
|
|
286 (f2-exists (file-exists-p file2)))
|
|
287 (cond
|
|
288 ((and (< (length file2) (length file1))
|
|
289 f2-exists)
|
|
290 (setcar triple file2))
|
|
291 ((and (< (length file1) (length file2))
|
|
292 f1-exists)
|
|
293 (setcar triple file1))
|
|
294 ((and f1-exists f2-exists
|
|
295 (string= file1 file2))
|
|
296 (setcar triple file1))
|
|
297 ((and f1-exists f2-exists)
|
|
298 (with-output-to-temp-buffer ediff-msg-buffer
|
|
299 (princ (format "
|
|
300 Ediff has inferred that
|
|
301 %s
|
|
302 %s
|
|
303 are possible targets for applying the patch.
|
|
304 Both files seem to be plausible alternatives.
|
|
305
|
|
306 Please advice:
|
|
307 Type `y' to use %s as the target;
|
|
308 Type `n' to use %s as the target.
|
|
309 "
|
|
310 file1 file2 file2 file1)))
|
|
311 (setcar triple
|
|
312 (if (y-or-n-p (format "Use %s ? " file2))
|
|
313 file2 file1)))
|
|
314 (f2-exists (setcar triple file2))
|
|
315 (f1-exists (setcar triple file1))
|
|
316 (t
|
|
317 (with-output-to-temp-buffer ediff-msg-buffer
|
|
318 (princ (format "
|
|
319 Ediff inferred that
|
|
320 %s
|
|
321 %s
|
|
322 are possible alternative targets for this patch.
|
|
323
|
|
324 However, these files do not exist.
|
|
325
|
|
326 Please enter an alternative patch target ...
|
|
327 "
|
|
328 file1 file2)))
|
|
329 (let ((directory t)
|
|
330 target)
|
|
331 (while directory
|
|
332 (setq target (read-file-name
|
|
333 "Please enter a patch target: "
|
|
334 actual-dir actual-dir t))
|
|
335 (if (not (file-directory-p target))
|
|
336 (setq directory nil)
|
|
337 (beep)
|
|
338 (message "%s is a directory" target)
|
|
339 (sit-for 2)))
|
|
340 (setcar triple target)))))))
|
|
341 ediff-patch-map)
|
|
342 ))
|
|
343
|
|
344 (defun ediff-show-patch-diagnostics ()
|
|
345 (interactive)
|
|
346 (cond ((window-live-p ediff-window-A)
|
|
347 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
|
|
348 ((window-live-p ediff-window-B)
|
|
349 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
|
|
350 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
|
|
351
|
|
352 (defun ediff-get-patch-buffer ()
|
|
353 "Obtain patch buffer. If patch is already in a buffer---use it.
|
|
354 Else, read patch file into a new buffer."
|
|
355 (let ((dir (cond (ediff-patch-default-directory) ; try patch default dir
|
|
356 (ediff-use-last-dir ediff-last-dir-patch)
|
|
357 (t default-directory)))
|
|
358 patch-buf)
|
|
359 (if (y-or-n-p "Is the patch already in a buffer? ")
|
|
360 (setq patch-buf
|
|
361 (get-buffer
|
|
362 (read-buffer
|
|
363 "Which buffer contains the patch? "
|
|
364 (current-buffer) 'must-match)))
|
|
365 (setq patch-buf
|
|
366 (find-file-noselect
|
12
|
367 (read-file-name "Which file contains the patch? "
|
|
368 dir nil 'must-match))))
|
0
|
369
|
|
370 (ediff-eval-in-buffer patch-buf
|
|
371 (goto-char (point-min))
|
|
372 (or (ediff-get-visible-buffer-window patch-buf)
|
|
373 (progn
|
|
374 (pop-to-buffer patch-buf 'other-window)
|
|
375 (select-window (previous-window)))))
|
|
376 (ediff-map-patch-buffer patch-buf)
|
|
377 patch-buf))
|
|
378
|
|
379 ;; Dispatch the right patch file function: regular or meta-level,
|
|
380 ;; depending on how many patches are in the patch file.
|
|
381 ;; At present, there is no support for meta-level patches.
|
|
382 ;; Should return either the ctl buffer or the meta-buffer
|
|
383 (defun ediff-dispatch-file-patching-job (patch-buf filename
|
|
384 &optional startup-hooks)
|
|
385 (ediff-eval-in-buffer patch-buf
|
|
386 ;; relativize names in the patch with respect to source-file
|
|
387 (ediff-fixup-patch-map filename)
|
|
388 (if (< (length ediff-patch-map) 2)
|
|
389 (ediff-patch-file-internal
|
|
390 patch-buf
|
|
391 (if (and (not (string-match "^/dev/null" (car (car ediff-patch-map))))
|
|
392 (> (length (car (car ediff-patch-map))) 1))
|
|
393 (car (car ediff-patch-map))
|
|
394 filename)
|
|
395 startup-hooks)
|
|
396 (ediff-multi-patch-internal patch-buf startup-hooks))
|
|
397 ))
|
|
398
|
|
399
|
|
400 (defun ediff-patch-buffer-internal (patch-buf buf-to-patch-name
|
|
401 &optional startup-hooks)
|
|
402 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
|
|
403 (file-name-ok (if buf-to-patch (buffer-file-name buf-to-patch)))
|
|
404 (buf-mod-status (buffer-modified-p buf-to-patch))
|
|
405 (multifile-patch-p (> (length (ediff-eval-in-buffer patch-buf
|
|
406 ediff-patch-map)) 1))
|
|
407 default-dir file-name ctl-buf)
|
|
408 (if file-name-ok
|
|
409 (setq file-name file-name-ok)
|
|
410 (if multifile-patch-p
|
|
411 (error
|
|
412 "Can't apply multi-file patches to buffers that visit no files"))
|
|
413 (ediff-eval-in-buffer buf-to-patch
|
|
414 (setq default-dir default-directory)
|
|
415 (setq file-name (ediff-make-temp-file buf-to-patch))
|
|
416 (set-visited-file-name file-name)
|
|
417 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
|
|
418 ;;don't confuse the user with a new bufname
|
|
419 (rename-buffer buf-to-patch-name)
|
|
420 (set-buffer-modified-p nil)
|
|
421 (set-visited-file-modtime) ; sync buffer and temp file
|
|
422 (setq default-directory default-dir)
|
|
423 ))
|
|
424
|
|
425 ;; dispatch a patch function
|
|
426 (setq ctl-buf (ediff-dispatch-file-patching-job
|
|
427 patch-buf file-name startup-hooks))
|
|
428
|
|
429 (if file-name-ok
|
|
430 ()
|
|
431 ;; buffer wasn't visiting any file,
|
|
432 ;; so we will not run meta-level ediff here
|
|
433 (ediff-eval-in-buffer ctl-buf
|
|
434 (delete-file (buffer-file-name ediff-buffer-A))
|
|
435 (delete-file (buffer-file-name ediff-buffer-B))
|
|
436 (ediff-eval-in-buffer ediff-buffer-A
|
|
437 (if default-dir (setq default-directory default-dir))
|
|
438 (set-visited-file-name nil)
|
|
439 (rename-buffer buf-to-patch-name)
|
|
440 (set-buffer-modified-p buf-mod-status))
|
|
441 (ediff-eval-in-buffer ediff-buffer-B
|
|
442 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
|
|
443 (if default-dir (setq default-directory default-dir))
|
|
444 (set-visited-file-name nil)
|
|
445 (rename-buffer (ediff-unique-buffer-name
|
|
446 (concat buf-to-patch-name "_patched") ""))
|
|
447 (set-buffer-modified-p t))))
|
|
448 ))
|
|
449
|
|
450 (defun ediff-patch-file-internal (patch-buf source-filename
|
|
451 &optional startup-hooks)
|
|
452 (setq source-filename (expand-file-name source-filename))
|
|
453
|
12
|
454 (let* ((shell-file-name ediff-shell)
|
0
|
455 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
|
|
456 ;; ediff-find-file may use a temp file to do the patch
|
|
457 ;; so, we save source-filename and true-source-filename as a var
|
|
458 ;; that initially is source-filename but may be changed to a temp
|
|
459 ;; file for the purpose of patching.
|
|
460 (true-source-filename source-filename)
|
|
461 (target-filename source-filename)
|
12
|
462 target-buf buf-to-patch file-name-magic-p
|
|
463 patch-return-code ctl-buf backup-style aux-wind)
|
0
|
464
|
|
465 (if (string-match "-V" ediff-patch-options)
|
|
466 (error
|
|
467 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
|
|
468
|
|
469 ;; Make a temp file, if source-filename has a magic file handler (or if
|
|
470 ;; it is handled via auto-mode-alist and similar magic).
|
|
471 ;; Check if there is a buffer visiting source-filename and if they are in
|
|
472 ;; sync; arrange for the deletion of temp file.
|
|
473 (ediff-find-file 'true-source-filename 'buf-to-patch
|
|
474 'ediff-last-dir-patch 'startup-hooks)
|
|
475
|
|
476 ;; Check if source file name has triggered black magic, such as file name
|
|
477 ;; handlers or auto mode alist, and make a note of it.
|
|
478 ;; true-source-filename should be either the original name or a
|
|
479 ;; temporary file where we put the after-product of the file handler.
|
|
480 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
|
|
481 (file-truename source-filename))))
|
|
482
|
12
|
483 ;; Checkout orig file, if necessary, so that the patched file
|
|
484 ;; could be checked back in.
|
|
485 (ediff-maybe-checkout buf-to-patch)
|
0
|
486
|
|
487 (ediff-eval-in-buffer patch-diagnostics
|
|
488 (insert-buffer patch-buf)
|
|
489 (message "Applying patch ... ")
|
|
490 ;; fix environment for gnu patch, so it won't make numbered extensions
|
|
491 (setq backup-style (getenv "VERSION_CONTROL"))
|
|
492 (setenv "VERSION_CONTROL" nil)
|
12
|
493 (setq patch-return-code
|
|
494 (call-process-region
|
|
495 (point-min) (point-max)
|
|
496 shell-file-name
|
|
497 t ; delete region (which contains the patch
|
|
498 t ; insert output (patch diagnostics) in current buffer
|
|
499 nil ; don't redisplay
|
|
500 shell-command-switch ; usually -c
|
|
501 (format "%s %s %s %s"
|
|
502 ediff-patch-program
|
|
503 ediff-patch-options
|
|
504 ediff-backup-specs
|
|
505 (expand-file-name true-source-filename))
|
|
506 ))
|
|
507
|
0
|
508 ;; restore environment for gnu patch
|
|
509 (setenv "VERSION_CONTROL" backup-style))
|
|
510
|
|
511 (message "Applying patch ... done")
|
|
512 (message "")
|
|
513
|
|
514 (switch-to-buffer patch-diagnostics)
|
|
515 (sit-for 0) ; synchronize - let the user see diagnostics
|
|
516
|
12
|
517 (or (and (eq patch-return-code 0) ; patch reported success
|
|
518 (file-exists-p
|
|
519 (concat true-source-filename ediff-backup-extension)))
|
|
520 (progn
|
|
521 (with-output-to-temp-buffer ediff-msg-buffer
|
|
522 (princ (format "
|
|
523 Patch has failed OR the backup version of the patched file was not created by
|
|
524 the patch program.
|
|
525
|
|
526 One reason may be that the values of the variables
|
|
527
|
|
528 ediff-patch-options = %S
|
|
529 ediff-backup-extension = %S
|
|
530 ediff-backup-specs = %S
|
|
531
|
|
532 are not appropriate for the program specified in the variable
|
|
533
|
|
534 ediff-patch-program = %S
|
|
535
|
|
536 Another reason could be that the %S program doesn't understand
|
|
537 the format of the patch file you used.
|
|
538
|
|
539 See Ediff on-line manual for more details on these variables.
|
|
540 \(Or use a GNU-compatible patch program and stay out of trouble.\)
|
|
541
|
|
542 Type any key to continue...
|
|
543 "
|
|
544 ediff-patch-options
|
|
545 ediff-backup-extension
|
|
546 ediff-backup-specs
|
|
547 ediff-patch-program
|
|
548 ediff-patch-program)))
|
|
549 (beep 1)
|
|
550 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
|
|
551 (progn
|
|
552 (select-window aux-wind)
|
|
553 (goto-char (point-max))))
|
|
554 (read-char-exclusive)
|
|
555 (if aux-wind (bury-buffer)) ; ediff-msg-buffer
|
|
556 (if (setq aux-wind (get-buffer-window patch-diagnostics))
|
|
557 (progn
|
|
558 (select-window aux-wind)
|
|
559 (bury-buffer)))
|
|
560 (error "Patch appears to have failed")))
|
14
|
561
|
0
|
562 ;; If black magic is involved, apply patch to a temp copy of the
|
|
563 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
|
|
564 ;; to temp copy, we name the result old-name_patched for local files
|
|
565 ;; and temp-copy_patched for remote files. The orig file name isn't
|
|
566 ;; changed, and the temp copy of the original is later deleted.
|
|
567 ;; Without magic, the original file is renamed (usually into
|
|
568 ;; old-name_orig) and the result of patching will have the same name as
|
|
569 ;; the original.
|
|
570 (if (not file-name-magic-p)
|
|
571 (ediff-eval-in-buffer buf-to-patch
|
12
|
572 (set-visited-file-name
|
|
573 (concat source-filename ediff-backup-extension))
|
0
|
574 (set-buffer-modified-p nil))
|
|
575
|
|
576 ;; Black magic in effect.
|
|
577 ;; If orig file was remote, put the patched file in the temp directory.
|
|
578 ;; If orig file is local, put the patched file in the directory of
|
|
579 ;; the orig file.
|
|
580 (setq target-filename
|
|
581 (concat
|
|
582 (if (ediff-file-remote-p (file-truename source-filename))
|
|
583 true-source-filename
|
|
584 source-filename)
|
|
585 "_patched"))
|
|
586
|
|
587 (rename-file true-source-filename target-filename t)
|
|
588
|
|
589 ;; arrange that the temp copy of orig will be deleted
|
12
|
590 (rename-file (concat true-source-filename ediff-backup-extension)
|
0
|
591 true-source-filename t))
|
14
|
592
|
0
|
593 ;; make orig buffer read-only
|
|
594 (setq startup-hooks
|
|
595 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
|
14
|
596
|
0
|
597 ;; set up a buf for the patched file
|
|
598 (setq target-buf (find-file-noselect target-filename))
|
|
599
|
|
600 (setq ctl-buf
|
|
601 (ediff-buffers-internal
|
|
602 buf-to-patch target-buf nil
|
|
603 startup-hooks 'epatch))
|
|
604 (ediff-eval-in-buffer ctl-buf
|
|
605 (setq ediff-patchbufer patch-buf
|
|
606 ediff-patch-diagnostics patch-diagnostics))
|
|
607
|
|
608 (bury-buffer patch-diagnostics)
|
|
609 (message "Type `P', if you need to see patch diagnostics")
|
|
610 ctl-buf))
|
|
611
|
|
612 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
|
|
613 (let (meta-buf)
|
|
614 (setq startup-hooks
|
|
615 ;; this sets various vars in the meta buffer inside
|
|
616 ;; ediff-prepare-meta-buffer
|
|
617 (cons (` (lambda ()
|
|
618 ;; tell what to do if the user clicks on a session record
|
|
619 (setq ediff-session-action-function
|
|
620 'ediff-patch-file-form-meta
|
|
621 ediff-meta-patchbufer patch-buf)
|
|
622 ))
|
|
623 startup-hooks))
|
|
624 (setq meta-buf (ediff-prepare-meta-buffer
|
|
625 'ediff-filegroup-action
|
|
626 (ediff-eval-in-buffer patch-buf
|
|
627 ;; nil replaces a regular expression
|
|
628 (cons (list nil (format "%S" patch-buf))
|
|
629 ediff-patch-map))
|
|
630 "*Ediff Session Group Panel"
|
|
631 'ediff-redraw-directory-group-buffer
|
|
632 'ediff-multifile-patch
|
|
633 startup-hooks))
|
|
634 (ediff-show-meta-buffer meta-buf)
|
|
635 ))
|
|
636
|
|
637
|
|
638
|
|
639
|
|
640 ;;; Local Variables:
|
|
641 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
|
|
642 ;;; eval: (put 'ediff-eval-in-buffer 'lisp-indent-hook 1)
|
12
|
643 ;;; eval: (put 'ediff-eval-in-buffer 'edebug-form-spec '(form body))
|
0
|
644 ;;; End:
|
|
645
|
|
646 ;;; ediff-ptch.el ends here
|