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 ;; - Retrieving snapshots doesn't filter `cvs update' output and thus
54 ;; parses bogus filenames. Don't know if it harms.
58 (eval-when-compile (require 'vc
))
62 ;;; Customization options
65 (defcustom vc-mcvs-global-switches nil
66 "*Global switches to pass to any Meta-CVS command."
67 :type
'(choice (const :tag
"None" nil
)
68 (string :tag
"Argument String")
69 (repeat :tag
"Argument List"
75 (defcustom vc-mcvs-register-switches nil
76 "*Extra switches for registering a file into Meta-CVS.
77 A string or list of strings passed to the checkin program by
79 :type
'(choice (const :tag
"None" nil
)
80 (string :tag
"Argument String")
81 (repeat :tag
"Argument List"
87 (defcustom vc-mcvs-diff-switches nil
88 "*A string or list of strings specifying extra switches for cvs diff under VC."
89 :type
'(choice (const :tag
"None" nil
)
90 (string :tag
"Argument String")
91 (repeat :tag
"Argument List"
97 (defcustom vc-mcvs-header
(or (cdr (assoc 'MCVS vc-header-alist
))
99 "*Header keywords to be inserted by `vc-insert-headers'."
101 :type
'(repeat string
)
104 (defcustom vc-mcvs-use-edit vc-cvs-use-edit
105 "*Non-nil means to use `cvs edit' to \"check out\" a file.
106 This is only meaningful if you don't use the implicit checkout model
107 \(i.e. if you have $CVSREAD set)."
113 ;;; State-querying functions
116 ;;;###autoload (defun vc-mcvs-registered (file)
117 ;;;###autoload (let ((dir file))
118 ;;;###autoload (while (and (stringp dir)
119 ;;;###autoload (not (equal dir (setq dir (file-name-directory dir)))))
120 ;;;###autoload (setq dir (if (file-directory-p
121 ;;;###autoload (expand-file-name "MCVS/CVS" dir))
122 ;;;###autoload t (directory-file-name dir))))
123 ;;;###autoload (if (eq dir t)
124 ;;;###autoload (progn
125 ;;;###autoload (load "vc-mcvs")
126 ;;;###autoload (vc-mcvs-registered file)))))
128 (defun vc-mcvs-root (file)
129 "Return the root directory of a Meta-CVS project, if any."
130 (or (vc-file-getprop file
'mcvs-root
)
135 (equal file
(setq file
(file-name-directory file
)))))
136 (if (file-directory-p (expand-file-name "MCVS/CVS" file
))
138 (setq file
(directory-file-name file
))))
141 (defun vc-mcvs-read (file)
142 (if (file-readable-p file
)
144 (insert-file-contents file
)
145 (goto-char (point-min))
146 (read (current-buffer)))))
148 (defun vc-mcvs-map-file (dir file
)
149 (let ((map (vc-mcvs-read (expand-file-name "MCVS/MAP" dir
)))
151 (dolist (x map inode
)
152 (if (equal (nth 2 x
) file
) (setq inode
(nth 1 x
))))))
154 (defun vc-mcvs-registered (file)
155 (let (root inode cvsfile
)
156 (when (and (setq root
(vc-mcvs-root file
))
157 (setq inode
(vc-mcvs-map-file
158 root
(file-relative-name file root
))))
159 (vc-file-setprop file
'mcvs-inode inode
)
160 ;; Avoid calling `mcvs diff' in vc-workfile-unchanged-p.
161 (vc-file-setprop file
'vc-checkout-time
162 (if (vc-cvs-registered
163 (setq cvsfile
(expand-file-name inode root
)))
164 (vc-file-getprop cvsfile
'vc-checkout-time
)
165 ;; The file might not be registered yet because
170 (defmacro vc-mcvs-cvs
(op file
&rest args
)
172 `(,(intern (concat "vc-cvs-" (symbol-name op
)))
173 (expand-file-name (vc-file-getprop ,file
'mcvs-inode
)
174 (vc-file-getprop ,file
'mcvs-root
))
177 (defun vc-mcvs-state (file)
178 ;; This would assume the Meta-CVS sandbox is synchronized.
179 ;; (vc-mcvs-cvs state file))
180 "Meta-CVS-specific version of `vc-state'."
181 (if (vc-stay-local-p file
)
182 (let ((state (vc-file-getprop file
'vc-state
)))
183 ;; If we should stay local, use the heuristic but only if
184 ;; we don't have a more precise state already available.
185 (if (memq state
'(up-to-date edited
))
186 (vc-mcvs-state-heuristic file
)
189 (setq default-directory
(vc-mcvs-root file
))
190 (vc-mcvs-command t
0 file
"status")
191 (vc-cvs-parse-status t
))))
194 (defalias 'vc-mcvs-state-heuristic
'vc-cvs-state-heuristic
)
196 (defun vc-mcvs-dir-state (dir)
197 "Find the Meta-CVS state of all files in DIR."
198 ;; if DIR is not under Meta-CVS control, don't do anything.
199 (when (file-readable-p (expand-file-name "MCVS/CVS/Entries" dir
))
200 (if (vc-stay-local-p dir
)
201 (vc-mcvs-dir-state-heuristic dir
)
202 (let ((default-directory dir
))
203 ;; Don't specify DIR in this command, the default-directory is
204 ;; enough. Otherwise it might fail with remote repositories.
206 (setq default-directory
(vc-mcvs-root dir
))
207 (vc-mcvs-command t
0 nil
"status" "-l")
208 (goto-char (point-min))
209 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t
)
210 (narrow-to-region (match-beginning 0) (match-end 0))
211 (vc-cvs-parse-status)
212 (goto-char (point-max))
215 (defun vc-mcvs-workfile-version (file) (vc-mcvs-cvs workfile-version file
))
217 (defalias 'vc-mcvs-checkout-model
'vc-cvs-checkout-model
)
219 (defun vc-mcvs-mode-line-string (file)
220 (let ((s (vc-mcvs-cvs mode-line-string file
)))
222 (if (and (not (memq (vc-state file
) '(up-to-date needs-patch
)))
223 (string-match "\\`CVS-" s
))
224 ;; The CVS file is not in sync, so we need to adjust the state.
225 (concat "MCVS:" (substring s
4))
229 ;;; State-changing functions
232 (defun vc-mcvs-register (file &optional rev comment
)
233 "Register FILE into the Meta-CVS version-control system.
234 COMMENT can be used to provide an initial description of FILE.
236 `vc-register-switches' and `vc-mcvs-register-switches' are passed to
237 the Meta-CVS command (in that order)."
238 (let* ((filename (file-name-nondirectory file
))
239 (extpos (string-match "\\." filename
))
240 (ext (if extpos
(substring filename
(1+ extpos
))))
241 (root (vc-mcvs-root file
))
242 (types-file (expand-file-name "MCVS/TYPES" root
))
243 (map-file (expand-file-name "MCVS/MAP" root
))
244 (types (vc-mcvs-read types-file
)))
245 ;; Make sure meta files like MCVS/MAP are not read-only (happens with
246 ;; CVSREAD) since Meta-CVS doesn't pay attention to it at all and goes
248 (unless (file-writable-p map-file
)
249 (vc-checkout map-file t
))
250 (unless (or (file-writable-p types-file
) (not (file-exists-p types-file
)))
251 (vc-checkout types-file t
))
252 ;; Make sure the `mcvs add' will not fire up the CVSEDITOR
253 ;; to add a rule for the given file's extension.
254 (when (and ext
(not (assoc ext types
)))
255 (let ((type (completing-read "Type to use [default]: "
256 '("default" "name-only" "keep-old"
257 "binary" "value-only")
258 nil t nil nil
"default")))
259 (push (list ext
(make-symbol (upcase (concat ":" type
)))) types
)
260 (setq types
(sort types
(lambda (x y
) (string< (car x
) (car y
)))))
261 (with-current-buffer (find-file-noselect types-file
)
263 (pp types
(current-buffer))
265 (unless (get-buffer-window (current-buffer) t
)
266 (kill-buffer (current-buffer)))))))
268 (prog1 (apply 'vc-mcvs-command nil
0 file
270 (and comment
(string-match "[^\t\n ]" comment
)
271 (concat "-m" comment
))
272 (vc-switches 'MCVS
'register
))
273 ;; I'm not sure exactly why, but if we don't setup the inode and root
274 ;; prop of the file, things break later on in vc-mode-line that
275 ;; ends up calling vc-mcvs-workfile-version.
276 ;; We also need to set vc-checkout-time so that vc-workfile-unchanged-p
277 ;; doesn't try to call `mcvs diff' on the file.
278 (vc-mcvs-registered file
)))
280 (defalias 'vc-mcvs-responsible-p
'vc-mcvs-root
281 "Return non-nil if CVS thinks it is responsible for FILE.")
283 (defalias 'vc-cvs-could-register
'vc-cvs-responsible-p
284 "Return non-nil if FILE could be registered in Meta-CVS.
285 This is only possible if Meta-CVS is responsible for FILE's directory.")
287 (defun vc-mcvs-checkin (file rev comment
)
288 "Meta-CVS-specific version of `vc-backend-checkin'."
289 (unless (or (not rev
) (vc-mcvs-valid-version-number-p rev
))
290 (if (not (vc-mcvs-valid-symbolic-tag-name-p rev
))
291 (error "%s is not a valid symbolic tag name" rev
)
292 ;; If the input revision is a valid symbolic tag name, we create it
293 ;; as a branch, commit and switch to it.
294 ;; This file-specific form of branching is deprecated.
295 ;; We can't use `mcvs branch' and `mcvs switch' because they cannot
296 ;; be applied just to this one file.
297 (apply 'vc-mcvs-command nil
0 file
"tag" "-b" (list rev
))
298 (apply 'vc-mcvs-command nil
0 file
"update" "-r" (list rev
))
299 (vc-file-setprop file
'vc-mcvs-sticky-tag rev
)
301 ;; This commit might cvs-commit several files (e.g. MAP and TYPES)
302 ;; so using numbered revs here is dangerous and somewhat meaningless.
303 (when rev
(error "Cannot commit to a specific revision number"))
304 (let ((status (apply 'vc-mcvs-command nil
1 file
306 (vc-switches 'MCVS
'checkin
))))
308 (goto-char (point-min))
309 (when (not (zerop status
))
310 ;; Check checkin problem.
312 ((re-search-forward "Up-to-date check failed" nil t
)
313 (vc-file-setprop file
'vc-state
'needs-merge
)
314 (error (substitute-command-keys
315 (concat "Up-to-date check failed: "
316 "type \\[vc-next-action] to merge in changes"))))
318 (pop-to-buffer (current-buffer))
319 (goto-char (point-min))
320 (shrink-window-if-larger-than-buffer)
321 (error "Check-in failed"))))
322 ;; Update file properties
324 file
'vc-workfile-version
325 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
326 ;; Forget the checkout model of the file, because we might have
327 ;; guessed wrong when we found the file. After commit, we can
328 ;; tell it from the permissions of the file (see
329 ;; vc-mcvs-checkout-model).
330 (vc-file-setprop file
'vc-checkout-model nil
)
332 ;; if this was an explicit check-in (does not include creation of
333 ;; a branch), remove the sticky tag.
334 (if (and rev
(not (vc-mcvs-valid-symbolic-tag-name-p rev
)))
335 (vc-mcvs-command nil
0 file
"update" "-A"))))
337 (defun vc-mcvs-find-version (file rev buffer
)
338 (apply 'vc-mcvs-command
340 "-Q" ; suppress diagnostic output
342 (and rev
(not (string= rev
""))
345 (vc-switches 'MCVS
'checkout
)))
347 (defun vc-mcvs-checkout (file &optional editable rev
)
348 (message "Checking out %s..." file
)
349 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
350 (vc-call update file editable rev
(vc-switches 'MCVS
'checkout
)))
352 (message "Checking out %s...done" file
))
354 (defun vc-mcvs-update (file editable rev switches
)
355 (if (and (file-exists-p file
) (not rev
))
356 ;; If no revision was specified, just make the file writable
357 ;; if necessary (using `cvs-edit' if requested).
358 (and editable
(not (eq (vc-mcvs-checkout-model file
) 'implicit
))
360 (vc-mcvs-command nil
0 file
"edit")
361 (set-file-modes file
(logior (file-modes file
) 128))
362 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
363 ;; Check out a particular version (or recreate the file).
364 (vc-file-setprop file
'vc-workfile-version nil
)
365 (apply 'vc-mcvs-command nil
0 file
368 ;; default for verbose checkout: clear the sticky tag so
369 ;; that the actual update will get the head of the trunk
370 (if (or (not rev
) (string= rev
""))
375 (defun vc-mcvs-rename-file (old new
)
376 (vc-mcvs-command nil
0 new
"move" (file-relative-name old
)))
378 (defun vc-mcvs-revert (file &optional contents-done
)
379 "Revert FILE to the version it was based on."
380 (vc-default-revert file contents-done
)
381 (unless (eq (vc-checkout-model file
) 'implicit
)
383 (vc-mcvs-command nil
0 file
"unedit")
384 ;; Make the file read-only by switching off all w-bits
385 (set-file-modes file
(logand (file-modes file
) 3950)))))
387 (defun vc-mcvs-merge (file first-version
&optional second-version
)
388 "Merge changes into current working copy of FILE.
389 The changes are between FIRST-VERSION and SECOND-VERSION."
390 (vc-mcvs-command nil
0 file
392 (concat "-j" first-version
)
393 (concat "-j" second-version
))
394 (vc-file-setprop file
'vc-state
'edited
)
395 (with-current-buffer (get-buffer "*vc*")
396 (goto-char (point-min))
397 (if (re-search-forward "conflicts during merge" nil t
)
399 0))) ; signal success
401 (defun vc-mcvs-merge-news (file)
402 "Merge in any new changes made to FILE."
403 (message "Merging changes into %s..." file
)
404 ;; (vc-file-setprop file 'vc-workfile-version nil)
405 (vc-file-setprop file
'vc-checkout-time
0)
406 (vc-mcvs-command nil
0 file
"update")
407 ;; Analyze the merge result reported by Meta-CVS, and set
408 ;; file properties accordingly.
409 (with-current-buffer (get-buffer "*vc*")
410 (goto-char (point-min))
411 ;; get new workfile version
412 (if (re-search-forward
413 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t
)
414 (vc-file-setprop file
'vc-workfile-version
(match-string 1))
415 (vc-file-setprop file
'vc-workfile-version nil
))
418 (if (eq (buffer-size) 0)
419 0 ;; there were no news; indicate success
420 (if (re-search-forward
421 (concat "^\\([CMUP] \\)?"
423 "\\( already contains the differences between \\)?")
426 ;; Merge successful, we are in sync with repository now
427 ((or (match-string 2)
428 (string= (match-string 1) "U ")
429 (string= (match-string 1) "P "))
430 (vc-file-setprop file
'vc-state
'up-to-date
)
431 (vc-file-setprop file
'vc-checkout-time
432 (nth 5 (file-attributes file
)))
433 0);; indicate success to the caller
434 ;; Merge successful, but our own changes are still in the file
435 ((string= (match-string 1) "M ")
436 (vc-file-setprop file
'vc-state
'edited
)
437 0);; indicate success to the caller
438 ;; Conflicts detected!
440 (vc-file-setprop file
'vc-state
'edited
)
441 1);; signal the error to the caller
443 (pop-to-buffer "*vc*")
444 (error "Couldn't analyze mcvs update result")))
445 (message "Merging changes into %s...done" file
))))
448 ;;; History functions
451 (defun vc-mcvs-print-log (file)
452 "Get change log associated with FILE."
453 (let ((default-directory (vc-mcvs-root file
)))
454 ;; Run the command from the root dir so that `mcvs filt' returns
455 ;; valid relative names.
458 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
461 (defun vc-mcvs-diff (file &optional oldvers newvers
)
462 "Get a difference report using Meta-CVS between two versions of FILE."
463 (if (string= (vc-workfile-version file
) "0")
464 ;; This file is added but not yet committed; there is no master file.
465 (if (or oldvers newvers
)
466 (error "No revisions of %s exist" file
)
467 ;; We regard this as "changed".
468 ;; Diff it against /dev/null.
469 ;; Note: this is NOT a "mcvs diff".
470 (apply 'vc-do-command
"*vc-diff*"
472 (append (vc-switches nil
'diff
) '("/dev/null")))
473 ;; Even if it's empty, it's locally modified.
475 (let* ((async (and (vc-stay-local-p file
) (fboundp 'start-process
)))
476 ;; Run the command from the root dir so that `mcvs filt' returns
477 ;; valid relative names.
478 (default-directory (vc-mcvs-root file
))
480 (apply 'vc-mcvs-command
"*vc-diff*"
483 (and oldvers
(concat "-r" oldvers
))
484 (and newvers
(concat "-r" newvers
))
485 (vc-switches 'MCVS
'diff
))))
486 (if async
1 status
)))) ; async diff, pessimistic assumption.
488 (defun vc-mcvs-diff-tree (dir &optional rev1 rev2
)
489 "Diff all files at and below DIR."
490 (with-current-buffer "*vc-diff*"
491 ;; Run the command from the root dir so that `mcvs filt' returns
492 ;; valid relative names.
493 (setq default-directory
(vc-mcvs-root dir
))
494 ;; cvs diff: use a single call for the entire tree
495 (let ((coding-system-for-read (or coding-system-for-read
'undecided
)))
496 (apply 'vc-mcvs-command
"*vc-diff*" 1 dir
"diff"
497 (and rev1
(concat "-r" rev1
))
498 (and rev2
(concat "-r" rev2
))
499 (vc-switches 'MCVS
'diff
)))))
501 (defun vc-mcvs-annotate-command (file buffer
&optional version
)
502 "Execute \"mcvs annotate\" on FILE, inserting the contents in BUFFER.
503 Optional arg VERSION is a version to annotate from."
506 (if (and (vc-stay-local-p file
) (fboundp 'start-process
)) 'async
0)
507 file
"annotate" (if version
(concat "-r" version
))))
509 (defalias 'vc-mcvs-annotate-current-time
'vc-cvs-annotate-current-time
)
510 (defalias 'vc-mcvs-annotate-time
'vc-cvs-annotate-time
)
516 (defun vc-mcvs-create-snapshot (dir name branchp
)
517 "Assign to DIR's current version a given NAME.
518 If BRANCHP is non-nil, the name is created as a branch (and the current
519 workspace is immediately moved to that new branch)."
521 (vc-mcvs-command nil
0 dir
"tag" "-c" name
)
522 (vc-mcvs-command nil
0 dir
"branch" name
)
523 (vc-mcvs-command nil
0 dir
"switch" name
)))
525 (defun vc-mcvs-retrieve-snapshot (dir name update
)
526 "Retrieve a snapshot at and below DIR.
527 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
528 If UPDATE is non-nil, then update (resynch) any affected buffers."
529 (with-current-buffer (get-buffer-create "*vc*")
530 (let ((default-directory dir
)
533 (if (or (not name
) (string= name
""))
534 (vc-mcvs-command t
0 nil
"update")
535 (vc-mcvs-command t
0 nil
"update" "-r" name
)
536 (setq sticky-tag name
))
538 (goto-char (point-min))
540 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
541 (let* ((file (expand-file-name (match-string 2) dir
))
542 (state (match-string 1))
543 (buffer (find-buffer-visiting file
)))
546 ((or (string= state
"U")
548 (vc-file-setprop file
'vc-state
'up-to-date
)
549 (vc-file-setprop file
'vc-workfile-version nil
)
550 (vc-file-setprop file
'vc-checkout-time
551 (nth 5 (file-attributes file
))))
552 ((or (string= state
"M")
554 (vc-file-setprop file
'vc-state
'edited
)
555 (vc-file-setprop file
'vc-workfile-version nil
)
556 (vc-file-setprop file
'vc-checkout-time
0)))
557 (vc-file-setprop file
'vc-mcvs-sticky-tag sticky-tag
)
558 (vc-resynch-buffer file t t
))))
559 (forward-line 1))))))
566 (defalias 'vc-mcvs-make-version-backups-p
'vc-stay-local-p
567 "Return non-nil if version backups should be made for FILE.")
568 (defalias 'vc-mcvs-check-headers
'vc-cvs-check-headers
)
572 ;;; Internal functions
575 (defun vc-mcvs-command (buffer okstatus file
&rest flags
)
576 "A wrapper around `vc-do-command' for use in vc-mcvs.el.
577 The difference to vc-do-command is that this function always invokes `mcvs',
578 and that it passes `vc-mcvs-global-switches' to it before FLAGS."
579 (let ((args (append '("--error-terminate")
580 (if (stringp vc-mcvs-global-switches
)
581 (cons vc-mcvs-global-switches flags
)
582 (append vc-mcvs-global-switches flags
)))))
583 (if (not (member (car flags
) '("diff" "log" "status")))
584 ;; No need to filter: do it the easy way.
585 (apply 'vc-do-command buffer okstatus
"mcvs" file args
)
586 ;; We need to filter the output.
587 ;; The output of the filter uses filenames relative to the root,
588 ;; so we need to change the default-directory.
589 (assert (equal default-directory
(vc-mcvs-root file
)))
591 buffer okstatus
"sh" nil
"-c"
594 'shell-quote-argument
595 (append (remq nil args
)
596 (if file
(list (file-relative-name file
))))
600 (defun vc-mcvs-repository-hostname (dirname)
601 (vc-cvs-repository-hostname (vc-mcvs-root dirname
)))
603 (defun vc-mcvs-dir-state-heuristic (dir)
604 "Find the Meta-CVS state of all files in DIR, using only local information."
606 (vc-cvs-get-entries dir
)
607 (goto-char (point-min))
609 ;; Meta-MCVS-removed files are not taken under VC control.
610 (when (looking-at "/\\([^/]*\\)/[^/-]")
611 (let ((file (expand-file-name (match-string 1) dir
)))
612 (unless (vc-file-getprop file
'vc-state
)
613 (vc-cvs-parse-entry file t
))))
616 (defalias 'vc-mcvs-valid-symbolic-tag-name-p
'vc-cvs-valid-symbolic-tag-name-p
)
617 (defalias 'vc-mcvs-valid-version-number-p
'vc-cvs-valid-version-number-p
)
620 ;;; vc-mcvs.el ends here