1 ;;; vc-mcvs.el --- VC backend for the Meta-CVS version-control system
3 ;; Copyright (C) 1995,98,99,2000,01,02,2003 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Stefan Monnier <monnier@gnu.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; The home page of the Meta-CVS version control system is at
29 ;; http://users.footprints.net/~kaz/mcvs.html
31 ;; This is derived from vc-cvs.el as follows:
32 ;; - cp vc-cvs.el vc-mcvs.el
33 ;; - Replace CVS/ with MCVS/CVS/
34 ;; - Replace 'CVS with 'MCVS
35 ;; - Replace -cvs- with -mcvs-
36 ;; - Replace most of the rest of CVS to Meta-CVS
38 ;; Then of course started the hacking. Only a small part of the code
39 ;; has been touched and not much more than that was tested, so if
40 ;; you bump into a bug, don't be surprised: just report it to me.
42 ;; What has been partly tested:
43 ;; - C-x v v to start editing a file that was checked out with CVSREAD on.
44 ;; - C-x v v to commit a file
49 ;; - M-x vc-rename-file RET
53 ;; - VC-dired doesn't work.
57 (eval-when-compile (require 'vc
))
61 ;;; Customization options
64 (defcustom vc-mcvs-global-switches nil
65 "*Global switches to pass to any Meta-CVS command."
66 :type
'(choice (const :tag
"None" nil
)
67 (string :tag
"Argument String")
68 (repeat :tag
"Argument List"
74 (defcustom vc-mcvs-register-switches nil
75 "*Extra switches for registering a file into Meta-CVS.
76 A string or list of strings passed to the checkin program by
78 :type
'(choice (const :tag
"None" nil
)
79 (string :tag
"Argument String")
80 (repeat :tag
"Argument List"
86 (defcustom vc-mcvs-diff-switches nil
87 "*A string or list of strings specifying extra switches for cvs diff under VC."
88 :type
'(choice (const :tag
"None" nil
)
89 (string :tag
"Argument String")
90 (repeat :tag
"Argument List"
96 (defcustom vc-mcvs-header
(or (cdr (assoc 'MCVS vc-header-alist
))
98 "*Header keywords to be inserted by `vc-insert-headers'."
100 :type
'(repeat string
)
103 (defcustom vc-mcvs-use-edit vc-cvs-use-edit
104 "*Non-nil means to use `cvs edit' to \"check out\" a file.
105 This is only meaningful if you don't use the implicit checkout model
106 \(i.e. if you have $CVSREAD set)."
112 ;;; State-querying functions
115 ;;;###autoload (defun vc-mcvs-registered (file)
116 ;;;###autoload (let ((dir file))
117 ;;;###autoload (while (and (stringp dir)
118 ;;;###autoload (not (equal dir (setq dir (file-name-directory dir)))))
119 ;;;###autoload (setq dir (if (file-directory-p
120 ;;;###autoload (expand-file-name "MCVS/CVS" dir))
121 ;;;###autoload t (directory-file-name dir))))
122 ;;;###autoload (if (eq dir t)
123 ;;;###autoload (progn
124 ;;;###autoload (load "vc-mcvs")
125 ;;;###autoload (vc-mcvs-registered file)))))
127 (defun vc-mcvs-root (file)
128 "Return the root directory of a Meta-CVS project, if any."
129 (or (vc-file-getprop file
'mcvs-root
)
134 (equal file
(setq file
(file-name-directory file
)))))
135 (if (file-directory-p (expand-file-name "MCVS/CVS" file
))
137 (setq file
(directory-file-name file
))))
140 (defun vc-mcvs-read (file)
141 (if (file-readable-p file
)
143 (insert-file-contents file
)
144 (goto-char (point-min))
145 (read (current-buffer)))))
147 (defun vc-mcvs-map-file (dir file
)
148 (let ((map (vc-mcvs-read (expand-file-name "MCVS/MAP" dir
)))
150 (dolist (x map inode
)
151 (if (equal (nth 2 x
) file
) (setq inode
(nth 1 x
))))))
153 (defun vc-mcvs-registered (file)
154 (let (root inode cvsfile
)
155 (when (and (setq root
(vc-mcvs-root file
))
156 (setq inode
(vc-mcvs-map-file
157 root
(file-relative-name file root
))))
158 (vc-file-setprop file
'mcvs-inode inode
)
159 ;; Avoid calling `mcvs diff' in vc-workfile-unchanged-p.
160 (vc-file-setprop file
'vc-checkout-time
161 (if (vc-cvs-registered
162 (setq cvsfile
(expand-file-name inode root
)))
163 (vc-file-getprop cvsfile
'vc-checkout-time
)
164 ;; The file might not be registered yet because
169 (defmacro vc-mcvs-cvs
(op file
&rest args
)
171 `(,(intern (concat "vc-cvs-" (symbol-name op
)))
172 (expand-file-name (vc-file-getprop ,file
'mcvs-inode
)
173 (vc-file-getprop ,file
'mcvs-root
))
176 (defun vc-mcvs-state (file)
177 ;; This would assume the Meta-CVS sandbox is synchronized.
178 ;; (vc-mcvs-cvs state file))
179 "Meta-CVS-specific version of `vc-state'."
180 (if (vc-stay-local-p file
)
181 (let ((state (vc-file-getprop file
'vc-state
)))
182 ;; If we should stay local, use the heuristic but only if
183 ;; we don't have a more precise state already available.
184 (if (memq state
'(up-to-date edited
))
185 (vc-mcvs-state-heuristic file
)
188 (cd (file-name-directory file
))
189 (vc-mcvs-command t
0 file
"status")
190 (vc-cvs-parse-status t
))))
193 (defalias 'vc-mcvs-state-heuristic
'vc-cvs-state-heuristic
)
195 (defun vc-mcvs-dir-state (dir)
196 "Find the Meta-CVS state of all files in DIR."
197 ;; if DIR is not under Meta-CVS control, don't do anything.
198 (when (file-readable-p (expand-file-name "MCVS/CVS/Entries" dir
))
199 (if (vc-stay-local-p dir
)
200 (vc-mcvs-dir-state-heuristic dir
)
201 (let ((default-directory dir
))
202 ;; Don't specify DIR in this command, the default-directory is
203 ;; enough. Otherwise it might fail with remote repositories.
205 (vc-mcvs-command t
0 nil
"status" "-l")
206 (goto-char (point-min))
207 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t
)
208 (narrow-to-region (match-beginning 0) (match-end 0))
209 (vc-cvs-parse-status)
210 (goto-char (point-max))
213 (defun vc-mcvs-workfile-version (file) (vc-mcvs-cvs workfile-version file
))
215 (defalias 'vc-mcvs-checkout-model
'vc-cvs-checkout-model
)
217 (defun vc-mcvs-mode-line-string (file)
218 (let ((s (vc-mcvs-cvs mode-line-string file
)))
219 (if s
(concat "M" s
))))
222 ;;; State-changing functions
225 (defun vc-mcvs-register (file &optional rev comment
)
226 "Register FILE into the Meta-CVS version-control system.
227 COMMENT can be used to provide an initial description of FILE.
229 `vc-register-switches' and `vc-mcvs-register-switches' are passed to
230 the Meta-CVS command (in that order)."
231 (let* ((filename (file-name-nondirectory file
))
232 (extpos (string-match "\\." filename
))
233 (ext (if extpos
(substring filename
(1+ extpos
))))
234 (root (vc-mcvs-root file
))
235 (types-file (expand-file-name "MCVS/TYPES" root
))
236 (map-file (expand-file-name "MCVS/MAP" root
))
237 (types (vc-mcvs-read types-file
)))
238 ;; Make sure meta files like MCVS/MAP are not read-only (happens with
239 ;; CVSREAD) since Meta-CVS doesn't pay attention to it at all and goes
241 (unless (file-writable-p map-file
)
242 (vc-checkout map-file t
))
243 (unless (or (file-writable-p types-file
) (not (file-exists-p types-file
)))
244 (vc-checkout types-file t
))
245 ;; Make sure the `mcvs add' will not fire up the CVSEDITOR
246 ;; to add a rule for the given file's extension.
247 (when (and ext
(not (assoc ext types
)))
248 (let ((type (completing-read "Type to use [default]: "
249 '("default" "name-only" "keep-old"
250 "binary" "value-only")
251 nil t nil nil
"default")))
252 (push (list ext
(make-symbol (upcase (concat ":" type
)))) types
)
253 (setq types
(sort types
(lambda (x y
) (string< (car x
) (car y
)))))
254 (with-current-buffer (find-file-noselect types-file
)
256 (pp types
(current-buffer))
258 (unless (get-buffer-window (current-buffer) t
)
259 (kill-buffer (current-buffer)))))))
261 (prog1 (apply 'vc-mcvs-command nil
0 file
263 (and comment
(string-match "[^\t\n ]" comment
)
264 (concat "-m" comment
))
265 (vc-switches 'MCVS
'register
))
266 ;; I'm not sure exactly why, but if we don't setup the inode and root
267 ;; prop of the file, things break later on in vc-mode-line that
268 ;; ends up calling vc-mcvs-workfile-version.
269 ;; We also need to set vc-checkout-time so that vc-workfile-unchanged-p
270 ;; doesn't try to call `mcvs diff' on the file.
271 (vc-mcvs-registered file
)))
273 (defalias 'vc-mcvs-responsible-p
'vc-mcvs-root
274 "Return non-nil if CVS thinks it is responsible for FILE.")
276 (defalias 'vc-cvs-could-register
'vc-cvs-responsible-p
277 "Return non-nil if FILE could be registered in Meta-CVS.
278 This is only possible if Meta-CVS is responsible for FILE's directory.")
280 (defun vc-mcvs-checkin (file rev comment
)
281 "Meta-CVS-specific version of `vc-backend-checkin'."
282 (unless (or (not rev
) (vc-mcvs-valid-version-number-p rev
))
283 (if (not (vc-mcvs-valid-symbolic-tag-name-p rev
))
284 (error "%s is not a valid symbolic tag name" rev
)
285 ;; If the input revision is a valid symbolic tag name, we create it
286 ;; as a branch, commit and switch to it.
287 (apply 'vc-mcvs-command nil
0 file
"tag" "-b" (list rev
))
288 (apply 'vc-mcvs-command nil
0 file
"update" "-r" (list rev
))
289 (vc-file-setprop file
'vc-mcvs-sticky-tag rev
)
291 ;; This commit might cvs-commit several files (e.g. MAP and TYPES)
292 ;; so using numbered revs here is dangerous and somewhat meaningless.
293 (when rev
(error "Cannot commit to a specific revision number"))
294 (let ((status (apply 'vc-mcvs-command nil
1 file
296 (vc-switches 'MCVS
'checkin
))))
298 (goto-char (point-min))
299 (when (not (zerop status
))
300 ;; Check checkin problem.
302 ((re-search-forward "Up-to-date check failed" nil t
)
303 (vc-file-setprop file
'vc-state
'needs-merge
)
304 (error (substitute-command-keys
305 (concat "Up-to-date check failed: "
306 "type \\[vc-next-action] to merge in changes"))))
308 (pop-to-buffer (current-buffer))
309 (goto-char (point-min))
310 (shrink-window-if-larger-than-buffer)
311 (error "Check-in failed"))))
312 ;; Update file properties
314 file
'vc-workfile-version
315 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
316 ;; Forget the checkout model of the file, because we might have
317 ;; guessed wrong when we found the file. After commit, we can
318 ;; tell it from the permissions of the file (see
319 ;; vc-mcvs-checkout-model).
320 (vc-file-setprop file
'vc-checkout-model nil
)
322 ;; if this was an explicit check-in (does not include creation of
323 ;; a branch), remove the sticky tag.
324 (if (and rev
(not (vc-mcvs-valid-symbolic-tag-name-p rev
)))
325 (vc-mcvs-command nil
0 file
"update" "-A"))))
327 (defun vc-mcvs-find-version (file rev buffer
)
328 (apply 'vc-mcvs-command
330 "-Q" ; suppress diagnostic output
332 (and rev
(not (string= rev
""))
335 (vc-switches 'MCVS
'checkout
)))
337 (defun vc-mcvs-checkout (file &optional editable rev
)
338 (message "Checking out %s..." file
)
339 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
340 (vc-call update file editable rev
(vc-switches 'MCVS
'checkout
)))
342 (message "Checking out %s...done" file
))
344 (defun vc-mcvs-update (file editable rev switches
)
345 (if (and (file-exists-p file
) (not rev
))
346 ;; If no revision was specified, just make the file writable
347 ;; if necessary (using `cvs-edit' if requested).
348 (and editable
(not (eq (vc-mcvs-checkout-model file
) 'implicit
))
350 (vc-mcvs-command nil
0 file
"edit")
351 (set-file-modes file
(logior (file-modes file
) 128))
352 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
353 ;; Check out a particular version (or recreate the file).
354 (vc-file-setprop file
'vc-workfile-version nil
)
355 (apply 'vc-mcvs-command nil
0 file
358 ;; default for verbose checkout: clear the sticky tag so
359 ;; that the actual update will get the head of the trunk
360 (if (or (not rev
) (string= rev
""))
365 (defun vc-mcvs-rename-file (old new
)
366 (vc-mcvs-command nil
0 new
"move" (file-relative-name old
)))
368 (defun vc-mcvs-revert (file &optional contents-done
)
369 "Revert FILE to the version it was based on."
370 (vc-default-revert file contents-done
)
371 (unless (eq (vc-checkout-model file
) 'implicit
)
373 (vc-mcvs-command nil
0 file
"unedit")
374 ;; Make the file read-only by switching off all w-bits
375 (set-file-modes file
(logand (file-modes file
) 3950)))))
377 (defun vc-mcvs-merge (file first-version
&optional second-version
)
378 "Merge changes into current working copy of FILE.
379 The changes are between FIRST-VERSION and SECOND-VERSION."
380 (vc-mcvs-command nil
0 file
382 (concat "-j" first-version
)
383 (concat "-j" second-version
))
384 (vc-file-setprop file
'vc-state
'edited
)
385 (with-current-buffer (get-buffer "*vc*")
386 (goto-char (point-min))
387 (if (re-search-forward "conflicts during merge" nil t
)
389 0))) ; signal success
391 (defun vc-mcvs-merge-news (file)
392 "Merge in any new changes made to FILE."
393 (message "Merging changes into %s..." file
)
394 ;; (vc-file-setprop file 'vc-workfile-version nil)
395 (vc-file-setprop file
'vc-checkout-time
0)
396 (vc-mcvs-command nil
0 file
"update")
397 ;; Analyze the merge result reported by Meta-CVS, and set
398 ;; file properties accordingly.
399 (with-current-buffer (get-buffer "*vc*")
400 (goto-char (point-min))
401 ;; get new workfile version
402 (if (re-search-forward
403 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t
)
404 (vc-file-setprop file
'vc-workfile-version
(match-string 1))
405 (vc-file-setprop file
'vc-workfile-version nil
))
408 (if (eq (buffer-size) 0)
409 0 ;; there were no news; indicate success
410 (if (re-search-forward
411 (concat "^\\([CMUP] \\)?"
413 "\\( already contains the differences between \\)?")
416 ;; Merge successful, we are in sync with repository now
417 ((or (match-string 2)
418 (string= (match-string 1) "U ")
419 (string= (match-string 1) "P "))
420 (vc-file-setprop file
'vc-state
'up-to-date
)
421 (vc-file-setprop file
'vc-checkout-time
422 (nth 5 (file-attributes file
)))
423 0);; indicate success to the caller
424 ;; Merge successful, but our own changes are still in the file
425 ((string= (match-string 1) "M ")
426 (vc-file-setprop file
'vc-state
'edited
)
427 0);; indicate success to the caller
428 ;; Conflicts detected!
430 (vc-file-setprop file
'vc-state
'edited
)
431 1);; signal the error to the caller
433 (pop-to-buffer "*vc*")
434 (error "Couldn't analyze mcvs update result")))
435 (message "Merging changes into %s...done" file
))))
438 ;;; History functions
441 (defun vc-mcvs-print-log (file)
442 "Get change log associated with FILE."
445 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
448 (defun vc-mcvs-diff (file &optional oldvers newvers
)
449 "Get a difference report using Meta-CVS between two versions of FILE."
450 (if (string= (vc-workfile-version file
) "0")
451 ;; This file is added but not yet committed; there is no master file.
452 (if (or oldvers newvers
)
453 (error "No revisions of %s exist" file
)
454 ;; We regard this as "changed".
455 ;; Diff it against /dev/null.
456 ;; Note: this is NOT a "mcvs diff".
457 (apply 'vc-do-command
"*vc-diff*"
459 (append (vc-switches nil
'diff
) '("/dev/null")))
460 ;; Even if it's empty, it's locally modified.
462 (let* ((async (and (vc-stay-local-p file
) (fboundp 'start-process
)))
464 (apply 'vc-mcvs-command
"*vc-diff*"
467 (and oldvers
(concat "-r" oldvers
))
468 (and newvers
(concat "-r" newvers
))
469 (vc-switches 'MCVS
'diff
))))
470 (if async
1 status
)))) ; async diff, pessimistic assumption.
472 (defun vc-mcvs-diff-tree (dir &optional rev1 rev2
)
473 "Diff all files at and below DIR."
474 (with-current-buffer "*vc-diff*"
475 (setq default-directory dir
)
476 (if (vc-stay-local-p dir
)
477 ;; local diff: do it filewise, and only for files that are modified
482 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f
)))
483 ;; possible optimization: fetch the state of all files
484 ;; in the tree via vc-mcvs-dir-state-heuristic
485 (unless (vc-up-to-date-p ',f
)
486 (message "Looking at %s" ',f
)
487 (vc-diff-internal ',f
',rev1
',rev2
))))))
488 ;; cvs diff: use a single call for the entire tree
489 (let ((coding-system-for-read
490 (or coding-system-for-read
'undecided
)))
491 (apply 'vc-mcvs-command
"*vc-diff*" 1 nil
"diff"
492 (and rev1
(concat "-r" rev1
))
493 (and rev2
(concat "-r" rev2
))
494 (vc-switches 'MCVS
'diff
))))))
496 (defun vc-mcvs-annotate-command (file buffer
&optional version
)
497 "Execute \"mcvs annotate\" on FILE, inserting the contents in BUFFER.
498 Optional arg VERSION is a version to annotate from."
501 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
502 file
"annotate" (if version
(concat "-r" version
))))
504 (defalias 'vc-mcvs-annotate-current-time
'vc-cvs-annotate-current-time
)
505 (defalias 'vc-mcvs-annotate-time
'vc-cvs-annotate-time
)
511 (defun vc-mcvs-create-snapshot (dir name branchp
)
512 "Assign to DIR's current version a given NAME.
513 If BRANCHP is non-nil, the name is created as a branch (and the current
514 workspace is immediately moved to that new branch)."
515 (vc-mcvs-command nil
0 dir
"tag" "-c" (if branchp
"-b") name
)
516 (when branchp
(vc-mcvs-command nil
0 dir
"update" "-r" name
)))
518 (defun vc-mcvs-retrieve-snapshot (dir name update
)
519 "Retrieve a snapshot at and below DIR.
520 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
521 If UPDATE is non-nil, then update (resynch) any affected buffers."
522 (with-current-buffer (get-buffer-create "*vc*")
523 (let ((default-directory dir
)
526 (if (or (not name
) (string= name
""))
527 (vc-mcvs-command t
0 nil
"update")
528 (vc-mcvs-command t
0 nil
"update" "-r" name
)
529 (setq sticky-tag name
))
531 (goto-char (point-min))
533 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
534 (let* ((file (expand-file-name (match-string 2) dir
))
535 (state (match-string 1))
536 (buffer (find-buffer-visiting file
)))
539 ((or (string= state
"U")
541 (vc-file-setprop file
'vc-state
'up-to-date
)
542 (vc-file-setprop file
'vc-workfile-version nil
)
543 (vc-file-setprop file
'vc-checkout-time
544 (nth 5 (file-attributes file
))))
545 ((or (string= state
"M")
547 (vc-file-setprop file
'vc-state
'edited
)
548 (vc-file-setprop file
'vc-workfile-version nil
)
549 (vc-file-setprop file
'vc-checkout-time
0)))
550 (vc-file-setprop file
'vc-mcvs-sticky-tag sticky-tag
)
551 (vc-resynch-buffer file t t
))))
552 (forward-line 1))))))
559 (defalias 'vc-mcvs-make-version-backups-p
'vc-stay-local-p
560 "Return non-nil if version backups should be made for FILE.")
561 (defalias 'vc-mcvs-check-headers
'vc-cvs-check-headers
)
565 ;;; Internal functions
568 (defun vc-mcvs-command (buffer okstatus file
&rest flags
)
569 "A wrapper around `vc-do-command' for use in vc-mcvs.el.
570 The difference to vc-do-command is that this function always invokes `mcvs',
571 and that it passes `vc-mcvs-global-switches' to it before FLAGS."
572 (let ((args (append '("--error-continue")
573 (if (stringp vc-mcvs-global-switches
)
574 (cons vc-mcvs-global-switches flags
)
575 (append vc-mcvs-global-switches
577 (if (member (car flags
) '("diff" "log"))
578 ;; We need to filter the output.
579 (vc-do-command buffer okstatus
"sh" nil
"-c"
582 'shell-quote-argument
583 (append (remq nil args
)
584 (if file
(list (file-relative-name file
))))
587 (apply 'vc-do-command buffer okstatus
"mcvs" file args
))))
589 (defun vc-mcvs-repository-hostname (dirname)
590 (vc-cvs-repository-hostname (vc-mcvs-root dirname
)))
592 (defun vc-mcvs-dir-state-heuristic (dir)
593 "Find the Meta-CVS state of all files in DIR, using only local information."
595 (vc-cvs-get-entries dir
)
596 (goto-char (point-min))
598 ;; Meta-MCVS-removed files are not taken under VC control.
599 (when (looking-at "/\\([^/]*\\)/[^/-]")
600 (let ((file (expand-file-name (match-string 1) dir
)))
601 (unless (vc-file-getprop file
'vc-state
)
602 (vc-cvs-parse-entry file t
))))
605 (defalias 'vc-mcvs-valid-symbolic-tag-name-p
'vc-cvs-valid-symbolic-tag-name-p
)
606 (defalias 'vc-mcvs-valid-version-number-p
'vc-cvs-valid-version-number-p
)
609 ;;; vc-mcvs.el ends here