1 ;;; ediff-ptch.el --- Ediff's patch support
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
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 2, or (at your option)
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; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
30 (defgroup ediff-ptch nil
31 "Ediff patch support."
37 (defvar ediff-window-A
)
38 (defvar ediff-window-B
)
39 (defvar ediff-window-C
)
40 (defvar ediff-use-last-dir
)
44 (let ((load-path (cons (expand-file-name ".") load-path
)))
45 (or (featurep 'ediff-init
)
46 (load "ediff-init.el" nil nil
'nosuffix
))
47 (or (featurep 'ediff-mult
)
48 (load "ediff-mult.el" nil nil
'nosuffix
))
50 (load "ediff.el" nil nil
'nosuffix
))
56 (defcustom ediff-patch-program
"patch"
57 "*Name of the program that applies patches.
58 It is recommended to use GNU-compatible versions."
61 (defcustom ediff-patch-options
"-f"
62 "*Options to pass to ediff-patch-program.
64 Note: the `-b' option should be specified in `ediff-backup-specs'.
66 It is recommended to pass the `-f' option to the patch program, so it won't ask
67 questions. However, some implementations don't accept this option, in which
68 case the default value for this variable should be changed."
72 (defvar ediff-last-dir-patch nil
73 "Last directory used by an Ediff command for file to patch.")
75 ;; the default backup extension
76 (defconst ediff-default-backup-extension
77 (if (memq system-type
'(vax-vms axp-vms emx ms-dos
))
81 (defcustom ediff-backup-extension ediff-default-backup-extension
82 "Backup extension used by the patch program.
83 See also `ediff-backup-specs'."
87 (defun ediff-test-patch-utility ()
89 (cond ((eq 0 (call-process ediff-patch-program nil nil nil
"-z." "-b"))
90 ;; GNU `patch' v. >= 2.2
92 ((eq 0 (call-process ediff-patch-program nil nil nil
"-b"))
97 (defcustom ediff-backup-specs
98 (let ((type (ediff-test-patch-utility)))
100 ;; GNU `patch' v. >= 2.2
101 (format "-z%s -b" ediff-backup-extension
))
103 ;; POSIX `patch' -- ediff-backup-extension must be ".orig"
104 (setq ediff-backup-extension ediff-default-backup-extension
)
107 ;; traditional `patch'
108 (format "-b %s" ediff-backup-extension
))))
109 "*Backup directives to pass to the patch program.
110 Ediff requires that the old version of the file \(before applying the patch\)
111 be saved in a file named `the-patch-file.extension'. Usually `extension' is
112 `.orig', but this can be changed by the user and may depend on the system.
113 Therefore, Ediff needs to know the backup extension used by the patch program.
115 Some versions of the patch program let you specify `-b backup-extension'.
116 Other versions only permit `-b', which assumes the extension `.orig'
117 \(in which case ediff-backup-extension MUST be also `.orig'\). The latest
118 versions of GNU patch require `-b -z backup-extension'.
120 Note that both `ediff-backup-extension' and `ediff-backup-specs'
121 must be set properly. If your patch program takes the option `-b',
122 but not `-b extension', the variable `ediff-backup-extension' must
123 still be set so Ediff will know which extension to use.
125 Ediff tries to guess the appropriate value for this variables. It is believed
126 to be working for `traditional' patch, all versions of GNU patch, and for POSIX
127 patch. So, don't change these variables, unless the default doesn't work."
132 (defcustom ediff-patch-default-directory nil
133 "*Default directory to look for patches."
134 :type
'(choice (const nil
) string
)
137 (defcustom ediff-context-diff-label-regexp
138 (concat "\\(" ; context diff 2-liner
139 "^\\*\\*\\* \\([^ \t]+\\)[^*]+[\t ]*\n--- \\([^ \t]+\\)"
140 "\\|" ; GNU unified format diff 2-liner
141 "^--- \\([^ \t]+\\)[\t ]+.*\n\\+\\+\\+ \\([^ \t]+\\)"
143 "*Regexp matching filename 2-liners at the start of each context diff.
144 You probably don't want to change that, unless you are using an obscure patch
149 ;; The buffer of the patch file. Local to control buffer.
150 (ediff-defvar-local ediff-patchbufer nil
"")
152 ;; The buffer where patch displays its diagnostics.
153 (ediff-defvar-local ediff-patch-diagnostics nil
"")
155 ;; Map of patch buffer. Has the form:
156 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
157 ;; where filenames are files to which patch would have applied the patch;
158 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
160 (ediff-defvar-local ediff-patch-map nil
"")
162 ;; strip prefix from filename
163 ;; returns /dev/null, if can't strip prefix
164 (defsubst ediff-file-name-sans-prefix
(filename prefix
)
167 (if (string-match (concat "^" (if (stringp prefix
)
168 (regexp-quote prefix
)
171 (substring filename
(match-end 0))
172 (concat "/null/" filename
)))
179 ;; return the number of matches of regexp in buf starting from the beginning
180 (defun ediff-count-matches (regexp buf
)
181 (ediff-with-current-buffer buf
182 (let ((count 0) opoint
)
184 (goto-char (point-min))
185 (while (and (not (eobp))
186 (progn (setq opoint
(point))
187 (re-search-forward regexp nil t
)))
188 (if (= opoint
(point))
190 (setq count
(1+ count
)))))
193 ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
194 ;; ((nil nil filename-spec1 marker1 marker2)
195 ;; (nil nil filename-spec2 marker1 marker2) ...)
196 ;; where filename-spec[12] are files to which the `patch' program would
197 ;; have applied the patch.
198 ;; nin, nil are placeholders. See ediff-make-new-meta-list-element in
199 ;; ediff-meta.el for the explanations.
200 ;; In the beginning we don't know exactly which files need to be patched.
201 ;; We usually come up with two candidates and ediff-file-name-sans-prefix
202 ;; resolves this later.
204 ;; The marker `marker1' delimits the beginning of the corresponding patch and
205 ;; `marker2' does it for the end.
206 ;; The result of ediff-map-patch-buffer is a list, which is then assigned
207 ;; to ediff-patch-map.
208 ;; The function returns the number of elements in the list ediff-patch-map
209 (defun ediff-map-patch-buffer (buf)
210 (ediff-with-current-buffer buf
212 (mark1 (move-marker (make-marker) (point-min)))
213 (mark1-end (point-min))
214 (possible-file-names '("/dev/null" .
"/dev/null"))
215 mark2-end mark2 filenames
219 (goto-char (point-min))
220 (setq opoint
(point))
221 (while (and (not (eobp))
222 (re-search-forward ediff-context-diff-label-regexp nil t
))
223 (if (= opoint
(point))
224 (forward-char 1) ; ensure progress towards the end
225 (setq mark2
(move-marker (make-marker) (match-beginning 0))
226 mark2-end
(match-end 0)
227 beg1
(or (match-beginning 2) (match-beginning 4))
228 end1
(or (match-end 2) (match-end 4))
229 beg2
(or (match-beginning 3) (match-beginning 5))
230 end2
(or (match-end 3) (match-end 5)))
231 ;; possible-file-names is holding the new file names until we
232 ;; insert the old file name in the patch map
234 ;; (filename-from-1st-header-line . fn from 2nd line)
235 (setq possible-file-names
236 (cons (if (and beg1 end1
)
237 (buffer-substring beg1 end1
)
240 (buffer-substring beg2 end2
)
242 ;; check for any `Index:' or `Prereq:' lines, but don't use them
243 (if (re-search-backward "^Index:" mark1-end
'noerror
)
244 (move-marker mark2
(match-beginning 0)))
245 (if (re-search-backward "^Prereq:" mark1-end
'noerror
)
246 (move-marker mark2
(match-beginning 0)))
248 (goto-char mark2-end
)
252 (cons (ediff-make-new-meta-list-element
253 filenames mark1 mark2
)
257 filenames possible-file-names
))
260 (setq mark2
(point-max-marker)
261 patch-map
(cons (ediff-make-new-meta-list-element
262 possible-file-names mark1 mark2
)
264 (setq ediff-patch-map
(nreverse patch-map
))
267 ;; Fix up the file names in the list using the argument FILENAME
268 ;; Algorithm: find the files' directories in the patch and, if a directory is
269 ;; absolute, cut it out from the corresponding file name in the patch.
270 ;; Relative directories are not cut out.
271 ;; Prepend the directory of FILENAME to each resulting file (which came
272 ;; originally from the patch).
273 ;; In addition, the first file in the patch document is replaced by FILENAME.
274 ;; Each file is actually a pair of files found in the context diff header
275 ;; In the end, for each pair, we ask the user which file to patch.
276 ;; Note: Ediff doesn't recognize multi-file patches that are separated
277 ;; with the `Index:' line. It treats them as a single-file patch.
279 ;; Executes inside the patch buffer
280 (defun ediff-fixup-patch-map (filename)
281 (setq filename
(expand-file-name filename
))
282 (let ((actual-dir (if (file-directory-p filename
)
283 ;; directory part of filename
284 (file-name-as-directory filename
)
285 (file-name-directory filename
)))
286 ;; In case 2 files are possible patch targets, the user will be offered
287 ;; to choose file1 or file2. In a multifile patch, if the user chooses
288 ;; 1 or 2, this choice is preserved to decide future alternatives.
292 ;; chop off base-dirs
293 (mapcar (lambda (session-info)
294 (let* ((proposed-file-names
295 ;; Filename-spec is objA; it is represented as
296 ;; (file1 . file2). Get it using ediff-get-session-objA.
297 (ediff-get-session-objA-name session-info
))
298 ;; base-dir1 is the dir part of the 1st file in the patch
300 (or (file-name-directory (car proposed-file-names
))
302 ;; directory part of the 2nd file in the patch
304 (or (file-name-directory (cdr proposed-file-names
))
307 ;; If both base-dir1 and base-dir2 are relative and exist,
309 ;; these dirs lead to the actual files starting at the present
310 ;; directory. So, we don't strip these relative dirs from the
311 ;; file names. This is a heuristic intended to improve guessing
312 (unless (or (file-name-absolute-p base-dir1
)
313 (file-name-absolute-p base-dir2
)
314 (not (file-exists-p base-dir1
))
315 (not (file-exists-p base-dir2
)))
318 (or (string= (car proposed-file-names
) "/dev/null")
319 (setcar proposed-file-names
320 (ediff-file-name-sans-prefix
321 (car proposed-file-names
) base-dir1
)))
323 (cdr proposed-file-names
) "/dev/null")
324 (setcdr proposed-file-names
325 (ediff-file-name-sans-prefix
326 (cdr proposed-file-names
) base-dir2
)))
330 ;; take the given file name into account
331 (or (file-directory-p filename
)
332 (string= "/dev/null" filename
)
333 (setcar (ediff-get-session-objA (car ediff-patch-map
))
334 (cons (file-name-nondirectory filename
)
335 (file-name-nondirectory filename
))))
337 ;; prepend actual-dir
338 (mapcar (lambda (session-info)
339 (let ((proposed-file-names
340 (ediff-get-session-objA-name session-info
)))
341 (if (and (string-match "^/null/" (car proposed-file-names
))
342 (string-match "^/null/" (cdr proposed-file-names
)))
343 ;; couldn't intuit the file name to patch, so
344 ;; something is amiss
346 (with-output-to-temp-buffer ediff-msg-buffer
347 (ediff-with-current-buffer standard-output
351 The patch file contains a context diff for
354 However, Ediff cannot infer the name of the actual file
355 to be patched on your system. If you know the correct file name,
358 If you don't know and still would like to apply patches to
359 other files, enter /dev/null
361 (substring (car proposed-file-names
) 6)
362 (substring (cdr proposed-file-names
) 6))))
368 "Please enter file name: "
369 actual-dir actual-dir t
))
370 (if (not (file-directory-p user-file
))
374 (message "%s is a directory" user-file
)
376 (setcar (ediff-get-session-objA session-info
)
377 (cons user-file user-file
))))
378 (setcar proposed-file-names
380 (concat actual-dir
(car proposed-file-names
))))
381 (setcdr proposed-file-names
383 (concat actual-dir
(cdr proposed-file-names
)))))
386 ;; Check for the existing files in each pair and discard the nonexisting
387 ;; ones. If both exist, ask the user.
388 (mapcar (lambda (session-info)
389 (let* ((file1 (car (ediff-get-session-objA-name session-info
)))
390 (file2 (cdr (ediff-get-session-objA-name session-info
)))
392 (ediff-get-session-objA session-info
))
393 (f1-exists (file-exists-p file1
))
394 (f2-exists (file-exists-p file2
)))
397 ;; The patch program prefers the shortest file as the patch
398 ;; target. However, this is a questionable heuristic. In an
399 ;; interactive program, like ediff, we can offer the user a
401 ;; (< (length file2) (length file1))
404 ;; replace file-pair with the winning file2
405 (setcar session-file-object file2
))
407 ;; (< (length file1) (length file2))
410 ;; replace file-pair with the winning file1
411 (setcar session-file-object file1
))
412 ((and f1-exists f2-exists
413 (string= file1 file2
))
414 (setcar session-file-object file1
))
415 ((and f1-exists f2-exists
(eq chosen-alternative
1))
416 (setcar session-file-object file1
))
417 ((and f1-exists f2-exists
(eq chosen-alternative
2))
418 (setcar session-file-object file2
))
419 ((and f1-exists f2-exists
)
420 (with-output-to-temp-buffer ediff-msg-buffer
421 (ediff-with-current-buffer standard-output
424 Ediff has inferred that
427 are two possible targets for applying the patch.
428 Both files seem to be plausible alternatives.
431 Type `y' to use %s as the target;
432 Type `n' to use %s as the target.
434 file1 file2 file1 file2
)))
435 (setcar session-file-object
436 (if (y-or-n-p (format "Use %s ? " file1
))
438 (setq chosen-alternative
1)
440 (setq chosen-alternative
2)
443 (f2-exists (setcar session-file-object file2
))
444 (f1-exists (setcar session-file-object file1
))
446 (with-output-to-temp-buffer ediff-msg-buffer
447 (ediff-with-current-buffer standard-output
449 (princ "\nEdiff has inferred that")
450 (if (string= file1 file2
)
453 is assumed to be the target for this patch. However, this file does not exist."
458 are two possible targets for this patch. However, these files do not exist."
461 \nPlease enter an alternative patch target ...\n"))
465 (setq target
(read-file-name
466 "Please enter a patch target: "
467 actual-dir actual-dir t
))
468 (if (not (file-directory-p target
))
471 (message "%s is a directory" target
)
473 (setcar session-file-object target
))))))
477 (defun ediff-show-patch-diagnostics ()
479 (cond ((window-live-p ediff-window-A
)
480 (set-window-buffer ediff-window-A ediff-patch-diagnostics
))
481 ((window-live-p ediff-window-B
)
482 (set-window-buffer ediff-window-B ediff-patch-diagnostics
))
483 (t (display-buffer ediff-patch-diagnostics
'not-this-window
))))
485 ;; prompt for file, get the buffer
486 (defun ediff-prompt-for-patch-file ()
487 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch
)
488 (ediff-patch-default-directory) ; try patch default dir
489 (t default-directory
)))
490 (coding-system-for-read ediff-coding-system-for-read
)
492 (setq patch-file-name
494 (format "Patch is in file%s: "
495 (cond ((and buffer-file-name
496 (equal (expand-file-name dir
)
497 (file-name-directory buffer-file-name
)))
500 (file-name-nondirectory buffer-file-name
)
503 dir buffer-file-name
'must-match
))
504 (if (file-directory-p patch-file-name
)
505 (error "Patch file cannot be a directory: %s" patch-file-name
)
506 (find-file-noselect patch-file-name
))
510 ;; Try current buffer, then the other window's buffer. Else, give up.
511 (defun ediff-prompt-for-patch-buffer ()
514 "Buffer that holds the patch: "
515 (cond ((save-excursion
516 (goto-char (point-min))
517 (re-search-forward ediff-context-diff-label-regexp nil t
))
519 ((save-window-excursion
522 (goto-char (point-min))
523 (and (re-search-forward ediff-context-diff-label-regexp nil t
)
525 ((save-window-excursion
528 (goto-char (point-min))
529 (and (re-search-forward ediff-context-diff-label-regexp nil t
)
531 (t (ediff-other-buffer (current-buffer))))
535 (defun ediff-get-patch-buffer (&optional arg patch-buf
)
536 "Obtain patch buffer. If patch is already in a buffer---use it.
537 Else, read patch file into a new buffer. If patch buffer is passed as an
538 optional argument, then use it."
539 (let ((last-nonmenu-event t
) ; Emacs: don't use dialog box
540 last-command-event
) ; XEmacs: don't use dialog box
542 (cond ((ediff-buffer-live-p patch-buf
))
543 ;; even prefix arg: patch in buffer
544 ((and (integerp arg
) (eq 0 (mod arg
2)))
545 (setq patch-buf
(ediff-prompt-for-patch-buffer)))
546 ;; odd prefix arg: get patch from a file
547 ((and (integerp arg
) (eq 1 (mod arg
2)))
548 (setq patch-buf
(ediff-prompt-for-patch-file)))
550 (if (y-or-n-p "Is the patch already in a buffer? ")
551 (ediff-prompt-for-patch-buffer)
552 (ediff-prompt-for-patch-file)))))
554 (ediff-with-current-buffer patch-buf
555 (goto-char (point-min))
556 (or (ediff-get-visible-buffer-window patch-buf
)
558 (pop-to-buffer patch-buf
'other-window
)
559 (select-window (previous-window)))))
560 (ediff-map-patch-buffer patch-buf
)
563 ;; Dispatch the right patch file function: regular or meta-level,
564 ;; depending on how many patches are in the patch file.
565 ;; At present, there is no support for meta-level patches.
566 ;; Should return either the ctl buffer or the meta-buffer
567 (defun ediff-dispatch-file-patching-job (patch-buf filename
568 &optional startup-hooks
)
569 (ediff-with-current-buffer patch-buf
570 ;; relativize names in the patch with respect to source-file
571 (ediff-fixup-patch-map filename
)
572 (if (< (length ediff-patch-map
) 2)
573 (ediff-patch-file-internal
575 (if (and ediff-patch-map
578 ;; this is the file to patch
579 (ediff-get-session-objA-name (car ediff-patch-map
))))
581 (ediff-get-session-objA-name (car ediff-patch-map
)))
583 (ediff-get-session-objA-name (car ediff-patch-map
))
586 (ediff-multi-patch-internal patch-buf startup-hooks
))
590 ;; When patching a buffer, never change the orig file. Instead, create a new
591 ;; buffer, ***_patched, even if the buff visits a file.
592 ;; Users who want to actually patch the buffer should use
593 ;; ediff-patch-file, not ediff-patch-buffer.
594 (defun ediff-patch-buffer-internal (patch-buf
596 &optional startup-hooks
)
597 (let* ((buf-to-patch (get-buffer buf-to-patch-name
))
598 (visited-file (if buf-to-patch
(buffer-file-name buf-to-patch
)))
599 (buf-mod-status (buffer-modified-p buf-to-patch
))
600 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
601 ediff-patch-map
)) 1))
602 default-dir file-name ctl-buf
)
603 (if multifile-patch-p
605 "To apply multi-file patches, please use `ediff-patch-file'"))
607 ;; create a temp file to patch
608 (ediff-with-current-buffer buf-to-patch
609 (setq default-dir default-directory
)
610 (setq file-name
(ediff-make-temp-file buf-to-patch
))
611 ;; temporarily switch visited file name, if any
612 (set-visited-file-name file-name
)
613 ;; don't create auto-save file, if buff was visiting a file
615 (setq buffer-auto-save-file-name nil
))
616 ;; don't confuse the user with a new bufname
617 (rename-buffer buf-to-patch-name
)
618 (set-buffer-modified-p nil
)
619 (set-visited-file-modtime) ; sync buffer and temp file
620 (setq default-directory default-dir
)
623 ;; dispatch a patch function
624 (setq ctl-buf
(ediff-dispatch-file-patching-job
625 patch-buf file-name startup-hooks
))
627 (ediff-with-current-buffer ctl-buf
628 (delete-file (buffer-file-name ediff-buffer-A
))
629 (delete-file (buffer-file-name ediff-buffer-B
))
630 (ediff-with-current-buffer ediff-buffer-A
631 (if default-dir
(setq default-directory default-dir
))
632 (set-visited-file-name visited-file
) ; visited-file might be nil
633 (rename-buffer buf-to-patch-name
)
634 (set-buffer-modified-p buf-mod-status
))
635 (ediff-with-current-buffer ediff-buffer-B
636 (setq buffer-auto-save-file-name nil
) ; don't create auto-save file
637 (if default-dir
(setq default-directory default-dir
))
638 (set-visited-file-name nil
)
639 (rename-buffer (ediff-unique-buffer-name
640 (concat buf-to-patch-name
"_patched") ""))
641 (set-buffer-modified-p t
)))
645 ;; Traditional patch has weird return codes.
646 ;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
647 ;; 0 is a good code in all cases.
648 ;; We'll do the concervative thing.
649 (defun ediff-patch-return-code-ok (code)
651 ;;; (if (eq (ediff-test-patch-utility) 'traditional)
653 ;;; (not (eq code 2))))
655 (defun ediff-patch-file-internal (patch-buf source-filename
656 &optional startup-hooks
)
657 (setq source-filename
(expand-file-name source-filename
))
659 (let* ((shell-file-name ediff-shell
)
660 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
661 ;; ediff-find-file may use a temp file to do the patch
662 ;; so, we save source-filename and true-source-filename as a var
663 ;; that initially is source-filename but may be changed to a temp
664 ;; file for the purpose of patching.
665 (true-source-filename source-filename
)
666 (target-filename source-filename
)
667 ;; this ensures that the patch process gets patch buffer in the
668 ;; encoding that Emacs thinks is right for that type of text
669 (coding-system-for-write
670 (if (boundp 'buffer-file-coding-system
) buffer-file-coding-system
))
671 target-buf buf-to-patch file-name-magic-p
672 patch-return-code ctl-buf backup-style aux-wind
)
674 (if (string-match "V" ediff-patch-options
)
676 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
678 ;; Make a temp file, if source-filename has a magic file handler (or if
679 ;; it is handled via auto-mode-alist and similar magic).
680 ;; Check if there is a buffer visiting source-filename and if they are in
681 ;; sync; arrange for the deletion of temp file.
682 (ediff-find-file 'true-source-filename
'buf-to-patch
683 'ediff-last-dir-patch
'startup-hooks
)
685 ;; Check if source file name has triggered black magic, such as file name
686 ;; handlers or auto mode alist, and make a note of it.
687 ;; true-source-filename should be either the original name or a
688 ;; temporary file where we put the after-product of the file handler.
689 (setq file-name-magic-p
(not (equal (file-truename true-source-filename
)
690 (file-truename source-filename
))))
692 ;; Checkout orig file, if necessary, so that the patched file
693 ;; could be checked back in.
694 (ediff-maybe-checkout buf-to-patch
)
696 (ediff-with-current-buffer patch-diagnostics
697 (insert-buffer-substring patch-buf
)
698 (message "Applying patch ... ")
699 ;; fix environment for gnu patch, so it won't make numbered extensions
700 (setq backup-style
(getenv "VERSION_CONTROL"))
701 (setenv "VERSION_CONTROL" nil
)
702 (setq patch-return-code
704 (point-min) (point-max)
706 t
; delete region (which contains the patch
707 t
; insert output (patch diagnostics) in current buffer
708 nil
; don't redisplay
709 shell-command-switch
; usually -c
710 (format "%s %s %s %s"
714 (expand-file-name true-source-filename
))
717 ;; restore environment for gnu patch
718 (setenv "VERSION_CONTROL" backup-style
))
720 (message "Applying patch ... done")
723 (switch-to-buffer patch-diagnostics
)
724 (sit-for 0) ; synchronize - let the user see diagnostics
726 (or (and (ediff-patch-return-code-ok patch-return-code
)
728 (concat true-source-filename ediff-backup-extension
)))
730 (with-output-to-temp-buffer ediff-msg-buffer
731 (ediff-with-current-buffer standard-output
734 "Patch program has failed due to a bad patch file,
735 it couldn't apply all hunks, OR
736 it couldn't create the backup for the file being patched.
738 The former could be caused by a corrupt patch file or because the %S
739 program doesn't understand the format of the patch file in use.
741 The second problem might be due to an incompatibility among these settings:
742 ediff-patch-program = %S ediff-patch-options = %S
743 ediff-backup-extension = %S ediff-backup-specs = %S
745 See Ediff on-line manual for more details on these variables.
746 In particular, check the documentation for `ediff-backup-specs'.
748 In any of the above cases, Ediff doesn't compare files automatically.
749 However, if the patch was applied partially and the backup file was created,
750 you can still examine the changes via M-x ediff-files"
754 ediff-backup-extension
758 (if (setq aux-wind
(get-buffer-window ediff-msg-buffer
))
760 (select-window aux-wind
)
761 (goto-char (point-max))))
762 (switch-to-buffer-other-window patch-diagnostics
)
763 (error "Patch appears to have failed")))
765 ;; If black magic is involved, apply patch to a temp copy of the
766 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
767 ;; to temp copy, we name the result old-name_patched for local files
768 ;; and temp-copy_patched for remote files. The orig file name isn't
769 ;; changed, and the temp copy of the original is later deleted.
770 ;; Without magic, the original file is renamed (usually into
771 ;; old-name_orig) and the result of patching will have the same name as
773 (if (not file-name-magic-p
)
774 (ediff-with-current-buffer buf-to-patch
775 (set-visited-file-name
776 (concat source-filename ediff-backup-extension
))
777 (set-buffer-modified-p nil
))
779 ;; Black magic in effect.
780 ;; If orig file was remote, put the patched file in the temp directory.
781 ;; If orig file is local, put the patched file in the directory of
783 (setq target-filename
785 (if (ediff-file-remote-p (file-truename source-filename
))
790 (rename-file true-source-filename target-filename t
)
792 ;; arrange that the temp copy of orig will be deleted
793 (rename-file (concat true-source-filename ediff-backup-extension
)
794 true-source-filename t
))
796 ;; make orig buffer read-only
798 (cons 'ediff-set-read-only-in-buf-A startup-hooks
))
800 ;; set up a buf for the patched file
801 (setq target-buf
(find-file-noselect target-filename
))
804 (ediff-buffers-internal
805 buf-to-patch target-buf nil
806 startup-hooks
'epatch
))
807 (ediff-with-current-buffer ctl-buf
808 (setq ediff-patchbufer patch-buf
809 ediff-patch-diagnostics patch-diagnostics
))
811 (bury-buffer patch-diagnostics
)
812 (message "Type `P', if you need to see patch diagnostics")
815 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks
)
818 ;; this sets various vars in the meta buffer inside
819 ;; ediff-prepare-meta-buffer
821 ;; tell what to do if the user clicks on a session record
822 (setq ediff-session-action-function
823 'ediff-patch-file-form-meta
824 ediff-meta-patchbufer patch-buf
) )
826 (setq meta-buf
(ediff-prepare-meta-buffer
827 'ediff-filegroup-action
828 (ediff-with-current-buffer patch-buf
829 (cons (ediff-make-new-meta-list-header
831 (format "%S" patch-buf
) ; obj A
832 nil nil
; objects B,C
833 nil
; merge-auto-store-dir
834 nil
; comparison-func
837 "*Ediff Session Group Panel"
838 'ediff-redraw-directory-group-buffer
839 'ediff-multifile-patch
841 (ediff-show-meta-buffer meta-buf
)
846 (provide 'ediff-ptch
)
850 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
851 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
852 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
855 ;;; arch-tag: 2fe2161e-e116-469b-90fa-5cbb44c1bd1b
856 ;;; ediff-ptch.el ends here