Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / vc / ediff-ptch.el
blobd1332351a748911764ec11614f3a58e5fa368165
1 ;;; ediff-ptch.el --- Ediff's patch support
3 ;; Copyright (C) 1996-2014 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 (concat "\\(" ; context diff 2-liner
124 "^\\*\\*\\* +\\([^ \t]+\\)[^*]+[\t ]*\n--- +\\([^ \t]+\\)"
125 "\\|" ; unified format diff 2-liner
126 "^--- +\\([^ \t]+\\).*\n\\+\\+\\+ +\\([^ \t]+\\)"
127 "\\)")
128 "Regexp matching filename 2-liners at the start of each context diff.
129 You probably don't want to change that, unless you are using an obscure patch
130 program."
131 :type 'regexp
132 :group 'ediff-ptch)
134 ;; The buffer of the patch file. Local to control buffer.
135 (ediff-defvar-local ediff-patchbufer nil "")
137 ;; The buffer where patch displays its diagnostics.
138 (ediff-defvar-local ediff-patch-diagnostics nil "")
140 ;; Map of patch buffer. Has the form:
141 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
142 ;; where filenames are files to which patch would have applied the patch;
143 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
144 ;; it for the end.
145 (ediff-defvar-local ediff-patch-map nil "")
147 ;; strip prefix from filename
148 ;; returns /dev/null, if can't strip prefix
149 (defsubst ediff-file-name-sans-prefix (filename prefix)
150 (if prefix
151 (save-match-data
152 (if (string-match (concat "^" (if (stringp prefix)
153 (regexp-quote prefix)
154 ""))
155 filename)
156 (substring filename (match-end 0))
157 (concat "/null/" filename)))
158 filename)
163 ;; no longer used
164 ;; return the number of matches of regexp in buf starting from the beginning
165 (defun ediff-count-matches (regexp buf)
166 (ediff-with-current-buffer buf
167 (let ((count 0) opoint)
168 (save-excursion
169 (goto-char (point-min))
170 (while (and (not (eobp))
171 (progn (setq opoint (point))
172 (re-search-forward regexp nil t)))
173 (if (= opoint (point))
174 (forward-char 1)
175 (setq count (1+ count)))))
176 count)))
178 ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
179 ;; ((nil nil filename-spec1 marker1 marker2)
180 ;; (nil nil filename-spec2 marker1 marker2) ...)
181 ;; where filename-spec[12] are files to which the `patch' program would
182 ;; have applied the patch.
183 ;; nin, nil are placeholders. See ediff-make-new-meta-list-element in
184 ;; ediff-meta.el for the explanations.
185 ;; In the beginning we don't know exactly which files need to be patched.
186 ;; We usually come up with two candidates and ediff-file-name-sans-prefix
187 ;; resolves this later.
189 ;; The marker `mark1' delimits the beginning of the corresponding patch and
190 ;; `mark2' does it for the end.
191 ;; The result of ediff-map-patch-buffer is a list, which is then assigned
192 ;; to ediff-patch-map.
193 ;; The function returns the number of elements in the list ediff-patch-map
194 (defun ediff-map-patch-buffer (buf)
195 (ediff-with-current-buffer buf
196 (let ((count 0)
197 (mark1 (point-min-marker))
198 (mark1-end (point-min))
199 (possible-file-names '("/dev/null" . "/dev/null"))
200 mark2-end mark2 filenames
201 beg1 beg2 end1 end2
202 patch-map opoint)
203 (save-excursion
204 (goto-char (point-min))
205 (setq opoint (point))
206 (while (and (not (eobp))
207 (re-search-forward ediff-context-diff-label-regexp nil t))
208 (if (= opoint (point))
209 (forward-char 1) ; ensure progress towards the end
210 (setq mark2 (move-marker (make-marker) (match-beginning 0))
211 mark2-end (match-end 0)
212 beg1 (or (match-beginning 2) (match-beginning 4))
213 end1 (or (match-end 2) (match-end 4))
214 beg2 (or (match-beginning 3) (match-beginning 5))
215 end2 (or (match-end 3) (match-end 5)))
216 ;; possible-file-names is holding the new file names until we
217 ;; insert the old file name in the patch map
218 ;; It is a pair
219 ;; (filename-from-1st-header-line . filename-from-2nd-line)
220 (setq possible-file-names
221 (cons (if (and beg1 end1)
222 (buffer-substring beg1 end1)
223 "/dev/null")
224 (if (and beg2 end2)
225 (buffer-substring beg2 end2)
226 "/dev/null")))
227 ;; check for any `Index:' or `Prereq:' lines, but don't use them
228 (if (re-search-backward "^Index:" mark1-end 'noerror)
229 (move-marker mark2 (match-beginning 0)))
230 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
231 (move-marker mark2 (match-beginning 0)))
233 (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 ;; In case 2 files are possible patch targets, the user will be offered
272 ;; to choose file1 or file2. In a multifile patch, if the user chooses
273 ;; 1 or 2, this choice is preserved to decide future alternatives.
274 chosen-alternative
277 ;; chop off base-dirs
278 (mapc (lambda (session-info)
279 (let* ((proposed-file-names
280 ;; Filename-spec is objA; it is represented as
281 ;; (file1 . file2). Get it using ediff-get-session-objA.
282 (ediff-get-session-objA-name session-info))
283 ;; base-dir1 is the dir part of the 1st file in the patch
284 (base-dir1
285 (or (file-name-directory (car proposed-file-names))
286 ""))
287 ;; directory part of the 2nd file in the patch
288 (base-dir2
289 (or (file-name-directory (cdr proposed-file-names))
290 ""))
292 ;; If both base-dir1 and base-dir2 are relative and exist,
293 ;; assume that
294 ;; these dirs lead to the actual files starting at the present
295 ;; directory. So, we don't strip these relative dirs from the
296 ;; file names. This is a heuristic intended to improve guessing
297 (let ((default-directory (file-name-directory filename)))
298 (unless (or (file-name-absolute-p base-dir1)
299 (file-name-absolute-p base-dir2)
300 (not (file-exists-p base-dir1))
301 (not (file-exists-p base-dir2)))
302 (setq base-dir1 ""
303 base-dir2 "")))
304 (or (string= (car proposed-file-names) "/dev/null")
305 (setcar proposed-file-names
306 (ediff-file-name-sans-prefix
307 (car proposed-file-names) base-dir1)))
308 (or (string=
309 (cdr proposed-file-names) "/dev/null")
310 (setcdr proposed-file-names
311 (ediff-file-name-sans-prefix
312 (cdr proposed-file-names) base-dir2)))
314 ediff-patch-map)
316 ;; take the given file name into account
317 (or (file-directory-p filename)
318 (string= "/dev/null" filename)
319 (setcar (ediff-get-session-objA (car ediff-patch-map))
320 (cons (file-name-nondirectory filename)
321 (file-name-nondirectory filename))))
323 ;; prepend actual-dir
324 (mapc (lambda (session-info)
325 (let ((proposed-file-names
326 (ediff-get-session-objA-name session-info)))
327 (if (and (string-match "^/null/" (car proposed-file-names))
328 (string-match "^/null/" (cdr proposed-file-names)))
329 ;; couldn't intuit the file name to patch, so
330 ;; something is amiss
331 (progn
332 (with-output-to-temp-buffer ediff-msg-buffer
333 (ediff-with-current-buffer standard-output
334 (fundamental-mode))
335 (princ
336 (format "
337 The patch file contains a context diff for
340 However, Ediff cannot infer the name of the actual file
341 to be patched on your system. If you know the correct file name,
342 please enter it now.
344 If you don't know and still would like to apply patches to
345 other files, enter /dev/null
347 (substring (car proposed-file-names) 6)
348 (substring (cdr proposed-file-names) 6))))
349 (let ((directory t)
350 user-file)
351 (while directory
352 (setq user-file
353 (read-file-name
354 "Please enter file name: "
355 actual-dir actual-dir t))
356 (if (not (file-directory-p user-file))
357 (setq directory nil)
358 (setq directory t)
359 (beep)
360 (message "%s is a directory" user-file)
361 (sit-for 2)))
362 (setcar (ediff-get-session-objA session-info)
363 (cons user-file user-file))))
364 (setcar proposed-file-names
365 (expand-file-name
366 (concat actual-dir (car proposed-file-names))))
367 (setcdr proposed-file-names
368 (expand-file-name
369 (concat actual-dir (cdr proposed-file-names)))))
371 ediff-patch-map)
372 ;; Check for the existing files in each pair and discard the nonexistent
373 ;; ones. If both exist, ask the user.
374 (mapcar (lambda (session-info)
375 (let* ((file1 (car (ediff-get-session-objA-name session-info)))
376 (file2 (cdr (ediff-get-session-objA-name session-info)))
377 (session-file-object
378 (ediff-get-session-objA session-info))
379 (f1-exists (file-exists-p file1))
380 (f2-exists (file-exists-p file2)))
381 (cond
382 ((and
383 ;; The patch program prefers the shortest file as the patch
384 ;; target. However, this is a questionable heuristic. In an
385 ;; interactive program, like ediff, we can offer the user a
386 ;; choice.
387 ;; (< (length file2) (length file1))
388 (not f1-exists)
389 f2-exists)
390 ;; replace file-pair with the winning file2
391 (setcar session-file-object file2))
392 ((and
393 ;; (< (length file1) (length file2))
394 (not f2-exists)
395 f1-exists)
396 ;; replace file-pair with the winning file1
397 (setcar session-file-object file1))
398 ((and f1-exists f2-exists
399 (string= file1 file2))
400 (setcar session-file-object file1))
401 ((and f1-exists f2-exists (eq chosen-alternative 1))
402 (setcar session-file-object file1))
403 ((and f1-exists f2-exists (eq chosen-alternative 2))
404 (setcar session-file-object file2))
405 ((and f1-exists f2-exists)
406 (with-output-to-temp-buffer ediff-msg-buffer
407 (ediff-with-current-buffer standard-output
408 (fundamental-mode))
409 (princ (format "
410 Ediff has inferred that
413 are two possible targets for applying the patch.
414 Both files seem to be plausible alternatives.
416 Please advise:
417 Type `y' to use %s as the target;
418 Type `n' to use %s as the target.
420 file1 file2 file1 file2)))
421 (setcar session-file-object
422 (if (y-or-n-p (format "Use %s ? " file1))
423 (progn
424 (setq chosen-alternative 1)
425 file1)
426 (setq chosen-alternative 2)
427 file2))
429 (f2-exists (setcar session-file-object file2))
430 (f1-exists (setcar session-file-object file1))
432 (with-output-to-temp-buffer ediff-msg-buffer
433 (ediff-with-current-buffer standard-output
434 (fundamental-mode))
435 (princ "\nEdiff has inferred that")
436 (if (string= file1 file2)
437 (princ (format "
439 is assumed to be the target for this patch. However, this file does not exist."
440 file1))
441 (princ (format "
444 are two possible targets for this patch. However, these files do not exist."
445 file1 file2)))
446 (princ "
447 \nPlease enter an alternative patch target ...\n"))
448 (let ((directory t)
449 target)
450 (while directory
451 (setq target (read-file-name
452 "Please enter a patch target: "
453 actual-dir actual-dir t))
454 (if (not (file-directory-p target))
455 (setq directory nil)
456 (beep)
457 (message "%s is a directory" target)
458 (sit-for 2)))
459 (setcar session-file-object target))))))
460 ediff-patch-map)
463 (defun ediff-show-patch-diagnostics ()
464 (interactive)
465 (cond ((window-live-p ediff-window-A)
466 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
467 ((window-live-p ediff-window-B)
468 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
469 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
471 (defvar ediff-use-last-dir)
473 ;; prompt for file, get the buffer
474 (defun ediff-prompt-for-patch-file ()
475 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
476 (ediff-patch-default-directory) ; try patch default dir
477 (t default-directory)))
478 (coding-system-for-read ediff-coding-system-for-read)
479 patch-file-name)
480 (setq patch-file-name
481 (read-file-name
482 (format "Patch is in file%s: "
483 (cond ((and buffer-file-name
484 (equal (expand-file-name dir)
485 (file-name-directory buffer-file-name)))
486 (concat
487 " (default "
488 (file-name-nondirectory buffer-file-name)
489 ")"))
490 (t "")))
491 dir buffer-file-name 'must-match))
492 (if (file-directory-p patch-file-name)
493 (error "Patch file cannot be a directory: %s" patch-file-name)
494 (find-file-noselect patch-file-name))
498 ;; Try current buffer, then the other window's buffer. Else, give up.
499 (defun ediff-prompt-for-patch-buffer ()
500 (get-buffer
501 (read-buffer
502 "Buffer that holds the patch: "
503 (cond ((save-excursion
504 (goto-char (point-min))
505 (re-search-forward ediff-context-diff-label-regexp nil t))
506 (current-buffer))
507 ((save-window-excursion
508 (other-window 1)
509 (save-excursion
510 (goto-char (point-min))
511 (and (re-search-forward ediff-context-diff-label-regexp nil t)
512 (current-buffer)))))
513 ((save-window-excursion
514 (other-window -1)
515 (save-excursion
516 (goto-char (point-min))
517 (and (re-search-forward ediff-context-diff-label-regexp nil t)
518 (current-buffer)))))
519 (t (ediff-other-buffer (current-buffer))))
520 'must-match)))
523 (defun ediff-get-patch-buffer (&optional arg patch-buf)
524 "Obtain patch buffer. If patch is already in a buffer---use it.
525 Else, read patch file into a new buffer. If patch buffer is passed as an
526 optional argument, then use it."
527 (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
528 last-command-event) ; XEmacs: don't use dialog box
530 (cond ((ediff-buffer-live-p patch-buf))
531 ;; even prefix arg: patch in buffer
532 ((and (integerp arg) (eq 0 (mod arg 2)))
533 (setq patch-buf (ediff-prompt-for-patch-buffer)))
534 ;; odd prefix arg: get patch from a file
535 ((and (integerp arg) (eq 1 (mod arg 2)))
536 (setq patch-buf (ediff-prompt-for-patch-file)))
537 (t (setq patch-buf
538 (if (y-or-n-p "Is the patch already in a buffer? ")
539 (ediff-prompt-for-patch-buffer)
540 (ediff-prompt-for-patch-file)))))
542 (ediff-with-current-buffer patch-buf
543 (goto-char (point-min))
544 (or (ediff-get-visible-buffer-window patch-buf)
545 (progn
546 (pop-to-buffer patch-buf 'other-window)
547 (select-window (previous-window)))))
548 (ediff-map-patch-buffer patch-buf)
549 patch-buf))
551 ;; Dispatch the right patch file function: regular or meta-level,
552 ;; depending on how many patches are in the patch file.
553 ;; At present, there is no support for meta-level patches.
554 ;; Should return either the ctl buffer or the meta-buffer
555 (defun ediff-dispatch-file-patching-job (patch-buf filename
556 &optional startup-hooks)
557 (ediff-with-current-buffer patch-buf
558 ;; relativize names in the patch with respect to source-file
559 (ediff-fixup-patch-map filename)
560 (if (< (length ediff-patch-map) 2)
561 (ediff-patch-file-internal
562 patch-buf
563 (if (and ediff-patch-map
564 (not (string-match
565 "^/dev/null"
566 ;; this is the file to patch
567 (ediff-get-session-objA-name (car ediff-patch-map))))
568 (> (length
569 (ediff-get-session-objA-name (car ediff-patch-map)))
571 (ediff-get-session-objA-name (car ediff-patch-map))
572 filename)
573 startup-hooks)
574 (ediff-multi-patch-internal patch-buf startup-hooks))
578 ;; When patching a buffer, never change the orig file. Instead, create a new
579 ;; buffer, ***_patched, even if the buff visits a file.
580 ;; Users who want to actually patch the buffer should use
581 ;; ediff-patch-file, not ediff-patch-buffer.
582 (defun ediff-patch-buffer-internal (patch-buf
583 buf-to-patch-name
584 &optional startup-hooks)
585 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
586 (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
587 (buf-mod-status (buffer-modified-p buf-to-patch))
588 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
589 ediff-patch-map)) 1))
590 default-dir file-name ctl-buf)
591 (if multifile-patch-p
592 (error
593 "To apply multi-file patches, please use `ediff-patch-file'"))
595 ;; create a temp file to patch
596 (ediff-with-current-buffer buf-to-patch
597 (setq default-dir default-directory)
598 (setq file-name (ediff-make-temp-file buf-to-patch))
599 ;; temporarily switch visited file name, if any
600 (set-visited-file-name file-name)
601 ;; don't create auto-save file, if buff was visiting a file
602 (or visited-file
603 (setq buffer-auto-save-file-name nil))
604 ;; don't confuse the user with a new bufname
605 (rename-buffer buf-to-patch-name)
606 (set-buffer-modified-p nil)
607 (set-visited-file-modtime) ; sync buffer and temp file
608 (setq default-directory default-dir)
611 ;; dispatch a patch function
612 (setq ctl-buf (ediff-dispatch-file-patching-job
613 patch-buf file-name startup-hooks))
615 (ediff-with-current-buffer ctl-buf
616 (delete-file (buffer-file-name ediff-buffer-A))
617 (delete-file (buffer-file-name ediff-buffer-B))
618 (ediff-with-current-buffer ediff-buffer-A
619 (if default-dir (setq default-directory default-dir))
620 (set-visited-file-name visited-file) ; visited-file might be nil
621 (rename-buffer buf-to-patch-name)
622 (set-buffer-modified-p buf-mod-status))
623 (ediff-with-current-buffer ediff-buffer-B
624 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
625 (if default-dir (setq default-directory default-dir))
626 (set-visited-file-name nil)
627 (rename-buffer (ediff-unique-buffer-name
628 (concat buf-to-patch-name "_patched") ""))
629 (set-buffer-modified-p t)))
633 ;; Traditional patch has weird return codes.
634 ;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
635 ;; 0 is a good code in all cases.
636 ;; We'll do the conservative thing.
637 (defun ediff-patch-return-code-ok (code)
638 (eq code 0))
639 ;;; (if (eq (ediff-test-patch-utility) 'traditional)
640 ;;; (eq code 0)
641 ;;; (not (eq code 2))))
643 (autoload 'ediff-find-file "ediff")
644 (declare-function ediff-buffers-internal "ediff"
645 (buf-a buf-b buf-c startup-hooks job-name
646 &optional merge-buffer-file))
648 (defun ediff-patch-file-internal (patch-buf source-filename
649 &optional startup-hooks)
650 (setq source-filename (expand-file-name source-filename))
652 (let* ((shell-file-name ediff-shell)
653 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
654 ;; ediff-find-file may use a temp file to do the patch
655 ;; so, we save source-filename and true-source-filename as a var
656 ;; that initially is source-filename but may be changed to a temp
657 ;; file for the purpose of patching.
658 (true-source-filename source-filename)
659 (target-filename source-filename)
660 ;; this ensures that the patch process gets patch buffer in the
661 ;; encoding that Emacs thinks is right for that type of text
662 (coding-system-for-write
663 (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
664 target-buf buf-to-patch file-name-magic-p
665 patch-return-code ctl-buf backup-style aux-wind)
667 (if (string-match "V" ediff-patch-options)
668 (error
669 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
671 ;; Make a temp file, if source-filename has a magic file handler (or if
672 ;; it is handled via auto-mode-alist and similar magic).
673 ;; Check if there is a buffer visiting source-filename and if they are in
674 ;; sync; arrange for the deletion of temp file.
675 (ediff-find-file 'true-source-filename 'buf-to-patch
676 'ediff-last-dir-patch 'startup-hooks)
678 ;; Check if source file name has triggered black magic, such as file name
679 ;; handlers or auto mode alist, and make a note of it.
680 ;; true-source-filename should be either the original name or a
681 ;; temporary file where we put the after-product of the file handler.
682 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
683 (file-truename source-filename))))
685 ;; Checkout orig file, if necessary, so that the patched file
686 ;; could be checked back in.
687 (ediff-maybe-checkout buf-to-patch)
689 (ediff-with-current-buffer patch-diagnostics
690 (insert-buffer-substring patch-buf)
691 (message "Applying patch ... ")
692 ;; fix environment for gnu patch, so it won't make numbered extensions
693 (setq backup-style (getenv "VERSION_CONTROL"))
694 (setenv "VERSION_CONTROL" nil)
695 (setq patch-return-code
696 (call-process-region
697 (point-min) (point-max)
698 shell-file-name
699 t ; delete region (which contains the patch
700 t ; insert output (patch diagnostics) in current buffer
701 nil ; don't redisplay
702 shell-command-switch ; usually -c
703 (format "%s %s %s %s"
704 ediff-patch-program
705 ediff-patch-options
706 ediff-backup-specs
707 (expand-file-name true-source-filename))
710 ;; restore environment for gnu patch
711 (setenv "VERSION_CONTROL" backup-style))
713 (message "Applying patch ... done")
714 (message "")
716 (switch-to-buffer patch-diagnostics)
717 (sit-for 0) ; synchronize - let the user see diagnostics
719 (or (and (ediff-patch-return-code-ok patch-return-code)
720 (file-exists-p
721 (concat true-source-filename ediff-backup-extension)))
722 (progn
723 (with-output-to-temp-buffer ediff-msg-buffer
724 (ediff-with-current-buffer standard-output
725 (fundamental-mode))
726 (princ (format
727 "Patch program has failed due to a bad patch file,
728 it couldn't apply all hunks, OR
729 it couldn't create the backup for the file being patched.
731 The former could be caused by a corrupt patch file or because the %S
732 program doesn't understand the format of the patch file in use.
734 The second problem might be due to an incompatibility among these settings:
735 ediff-patch-program = %S ediff-patch-options = %S
736 ediff-backup-extension = %S ediff-backup-specs = %S
738 See Ediff on-line manual for more details on these variables.
739 In particular, check the documentation for `ediff-backup-specs'.
741 In any of the above cases, Ediff doesn't compare files automatically.
742 However, if the patch was applied partially and the backup file was created,
743 you can still examine the changes via M-x ediff-files"
744 ediff-patch-program
745 ediff-patch-program
746 ediff-patch-options
747 ediff-backup-extension
748 ediff-backup-specs
750 (beep 1)
751 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
752 (progn
753 (select-window aux-wind)
754 (goto-char (point-max))))
755 (switch-to-buffer-other-window patch-diagnostics)
756 (error "Patch appears to have failed")))
758 ;; If black magic is involved, apply patch to a temp copy of the
759 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
760 ;; to temp copy, we name the result old-name_patched for local files
761 ;; and temp-copy_patched for remote files. The orig file name isn't
762 ;; changed, and the temp copy of the original is later deleted.
763 ;; Without magic, the original file is renamed (usually into
764 ;; old-name_orig) and the result of patching will have the same name as
765 ;; the original.
766 (if (not file-name-magic-p)
767 (ediff-with-current-buffer buf-to-patch
768 (set-visited-file-name
769 (concat source-filename ediff-backup-extension))
770 (set-buffer-modified-p nil))
772 ;; Black magic in effect.
773 ;; If orig file was remote, put the patched file in the temp directory.
774 ;; If orig file is local, put the patched file in the directory of
775 ;; the orig file.
776 (setq target-filename
777 (concat
778 (if (ediff-file-remote-p (file-truename source-filename))
779 true-source-filename
780 source-filename)
781 "_patched"))
783 (rename-file true-source-filename target-filename t)
785 ;; arrange that the temp copy of orig will be deleted
786 (rename-file (concat true-source-filename ediff-backup-extension)
787 true-source-filename t))
789 ;; make orig buffer read-only
790 (setq startup-hooks
791 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
793 ;; set up a buf for the patched file
794 (setq target-buf (find-file-noselect target-filename))
796 (setq ctl-buf
797 (ediff-buffers-internal
798 buf-to-patch target-buf nil
799 startup-hooks 'epatch))
800 (ediff-with-current-buffer ctl-buf
801 (setq ediff-patchbufer patch-buf
802 ediff-patch-diagnostics patch-diagnostics))
804 (bury-buffer patch-diagnostics)
805 (message "Type `P', if you need to see patch diagnostics")
806 ctl-buf))
808 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
809 (let (meta-buf)
810 (setq startup-hooks
811 ;; this sets various vars in the meta buffer inside
812 ;; ediff-prepare-meta-buffer
813 (cons `(lambda ()
814 ;; tell what to do if the user clicks on a session record
815 (setq ediff-session-action-function
816 'ediff-patch-file-form-meta
817 ediff-meta-patchbufer patch-buf) )
818 startup-hooks))
819 (setq meta-buf (ediff-prepare-meta-buffer
820 'ediff-filegroup-action
821 (ediff-with-current-buffer patch-buf
822 (cons (ediff-make-new-meta-list-header
823 nil ; regexp
824 (format "%S" patch-buf) ; obj A
825 nil nil ; objects B,C
826 nil ; merge-auto-store-dir
827 nil ; comparison-func
829 ediff-patch-map))
830 "*Ediff Session Group Panel"
831 'ediff-redraw-directory-group-buffer
832 'ediff-multifile-patch
833 startup-hooks))
834 (ediff-show-meta-buffer meta-buf)
840 ;; Local Variables:
841 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
842 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
843 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
844 ;; End:
846 ;;; ediff-ptch.el ends here