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)
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.
28 (defvar rcs-default-co-switches
)
32 (defvar cvs-cookie-handle
)
33 (defvar ediff-temp-file-prefix
)
37 (load "pcl-cvs" 'noerror
)
39 (load "generic-sc" 'noerror
)
40 (load "vc" 'noerror
)))
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
)
51 (vc-version-other-window rev1
)
52 (setq rev1buf
(current-buffer)
53 file1
(buffer-file-name)))
55 (or (string= rev2
"") ; use current buffer
56 (vc-version-other-window rev2
))
57 (setq rev2buf
(current-buffer)
58 file2
(buffer-file-name)))
61 (delete-file (, file1
))
62 (or (, (string= rev2
"")) (delete-file (, file2
)))
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
)
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
)))))
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
)))
103 (setq buffer-read-only nil
104 default-directory
(file-name-directory (expand-file-name file
)))
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
"")
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"))
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
)
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)))
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
150 &optional startup-hooks merge-buffer-file
)
151 ;; If ANCESTOR-REV non-nil, merge with ancestor
152 (let (buf1 buf2 ancestor-buf
)
154 (vc-version-other-window rev1
)
155 (setq buf1
(current-buffer)))
157 (or (string= rev2
"")
158 (vc-version-other-window rev2
))
159 (setq buf2
(current-buffer)))
162 (or (string= ancestor-rev
"")
163 (vc-version-other-window ancestor-rev
))
164 (setq ancestor-buf
(current-buffer))))
168 (delete-file (, (buffer-file-name buf1
)))
169 (or (, (string= rev2
""))
170 (delete-file (, (buffer-file-name buf2
))))
171 (or (, (string= ancestor-rev
""))
172 (, (not ancestor-rev
))
173 (delete-file (, (buffer-file-name ancestor-buf
))))
177 (ediff-merge-buffers-with-ancestor
178 buf1 buf2 ancestor-buf
179 startup-hooks
'ediff-merge-revisions-with-ancestor merge-buffer-file
)
181 buf1 buf2 startup-hooks
'ediff-merge-revisions merge-buffer-file
))
184 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
186 startup-hooks merge-buffer-file
)
187 ;; If ANCESTOR-REV non-nil, merge with ancestor
188 (let (buf1 buf2 ancestor-buf
)
189 (setq buf1
(rcs-ediff-view-revision rev1
)
190 buf2
(if (string= rev2
"")
192 (rcs-ediff-view-revision rev2
))
193 ancestor-buf
(if ancestor-rev
194 (if (string= ancestor-rev
"")
196 (rcs-ediff-view-revision ancestor-rev
))))
197 ;; rcs.el doesn't create temp version files, so we don't have to delete
198 ;; anything in startup hooks to ediff-buffers
200 (ediff-merge-buffers-with-ancestor
201 buf1 buf2 ancestor-buf
202 startup-hooks
'ediff-merge-revisions-with-ancestor merge-buffer-file
)
204 buf1 buf2 startup-hooks
'ediff-merge-revisions merge-buffer-file
))))
206 (defun ediff-generic-sc-merge-internal (rev1 rev2 ancestor-rev
208 startup-hooks merge-buffer-file
)
209 ;; If ANCESTOR-REV non-nil, merge with ancestor
210 (let (buf1 buf2 ancestor-buf
)
212 (if (string= rev1
"")
213 (setq rev1
(generic-sc-get-latest-rev)))
214 (sc-visit-previous-revision rev1
)
215 (setq buf1
(current-buffer)))
217 (or (string= rev2
"")
218 (sc-visit-previous-revision rev2
))
219 (setq buf2
(current-buffer)))
222 (or (string= ancestor-rev
"")
223 (sc-visit-previous-revision ancestor-rev
))
224 (setq ancestor-buf
(current-buffer))))
226 (ediff-merge-buffers-with-ancestor
227 buf1 buf2 ancestor-buf
228 startup-hooks
'ediff-merge-revisions-with-ancestor merge-buffer-file
)
230 buf1 buf2 startup-hooks
'ediff-merge-revisions merge-buffer-file
))))
233 ;; PCL-CVS.el support
235 (defun ediff-pcl-cvs-internal (rev1 rev2
&optional startup-hooks
)
236 ;; Run Ediff on a pair of revisions of the current buffer.
237 ;; If REV1 is "", use the latest revision.
238 ;; If REV2 is "", use the current buffer as the second file to compare.
239 (let ((orig-buf (current-buffer))
240 orig-file-name buf1 buf2 file1 file2
)
242 (or (setq orig-file-name
(buffer-file-name (current-buffer)))
243 (error "Current buffer is not visiting any file"))
244 (if (string= rev1
"") (setq rev1 nil
)) ; latest revision
245 (setq buf1
(ediff-pcl-cvs-view-revision orig-file-name rev1
)
246 buf2
(if (string= rev2
"")
248 (ediff-pcl-cvs-view-revision orig-file-name rev2
))
249 file1
(buffer-file-name buf1
)
250 file2
(buffer-file-name buf2
))
253 (delete-file (, file1
))
254 (or (, (string= rev2
"")) (delete-file (, file2
)))
257 (ediff-buffers buf1 buf2 startup-hooks
'ediff-revision
)))
259 ;; This function is the standard Ediff's interface to pcl-cvs.
260 ;; Works like with other interfaces: runs ediff on versions of the file in the
262 (defun ediff-pcl-cvs-merge-internal (rev1 rev2 ancestor-rev
264 startup-hooks merge-buffer-file
)
265 ;; Ediff-merge appropriate revisions of the selected file.
266 ;; If REV1 is "" then use the latest revision.
267 ;; If REV2 is "" then merge current buffer's file with REV1.
268 ;; If ANCESTOR-REV is "" then use current buffer's file as ancestor.
269 ;; If ANCESTOR-REV is nil, then merge without the ancestor.
270 (let ((orig-buf (current-buffer))
271 orig-file-name buf1 buf2 ancestor-buf
)
273 (or (setq orig-file-name
(buffer-file-name (current-buffer)))
274 (error "Current buffer is not visiting any file"))
275 (if (string= rev1
"") (setq rev1 nil
)) ; latest revision
277 (setq buf1
(ediff-pcl-cvs-view-revision orig-file-name rev1
))
278 (setq buf2
(if (string= rev2
"")
280 (ediff-pcl-cvs-view-revision orig-file-name rev2
)))
281 (if (stringp ancestor-rev
)
283 (if (string= ancestor-rev
"")
285 (ediff-pcl-cvs-view-revision orig-file-name ancestor-rev
))))
290 (delete-file (, (buffer-file-name buf1
)))
291 (or (, (string= rev2
""))
292 (delete-file (, (buffer-file-name buf2
))))
293 (or (, (string= ancestor-rev
""))
294 (, (not ancestor-rev
))
295 (delete-file (, (buffer-file-name ancestor-buf
))))
300 (ediff-merge-buffers-with-ancestor
301 buf1 buf2 ancestor-buf startup-hooks
302 'ediff-merge-revisions-with-ancestor merge-buffer-file
)
304 buf1 buf2 startup-hooks
'ediff-merge-revisions merge-buffer-file
))
307 (defun ediff-pcl-cvs-view-revision (file rev
)
308 ;; if rev = "", get the latest revision
309 (let ((temp-name (make-temp-name
310 (concat ediff-temp-file-prefix
312 (cvs-kill-buffer-visiting temp-name
)
314 (message "Retrieving revision %s..." rev
)
315 (message "Retrieving latest revision..."))
316 (let ((res (call-process cvs-shell nil nil nil
"-c"
317 (concat cvs-program
" update -p "
319 (concat "-r " rev
" ")
323 (if (and res
(not (and (integerp res
) (zerop res
))))
324 (error "Failed to retrieve revision: %s" res
))
327 (message "Retrieving revision %s... Done." rev
)
328 (message "Retrieving latest revision... Done."))
329 (find-file-noselect temp-name
))))
332 (defun cvs-run-ediff-on-file-descriptor (tin)
333 ;; This is a replacement for cvs-emerge-mode
334 ;; Runs after cvs-update.
335 ;; Ediff-merge appropriate revisions of the selected file.
336 (let* ((fileinfo (tin-cookie cvs-cookie-handle tin
))
337 (type (cvs-fileinfo->type fileinfo
))
339 (cvs-retrieve-revision-to-tmpfile fileinfo
))
341 (file-name-as-directory (cvs-fileinfo->dir fileinfo
)))
344 (or (memq type
'(MERGED CONFLICT MODIFIED
))
346 "Can only merge `Modified', `Merged' or `Conflict' files"))
348 (cond ((memq type
'(MERGED CONFLICT
))
350 (cvs-retrieve-revision-to-tmpfile
353 (cvs-fileinfo->base-revision fileinfo
)))
354 (ediff-merge-buffers-with-ancestor
355 (find-file-noselect tmp-file
)
356 (find-file-noselect (cvs-fileinfo->backup-file fileinfo
))
357 (find-file-noselect ancestor-file
)
359 'ediff-merge-revisions-with-ancestor
))
362 (find-file-noselect tmp-file
)
363 (find-file-noselect (cvs-fileinfo->full-path fileinfo
))
366 (if (stringp tmp-file
) (delete-file tmp-file
))
367 (if (stringp ancestor-file
) (delete-file ancestor-file
))))
370 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
371 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
372 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
375 (provide 'ediff-vers
)
377 ;;; ediff-vers.el ends here