*** empty log message ***
[emacs.git] / lisp / ediff-vers.el
blobe73386b180f3b0d0be523051793c5abacf86956c
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
150 &optional startup-hooks merge-buffer-file)
151 ;; If ANCESTOR-REV non-nil, merge with ancestor
152 (let (buf1 buf2 ancestor-buf)
153 (save-excursion
154 (vc-version-other-window rev1)
155 (setq buf1 (current-buffer)))
156 (save-excursion
157 (or (string= rev2 "")
158 (vc-version-other-window rev2))
159 (setq buf2 (current-buffer)))
160 (if ancestor-rev
161 (save-excursion
162 (if (string= ancestor-rev "")
163 (setq ancestor-rev (vc-workfile-version buffer-file-name)))
164 (vc-version-other-window ancestor-rev)
165 (setq ancestor-buf (current-buffer))))
166 (setq startup-hooks
167 (cons
168 (` (lambda ()
169 (delete-file (, (buffer-file-name buf1)))
170 (or (, (string= rev2 ""))
171 (delete-file (, (buffer-file-name buf2))))
172 (or (, (string= ancestor-rev ""))
173 (, (not ancestor-rev))
174 (delete-file (, (buffer-file-name ancestor-buf))))
176 startup-hooks))
177 (if ancestor-rev
178 (ediff-merge-buffers-with-ancestor
179 buf1 buf2 ancestor-buf
180 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
181 (ediff-merge-buffers
182 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))
185 (defun ediff-rcs-merge-internal (rev1 rev2 ancestor-rev
186 &optional
187 startup-hooks merge-buffer-file)
188 ;; If ANCESTOR-REV non-nil, merge with ancestor
189 (let (buf1 buf2 ancestor-buf)
190 (setq buf1 (rcs-ediff-view-revision rev1)
191 buf2 (if (string= rev2 "")
192 (current-buffer)
193 (rcs-ediff-view-revision rev2))
194 ancestor-buf (if ancestor-rev
195 (if (string= ancestor-rev "")
196 (current-buffer)
197 (rcs-ediff-view-revision ancestor-rev))))
198 ;; rcs.el doesn't create temp version files, so we don't have to delete
199 ;; anything in startup hooks to ediff-buffers
200 (if ancestor-rev
201 (ediff-merge-buffers-with-ancestor
202 buf1 buf2 ancestor-buf
203 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
204 (ediff-merge-buffers
205 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
207 (defun ediff-generic-sc-merge-internal (rev1 rev2 ancestor-rev
208 &optional
209 startup-hooks merge-buffer-file)
210 ;; If ANCESTOR-REV non-nil, merge with ancestor
211 (let (buf1 buf2 ancestor-buf)
212 (save-excursion
213 (if (string= rev1 "")
214 (setq rev1 (generic-sc-get-latest-rev)))
215 (sc-visit-previous-revision rev1)
216 (setq buf1 (current-buffer)))
217 (save-excursion
218 (or (string= rev2 "")
219 (sc-visit-previous-revision rev2))
220 (setq buf2 (current-buffer)))
221 (if ancestor-rev
222 (save-excursion
223 (or (string= ancestor-rev "")
224 (sc-visit-previous-revision ancestor-rev))
225 (setq ancestor-buf (current-buffer))))
226 (if ancestor-rev
227 (ediff-merge-buffers-with-ancestor
228 buf1 buf2 ancestor-buf
229 startup-hooks 'ediff-merge-revisions-with-ancestor merge-buffer-file)
230 (ediff-merge-buffers
231 buf1 buf2 startup-hooks 'ediff-merge-revisions merge-buffer-file))))
234 ;; PCL-CVS.el support
237 (defun cvs-run-ediff-on-file-descriptor (tin)
238 ;; This is a replacement for cvs-emerge-mode
239 ;; Runs after cvs-update.
240 ;; Ediff-merge appropriate revisions of the selected file.
241 (let* ((fileinfo (tin-cookie cvs-cookie-handle tin))
242 (type (cvs-fileinfo->type fileinfo))
243 (tmp-file
244 (cvs-retrieve-revision-to-tmpfile fileinfo))
245 (default-directory
246 (file-name-as-directory (cvs-fileinfo->dir fileinfo)))
247 ancestor-file)
249 (or (memq type '(MERGED CONFLICT MODIFIED))
250 (error
251 "Can only merge `Modified', `Merged' or `Conflict' files"))
253 (cond ((memq type '(MERGED CONFLICT))
254 (setq ancestor-file
255 (cvs-retrieve-revision-to-tmpfile
256 fileinfo
257 ;; revision
258 (cvs-fileinfo->base-revision fileinfo)))
259 (ediff-merge-buffers-with-ancestor
260 (find-file-noselect tmp-file)
261 (find-file-noselect (cvs-fileinfo->backup-file fileinfo))
262 (find-file-noselect ancestor-file)
263 nil ; startup-hooks
264 'ediff-merge-revisions-with-ancestor))
265 ((eq type 'MODIFIED)
266 (ediff-buffers
267 (find-file-noselect tmp-file)
268 (find-file-noselect (cvs-fileinfo->full-path fileinfo))
269 nil ; startup-hooks
270 'ediff-revisions)))
271 (if (stringp tmp-file) (delete-file tmp-file))
272 (if (stringp ancestor-file) (delete-file ancestor-file))))
274 ;;; Local Variables:
275 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
276 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
277 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
278 ;;; End:
280 (provide 'ediff-vers)
282 ;;; ediff-vers.el ends here