new version
[emacs.git] / lisp / ediff-vers.el
blob00b7b41f51ef0abad6740d779c178bb43fb15825
1 ;;; ediff-vers.el --- version control interface to Ediff
3 ;;; Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
7 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Code:
27 ;; Compiler pacifier
28 (defvar rcs-default-co-switches)
29 (defvar sc-mode)
30 (defvar cvs-shell)
31 (defvar cvs-program)
32 (defvar cvs-cookie-handle)
33 (defvar ediff-temp-file-prefix)
35 (and noninteractive
36 (eval-when-compile
37 (load "pcl-cvs" 'noerror)
38 (load "rcs" 'noerror)
39 (load "generic-sc" 'noerror)
40 (load "vc" 'noerror)))
41 ;; end pacifier
43 ;; VC.el support
44 (defun ediff-vc-internal (rev1 rev2 &optional startup-hooks)
45 ;; Run Ediff on versions of the current buffer.
46 ;; If REV2 is "" then compare current buffer with REV1.
47 ;; If the current buffer is named `F', the version is named `F.~REV~'.
48 ;; If `F.~REV~' already exists, it is used instead of being re-created.
49 (let (file1 file2 rev1buf rev2buf)
50 (save-excursion
51 (vc-version-other-window rev1)
52 (setq rev1buf (current-buffer)
53 file1 (buffer-file-name)))
54 (save-excursion
55 (or (string= rev2 "") ; use current buffer
56 (vc-version-other-window rev2))
57 (setq rev2buf (current-buffer)
58 file2 (buffer-file-name)))
59 (setq startup-hooks
60 (cons (` (lambda ()
61 (delete-file (, file1))
62 (or (, (string= rev2 "")) (delete-file (, file2)))
64 startup-hooks))
65 (ediff-buffers
66 rev1buf rev2buf
67 startup-hooks
68 'ediff-revision)))
70 ;; RCS.el support
71 (defun rcs-ediff-view-revision (&optional rev)
72 ;; View previous RCS revision of current file.
73 ;; With prefix argument, prompts for a revision name.
74 (interactive (list (if current-prefix-arg
75 (read-string "Revision: "))))
76 (let* ((filename (buffer-file-name (current-buffer)))
77 (switches (append '("-p")
78 (if rev (list (concat "-r" rev)) nil)))
79 (buff (concat (file-name-nondirectory filename) ".~" rev "~")))
80 (message "Working ...")
81 (setq filename (expand-file-name filename))
82 (with-output-to-temp-buffer buff
83 (let ((output-buffer (ediff-rcs-get-output-buffer filename buff)))
84 (delete-windows-on output-buffer)
85 (save-excursion
86 (set-buffer output-buffer)
87 (apply 'call-process "co" nil t nil
88 ;; -q: quiet (no diagnostics)
89 (append switches rcs-default-co-switches
90 (list "-q" filename)))))
91 (message "")
92 buff)))
94 (defun ediff-rcs-get-output-buffer (file name)
95 ;; Get a buffer for RCS output for FILE, make it writable and clean it up.
96 ;; Optional NAME is name to use instead of `*RCS-output*'.
97 ;; This is a modified version from rcs.el v1.1. I use it here to make
98 ;; Ediff immune to changes in rcs.el
99 (let* ((default-major-mode 'fundamental-mode) ; no frills!
100 (buf (get-buffer-create name)))
101 (save-excursion
102 (set-buffer buf)
103 (setq buffer-read-only nil
104 default-directory (file-name-directory (expand-file-name file)))
105 (erase-buffer))
106 buf))
108 (defun ediff-rcs-internal (rev1 rev2 &optional startup-hooks)
109 ;; Run Ediff on versions of the current buffer.
110 ;; If REV2 is "" then use current buffer.
111 (let ((rev2buf (if (string= rev2 "")
112 (current-buffer)
113 (rcs-ediff-view-revision rev2)))
114 (rev1buf (rcs-ediff-view-revision rev1)))
116 ;; rcs.el doesn't create temp version files, so we don't have to delete
117 ;; anything in startup hooks to ediff-buffers
118 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)
122 ;; GENERIC-SC.el support
124 (defun generic-sc-get-latest-rev ()
125 (cond ((eq sc-mode 'CCASE)
126 (eval "main/LATEST"))
127 (t (eval ""))))
129 (defun ediff-generic-sc-internal (rev1 rev2 &optional startup-hooks)
130 ;; Run Ediff on versions of the current buffer.
131 ;; If REV2 is "" then compare current buffer with REV1.
132 ;; If the current buffer is named `F', the version is named `F.~REV~'.
133 ;; If `F.~REV~' already exists, it is used instead of being re-created.
134 (let (rev1buf rev2buf)
135 (save-excursion
136 (if (or (not rev1) (string= rev1 ""))
137 (setq rev1 (generic-sc-get-latest-rev)))
138 (sc-visit-previous-revision rev1)
139 (setq rev1buf (current-buffer)))
140 (save-excursion
141 (or (string= rev2 "") ; use current buffer
142 (sc-visit-previous-revision rev2))
143 (setq rev2buf (current-buffer)))
144 (ediff-buffers rev1buf rev2buf startup-hooks 'ediff-revision)))
147 ;;; Merge with Version Control
149 (defun ediff-vc-merge-internal (rev1 rev2 ancestor-rev &optional startup-hooks)
150 ;; If ANCESTOR-REV non-nil, merge with ancestor
151 (let (buf1 buf2 ancestor-buf)
152 (save-excursion
153 (vc-version-other-window rev1)
154 (setq buf1 (current-buffer)))
155 (save-excursion
156 (or (string= rev2 "")
157 (vc-version-other-window rev2))
158 (setq buf2 (current-buffer)))
159 (if ancestor-rev
160 (save-excursion
161 (or (string= ancestor-rev "")
162 (vc-version-other-window ancestor-rev))
163 (setq ancestor-buf (current-buffer))))
164 (setq startup-hooks
165 (cons
166 (` (lambda ()
167 (delete-file (, (buffer-file-name buf1)))
168 (or (, (string= rev2 ""))
169 (delete-file (, (buffer-file-name buf2))))
170 (or (, (string= ancestor-rev ""))
171 (, (not ancestor-rev))
172 (delete-file (, (buffer-file-name ancestor-buf))))
174 startup-hooks))
175 (if ancestor-rev
176 (ediff-merge-buffers-with-ancestor
177 buf1 buf2 ancestor-buf
178 startup-hooks 'ediff-merge-revisions-with-ancestor)
179 (ediff-merge-buffers buf1 buf2 startup-hooks 'ediff-merge-revisions))
182 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
183 &optional startup-hooks)
184 ;; If ANCESTOR-REV non-nil, merge with ancestor
185 (let (buf1 buf2 ancestor-buf)
186 (setq buf1 (rcs-ediff-view-revision rev1)
187 buf2 (if (string= rev2 "")
188 (current-buffer)
189 (rcs-ediff-view-revision rev2))
190 ancestor-buf (if ancestor-rev
191 (if (string= ancestor-rev "")
192 (current-buffer)
193 (rcs-ediff-view-revision ancestor-rev))))
194 ;; rcs.el doesn't create temp version files, so we don't have to delete
195 ;; anything in startup hooks to ediff-buffers
196 (if ancestor-rev
197 (ediff-merge-buffers-with-ancestor
198 buf1 buf2 ancestor-buf
199 startup-hooks 'ediff-merge-revisions-with-ancestor)
200 (ediff-merge-buffers buf1 buf2 startup-hooks 'ediff-merge-revisions))))
202 (defun ediff-generic-sc-merge-internal (rev1 rev2 ancestor-rev
203 &optional startup-hooks)
204 ;; If ANCESTOR-REV non-nil, merge with ancestor
205 (let (buf1 buf2 ancestor-buf)
206 (save-excursion
207 (if (string= rev1 "")
208 (setq rev1 (generic-sc-get-latest-rev)))
209 (sc-visit-previous-revision rev1)
210 (setq buf1 (current-buffer)))
211 (save-excursion
212 (or (string= rev2 "")
213 (sc-visit-previous-revision rev2))
214 (setq buf2 (current-buffer)))
215 (if ancestor-rev
216 (save-excursion
217 (or (string= ancestor-rev "")
218 (sc-visit-previous-revision ancestor-rev))
219 (setq ancestor-buf (current-buffer))))
220 (if ancestor-rev
221 (ediff-merge-buffers-with-ancestor
222 buf1 buf2 ancestor-buf
223 startup-hooks 'ediff-merge-revisions-with-ancestor)
224 (ediff-merge-buffers buf1 buf2 startup-hooks 'ediff-merge-revisions))))
227 ;; PCL-CVS.el support
229 (defun ediff-pcl-cvs-internal (rev1 rev2 &optional startup-hooks)
230 ;; Run Ediff on a pair of revisions of the current buffer.
231 ;; If REV1 is "", use the latest revision.
232 ;; If REV2 is "", use the current buffer as the second file to compare.
233 (let ((orig-buf (current-buffer))
234 orig-file-name buf1 buf2 file1 file2)
236 (or (setq orig-file-name (buffer-file-name (current-buffer)))
237 (error "Current buffer is not visiting any file"))
238 (if (string= rev1 "") (setq rev1 nil)) ; latest revision
239 (setq buf1 (ediff-pcl-cvs-view-revision orig-file-name rev1)
240 buf2 (if (string= rev2 "")
241 orig-buf
242 (ediff-pcl-cvs-view-revision orig-file-name rev2))
243 file1 (buffer-file-name buf1)
244 file2 (buffer-file-name buf2))
245 (setq startup-hooks
246 (cons (` (lambda ()
247 (delete-file (, file1))
248 (or (, (string= rev2 "")) (delete-file (, file2)))
250 startup-hooks))
251 (ediff-buffers buf1 buf2 startup-hooks 'ediff-revision)))
253 ;; This function is the standard Ediff's interface to pcl-cvs.
254 ;; Works like with other interfaces: runs ediff on versions of the file in the
255 ;; current buffer.
256 (defun ediff-pcl-cvs-merge-internal (rev1 rev2 ancestor-rev
257 &optional startup-hooks)
258 ;; Ediff-merge appropriate revisions of the selected file.
259 ;; If REV1 is "" then use the latest revision.
260 ;; If REV2 is "" then merge current buffer's file with REV1.
261 ;; If ANCESTOR-REV is "" then use current buffer's file as ancestor.
262 ;; If ANCESTOR-REV is nil, then merge without the ancestor.
263 (let ((orig-buf (current-buffer))
264 orig-file-name buf1 buf2 ancestor-buf)
266 (or (setq orig-file-name (buffer-file-name (current-buffer)))
267 (error "Current buffer is not visiting any file"))
268 (if (string= rev1 "") (setq rev1 nil)) ; latest revision
270 (setq buf1 (ediff-pcl-cvs-view-revision orig-file-name rev1))
271 (setq buf2 (if (string= rev2 "")
272 orig-buf
273 (ediff-pcl-cvs-view-revision orig-file-name rev2)))
274 (if (stringp ancestor-rev)
275 (setq ancestor-buf
276 (if (string= ancestor-rev "")
277 orig-buf
278 (ediff-pcl-cvs-view-revision orig-file-name ancestor-rev))))
280 (setq startup-hooks
281 (cons
282 (` (lambda ()
283 (delete-file (, (buffer-file-name buf1)))
284 (or (, (string= rev2 ""))
285 (delete-file (, (buffer-file-name buf2))))
286 (or (, (string= ancestor-rev ""))
287 (, (not ancestor-rev))
288 (delete-file (, (buffer-file-name ancestor-buf))))
290 startup-hooks))
292 (if ancestor-buf
293 (ediff-merge-buffers-with-ancestor
294 buf1 buf2 ancestor-buf startup-hooks
295 'ediff-merge-revisions-with-ancestor)
296 (ediff-merge-buffers
297 buf1 buf2 startup-hooks 'ediff-merge-revisions))
300 (defun ediff-pcl-cvs-view-revision (file rev)
301 ;; if rev = "", get the latest revision
302 (let ((temp-name (make-temp-name
303 (concat ediff-temp-file-prefix
304 "ediff_" rev))))
305 (cvs-kill-buffer-visiting temp-name)
306 (if rev
307 (message "Retrieving revision %s..." rev)
308 (message "Retrieving latest revision..."))
309 (let ((res (call-process cvs-shell nil nil nil "-c"
310 (concat cvs-program " update -p "
311 (if rev
312 (concat "-r " rev " ")
314 file
315 " > " temp-name))))
316 (if (and res (not (and (integerp res) (zerop res))))
317 (error "Failed to retrieve revision: %s" res))
319 (if rev
320 (message "Retrieving revision %s... Done." rev)
321 (message "Retrieving latest revision... Done."))
322 (find-file-noselect temp-name))))
325 (defun cvs-run-ediff-on-file-descriptor (tin)
326 ;; This is a replacement for cvs-emerge-mode
327 ;; Run after cvs-update.
328 ;; Ediff-merge appropriate revisions of the selected file.
329 (let* ((fileinfo (tin-cookie cvs-cookie-handle tin))
330 (type (cvs-fileinfo->type fileinfo))
331 (tmp-file
332 (cvs-retrieve-revision-to-tmpfile fileinfo))
333 ancestor-file)
335 (or (memq type '(MERGED CONFLICT MODIFIED))
336 (error
337 "Can only merge `Modified', `Merged' or `Conflict' files"))
339 (cond ((memq type '(MERGED CONFLICT))
340 (setq ancestor-file
341 (cvs-retrieve-revision-to-tmpfile
342 fileinfo
343 ;; revision
344 (cvs-fileinfo->base-revision fileinfo)))
345 (ediff-merge-buffers-with-ancestor
346 (find-file-noselect tmp-file)
347 (find-file-noselect (cvs-fileinfo->backup-file fileinfo))
348 (find-file-noselect ancestor-file)
349 nil ; startup-hooks
350 'ediff-merge-revisions-with-ancestor))
351 ((eq type 'MODIFIED)
352 (ediff-merge-buffers
353 (find-file-noselect tmp-file)
354 (find-file-noselect (cvs-fileinfo->full-path fileinfo))
355 nil ; startup-hooks
356 'ediff-merge-revisions)))
357 (if (stringp tmp-file) (delete-file tmp-file))
358 (if (stringp ancestor-file) (delete-file ancestor-file))))
360 ;;; Local Variables:
361 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
362 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
363 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
364 ;;; End:
366 (provide 'ediff-vers)
368 ;;; ediff-vers.el ends here