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