Initial revision
[bpt/emacs.git] / lisp / ediff-ptch.el
CommitLineData
fce30d79
MK
1;;; ediff-ptch.el --- Ediff's patch support
2
3;; Copyright (C) 1996 Free Software Foundation, Inc.
4
5;; Author: Michael Kifer <kifer@cs.sunysb.edu>
6
7;; This file is part of GNU Emacs.
8
9;; GNU Emacs is free software; you can redistribute it and/or modify
10;; it under the terms of the GNU General Public License as published by
11;; the Free Software Foundation; either version 2, or (at your option)
12;; any later version.
13
14;; GNU Emacs is distributed in the hope that it will be useful,
15;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;; GNU General Public License for more details.
18
19;; You should have received a copy of the GNU General Public License
20;; along with GNU Emacs; see the file COPYING. If not, write to the
21;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22;; Boston, MA 02111-1307, USA.
23
24
25;;; Code:
26
4b45b44f
RS
27(require 'ediff-init)
28
fce30d79
MK
29(defvar ediff-last-dir-patch nil
30 "Last directory used by an Ediff command for file to patch.")
31
32(defvar ediff-backup-extension
33 (if (memq system-type '(vax-vms axp-vms emx ms-dos windows-nt windows-95))
34 "_orig" ".orig")
35 "Default backup extension for the patch program.")
36
37(defvar ediff-patch-default-directory nil
38 "*Default directory to look for patches.")
39
40(defvar ediff-context-diff-label-regexp
41 (concat "\\(" ; context diff 2-liner
42 "^\\*\\*\\* \\([^ \t]+\\)[^*]+[\t ]*\n--- \\([^ \t]+\\)"
43 "\\|" ; GNU unified format diff 2-liner
a8b7f4b9 44 "^--- \\([^ \t]+\\)[\t ]+.*\n\\+\\+\\+ \\([^ \t]+\\)"
fce30d79
MK
45 "\\)")
46 "*Regexp matching filename 2-liners at the start of each context diff.")
47
48(defvar ediff-patch-program "patch"
49 "*Name of the program that applies patches.")
50(defvar ediff-patch-options ""
51 "*Options to pass to ediff-patch-program.")
52
53;; The buffer of the patch file. Local to control buffer.
54(ediff-defvar-local ediff-patchbufer nil "")
55
56;; The buffer where patch displays its diagnostics.
57(ediff-defvar-local ediff-patch-diagnostics nil "")
58
59;; Map of patch buffer. Has the form:
60;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
61;; where filenames are files to which patch would have applied the patch;
62;; marker1 delimits the beginning of the corresponding patch and marker2 does
63;; it for the end.
64(ediff-defvar-local ediff-patch-map nil "")
65
66;; strip prefix from filename
67;; returns /dev/null, if can't strip prefix
68(defsubst ediff-file-name-sans-prefix (filename prefix)
69 (save-match-data
70 (if (string-match (concat "^" prefix) filename)
71 (substring filename (match-end 0))
72 (concat "/null/" filename))))
73
74
75
76;; no longer used
77;; return the number of matches of regexp in buf starting from the beginning
78(defun ediff-count-matches (regexp buf)
79 (ediff-eval-in-buffer buf
80 (let ((count 0) opoint)
81 (save-excursion
82 (goto-char (point-min))
83 (while (and (not (eobp))
84 (progn (setq opoint (point))
85 (re-search-forward regexp nil t)))
86 (if (= opoint (point))
87 (forward-char 1)
88 (setq count (1+ count)))))
89 count)))
90
91;; Scan BUF (which is supposed to contain a patch) and make a list of the form
92;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
93;; where filenames are files to which patch would have applied the patch;
94;; marker1 delimits the beginning of the corresponding patch and marker2 does
95;; it for the end. This list is then assigned to ediff-patch-map.
96;; Returns the number of elements in the list ediff-patch-map
97(defun ediff-map-patch-buffer (buf)
98 (ediff-eval-in-buffer buf
99 (let ((count 0)
100 (mark1 (move-marker (make-marker) (point-min)))
101 (mark1-end (point-min))
102 (possible-file-names '("/dev/null" . "/dev/null"))
103 mark2-end mark2 filenames
104 beg1 beg2 end1 end2
105 patch-map opoint)
106 (save-excursion
107 (goto-char (point-min))
108 (setq opoint (point))
109 (while (and (not (eobp))
110 (re-search-forward ediff-context-diff-label-regexp nil t))
111 (if (= opoint (point))
112 (forward-char 1) ; ensure progress towards the end
113 (setq mark2 (move-marker (make-marker) (match-beginning 0))
114 mark2-end (match-end 0)
a8b7f4b9
RS
115 beg1 (or (match-beginning 2) (match-beginning 4))
116 end1 (or (match-end 2) (match-end 4))
117 beg2 (or (match-beginning 3) (match-beginning 5))
118 end2 (or (match-end 3) (match-end 5)))
fce30d79
MK
119 ;; possible-file-names is holding the new file names until we
120 ;; insert the old file name in the patch map
121 ;; It is a pair (filename from 1st header line . fn from 2nd line)
122 (setq possible-file-names
123 (cons (if (and beg1 end1)
124 (buffer-substring beg1 end1)
125 "/dev/null")
126 (if (and beg2 end2)
127 (buffer-substring beg2 end2)
128 "/dev/null")))
129 ;; check for any `Index:' or `Prereq:' lines, but don't use them
130 (if (re-search-backward "^Index:" mark1-end 'noerror)
131 (move-marker mark2 (match-beginning 0)))
132 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
133 (move-marker mark2 (match-beginning 0)))
134
135 (goto-char mark2-end)
136
137 (if filenames
138 (setq patch-map (cons (list filenames mark1 mark2) patch-map)))
139 (setq mark1 mark2
140 mark1-end mark2-end
141 filenames possible-file-names))
142 (setq opoint (point)
143 count (1+ count))))
144 (setq mark2 (point-max-marker)
145 patch-map (cons (list possible-file-names mark1 mark2) patch-map))
146 (setq ediff-patch-map (nreverse patch-map))
147 count)))
148
149;; Fix up the file names in the list using the argument FILENAME
150;; Algorithm: find the first file's directory and cut it out from each file
151;; name in the patch. Prepend the directory of FILENAME to each file in the
152;; patch. In addition, the first file in the patch is replaced by FILENAME.
153;; Each file is actually a file-pair of files found in the context diff header
154;; In the end, for each pair, we select the shortest existing file.
155;; Note: Ediff doesn't recognize multi-file patches that are separated
156;; with the `Index:' line. It treats them as a single-file patch.
157;;
158;; Executes inside the patch buffer
159(defun ediff-fixup-patch-map (filename)
160 (setq filename (expand-file-name filename))
161 (let ((actual-dir (if (file-directory-p filename)
162 ;; directory part of filename
163 (file-name-as-directory filename)
164 (file-name-directory filename)))
165 ;; directory part of the first file in the patch
166 (base-dir1 (file-name-directory (car (car (car ediff-patch-map)))))
167 (base-dir2 (file-name-directory (cdr (car (car ediff-patch-map)))))
168 )
169
170 ;; chop off base-dirs
171 (mapcar (function (lambda (triple)
172 (or (string= (car (car triple)) "/dev/null")
173 (setcar (car triple)
174 (ediff-file-name-sans-prefix
175 (car (car triple)) base-dir1)))
176 (or (string= (cdr (car triple)) "/dev/null")
177 (setcdr (car triple)
178 (ediff-file-name-sans-prefix
179 (cdr (car triple)) base-dir2)))
180 ))
181 ediff-patch-map)
182
183 ;; take the given file name into account
184 (or (file-directory-p filename)
185 (string= "/dev/null" filename)
186 (progn
187 (setcar (car ediff-patch-map)
188 (cons (file-name-nondirectory filename)
189 (file-name-nondirectory filename)))))
190
191 ;; prepend actual-dir
192 (mapcar (function (lambda (triple)
193 (if (and (string-match "^/null/" (car (car triple)))
194 (string-match "^/null/" (cdr (car triple))))
195 ;; couldn't strip base-dir1 and base-dir2
196 ;; hence, something wrong
197 (progn
198 (with-output-to-temp-buffer ediff-msg-buffer
199 (princ
200 (format "
201The patch file contains a context diff for
202 %s
203 %s
204
205However, Ediff cannot infer the name of the actual file
206to be patched on your system. If you know the correct file name,
207please enter it now.
208
209If you don't know and still would like to apply patches to
210other files, enter /dev/null
211"
212 (substring (car (car triple)) 6)
213 (substring (cdr (car triple)) 6))))
214 (let ((directory t)
215 user-file)
216 (while directory
217 (setq user-file
218 (read-file-name
219 "Please enter file name: "
220 actual-dir actual-dir t))
221 (if (not (file-directory-p user-file))
222 (setq directory nil)
223 (setq directory t)
224 (beep)
225 (message "%s is a directory" user-file)
226 (sit-for 2)))
227 (setcar triple (cons user-file user-file))))
228 (setcar (car triple)
229 (expand-file-name
230 (concat actual-dir (car (car triple)))))
231 (setcdr (car triple)
232 (expand-file-name
233 (concat actual-dir (cdr (car triple))))))
234 ))
235 ediff-patch-map)
236 ;; check for the shorter existing file in each pair and discard the other
237 ;; one
238 (mapcar (function (lambda (triple)
239 (let* ((file1 (car (car triple)))
240 (file2 (cdr (car triple)))
241 (f1-exists (file-exists-p file1))
242 (f2-exists (file-exists-p file2)))
243 (cond
244 ((and (< (length file2) (length file1))
245 f2-exists)
246 (setcar triple file2))
247 ((and (< (length file1) (length file2))
248 f1-exists)
249 (setcar triple file1))
250 ((and f1-exists f2-exists
251 (string= file1 file2))
252 (setcar triple file1))
253 ((and f1-exists f2-exists)
254 (with-output-to-temp-buffer ediff-msg-buffer
255 (princ (format "
256Ediff has inferred that
257 %s
258 %s
259are possible targets for applying the patch.
260Both files seem to be plausible alternatives.
261
262Please advice:
263 Type `y' to use %s as the target;
264 Type `n' to use %s as the target.
265"
266 file1 file2 file2 file1)))
267 (setcar triple
268 (if (y-or-n-p (format "Use %s ? " file2))
269 file2 file1)))
270 (f2-exists (setcar triple file2))
271 (f1-exists (setcar triple file1))
272 (t
273 (with-output-to-temp-buffer ediff-msg-buffer
274 (princ (format "
275Ediff inferred that
276 %s
277 %s
278are possible alternative targets for this patch.
279
280However, these files do not exist.
281
282Please enter an alternative patch target ...
283"
284 file1 file2)))
285 (let ((directory t)
286 target)
287 (while directory
288 (setq target (read-file-name
289 "Please enter a patch target: "
290 actual-dir actual-dir t))
291 (if (not (file-directory-p target))
292 (setq directory nil)
293 (beep)
294 (message "%s is a directory" target)
295 (sit-for 2)))
296 (setcar triple target)))))))
297 ediff-patch-map)
298 ))
299
300(defun ediff-show-patch-diagnostics ()
301 (interactive)
302 (cond ((window-live-p ediff-window-A)
303 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
304 ((window-live-p ediff-window-B)
305 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
306 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
307
308(defun ediff-get-patch-buffer ()
309 "Obtain patch buffer. If patch is already in a buffer---use it.
310Else, read patch file into a new buffer."
311 (let ((dir (cond (ediff-patch-default-directory) ; try patch default dir
312 (ediff-use-last-dir ediff-last-dir-patch)
313 (t default-directory)))
314 patch-buf)
315 (if (y-or-n-p "Is the patch already in a buffer? ")
316 (setq patch-buf
317 (get-buffer
318 (read-buffer
319 "Which buffer contains the patch? "
320 (current-buffer) 'must-match)))
321 (setq patch-buf
322 (find-file-noselect
323 (read-file-name "Which file contains the patch? " dir))))
324
325 (ediff-eval-in-buffer patch-buf
326 (goto-char (point-min))
327 (or (ediff-get-visible-buffer-window patch-buf)
328 (progn
329 (pop-to-buffer patch-buf 'other-window)
330 (select-window (previous-window)))))
331 (ediff-map-patch-buffer patch-buf)
332 patch-buf))
333
334;; Dispatch the right patch file function: regular or meta-level,
335;; depending on how many patches are in the patch file.
336;; At present, there is no support for meta-level patches.
337;; Should return either the ctl buffer or the meta-buffer
338(defun ediff-dispatch-file-patching-job (patch-buf filename
339 &optional startup-hooks)
340 (ediff-eval-in-buffer patch-buf
341 ;; relativize names in the patch with respect to source-file
342 (ediff-fixup-patch-map filename)
343 (if (< (length ediff-patch-map) 2)
344 (ediff-patch-file-internal
345 patch-buf
346 (if (and (not (string-match "^/dev/null" (car (car ediff-patch-map))))
347 (> (length (car (car ediff-patch-map))) 1))
348 (car (car ediff-patch-map))
349 filename)
350 startup-hooks)
351 (ediff-multi-patch-internal patch-buf startup-hooks))
352 ))
353
354
355(defun ediff-patch-buffer-internal (patch-buf buf-to-patch-name
356 &optional startup-hooks)
357 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
358 (file-name-ok (if buf-to-patch (buffer-file-name buf-to-patch)))
359 (buf-mod-status (buffer-modified-p buf-to-patch))
360 (multifile-patch-p (> (length (ediff-eval-in-buffer patch-buf
361 ediff-patch-map)) 1))
362 default-dir file-name ctl-buf)
363 (if file-name-ok
364 (setq file-name file-name-ok)
365 (if multifile-patch-p
366 (error
367 "Can't apply multi-file patches to buffers that visit no files"))
368 (ediff-eval-in-buffer buf-to-patch
369 (setq default-dir default-directory)
370 (setq file-name (ediff-make-temp-file buf-to-patch))
371 (set-visited-file-name file-name)
372 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
373 ;;don't confuse the user with a new bufname
374 (rename-buffer buf-to-patch-name)
375 (set-buffer-modified-p nil)
376 (set-visited-file-modtime) ; sync buffer and temp file
377 (setq default-directory default-dir)
378 ))
379
380 ;; dispatch a patch function
381 (setq ctl-buf (ediff-dispatch-file-patching-job
382 patch-buf file-name startup-hooks))
383
384 (if file-name-ok
385 ()
386 ;; buffer wasn't visiting any file,
387 ;; so we will not run meta-level ediff here
388 (ediff-eval-in-buffer ctl-buf
389 (delete-file (buffer-file-name ediff-buffer-A))
390 (delete-file (buffer-file-name ediff-buffer-B))
391 (ediff-eval-in-buffer ediff-buffer-A
392 (if default-dir (setq default-directory default-dir))
393 (set-visited-file-name nil)
394 (rename-buffer buf-to-patch-name)
395 (set-buffer-modified-p buf-mod-status))
396 (ediff-eval-in-buffer ediff-buffer-B
397 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
398 (if default-dir (setq default-directory default-dir))
399 (set-visited-file-name nil)
400 (rename-buffer (ediff-unique-buffer-name
401 (concat buf-to-patch-name "_patched") ""))
402 (set-buffer-modified-p t))))
403 ))
404
405(defun ediff-patch-file-internal (patch-buf source-filename
406 &optional startup-hooks)
407 (setq source-filename (expand-file-name source-filename))
408
409 (let* ((backup-extension
410 ;; if the user specified a -b option, extract the backup
411 ;; extension from there; else use ediff-backup-extension
412 (substring ediff-patch-options
413 (if (string-match "-b[ \t]+" ediff-patch-options)
414 (match-end 0) 0)
415 (if (string-match "-b[ \t]+[^ \t]+" ediff-patch-options)
416 (match-end 0) 0)))
417 (shell-file-name ediff-shell)
418 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
419 ;; ediff-find-file may use a temp file to do the patch
420 ;; so, we save source-filename and true-source-filename as a var
421 ;; that initially is source-filename but may be changed to a temp
422 ;; file for the purpose of patching.
423 (true-source-filename source-filename)
424 (target-filename source-filename)
425 target-buf buf-to-patch file-name-magic-p ctl-buf backup-style)
426
427 ;; if the user didn't specify a backup extension, use
428 ;; ediff-backup-extension
429 (if (string= backup-extension "")
430 (setq backup-extension ediff-backup-extension))
431 (if (string-match "-V" ediff-patch-options)
432 (error
433 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
434
435 ;; Make a temp file, if source-filename has a magic file handler (or if
436 ;; it is handled via auto-mode-alist and similar magic).
437 ;; Check if there is a buffer visiting source-filename and if they are in
438 ;; sync; arrange for the deletion of temp file.
439 (ediff-find-file 'true-source-filename 'buf-to-patch
440 'ediff-last-dir-patch 'startup-hooks)
441
442 ;; Check if source file name has triggered black magic, such as file name
443 ;; handlers or auto mode alist, and make a note of it.
444 ;; true-source-filename should be either the original name or a
445 ;; temporary file where we put the after-product of the file handler.
446 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
447 (file-truename source-filename))))
448
449 ;; Checkout orig file, if necessary, so that the patched file could be
450 ;; checked back in.
451 (if (ediff-file-checked-in-p (buffer-file-name buf-to-patch))
452 (ediff-toggle-read-only buf-to-patch))
453
454 (ediff-eval-in-buffer patch-diagnostics
455 (insert-buffer patch-buf)
456 (message "Applying patch ... ")
457 ;; fix environment for gnu patch, so it won't make numbered extensions
458 (setq backup-style (getenv "VERSION_CONTROL"))
459 (setenv "VERSION_CONTROL" nil)
460 ;; always pass patch the -f option, so it won't ask any questions
461 (shell-command-on-region
462 (point-min) (point-max)
463 (format "%s -f %s -b %s %s"
464 ediff-patch-program ediff-patch-options
465 backup-extension
466 (expand-file-name true-source-filename))
467 t)
468 ;; restore environment for gnu patch
469 (setenv "VERSION_CONTROL" backup-style))
470
471 (message "Applying patch ... done")
472 (message "")
473
474 (switch-to-buffer patch-diagnostics)
475 (sit-for 0) ; synchronize - let the user see diagnostics
476
477 (or (file-exists-p (concat true-source-filename backup-extension))
478 (error "Patch appears to have failed"))
479
480 ;; If black magic is involved, apply patch to a temp copy of the
481 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
482 ;; to temp copy, we name the result old-name_patched for local files
483 ;; and temp-copy_patched for remote files. The orig file name isn't
484 ;; changed, and the temp copy of the original is later deleted.
485 ;; Without magic, the original file is renamed (usually into
486 ;; old-name_orig) and the result of patching will have the same name as
487 ;; the original.
488 (if (not file-name-magic-p)
489 (ediff-eval-in-buffer buf-to-patch
490 (set-visited-file-name (concat source-filename backup-extension))
491 (set-buffer-modified-p nil))
492
493 ;; Black magic in effect.
494 ;; If orig file was remote, put the patched file in the temp directory.
495 ;; If orig file is local, put the patched file in the directory of
496 ;; the orig file.
497 (setq target-filename
498 (concat
499 (if (ediff-file-remote-p (file-truename source-filename))
500 true-source-filename
501 source-filename)
502 "_patched"))
503
504 (rename-file true-source-filename target-filename t)
505
506 ;; arrange that the temp copy of orig will be deleted
507 (rename-file (concat true-source-filename backup-extension)
508 true-source-filename t))
509
510 ;; make orig buffer read-only
511 (setq startup-hooks
512 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
513
514 ;; set up a buf for the patched file
515 (setq target-buf (find-file-noselect target-filename))
516
517 (setq ctl-buf
518 (ediff-buffers-internal
519 buf-to-patch target-buf nil
520 startup-hooks 'epatch))
521 (ediff-eval-in-buffer ctl-buf
522 (setq ediff-patchbufer patch-buf
523 ediff-patch-diagnostics patch-diagnostics))
524
525 (bury-buffer patch-diagnostics)
526 (message "Type `P', if you need to see patch diagnostics")
527 ctl-buf))
528
529(defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
530 (let (meta-buf)
531 (setq startup-hooks
532 ;; this sets various vars in the meta buffer inside
533 ;; ediff-prepare-meta-buffer
534 (cons (` (lambda ()
535 ;; tell what to do if the user clicks on a session record
536 (setq ediff-session-action-function
537 'ediff-patch-file-form-meta
538 ediff-meta-patchbufer patch-buf)
539 ))
540 startup-hooks))
541 (setq meta-buf (ediff-prepare-meta-buffer
542 'ediff-filegroup-action
543 (ediff-eval-in-buffer patch-buf
544 ;; nil replaces a regular expression
545 (cons (list nil (format "%S" patch-buf))
546 ediff-patch-map))
547 "*Ediff Session Group Panel"
548 'ediff-redraw-directory-group-buffer
549 'ediff-multifile-patch
550 startup-hooks))
551 (ediff-show-meta-buffer meta-buf)
552 ))
553
554
555
556
557;;; Local Variables:
558;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
559;;; eval: (put 'ediff-eval-in-buffer 'lisp-indent-hook 1)
560;;; End:
561
562(provide 'ediff-ptch)
563
564;;; ediff-ptch.el ends here