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