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