More tweaks of skeleton documentation wrt \n behavior at bol/eol.
[bpt/emacs.git] / lisp / vc / ediff-ptch.el
CommitLineData
fce30d79
MK
1;;; ediff-ptch.el --- Ediff's patch support
2
ba318903 3;; Copyright (C) 1996-2014 Free Software Foundation, Inc.
fce30d79 4
50a07e18 5;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
bd78fa1d 6;; Package: ediff
fce30d79
MK
7
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
fce30d79 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
fce30d79
MK
14
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.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
fce30d79 22
3afbc435 23;;; Commentary:
fce30d79
MK
24
25;;; Code:
71296446 26
ddc90f39 27
2d84cc27
MK
28(provide 'ediff-ptch)
29
ddc90f39 30(defgroup ediff-ptch nil
143b42a6 31 "Ediff patch support."
ddc90f39
MK
32 :tag "Patch"
33 :prefix "ediff-"
34 :group 'ediff)
35
4b45b44f 36(require 'ediff-init)
a5c7df1a 37(require 'ediff-util)
4b45b44f 38
1e70790f 39(defcustom ediff-patch-program "patch"
9201cc28 40 "Name of the program that applies patches.
1e70790f
MK
41It is recommended to use GNU-compatible versions."
42 :type 'string
43 :group 'ediff-ptch)
44(defcustom ediff-patch-options "-f"
9201cc28 45 "Options to pass to ediff-patch-program.
1e70790f
MK
46
47Note: the `-b' option should be specified in `ediff-backup-specs'.
48
49It is recommended to pass the `-f' option to the patch program, so it won't ask
3af0304a 50questions. However, some implementations don't accept this option, in which
1e70790f
MK
51case the default value for this variable should be changed."
52 :type 'string
53 :group 'ediff-ptch)
54
fce30d79
MK
55(defvar ediff-last-dir-patch nil
56 "Last directory used by an Ediff command for file to patch.")
57
1e70790f
MK
58;; the default backup extension
59(defconst ediff-default-backup-extension
24ac444f 60 (if (eq system-type 'ms-dos)
1e70790f 61 "_orig" ".orig"))
71296446 62
1e70790f
MK
63
64(defcustom ediff-backup-extension ediff-default-backup-extension
92c51e07 65 "Backup extension used by the patch program.
1e70790f
MK
66See also `ediff-backup-specs'."
67 :type 'string
68 :group 'ediff-ptch)
92c51e07 69
bd698e98 70(defun ediff-test-patch-utility ()
d29a70fe 71 (condition-case nil
15502042 72 (cond ((eq 0 (call-process ediff-patch-program nil nil nil "-z." "-b"))
d29a70fe
RS
73 ;; GNU `patch' v. >= 2.2
74 'gnu)
15502042 75 ((eq 0 (call-process ediff-patch-program nil nil nil "-b"))
d29a70fe
RS
76 'posix)
77 (t 'traditional))
78 (file-error nil)))
bd698e98 79
71296446 80(defcustom ediff-backup-specs
bd698e98
MK
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")
89 (t
90 ;; traditional `patch'
91 (format "-b %s" ediff-backup-extension))))
9201cc28 92 "Backup directives to pass to the patch program.
92c51e07 93Ediff requires that the old version of the file \(before applying the patch\)
3af0304a 94be saved in a file named `the-patch-file.extension'. Usually `extension' is
92c51e07
MK
95`.orig', but this can be changed by the user and may depend on the system.
96Therefore, Ediff needs to know the backup extension used by the patch program.
97
98Some versions of the patch program let you specify `-b backup-extension'.
1e70790f 99Other versions only permit `-b', which assumes the extension `.orig'
3af0304a 100\(in which case ediff-backup-extension MUST be also `.orig'\). The latest
1e70790f 101versions of GNU patch require `-b -z backup-extension'.
92c51e07
MK
102
103Note that both `ediff-backup-extension' and `ediff-backup-specs'
3af0304a 104must be set properly. If your patch program takes the option `-b',
92c51e07 105but not `-b extension', the variable `ediff-backup-extension' must
bd698e98
MK
106still be set so Ediff will know which extension to use.
107
3af0304a 108Ediff tries to guess the appropriate value for this variables. It is believed
bd698e98 109to be working for `traditional' patch, all versions of GNU patch, and for POSIX
3af0304a 110patch. So, don't change these variables, unless the default doesn't work."
ddc90f39
MK
111 :type 'string
112 :group 'ediff-ptch)
92c51e07 113
fce30d79 114
ddc90f39 115(defcustom ediff-patch-default-directory nil
9201cc28 116 "Default directory to look for patches."
ddc90f39
MK
117 :type '(choice (const nil) string)
118 :group 'ediff-ptch)
fce30d79 119
141f0c03
MK
120;; This context diff does not recognize spaces inside files, but removing ' '
121;; from [^ \t] breaks normal patches for some reason
ddc90f39 122(defcustom ediff-context-diff-label-regexp
fce30d79 123 (concat "\\(" ; context diff 2-liner
141f0c03 124 "^\\*\\*\\* +\\([^ \t]+\\)[^*]+[\t ]*\n--- +\\([^ \t]+\\)"
ff6f4585
JL
125 "\\|" ; unified format diff 2-liner
126 "^--- +\\([^ \t]+\\).*\n\\+\\+\\+ +\\([^ \t]+\\)"
77c57270 127 "\\)")
9201cc28 128 "Regexp matching filename 2-liners at the start of each context diff.
1e70790f
MK
129You probably don't want to change that, unless you are using an obscure patch
130program."
ddc90f39
MK
131 :type 'regexp
132 :group 'ediff-ptch)
fce30d79 133
3af0304a 134;; The buffer of the patch file. Local to control buffer.
fce30d79
MK
135(ediff-defvar-local ediff-patchbufer nil "")
136
137;; The buffer where patch displays its diagnostics.
138(ediff-defvar-local ediff-patch-diagnostics nil "")
139
3af0304a 140;; Map of patch buffer. Has the form:
fce30d79
MK
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 "")
146
147;; strip prefix from filename
148;; returns /dev/null, if can't strip prefix
149(defsubst ediff-file-name-sans-prefix (filename prefix)
15c77b9e
MK
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)
159 )
fce30d79
MK
160
161
162
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)
e756eb9f 166 (ediff-with-current-buffer buf
fce30d79
MK
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)))
177
71296446 178;; Scan BUF (which is supposed to contain a patch) and make a list of the form
743a79af
MK
179;; ((nil nil filename-spec1 marker1 marker2)
180;; (nil nil filename-spec2 marker1 marker2) ...)
71296446 181;; where filename-spec[12] are files to which the `patch' program would
743a79af
MK
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.
188;;
87523aed
DA
189;; The marker `mark1' delimits the beginning of the corresponding patch and
190;; `mark2' does it for the end.
743a79af
MK
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
fce30d79 194(defun ediff-map-patch-buffer (buf)
e756eb9f 195 (ediff-with-current-buffer buf
fce30d79 196 (let ((count 0)
87523aed 197 (mark1 (point-min-marker))
fce30d79
MK
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)
a8b7f4b9 212 beg1 (or (match-beginning 2) (match-beginning 4))
92c51e07
MK
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)))
fce30d79
MK
216 ;; possible-file-names is holding the new file names until we
217 ;; insert the old file name in the patch map
743a79af 218 ;; It is a pair
ac64a728 219 ;; (filename-from-1st-header-line . filename-from-2nd-line)
fce30d79
MK
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)))
232
233 (goto-char mark2-end)
71296446 234
fce30d79 235 (if filenames
743a79af
MK
236 (setq patch-map
237 (cons (ediff-make-new-meta-list-element
238 filenames mark1 mark2)
239 patch-map)))
fce30d79
MK
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)
743a79af
MK
246 patch-map (cons (ediff-make-new-meta-list-element
247 possible-file-names mark1 mark2)
248 patch-map))
fce30d79
MK
249 (setq ediff-patch-map (nreverse patch-map))
250 count)))
251
252;; Fix up the file names in the list using the argument FILENAME
15c77b9e
MK
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.
fce30d79 261;; Note: Ediff doesn't recognize multi-file patches that are separated
3af0304a 262;; with the `Index:' line. It treats them as a single-file patch.
fce30d79
MK
263;;
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)))
15c77b9e
MK
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
fce30d79
MK
275 )
276
277 ;; chop off base-dirs
3f0f4f6f
JB
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 ""))
291 )
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)))
313 ))
314 ediff-patch-map)
fce30d79
MK
315
316 ;; take the given file name into account
317 (or (file-directory-p filename)
318 (string= "/dev/null" filename)
743a79af
MK
319 (setcar (ediff-get-session-objA (car ediff-patch-map))
320 (cons (file-name-nondirectory filename)
321 (file-name-nondirectory filename))))
fce30d79
MK
322
323 ;; prepend actual-dir
3f0f4f6f
JB
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 "
fce30d79
MK
337The patch file contains a context diff for
338 %s
339 %s
fce30d79 340However, Ediff cannot infer the name of the actual file
3af0304a 341to be patched on your system. If you know the correct file name,
fce30d79
MK
342please enter it now.
343
344If you don't know and still would like to apply patches to
345other files, enter /dev/null
346"
3f0f4f6f
JB
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)))))
370 ))
371 ediff-patch-map)
fac916bf 372 ;; Check for the existing files in each pair and discard the nonexistent
e2de3a29 373 ;; ones. If both exist, ask the user.
743a79af
MK
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))
3af0304a
MK
379 (f1-exists (file-exists-p file1))
380 (f2-exists (file-exists-p file2)))
381 (cond
15c77b9e
MK
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)
743a79af
MK
390 ;; replace file-pair with the winning file2
391 (setcar session-file-object file2))
15c77b9e
MK
392 ((and
393 ;; (< (length file1) (length file2))
394 (not f2-exists)
395 f1-exists)
743a79af
MK
396 ;; replace file-pair with the winning file1
397 (setcar session-file-object file1))
3af0304a
MK
398 ((and f1-exists f2-exists
399 (string= file1 file2))
743a79af 400 (setcar session-file-object file1))
15c77b9e
MK
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))
3af0304a
MK
405 ((and f1-exists f2-exists)
406 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
407 (ediff-with-current-buffer standard-output
408 (fundamental-mode))
3af0304a 409 (princ (format "
fce30d79
MK
410Ediff has inferred that
411 %s
412 %s
bd698e98 413are two possible targets for applying the patch.
fce30d79
MK
414Both files seem to be plausible alternatives.
415
dc4c6a7a 416Please advise:
fce30d79
MK
417 Type `y' to use %s as the target;
418 Type `n' to use %s as the target.
419"
15c77b9e 420 file1 file2 file1 file2)))
743a79af 421 (setcar session-file-object
15c77b9e
MK
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))
428 )
743a79af
MK
429 (f2-exists (setcar session-file-object file2))
430 (f1-exists (setcar session-file-object file1))
3af0304a
MK
431 (t
432 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
433 (ediff-with-current-buffer standard-output
434 (fundamental-mode))
3af0304a
MK
435 (princ "\nEdiff has inferred that")
436 (if (string= file1 file2)
437 (princ (format "
fce30d79 438 %s
15c77b9e 439is assumed to be the target for this patch. However, this file does not exist."
3af0304a
MK
440 file1))
441 (princ (format "
fce30d79 442 %s
bd698e98 443 %s
3af0304a
MK
444are two possible targets for this patch. However, these files do not exist."
445 file1 file2)))
446 (princ "
bd698e98 447\nPlease enter an alternative patch target ...\n"))
3af0304a
MK
448 (let ((directory t)
449 target)
450 (while directory
71296446 451 (setq target (read-file-name
3af0304a
MK
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)))
743a79af 459 (setcar session-file-object target))))))
fce30d79
MK
460 ediff-patch-map)
461 ))
462
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))))
470
a5c7df1a
GM
471(defvar ediff-use-last-dir)
472
3af0304a
MK
473;; prompt for file, get the buffer
474(defun ediff-prompt-for-patch-file ()
15c77b9e
MK
475 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
476 (ediff-patch-default-directory) ; try patch default dir
4960e757 477 (t default-directory)))
15c77b9e
MK
478 (coding-system-for-read ediff-coding-system-for-read)
479 patch-file-name)
480 (setq patch-file-name
481 (read-file-name
5b76833f 482 (format "Patch is in file%s: "
15c77b9e
MK
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))
3af0304a
MK
495 ))
496
497
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
7261ece3 502 "Buffer that holds the patch: "
3af0304a
MK
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)))))
4960e757 519 (t (ediff-other-buffer (current-buffer))))
3af0304a
MK
520 'must-match)))
521
522
523(defun ediff-get-patch-buffer (&optional arg patch-buf)
524 "Obtain patch buffer. If patch is already in a buffer---use it.
525Else, read patch file into a new buffer. If patch buffer is passed as an
526optional 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
529
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)))))
71296446 541
e756eb9f 542 (ediff-with-current-buffer patch-buf
fce30d79
MK
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))
550
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)
e756eb9f 557 (ediff-with-current-buffer patch-buf
fce30d79
MK
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
2acc9e43 563 (if (and ediff-patch-map
743a79af
MK
564 (not (string-match
565 "^/dev/null"
566 ;; this is the file to patch
567 (ediff-get-session-objA-name (car ediff-patch-map))))
71296446 568 (> (length
743a79af
MK
569 (ediff-get-session-objA-name (car ediff-patch-map)))
570 1))
571 (ediff-get-session-objA-name (car ediff-patch-map))
fce30d79
MK
572 filename)
573 startup-hooks)
574 (ediff-multi-patch-internal patch-buf startup-hooks))
575 ))
576
577
3af0304a 578;; When patching a buffer, never change the orig file. Instead, create a new
bd698e98
MK
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)
fce30d79 585 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
bd698e98 586 (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
fce30d79 587 (buf-mod-status (buffer-modified-p buf-to-patch))
e756eb9f 588 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
fce30d79
MK
589 ediff-patch-map)) 1))
590 default-dir file-name ctl-buf)
bd698e98
MK
591 (if multifile-patch-p
592 (error
3af0304a 593 "To apply multi-file patches, please use `ediff-patch-file'"))
bd698e98
MK
594
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)
609 )
71296446 610
fce30d79
MK
611 ;; dispatch a patch function
612 (setq ctl-buf (ediff-dispatch-file-patching-job
613 patch-buf file-name startup-hooks))
71296446 614
bd698e98
MK
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)
71296446 627 (rename-buffer (ediff-unique-buffer-name
bd698e98
MK
628 (concat buf-to-patch-name "_patched") ""))
629 (set-buffer-modified-p t)))
fce30d79
MK
630 ))
631
bd698e98
MK
632
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.
da6062e6 636;; We'll do the conservative thing.
bd698e98
MK
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))))
642
a5c7df1a
GM
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))
647
fce30d79
MK
648(defun ediff-patch-file-internal (patch-buf source-filename
649 &optional startup-hooks)
650 (setq source-filename (expand-file-name source-filename))
71296446 651
92c51e07 652 (let* ((shell-file-name ediff-shell)
fce30d79
MK
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)
4960e757
MK
660 ;; this ensures that the patch process gets patch buffer in the
661 ;; encoding that Emacs thinks is right for that type of text
71296446 662 (coding-system-for-write
4960e757 663 (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
71296446 664 target-buf buf-to-patch file-name-magic-p
e756eb9f 665 patch-return-code ctl-buf backup-style aux-wind)
71296446 666
bd698e98 667 (if (string-match "V" ediff-patch-options)
fce30d79
MK
668 (error
669 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
71296446 670
fce30d79
MK
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)
677
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))))
71296446
JB
684
685 ;; Checkout orig file, if necessary, so that the patched file
bf5d92c5
MK
686 ;; could be checked back in.
687 (ediff-maybe-checkout buf-to-patch)
fce30d79 688
e756eb9f 689 (ediff-with-current-buffer patch-diagnostics
15c77b9e 690 (insert-buffer-substring patch-buf)
fce30d79
MK
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)
92c51e07
MK
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))
708 ))
709
fce30d79
MK
710 ;; restore environment for gnu patch
711 (setenv "VERSION_CONTROL" backup-style))
712
713 (message "Applying patch ... done")
714 (message "")
715
716 (switch-to-buffer patch-diagnostics)
717 (sit-for 0) ; synchronize - let the user see diagnostics
71296446 718
bd698e98 719 (or (and (ediff-patch-return-code-ok patch-return-code)
92c51e07
MK
720 (file-exists-p
721 (concat true-source-filename ediff-backup-extension)))
722 (progn
723 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
724 (ediff-with-current-buffer standard-output
725 (fundamental-mode))
71296446 726 (princ (format
bd698e98
MK
727 "Patch program has failed due to a bad patch file,
728it couldn't apply all hunks, OR
729it couldn't create the backup for the file being patched.
92c51e07 730
1e70790f
MK
731The former could be caused by a corrupt patch file or because the %S
732program doesn't understand the format of the patch file in use.
92c51e07 733
1e70790f 734The second problem might be due to an incompatibility among these settings:
bd698e98
MK
735 ediff-patch-program = %S ediff-patch-options = %S
736 ediff-backup-extension = %S ediff-backup-specs = %S
92c51e07 737
92c51e07 738See Ediff on-line manual for more details on these variables.
71296446 739In particular, check the documentation for `ediff-backup-specs'.
bd698e98
MK
740
741In any of the above cases, Ediff doesn't compare files automatically.
742However, if the patch was applied partially and the backup file was created,
743you can still examine the changes via M-x ediff-files"
2acc9e43
DL
744 ediff-patch-program
745 ediff-patch-program
746 ediff-patch-options
747 ediff-backup-extension
748 ediff-backup-specs
749 )))
92c51e07
MK
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))))
1e70790f 755 (switch-to-buffer-other-window patch-diagnostics)
92c51e07 756 (error "Patch appears to have failed")))
71296446 757
fce30d79 758 ;; If black magic is involved, apply patch to a temp copy of the
3af0304a 759 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
fce30d79 760 ;; to temp copy, we name the result old-name_patched for local files
3af0304a 761 ;; and temp-copy_patched for remote files. The orig file name isn't
fce30d79
MK
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)
e756eb9f 767 (ediff-with-current-buffer buf-to-patch
92c51e07
MK
768 (set-visited-file-name
769 (concat source-filename ediff-backup-extension))
fce30d79 770 (set-buffer-modified-p nil))
71296446 771
fce30d79
MK
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"))
71296446 782
fce30d79 783 (rename-file true-source-filename target-filename t)
71296446 784
fce30d79 785 ;; arrange that the temp copy of orig will be deleted
92c51e07 786 (rename-file (concat true-source-filename ediff-backup-extension)
fce30d79 787 true-source-filename t))
71296446 788
fce30d79
MK
789 ;; make orig buffer read-only
790 (setq startup-hooks
791 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
71296446 792
fce30d79
MK
793 ;; set up a buf for the patched file
794 (setq target-buf (find-file-noselect target-filename))
71296446 795
fce30d79
MK
796 (setq ctl-buf
797 (ediff-buffers-internal
798 buf-to-patch target-buf nil
799 startup-hooks 'epatch))
e756eb9f 800 (ediff-with-current-buffer ctl-buf
fce30d79
MK
801 (setq ediff-patchbufer patch-buf
802 ediff-patch-diagnostics patch-diagnostics))
71296446 803
fce30d79
MK
804 (bury-buffer patch-diagnostics)
805 (message "Type `P', if you need to see patch diagnostics")
806 ctl-buf))
807
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
086171bf
MK
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) )
fce30d79 818 startup-hooks))
71296446 819 (setq meta-buf (ediff-prepare-meta-buffer
fce30d79 820 'ediff-filegroup-action
e756eb9f 821 (ediff-with-current-buffer patch-buf
743a79af
MK
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
828 )
fce30d79
MK
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)
835 ))
836
71296446
JB
837
838
fce30d79 839
fa043571
SM
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:
fce30d79 845
fce30d79 846;;; ediff-ptch.el ends here