ediff-context-diff-label-regexp: Detect the end of second file
[emacs.git] / lisp / vc / ediff-ptch.el
blob6e8e9473fcc6d14148a22b757042e1abc30a205a
1 ;;; ediff-ptch.el --- Ediff's patch support
3 ;; Copyright (C) 1996-2016 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 ;; 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 (with-output-to-temp-buffer ediff-msg-buffer
434 (ediff-with-current-buffer standard-output
435 (fundamental-mode))
436 (princ "\nEdiff has inferred that")
437 (if (string= file1 file2)
438 (princ (format "
440 is assumed to be the target for this patch. However, this file does not exist."
441 file1))
442 (princ (format "
445 are two possible targets for this patch. However, these files do not exist."
446 file1 file2)))
447 (princ "
448 \nPlease enter an alternative patch target ...\n"))
449 (let ((directory t)
450 target)
451 (while directory
452 (setq target (read-file-name
453 "Please enter a patch target: "
454 actual-dir actual-dir t))
455 (if (not (file-directory-p target))
456 (setq directory nil)
457 (beep)
458 (message "%s is a directory" target)
459 (sit-for 2)))
460 (setcar session-file-object target))))))
461 ediff-patch-map)
464 (defun ediff-show-patch-diagnostics ()
465 (interactive)
466 (cond ((window-live-p ediff-window-A)
467 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
468 ((window-live-p ediff-window-B)
469 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
470 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
472 (defvar ediff-use-last-dir)
474 ;; prompt for file, get the buffer
475 (defun ediff-prompt-for-patch-file ()
476 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
477 (ediff-patch-default-directory) ; try patch default dir
478 (t default-directory)))
479 (coding-system-for-read ediff-coding-system-for-read)
480 patch-file-name)
481 (setq patch-file-name
482 (read-file-name
483 (format "Patch is in file%s: "
484 (cond ((and buffer-file-name
485 (equal (expand-file-name dir)
486 (file-name-directory buffer-file-name)))
487 (concat
488 " (default "
489 (file-name-nondirectory buffer-file-name)
490 ")"))
491 (t "")))
492 dir buffer-file-name 'must-match))
493 (if (file-directory-p patch-file-name)
494 (error "Patch file cannot be a directory: %s" patch-file-name)
495 (find-file-noselect patch-file-name))
499 ;; Try current buffer, then the other window's buffer. Else, give up.
500 (defun ediff-prompt-for-patch-buffer ()
501 (get-buffer
502 (read-buffer
503 "Buffer that holds the patch: "
504 (cond ((save-excursion
505 (goto-char (point-min))
506 (re-search-forward ediff-context-diff-label-regexp nil t))
507 (current-buffer))
508 ((save-window-excursion
509 (other-window 1)
510 (save-excursion
511 (goto-char (point-min))
512 (and (re-search-forward ediff-context-diff-label-regexp nil t)
513 (current-buffer)))))
514 ((save-window-excursion
515 (other-window -1)
516 (save-excursion
517 (goto-char (point-min))
518 (and (re-search-forward ediff-context-diff-label-regexp nil t)
519 (current-buffer)))))
520 (t (ediff-other-buffer (current-buffer))))
521 'must-match)))
524 (defun ediff-get-patch-buffer (&optional arg patch-buf)
525 "Obtain patch buffer. If patch is already in a buffer---use it.
526 Else, read patch file into a new buffer. If patch buffer is passed as an
527 optional argument, then use it."
528 (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
529 last-command-event) ; XEmacs: don't use dialog box
531 (cond ((ediff-buffer-live-p patch-buf))
532 ;; even prefix arg: patch in buffer
533 ((and (integerp arg) (eq 0 (mod arg 2)))
534 (setq patch-buf (ediff-prompt-for-patch-buffer)))
535 ;; odd prefix arg: get patch from a file
536 ((and (integerp arg) (eq 1 (mod arg 2)))
537 (setq patch-buf (ediff-prompt-for-patch-file)))
538 (t (setq patch-buf
539 (if (y-or-n-p "Is the patch already in a buffer? ")
540 (ediff-prompt-for-patch-buffer)
541 (ediff-prompt-for-patch-file)))))
543 (ediff-with-current-buffer patch-buf
544 (goto-char (point-min))
545 (or (ediff-get-visible-buffer-window patch-buf)
546 (progn
547 (pop-to-buffer patch-buf 'other-window)
548 (select-window (previous-window)))))
549 (ediff-map-patch-buffer patch-buf)
550 patch-buf))
552 ;; Dispatch the right patch file function: regular or meta-level,
553 ;; depending on how many patches are in the patch file.
554 ;; At present, there is no support for meta-level patches.
555 ;; Should return either the ctl buffer or the meta-buffer
556 (defun ediff-dispatch-file-patching-job (patch-buf filename
557 &optional startup-hooks)
558 (ediff-with-current-buffer patch-buf
559 ;; relativize names in the patch with respect to source-file
560 (ediff-fixup-patch-map filename)
561 (if (< (length ediff-patch-map) 2)
562 (ediff-patch-file-internal
563 patch-buf
564 (if (and ediff-patch-map
565 (not (string-match
566 "^/dev/null"
567 ;; this is the file to patch
568 (ediff-get-session-objA-name (car ediff-patch-map))))
569 (> (length
570 (ediff-get-session-objA-name (car ediff-patch-map)))
572 (ediff-get-session-objA-name (car ediff-patch-map))
573 filename)
574 startup-hooks)
575 (ediff-multi-patch-internal patch-buf startup-hooks))
579 ;; When patching a buffer, never change the orig file. Instead, create a new
580 ;; buffer, ***_patched, even if the buff visits a file.
581 ;; Users who want to actually patch the buffer should use
582 ;; ediff-patch-file, not ediff-patch-buffer.
583 (defun ediff-patch-buffer-internal (patch-buf
584 buf-to-patch-name
585 &optional startup-hooks)
586 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
587 (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
588 (buf-mod-status (buffer-modified-p buf-to-patch))
589 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
590 ediff-patch-map)) 1))
591 default-dir file-name ctl-buf)
592 (if multifile-patch-p
593 (error
594 "To apply multi-file patches, please use `ediff-patch-file'"))
596 ;; create a temp file to patch
597 (ediff-with-current-buffer buf-to-patch
598 (setq default-dir default-directory)
599 (setq file-name (ediff-make-temp-file buf-to-patch))
600 ;; temporarily switch visited file name, if any
601 (set-visited-file-name file-name)
602 ;; don't create auto-save file, if buff was visiting a file
603 (or visited-file
604 (setq buffer-auto-save-file-name nil))
605 ;; don't confuse the user with a new bufname
606 (rename-buffer buf-to-patch-name)
607 (set-buffer-modified-p nil)
608 (set-visited-file-modtime) ; sync buffer and temp file
609 (setq default-directory default-dir)
612 ;; dispatch a patch function
613 (setq ctl-buf (ediff-dispatch-file-patching-job
614 patch-buf file-name startup-hooks))
616 (ediff-with-current-buffer ctl-buf
617 (delete-file (buffer-file-name ediff-buffer-A))
618 (delete-file (buffer-file-name ediff-buffer-B))
619 (ediff-with-current-buffer ediff-buffer-A
620 (if default-dir (setq default-directory default-dir))
621 (set-visited-file-name visited-file) ; visited-file might be nil
622 (rename-buffer buf-to-patch-name)
623 (set-buffer-modified-p buf-mod-status))
624 (ediff-with-current-buffer ediff-buffer-B
625 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
626 (if default-dir (setq default-directory default-dir))
627 (set-visited-file-name nil)
628 (rename-buffer (ediff-unique-buffer-name
629 (concat buf-to-patch-name "_patched") ""))
630 (set-buffer-modified-p t)))
634 ;; Traditional patch has weird return codes.
635 ;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
636 ;; 0 is a good code in all cases.
637 ;; We'll do the conservative thing.
638 (defun ediff-patch-return-code-ok (code)
639 (eq code 0))
640 ;;; (if (eq (ediff-test-patch-utility) 'traditional)
641 ;;; (eq code 0)
642 ;;; (not (eq code 2))))
644 (autoload 'ediff-find-file "ediff")
645 (declare-function ediff-buffers-internal "ediff"
646 (buf-a buf-b buf-c startup-hooks job-name
647 &optional merge-buffer-file))
649 (defun ediff-patch-file-internal (patch-buf source-filename
650 &optional startup-hooks)
651 (setq source-filename (expand-file-name source-filename))
653 (let* ((shell-file-name ediff-shell)
654 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
655 ;; ediff-find-file may use a temp file to do the patch
656 ;; so, we save source-filename and true-source-filename as a var
657 ;; that initially is source-filename but may be changed to a temp
658 ;; file for the purpose of patching.
659 (true-source-filename source-filename)
660 (target-filename source-filename)
661 ;; this ensures that the patch process gets patch buffer in the
662 ;; encoding that Emacs thinks is right for that type of text
663 (coding-system-for-write
664 (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
665 target-buf buf-to-patch file-name-magic-p
666 patch-return-code ctl-buf backup-style aux-wind)
668 (if (string-match "V" ediff-patch-options)
669 (error
670 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
672 ;; Make a temp file, if source-filename has a magic file handler (or if
673 ;; it is handled via auto-mode-alist and similar magic).
674 ;; Check if there is a buffer visiting source-filename and if they are in
675 ;; sync; arrange for the deletion of temp file.
676 (ediff-find-file 'true-source-filename 'buf-to-patch
677 'ediff-last-dir-patch 'startup-hooks)
679 ;; Check if source file name has triggered black magic, such as file name
680 ;; handlers or auto mode alist, and make a note of it.
681 ;; true-source-filename should be either the original name or a
682 ;; temporary file where we put the after-product of the file handler.
683 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
684 (file-truename source-filename))))
686 ;; Checkout orig file, if necessary, so that the patched file
687 ;; could be checked back in.
688 (ediff-maybe-checkout buf-to-patch)
690 (ediff-with-current-buffer patch-diagnostics
691 (insert-buffer-substring patch-buf)
692 (message "Applying patch ... ")
693 ;; fix environment for gnu patch, so it won't make numbered extensions
694 (setq backup-style (getenv "VERSION_CONTROL"))
695 (setenv "VERSION_CONTROL" nil)
696 (setq patch-return-code
697 (call-process-region
698 (point-min) (point-max)
699 shell-file-name
700 t ; delete region (which contains the patch
701 t ; insert output (patch diagnostics) in current buffer
702 nil ; don't redisplay
703 shell-command-switch ; usually -c
704 (format "%s %s %s %s"
705 ediff-patch-program
706 ediff-patch-options
707 ediff-backup-specs
708 (expand-file-name true-source-filename))
711 ;; restore environment for gnu patch
712 (setenv "VERSION_CONTROL" backup-style))
714 (message "Applying patch ... done")
715 (message "")
717 (switch-to-buffer patch-diagnostics)
718 (sit-for 0) ; synchronize - let the user see diagnostics
720 (or (and (ediff-patch-return-code-ok patch-return-code)
721 (file-exists-p
722 (concat true-source-filename ediff-backup-extension)))
723 (progn
724 (with-output-to-temp-buffer ediff-msg-buffer
725 (ediff-with-current-buffer standard-output
726 (fundamental-mode))
727 (princ (format-message
728 "Patch program has failed due to a bad patch file,
729 it couldn't apply all hunks, OR
730 it couldn't create the backup for the file being patched.
732 The former could be caused by a corrupt patch file or because the %S
733 program doesn't understand the format of the patch file in use.
735 The second problem might be due to an incompatibility among these settings:
736 ediff-patch-program = %S ediff-patch-options = %S
737 ediff-backup-extension = %S ediff-backup-specs = %S
739 See Ediff manual for more details on these variables.
740 In particular, check the documentation for `ediff-backup-specs'.
742 In any of the above cases, Ediff doesn't compare files automatically.
743 However, if the patch was applied partially and the backup file was created,
744 you can still examine the changes via M-x ediff-files"
745 ediff-patch-program
746 ediff-patch-program
747 ediff-patch-options
748 ediff-backup-extension
749 ediff-backup-specs
751 (beep 1)
752 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
753 (progn
754 (select-window aux-wind)
755 (goto-char (point-max))))
756 (switch-to-buffer-other-window patch-diagnostics)
757 (error "Patch appears to have failed")))
759 ;; If black magic is involved, apply patch to a temp copy of the
760 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
761 ;; to temp copy, we name the result old-name_patched for local files
762 ;; and temp-copy_patched for remote files. The orig file name isn't
763 ;; changed, and the temp copy of the original is later deleted.
764 ;; Without magic, the original file is renamed (usually into
765 ;; old-name_orig) and the result of patching will have the same name as
766 ;; the original.
767 (if (not file-name-magic-p)
768 (ediff-with-current-buffer buf-to-patch
769 (set-visited-file-name
770 (concat source-filename ediff-backup-extension))
771 (set-buffer-modified-p nil))
773 ;; Black magic in effect.
774 ;; If orig file was remote, put the patched file in the temp directory.
775 ;; If orig file is local, put the patched file in the directory of
776 ;; the orig file.
777 (setq target-filename
778 (concat
779 (if (ediff-file-remote-p (file-truename source-filename))
780 true-source-filename
781 source-filename)
782 "_patched"))
784 (rename-file true-source-filename target-filename t)
786 ;; arrange that the temp copy of orig will be deleted
787 (rename-file (concat true-source-filename ediff-backup-extension)
788 true-source-filename t))
790 ;; make orig buffer read-only
791 (setq startup-hooks
792 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
794 ;; set up a buf for the patched file
795 (setq target-buf (find-file-noselect target-filename))
797 (setq ctl-buf
798 (ediff-buffers-internal
799 buf-to-patch target-buf nil
800 startup-hooks 'epatch))
801 (ediff-with-current-buffer ctl-buf
802 (setq ediff-patchbufer patch-buf
803 ediff-patch-diagnostics patch-diagnostics))
805 (bury-buffer patch-diagnostics)
806 (message "Type `P', if you need to see patch diagnostics")
807 ctl-buf))
809 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
810 (let (meta-buf)
811 (setq startup-hooks
812 ;; this sets various vars in the meta buffer inside
813 ;; ediff-prepare-meta-buffer
814 (cons `(lambda ()
815 ;; tell what to do if the user clicks on a session record
816 (setq ediff-session-action-function
817 'ediff-patch-file-form-meta
818 ediff-meta-patchbufer patch-buf) )
819 startup-hooks))
820 (setq meta-buf (ediff-prepare-meta-buffer
821 'ediff-filegroup-action
822 (ediff-with-current-buffer patch-buf
823 (cons (ediff-make-new-meta-list-header
824 nil ; regexp
825 (format "%S" patch-buf) ; obj A
826 nil nil ; objects B,C
827 nil ; merge-auto-store-dir
828 nil ; comparison-func
830 ediff-patch-map))
831 "*Ediff Session Group Panel"
832 'ediff-redraw-directory-group-buffer
833 'ediff-multifile-patch
834 startup-hooks))
835 (ediff-show-meta-buffer meta-buf)
841 ;; Local Variables:
842 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
843 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
844 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
845 ;; End:
847 ;;; ediff-ptch.el ends here