Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / vc / ediff-ptch.el
blobb6c7f6ab7ba46992ed9bf7bcd98ef1d2cd2233dd
1 ;;; ediff-ptch.el --- Ediff's patch support
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
7 ;; Package: ediff
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;;; Code:
29 (provide 'ediff-ptch)
31 (defgroup ediff-ptch nil
32 "Ediff patch support."
33 :tag "Patch"
34 :prefix "ediff-"
35 :group 'ediff)
37 ;; compiler pacifier
38 (eval-when-compile
39 (require 'ediff))
40 ;; end pacifier
42 (require 'ediff-init)
44 (defcustom ediff-patch-program "patch"
45 "Name of the program that applies patches.
46 It is recommended to use GNU-compatible versions."
47 :type 'string
48 :group 'ediff-ptch)
49 (defcustom ediff-patch-options "-f"
50 "Options to pass to ediff-patch-program.
52 Note: the `-b' option should be specified in `ediff-backup-specs'.
54 It is recommended to pass the `-f' option to the patch program, so it won't ask
55 questions. However, some implementations don't accept this option, in which
56 case the default value for this variable should be changed."
57 :type 'string
58 :group 'ediff-ptch)
60 (defvar ediff-last-dir-patch nil
61 "Last directory used by an Ediff command for file to patch.")
63 ;; the default backup extension
64 (defconst ediff-default-backup-extension
65 (if (memq system-type '(emx ms-dos))
66 "_orig" ".orig"))
69 (defcustom ediff-backup-extension ediff-default-backup-extension
70 "Backup extension used by the patch program.
71 See also `ediff-backup-specs'."
72 :type 'string
73 :group 'ediff-ptch)
75 (defun ediff-test-patch-utility ()
76 (condition-case nil
77 (cond ((eq 0 (call-process ediff-patch-program nil nil nil "-z." "-b"))
78 ;; GNU `patch' v. >= 2.2
79 'gnu)
80 ((eq 0 (call-process ediff-patch-program nil nil nil "-b"))
81 'posix)
82 (t 'traditional))
83 (file-error nil)))
85 (defcustom ediff-backup-specs
86 (let ((type (ediff-test-patch-utility)))
87 (cond ((eq type 'gnu)
88 ;; GNU `patch' v. >= 2.2
89 (format "-z%s -b" ediff-backup-extension))
90 ((eq type 'posix)
91 ;; POSIX `patch' -- ediff-backup-extension must be ".orig"
92 (setq ediff-backup-extension ediff-default-backup-extension)
93 "-b")
95 ;; traditional `patch'
96 (format "-b %s" ediff-backup-extension))))
97 "Backup directives to pass to the patch program.
98 Ediff requires that the old version of the file \(before applying the patch\)
99 be saved in a file named `the-patch-file.extension'. Usually `extension' is
100 `.orig', but this can be changed by the user and may depend on the system.
101 Therefore, Ediff needs to know the backup extension used by the patch program.
103 Some versions of the patch program let you specify `-b backup-extension'.
104 Other versions only permit `-b', which assumes the extension `.orig'
105 \(in which case ediff-backup-extension MUST be also `.orig'\). The latest
106 versions of GNU patch require `-b -z backup-extension'.
108 Note that both `ediff-backup-extension' and `ediff-backup-specs'
109 must be set properly. If your patch program takes the option `-b',
110 but not `-b extension', the variable `ediff-backup-extension' must
111 still be set so Ediff will know which extension to use.
113 Ediff tries to guess the appropriate value for this variables. It is believed
114 to be working for `traditional' patch, all versions of GNU patch, and for POSIX
115 patch. So, don't change these variables, unless the default doesn't work."
116 :type 'string
117 :group 'ediff-ptch)
120 (defcustom ediff-patch-default-directory nil
121 "Default directory to look for patches."
122 :type '(choice (const nil) string)
123 :group 'ediff-ptch)
125 ;; This context diff does not recognize spaces inside files, but removing ' '
126 ;; from [^ \t] breaks normal patches for some reason
127 (defcustom ediff-context-diff-label-regexp
128 (concat "\\(" ; context diff 2-liner
129 "^\\*\\*\\* +\\([^ \t]+\\)[^*]+[\t ]*\n--- +\\([^ \t]+\\)"
130 "\\|" ; unified format diff 2-liner
131 "^--- +\\([^ \t]+\\).*\n\\+\\+\\+ +\\([^ \t]+\\)"
132 "\\)")
133 "Regexp matching filename 2-liners at the start of each context diff.
134 You probably don't want to change that, unless you are using an obscure patch
135 program."
136 :type 'regexp
137 :group 'ediff-ptch)
139 ;; The buffer of the patch file. Local to control buffer.
140 (ediff-defvar-local ediff-patchbufer nil "")
142 ;; The buffer where patch displays its diagnostics.
143 (ediff-defvar-local ediff-patch-diagnostics nil "")
145 ;; Map of patch buffer. Has the form:
146 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
147 ;; where filenames are files to which patch would have applied the patch;
148 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
149 ;; it for the end.
150 (ediff-defvar-local ediff-patch-map nil "")
152 ;; strip prefix from filename
153 ;; returns /dev/null, if can't strip prefix
154 (defsubst ediff-file-name-sans-prefix (filename prefix)
155 (if prefix
156 (save-match-data
157 (if (string-match (concat "^" (if (stringp prefix)
158 (regexp-quote prefix)
159 ""))
160 filename)
161 (substring filename (match-end 0))
162 (concat "/null/" filename)))
163 filename)
168 ;; no longer used
169 ;; return the number of matches of regexp in buf starting from the beginning
170 (defun ediff-count-matches (regexp buf)
171 (ediff-with-current-buffer buf
172 (let ((count 0) opoint)
173 (save-excursion
174 (goto-char (point-min))
175 (while (and (not (eobp))
176 (progn (setq opoint (point))
177 (re-search-forward regexp nil t)))
178 (if (= opoint (point))
179 (forward-char 1)
180 (setq count (1+ count)))))
181 count)))
183 ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
184 ;; ((nil nil filename-spec1 marker1 marker2)
185 ;; (nil nil filename-spec2 marker1 marker2) ...)
186 ;; where filename-spec[12] are files to which the `patch' program would
187 ;; have applied the patch.
188 ;; nin, nil are placeholders. See ediff-make-new-meta-list-element in
189 ;; ediff-meta.el for the explanations.
190 ;; In the beginning we don't know exactly which files need to be patched.
191 ;; We usually come up with two candidates and ediff-file-name-sans-prefix
192 ;; resolves this later.
194 ;; The marker `marker1' delimits the beginning of the corresponding patch and
195 ;; `marker2' does it for the end.
196 ;; The result of ediff-map-patch-buffer is a list, which is then assigned
197 ;; to ediff-patch-map.
198 ;; The function returns the number of elements in the list ediff-patch-map
199 (defun ediff-map-patch-buffer (buf)
200 (ediff-with-current-buffer buf
201 (let ((count 0)
202 (mark1 (move-marker (make-marker) (point-min)))
203 (mark1-end (point-min))
204 (possible-file-names '("/dev/null" . "/dev/null"))
205 mark2-end mark2 filenames
206 beg1 beg2 end1 end2
207 patch-map opoint)
208 (save-excursion
209 (goto-char (point-min))
210 (setq opoint (point))
211 (while (and (not (eobp))
212 (re-search-forward ediff-context-diff-label-regexp nil t))
213 (if (= opoint (point))
214 (forward-char 1) ; ensure progress towards the end
215 (setq mark2 (move-marker (make-marker) (match-beginning 0))
216 mark2-end (match-end 0)
217 beg1 (or (match-beginning 2) (match-beginning 4))
218 end1 (or (match-end 2) (match-end 4))
219 beg2 (or (match-beginning 3) (match-beginning 5))
220 end2 (or (match-end 3) (match-end 5)))
221 ;; possible-file-names is holding the new file names until we
222 ;; insert the old file name in the patch map
223 ;; It is a pair
224 ;; (filename-from-1st-header-line . filename-from-2nd-line)
225 (setq possible-file-names
226 (cons (if (and beg1 end1)
227 (buffer-substring beg1 end1)
228 "/dev/null")
229 (if (and beg2 end2)
230 (buffer-substring beg2 end2)
231 "/dev/null")))
232 ;; check for any `Index:' or `Prereq:' lines, but don't use them
233 (if (re-search-backward "^Index:" mark1-end 'noerror)
234 (move-marker mark2 (match-beginning 0)))
235 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
236 (move-marker mark2 (match-beginning 0)))
238 (goto-char mark2-end)
240 (if filenames
241 (setq patch-map
242 (cons (ediff-make-new-meta-list-element
243 filenames mark1 mark2)
244 patch-map)))
245 (setq mark1 mark2
246 mark1-end mark2-end
247 filenames possible-file-names))
248 (setq opoint (point)
249 count (1+ count))))
250 (setq mark2 (point-max-marker)
251 patch-map (cons (ediff-make-new-meta-list-element
252 possible-file-names mark1 mark2)
253 patch-map))
254 (setq ediff-patch-map (nreverse patch-map))
255 count)))
257 ;; Fix up the file names in the list using the argument FILENAME
258 ;; Algorithm: find the files' directories in the patch and, if a directory is
259 ;; absolute, cut it out from the corresponding file name in the patch.
260 ;; Relative directories are not cut out.
261 ;; Prepend the directory of FILENAME to each resulting file (which came
262 ;; originally from the patch).
263 ;; In addition, the first file in the patch document is replaced by FILENAME.
264 ;; Each file is actually a pair of files found in the context diff header
265 ;; In the end, for each pair, we ask the user which file to patch.
266 ;; Note: Ediff doesn't recognize multi-file patches that are separated
267 ;; with the `Index:' line. It treats them as a single-file patch.
269 ;; Executes inside the patch buffer
270 (defun ediff-fixup-patch-map (filename)
271 (setq filename (expand-file-name filename))
272 (let ((actual-dir (if (file-directory-p filename)
273 ;; directory part of filename
274 (file-name-as-directory filename)
275 (file-name-directory filename)))
276 ;; In case 2 files are possible patch targets, the user will be offered
277 ;; to choose file1 or file2. In a multifile patch, if the user chooses
278 ;; 1 or 2, this choice is preserved to decide future alternatives.
279 chosen-alternative
282 ;; chop off base-dirs
283 (mapc (lambda (session-info)
284 (let* ((proposed-file-names
285 ;; Filename-spec is objA; it is represented as
286 ;; (file1 . file2). Get it using ediff-get-session-objA.
287 (ediff-get-session-objA-name session-info))
288 ;; base-dir1 is the dir part of the 1st file in the patch
289 (base-dir1
290 (or (file-name-directory (car proposed-file-names))
291 ""))
292 ;; directory part of the 2nd file in the patch
293 (base-dir2
294 (or (file-name-directory (cdr proposed-file-names))
295 ""))
297 ;; If both base-dir1 and base-dir2 are relative and exist,
298 ;; assume that
299 ;; these dirs lead to the actual files starting at the present
300 ;; directory. So, we don't strip these relative dirs from the
301 ;; file names. This is a heuristic intended to improve guessing
302 (let ((default-directory (file-name-directory filename)))
303 (unless (or (file-name-absolute-p base-dir1)
304 (file-name-absolute-p base-dir2)
305 (not (file-exists-p base-dir1))
306 (not (file-exists-p base-dir2)))
307 (setq base-dir1 ""
308 base-dir2 "")))
309 (or (string= (car proposed-file-names) "/dev/null")
310 (setcar proposed-file-names
311 (ediff-file-name-sans-prefix
312 (car proposed-file-names) base-dir1)))
313 (or (string=
314 (cdr proposed-file-names) "/dev/null")
315 (setcdr proposed-file-names
316 (ediff-file-name-sans-prefix
317 (cdr proposed-file-names) base-dir2)))
319 ediff-patch-map)
321 ;; take the given file name into account
322 (or (file-directory-p filename)
323 (string= "/dev/null" filename)
324 (setcar (ediff-get-session-objA (car ediff-patch-map))
325 (cons (file-name-nondirectory filename)
326 (file-name-nondirectory filename))))
328 ;; prepend actual-dir
329 (mapc (lambda (session-info)
330 (let ((proposed-file-names
331 (ediff-get-session-objA-name session-info)))
332 (if (and (string-match "^/null/" (car proposed-file-names))
333 (string-match "^/null/" (cdr proposed-file-names)))
334 ;; couldn't intuit the file name to patch, so
335 ;; something is amiss
336 (progn
337 (with-output-to-temp-buffer ediff-msg-buffer
338 (ediff-with-current-buffer standard-output
339 (fundamental-mode))
340 (princ
341 (format "
342 The patch file contains a context diff for
345 However, Ediff cannot infer the name of the actual file
346 to be patched on your system. If you know the correct file name,
347 please enter it now.
349 If you don't know and still would like to apply patches to
350 other files, enter /dev/null
352 (substring (car proposed-file-names) 6)
353 (substring (cdr proposed-file-names) 6))))
354 (let ((directory t)
355 user-file)
356 (while directory
357 (setq user-file
358 (read-file-name
359 "Please enter file name: "
360 actual-dir actual-dir t))
361 (if (not (file-directory-p user-file))
362 (setq directory nil)
363 (setq directory t)
364 (beep)
365 (message "%s is a directory" user-file)
366 (sit-for 2)))
367 (setcar (ediff-get-session-objA session-info)
368 (cons user-file user-file))))
369 (setcar proposed-file-names
370 (expand-file-name
371 (concat actual-dir (car proposed-file-names))))
372 (setcdr proposed-file-names
373 (expand-file-name
374 (concat actual-dir (cdr proposed-file-names)))))
376 ediff-patch-map)
377 ;; Check for the existing files in each pair and discard the nonexisting
378 ;; ones. If both exist, ask the user.
379 (mapcar (lambda (session-info)
380 (let* ((file1 (car (ediff-get-session-objA-name session-info)))
381 (file2 (cdr (ediff-get-session-objA-name session-info)))
382 (session-file-object
383 (ediff-get-session-objA session-info))
384 (f1-exists (file-exists-p file1))
385 (f2-exists (file-exists-p file2)))
386 (cond
387 ((and
388 ;; The patch program prefers the shortest file as the patch
389 ;; target. However, this is a questionable heuristic. In an
390 ;; interactive program, like ediff, we can offer the user a
391 ;; choice.
392 ;; (< (length file2) (length file1))
393 (not f1-exists)
394 f2-exists)
395 ;; replace file-pair with the winning file2
396 (setcar session-file-object file2))
397 ((and
398 ;; (< (length file1) (length file2))
399 (not f2-exists)
400 f1-exists)
401 ;; replace file-pair with the winning file1
402 (setcar session-file-object file1))
403 ((and f1-exists f2-exists
404 (string= file1 file2))
405 (setcar session-file-object file1))
406 ((and f1-exists f2-exists (eq chosen-alternative 1))
407 (setcar session-file-object file1))
408 ((and f1-exists f2-exists (eq chosen-alternative 2))
409 (setcar session-file-object file2))
410 ((and f1-exists f2-exists)
411 (with-output-to-temp-buffer ediff-msg-buffer
412 (ediff-with-current-buffer standard-output
413 (fundamental-mode))
414 (princ (format "
415 Ediff has inferred that
418 are two possible targets for applying the patch.
419 Both files seem to be plausible alternatives.
421 Please advice:
422 Type `y' to use %s as the target;
423 Type `n' to use %s as the target.
425 file1 file2 file1 file2)))
426 (setcar session-file-object
427 (if (y-or-n-p (format "Use %s ? " file1))
428 (progn
429 (setq chosen-alternative 1)
430 file1)
431 (setq chosen-alternative 2)
432 file2))
434 (f2-exists (setcar session-file-object file2))
435 (f1-exists (setcar session-file-object file1))
437 (with-output-to-temp-buffer ediff-msg-buffer
438 (ediff-with-current-buffer standard-output
439 (fundamental-mode))
440 (princ "\nEdiff has inferred that")
441 (if (string= file1 file2)
442 (princ (format "
444 is assumed to be the target for this patch. However, this file does not exist."
445 file1))
446 (princ (format "
449 are two possible targets for this patch. However, these files do not exist."
450 file1 file2)))
451 (princ "
452 \nPlease enter an alternative patch target ...\n"))
453 (let ((directory t)
454 target)
455 (while directory
456 (setq target (read-file-name
457 "Please enter a patch target: "
458 actual-dir actual-dir t))
459 (if (not (file-directory-p target))
460 (setq directory nil)
461 (beep)
462 (message "%s is a directory" target)
463 (sit-for 2)))
464 (setcar session-file-object target))))))
465 ediff-patch-map)
468 (defun ediff-show-patch-diagnostics ()
469 (interactive)
470 (cond ((window-live-p ediff-window-A)
471 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
472 ((window-live-p ediff-window-B)
473 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
474 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
476 ;; prompt for file, get the buffer
477 (defun ediff-prompt-for-patch-file ()
478 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
479 (ediff-patch-default-directory) ; try patch default dir
480 (t default-directory)))
481 (coding-system-for-read ediff-coding-system-for-read)
482 patch-file-name)
483 (setq patch-file-name
484 (read-file-name
485 (format "Patch is in file%s: "
486 (cond ((and buffer-file-name
487 (equal (expand-file-name dir)
488 (file-name-directory buffer-file-name)))
489 (concat
490 " (default "
491 (file-name-nondirectory buffer-file-name)
492 ")"))
493 (t "")))
494 dir buffer-file-name 'must-match))
495 (if (file-directory-p patch-file-name)
496 (error "Patch file cannot be a directory: %s" patch-file-name)
497 (find-file-noselect patch-file-name))
501 ;; Try current buffer, then the other window's buffer. Else, give up.
502 (defun ediff-prompt-for-patch-buffer ()
503 (get-buffer
504 (read-buffer
505 "Buffer that holds the patch: "
506 (cond ((save-excursion
507 (goto-char (point-min))
508 (re-search-forward ediff-context-diff-label-regexp nil t))
509 (current-buffer))
510 ((save-window-excursion
511 (other-window 1)
512 (save-excursion
513 (goto-char (point-min))
514 (and (re-search-forward ediff-context-diff-label-regexp nil t)
515 (current-buffer)))))
516 ((save-window-excursion
517 (other-window -1)
518 (save-excursion
519 (goto-char (point-min))
520 (and (re-search-forward ediff-context-diff-label-regexp nil t)
521 (current-buffer)))))
522 (t (ediff-other-buffer (current-buffer))))
523 'must-match)))
526 (defun ediff-get-patch-buffer (&optional arg patch-buf)
527 "Obtain patch buffer. If patch is already in a buffer---use it.
528 Else, read patch file into a new buffer. If patch buffer is passed as an
529 optional argument, then use it."
530 (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
531 last-command-event) ; XEmacs: don't use dialog box
533 (cond ((ediff-buffer-live-p patch-buf))
534 ;; even prefix arg: patch in buffer
535 ((and (integerp arg) (eq 0 (mod arg 2)))
536 (setq patch-buf (ediff-prompt-for-patch-buffer)))
537 ;; odd prefix arg: get patch from a file
538 ((and (integerp arg) (eq 1 (mod arg 2)))
539 (setq patch-buf (ediff-prompt-for-patch-file)))
540 (t (setq patch-buf
541 (if (y-or-n-p "Is the patch already in a buffer? ")
542 (ediff-prompt-for-patch-buffer)
543 (ediff-prompt-for-patch-file)))))
545 (ediff-with-current-buffer patch-buf
546 (goto-char (point-min))
547 (or (ediff-get-visible-buffer-window patch-buf)
548 (progn
549 (pop-to-buffer patch-buf 'other-window)
550 (select-window (previous-window)))))
551 (ediff-map-patch-buffer patch-buf)
552 patch-buf))
554 ;; Dispatch the right patch file function: regular or meta-level,
555 ;; depending on how many patches are in the patch file.
556 ;; At present, there is no support for meta-level patches.
557 ;; Should return either the ctl buffer or the meta-buffer
558 (defun ediff-dispatch-file-patching-job (patch-buf filename
559 &optional startup-hooks)
560 (ediff-with-current-buffer patch-buf
561 ;; relativize names in the patch with respect to source-file
562 (ediff-fixup-patch-map filename)
563 (if (< (length ediff-patch-map) 2)
564 (ediff-patch-file-internal
565 patch-buf
566 (if (and ediff-patch-map
567 (not (string-match
568 "^/dev/null"
569 ;; this is the file to patch
570 (ediff-get-session-objA-name (car ediff-patch-map))))
571 (> (length
572 (ediff-get-session-objA-name (car ediff-patch-map)))
574 (ediff-get-session-objA-name (car ediff-patch-map))
575 filename)
576 startup-hooks)
577 (ediff-multi-patch-internal patch-buf startup-hooks))
581 ;; When patching a buffer, never change the orig file. Instead, create a new
582 ;; buffer, ***_patched, even if the buff visits a file.
583 ;; Users who want to actually patch the buffer should use
584 ;; ediff-patch-file, not ediff-patch-buffer.
585 (defun ediff-patch-buffer-internal (patch-buf
586 buf-to-patch-name
587 &optional startup-hooks)
588 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
589 (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
590 (buf-mod-status (buffer-modified-p buf-to-patch))
591 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
592 ediff-patch-map)) 1))
593 default-dir file-name ctl-buf)
594 (if multifile-patch-p
595 (error
596 "To apply multi-file patches, please use `ediff-patch-file'"))
598 ;; create a temp file to patch
599 (ediff-with-current-buffer buf-to-patch
600 (setq default-dir default-directory)
601 (setq file-name (ediff-make-temp-file buf-to-patch))
602 ;; temporarily switch visited file name, if any
603 (set-visited-file-name file-name)
604 ;; don't create auto-save file, if buff was visiting a file
605 (or visited-file
606 (setq buffer-auto-save-file-name nil))
607 ;; don't confuse the user with a new bufname
608 (rename-buffer buf-to-patch-name)
609 (set-buffer-modified-p nil)
610 (set-visited-file-modtime) ; sync buffer and temp file
611 (setq default-directory default-dir)
614 ;; dispatch a patch function
615 (setq ctl-buf (ediff-dispatch-file-patching-job
616 patch-buf file-name startup-hooks))
618 (ediff-with-current-buffer ctl-buf
619 (delete-file (buffer-file-name ediff-buffer-A))
620 (delete-file (buffer-file-name ediff-buffer-B))
621 (ediff-with-current-buffer ediff-buffer-A
622 (if default-dir (setq default-directory default-dir))
623 (set-visited-file-name visited-file) ; visited-file might be nil
624 (rename-buffer buf-to-patch-name)
625 (set-buffer-modified-p buf-mod-status))
626 (ediff-with-current-buffer ediff-buffer-B
627 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
628 (if default-dir (setq default-directory default-dir))
629 (set-visited-file-name nil)
630 (rename-buffer (ediff-unique-buffer-name
631 (concat buf-to-patch-name "_patched") ""))
632 (set-buffer-modified-p t)))
636 ;; Traditional patch has weird return codes.
637 ;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
638 ;; 0 is a good code in all cases.
639 ;; We'll do the concervative thing.
640 (defun ediff-patch-return-code-ok (code)
641 (eq code 0))
642 ;;; (if (eq (ediff-test-patch-utility) 'traditional)
643 ;;; (eq code 0)
644 ;;; (not (eq code 2))))
646 (defun ediff-patch-file-internal (patch-buf source-filename
647 &optional startup-hooks)
648 (setq source-filename (expand-file-name source-filename))
650 (let* ((shell-file-name ediff-shell)
651 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
652 ;; ediff-find-file may use a temp file to do the patch
653 ;; so, we save source-filename and true-source-filename as a var
654 ;; that initially is source-filename but may be changed to a temp
655 ;; file for the purpose of patching.
656 (true-source-filename source-filename)
657 (target-filename source-filename)
658 ;; this ensures that the patch process gets patch buffer in the
659 ;; encoding that Emacs thinks is right for that type of text
660 (coding-system-for-write
661 (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
662 target-buf buf-to-patch file-name-magic-p
663 patch-return-code ctl-buf backup-style aux-wind)
665 (if (string-match "V" ediff-patch-options)
666 (error
667 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
669 ;; Make a temp file, if source-filename has a magic file handler (or if
670 ;; it is handled via auto-mode-alist and similar magic).
671 ;; Check if there is a buffer visiting source-filename and if they are in
672 ;; sync; arrange for the deletion of temp file.
673 (ediff-find-file 'true-source-filename 'buf-to-patch
674 'ediff-last-dir-patch 'startup-hooks)
676 ;; Check if source file name has triggered black magic, such as file name
677 ;; handlers or auto mode alist, and make a note of it.
678 ;; true-source-filename should be either the original name or a
679 ;; temporary file where we put the after-product of the file handler.
680 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
681 (file-truename source-filename))))
683 ;; Checkout orig file, if necessary, so that the patched file
684 ;; could be checked back in.
685 (ediff-maybe-checkout buf-to-patch)
687 (ediff-with-current-buffer patch-diagnostics
688 (insert-buffer-substring patch-buf)
689 (message "Applying patch ... ")
690 ;; fix environment for gnu patch, so it won't make numbered extensions
691 (setq backup-style (getenv "VERSION_CONTROL"))
692 (setenv "VERSION_CONTROL" nil)
693 (setq patch-return-code
694 (call-process-region
695 (point-min) (point-max)
696 shell-file-name
697 t ; delete region (which contains the patch
698 t ; insert output (patch diagnostics) in current buffer
699 nil ; don't redisplay
700 shell-command-switch ; usually -c
701 (format "%s %s %s %s"
702 ediff-patch-program
703 ediff-patch-options
704 ediff-backup-specs
705 (expand-file-name true-source-filename))
708 ;; restore environment for gnu patch
709 (setenv "VERSION_CONTROL" backup-style))
711 (message "Applying patch ... done")
712 (message "")
714 (switch-to-buffer patch-diagnostics)
715 (sit-for 0) ; synchronize - let the user see diagnostics
717 (or (and (ediff-patch-return-code-ok patch-return-code)
718 (file-exists-p
719 (concat true-source-filename ediff-backup-extension)))
720 (progn
721 (with-output-to-temp-buffer ediff-msg-buffer
722 (ediff-with-current-buffer standard-output
723 (fundamental-mode))
724 (princ (format
725 "Patch program has failed due to a bad patch file,
726 it couldn't apply all hunks, OR
727 it couldn't create the backup for the file being patched.
729 The former could be caused by a corrupt patch file or because the %S
730 program doesn't understand the format of the patch file in use.
732 The second problem might be due to an incompatibility among these settings:
733 ediff-patch-program = %S ediff-patch-options = %S
734 ediff-backup-extension = %S ediff-backup-specs = %S
736 See Ediff on-line manual for more details on these variables.
737 In particular, check the documentation for `ediff-backup-specs'.
739 In any of the above cases, Ediff doesn't compare files automatically.
740 However, if the patch was applied partially and the backup file was created,
741 you can still examine the changes via M-x ediff-files"
742 ediff-patch-program
743 ediff-patch-program
744 ediff-patch-options
745 ediff-backup-extension
746 ediff-backup-specs
748 (beep 1)
749 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
750 (progn
751 (select-window aux-wind)
752 (goto-char (point-max))))
753 (switch-to-buffer-other-window patch-diagnostics)
754 (error "Patch appears to have failed")))
756 ;; If black magic is involved, apply patch to a temp copy of the
757 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
758 ;; to temp copy, we name the result old-name_patched for local files
759 ;; and temp-copy_patched for remote files. The orig file name isn't
760 ;; changed, and the temp copy of the original is later deleted.
761 ;; Without magic, the original file is renamed (usually into
762 ;; old-name_orig) and the result of patching will have the same name as
763 ;; the original.
764 (if (not file-name-magic-p)
765 (ediff-with-current-buffer buf-to-patch
766 (set-visited-file-name
767 (concat source-filename ediff-backup-extension))
768 (set-buffer-modified-p nil))
770 ;; Black magic in effect.
771 ;; If orig file was remote, put the patched file in the temp directory.
772 ;; If orig file is local, put the patched file in the directory of
773 ;; the orig file.
774 (setq target-filename
775 (concat
776 (if (ediff-file-remote-p (file-truename source-filename))
777 true-source-filename
778 source-filename)
779 "_patched"))
781 (rename-file true-source-filename target-filename t)
783 ;; arrange that the temp copy of orig will be deleted
784 (rename-file (concat true-source-filename ediff-backup-extension)
785 true-source-filename t))
787 ;; make orig buffer read-only
788 (setq startup-hooks
789 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
791 ;; set up a buf for the patched file
792 (setq target-buf (find-file-noselect target-filename))
794 (setq ctl-buf
795 (ediff-buffers-internal
796 buf-to-patch target-buf nil
797 startup-hooks 'epatch))
798 (ediff-with-current-buffer ctl-buf
799 (setq ediff-patchbufer patch-buf
800 ediff-patch-diagnostics patch-diagnostics))
802 (bury-buffer patch-diagnostics)
803 (message "Type `P', if you need to see patch diagnostics")
804 ctl-buf))
806 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
807 (let (meta-buf)
808 (setq startup-hooks
809 ;; this sets various vars in the meta buffer inside
810 ;; ediff-prepare-meta-buffer
811 (cons `(lambda ()
812 ;; tell what to do if the user clicks on a session record
813 (setq ediff-session-action-function
814 'ediff-patch-file-form-meta
815 ediff-meta-patchbufer patch-buf) )
816 startup-hooks))
817 (setq meta-buf (ediff-prepare-meta-buffer
818 'ediff-filegroup-action
819 (ediff-with-current-buffer patch-buf
820 (cons (ediff-make-new-meta-list-header
821 nil ; regexp
822 (format "%S" patch-buf) ; obj A
823 nil nil ; objects B,C
824 nil ; merge-auto-store-dir
825 nil ; comparison-func
827 ediff-patch-map))
828 "*Ediff Session Group Panel"
829 'ediff-redraw-directory-group-buffer
830 'ediff-multifile-patch
831 startup-hooks))
832 (ediff-show-meta-buffer meta-buf)
838 ;; Local Variables:
839 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
840 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
841 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
842 ;; End:
844 ;; arch-tag: 2fe2161e-e116-469b-90fa-5cbb44c1bd1b
845 ;;; ediff-ptch.el ends here