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