1 ;;; vc-mcvs.el --- VC backend for the Meta-CVS version-control system
3 ;; Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
25 ;; The home page of the Meta-CVS version control system is at
27 ;; http://users.footprints.net/~kaz/mcvs.html
29 ;; This is derived from vc-cvs.el as follows:
30 ;; - cp vc-cvs.el vc-mcvs.el
31 ;; - Replace CVS/ with MCVS/CVS/
32 ;; - Replace 'CVS with 'MCVS
33 ;; - Replace -cvs- with -mcvs-
34 ;; - Replace most of the rest of CVS to Meta-CVS
36 ;; Then of course started the hacking. Only a small part of the code
37 ;; has been touched and not much more than that was tested, so if
38 ;; you bump into a bug, don't be surprised: just report it to me.
40 ;; What has been partly tested:
41 ;; - C-x v v to start editing a file that was checked out with CVSREAD on.
42 ;; - C-x v v to commit a file
47 ;; - M-x vc-rename-file RET
51 ;; - Retrieving snapshots doesn't filter `cvs update' output and thus
52 ;; parses bogus filenames. Don't know if it harms.
56 (eval-when-compile (require 'vc
))
60 ;;; Customization options
63 (defcustom vc-mcvs-global-switches nil
64 "*Global switches to pass to any Meta-CVS command."
65 :type
'(choice (const :tag
"None" nil
)
66 (string :tag
"Argument String")
67 (repeat :tag
"Argument List"
73 (defcustom vc-mcvs-register-switches nil
74 "*Extra switches for registering a file into Meta-CVS.
75 A string or list of strings passed to the checkin program by
77 :type
'(choice (const :tag
"None" nil
)
78 (string :tag
"Argument String")
79 (repeat :tag
"Argument List"
85 (defcustom vc-mcvs-diff-switches nil
86 "*A string or list of strings specifying extra switches for cvs diff under VC."
87 :type
'(choice (const :tag
"None" nil
)
88 (string :tag
"Argument String")
89 (repeat :tag
"Argument List"
95 (defcustom vc-mcvs-header
(or (cdr (assoc 'MCVS vc-header-alist
))
97 "*Header keywords to be inserted by `vc-insert-headers'."
99 :type
'(repeat string
)
102 (defcustom vc-mcvs-use-edit vc-cvs-use-edit
103 "*Non-nil means to use `cvs edit' to \"check out\" a file.
104 This is only meaningful if you don't use the implicit checkout model
105 \(i.e. if you have $CVSREAD set)."
110 ;;; Properties of the backend
112 (defalias 'vc-mcvs-revision-granularity
'vc-cvs-revision-granularity
)
113 (defalias 'vc-mcvs-checkout-model
'vc-cvs-checkout-model
)
116 ;;; State-querying functions
119 ;;;###autoload (defun vc-mcvs-registered (file)
120 ;;;###autoload (if (vc-find-root file "MCVS/CVS")
121 ;;;###autoload (progn
122 ;;;###autoload (load "vc-mcvs")
123 ;;;###autoload (vc-mcvs-registered file))))
125 (defun vc-mcvs-root (file)
126 "Return the root directory of a Meta-CVS project, if any."
127 (or (vc-file-getprop file
'mcvs-root
)
128 (vc-file-setprop file
'mcvs-root
(vc-find-root file
"MCVS/CVS"))))
130 (defun vc-mcvs-read (file)
131 (if (file-readable-p file
)
133 (insert-file-contents file
)
134 (goto-char (point-min))
135 (read (current-buffer)))))
137 (defun vc-mcvs-map-file (dir file
)
138 (let ((map (vc-mcvs-read (expand-file-name "MCVS/MAP" dir
)))
140 (dolist (x map inode
)
141 (if (equal (nth 2 x
) file
) (setq inode
(nth 1 x
))))))
143 (defun vc-mcvs-registered (file)
144 (let (root inode cvsfile
)
145 (when (and (setq root
(vc-mcvs-root file
))
146 (setq inode
(vc-mcvs-map-file
147 root
(file-relative-name file root
))))
148 (vc-file-setprop file
'mcvs-inode inode
)
149 ;; Avoid calling `mcvs diff' in vc-workfile-unchanged-p.
150 (vc-file-setprop file
'vc-checkout-time
151 (if (vc-cvs-registered
152 (setq cvsfile
(expand-file-name inode root
)))
153 (vc-file-getprop cvsfile
'vc-checkout-time
)
154 ;; The file might not be registered yet because
159 (defun vc-mcvs-state (file)
160 ;; This would assume the Meta-CVS sandbox is synchronized.
161 ;; (vc-mcvs-cvs state file))
162 "Meta-CVS-specific version of `vc-state'."
163 (if (vc-stay-local-p file
)
164 (let ((state (vc-file-getprop file
'vc-state
)))
165 ;; If we should stay local, use the heuristic but only if
166 ;; we don't have a more precise state already available.
167 (if (memq state
'(up-to-date edited
))
168 (vc-mcvs-state-heuristic file
)
171 (setq default-directory
(vc-mcvs-root file
))
172 (vc-mcvs-command t
0 file
"status")
173 (vc-cvs-parse-status t
))))
176 (defalias 'vc-mcvs-state-heuristic
'vc-cvs-state-heuristic
)
178 (defun vc-mcvs-dir-state (dir)
179 "Find the Meta-CVS state of all files in DIR and subdirectories."
180 ;; if DIR is not under Meta-CVS control, don't do anything.
181 (when (file-readable-p (expand-file-name "MCVS/CVS/Entries" dir
))
182 (if (vc-stay-local-p dir
)
183 (vc-mcvs-dir-state-heuristic dir
)
184 (let ((default-directory dir
))
185 ;; Don't specify DIR in this command, the default-directory is
186 ;; enough. Otherwise it might fail with remote repositories.
188 (buffer-disable-undo) ;; Because these buffers can get huge
189 (setq default-directory
(vc-mcvs-root dir
))
190 (vc-mcvs-command t
0 nil
"status")
191 (goto-char (point-min))
192 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t
)
193 (narrow-to-region (match-beginning 0) (match-end 0))
194 (vc-cvs-parse-status)
195 (goto-char (point-max))
198 (defun vc-mcvs-working-revision (file)
199 (vc-cvs-working-revision
200 (expand-file-name (vc-file-getprop file
'mcvs-inode
)
201 (vc-file-getprop file
'mcvs-root
))))
204 ;;; State-changing functions
207 (defun vc-mcvs-register (files &optional rev comment
)
208 "Register FILES into the Meta-CVS version-control system.
209 COMMENT can be used to provide an initial description of FILE.
211 `vc-register-switches' and `vc-mcvs-register-switches' are passed to
212 the Meta-CVS command (in that order)."
213 ;; FIXME: multiple-file case should be made to work
214 (if (> (length files
) 1) (error "Registering filesets is not yet supported."))
215 (let* ((file (car files
))
216 (filename (file-name-nondirectory file
))
217 (extpos (string-match "\\." filename
))
218 (ext (if extpos
(substring filename
(1+ extpos
))))
219 (root (vc-mcvs-root file
))
220 (types-file (expand-file-name "MCVS/TYPES" root
))
221 (map-file (expand-file-name "MCVS/MAP" root
))
222 (types (vc-mcvs-read types-file
)))
223 ;; Make sure meta files like MCVS/MAP are not read-only (happens with
224 ;; CVSREAD) since Meta-CVS doesn't pay attention to it at all and goes
226 (unless (file-writable-p map-file
)
227 (vc-checkout map-file t
))
228 (unless (or (file-writable-p types-file
) (not (file-exists-p types-file
)))
229 (vc-checkout types-file t
))
230 ;; Make sure the `mcvs add' will not fire up the CVSEDITOR
231 ;; to add a rule for the given file's extension.
232 (when (and ext
(not (assoc ext types
)))
233 (let ((type (completing-read "Type to use (default): "
234 '("default" "name-only" "keep-old"
235 "binary" "value-only")
236 nil t nil nil
"default")))
237 (push (list ext
(make-symbol (upcase (concat ":" type
)))) types
)
238 (setq types
(sort types
(lambda (x y
) (string< (car x
) (car y
)))))
239 (with-current-buffer (find-file-noselect types-file
)
241 (pp types
(current-buffer))
243 (unless (get-buffer-window (current-buffer) t
)
244 (kill-buffer (current-buffer))))))
246 (prog1 (apply 'vc-mcvs-command nil
0 file
248 (and comment
(string-match "[^\t\n ]" comment
)
249 (concat "-m" comment
))
250 (vc-switches 'MCVS
'register
))
251 ;; I'm not sure exactly why, but if we don't setup the inode and root
252 ;; prop of the file, things break later on in vc-mode-line that
253 ;; ends up calling vc-mcvs-working-revision.
254 ;; We also need to set vc-checkout-time so that vc-workfile-unchanged-p
255 ;; doesn't try to call `mcvs diff' on the file.
256 (vc-mcvs-registered file
))))
258 (defalias 'vc-mcvs-responsible-p
'vc-mcvs-root
259 "Return non-nil if CVS thinks it is responsible for FILE.")
261 (defalias 'vc-cvs-could-register
'vc-cvs-responsible-p
262 "Return non-nil if FILE could be registered in Meta-CVS.
263 This is only possible if Meta-CVS is responsible for FILE's directory.")
265 (defun vc-mcvs-checkin (files rev comment
)
266 "Meta-CVS-specific version of `vc-backend-checkin'."
267 (unless (or (not rev
) (vc-mcvs-valid-revision-number-p rev
))
268 (if (not (vc-mcvs-valid-symbolic-tag-name-p rev
))
269 (error "%s is not a valid symbolic tag name" rev
)
270 ;; If the input revision is a valid symbolic tag name, we create it
271 ;; as a branch, commit and switch to it.
272 ;; This file-specific form of branching is deprecated.
273 ;; We can't use `mcvs branch' and `mcvs switch' because they cannot
274 ;; be applied just to this one file.
275 (apply 'vc-mcvs-command nil
0 files
"tag" "-b" (list rev
))
276 (apply 'vc-mcvs-command nil
0 files
"update" "-r" (list rev
))
277 (mapc (lambda (file) (vc-file-setprop file
'vc-mcvs-sticky-tag rev
))
280 ;; This commit might cvs-commit several files (e.g. MAP and TYPES)
281 ;; so using numbered revs here is dangerous and somewhat meaningless.
282 (when rev
(error "Cannot commit to a specific revision number"))
283 (let ((status (apply 'vc-mcvs-command nil
1 files
285 (vc-switches 'MCVS
'checkin
))))
287 (goto-char (point-min))
288 (when (not (zerop status
))
289 ;; Check checkin problem.
291 ((re-search-forward "Up-to-date check failed" nil t
)
292 (mapc (lambda (file) (vc-file-setprop file
'vc-state
'needs-merge
))
294 (error "%s" (substitute-command-keys
295 (concat "Up-to-date check failed: "
296 "type \\[vc-next-action] to merge in changes"))))
298 (pop-to-buffer (current-buffer))
299 (goto-char (point-min))
300 (shrink-window-if-larger-than-buffer)
301 (error "Check-in failed"))))
302 ;; Single-file commit? Then update the revision by parsing the buffer.
303 ;; Otherwise we can't necessarily tell what goes with what; clear
304 ;; its properties so they have to be refetched.
305 (if (= (length files
) 1)
307 (car files
) 'vc-working-revision
308 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
309 (mapc (lambda (file) (vc-file-clearprops file
)) files
))
310 ;; Anyway, forget the checkout model of the file, because we might have
311 ;; guessed wrong when we found the file. After commit, we can
312 ;; tell it from the permissions of the file (see
313 ;; vc-mcvs-checkout-model).
314 (mapc (lambda (file) (vc-file-setprop file
'vc-checkout-model nil
))
317 ;; if this was an explicit check-in (does not include creation of
318 ;; a branch), remove the sticky tag.
319 (if (and rev
(not (vc-mcvs-valid-symbolic-tag-name-p rev
)))
320 (vc-mcvs-command nil
0 files
"update" "-A"))))
322 (defun vc-mcvs-find-revision (file rev buffer
)
323 (apply 'vc-mcvs-command
325 "-Q" ; suppress diagnostic output
327 (and rev
(not (string= rev
""))
330 (vc-switches 'MCVS
'checkout
)))
332 (defun vc-mcvs-checkout (file &optional editable rev
)
333 (message "Checking out %s..." file
)
334 (with-current-buffer (or (get-file-buffer file
) (current-buffer))
335 (vc-call update file editable rev
(vc-switches 'MCVS
'checkout
)))
337 (message "Checking out %s...done" file
))
339 (defun vc-mcvs-update (file editable rev switches
)
340 (if (and (file-exists-p file
) (not rev
))
341 ;; If no revision was specified, just make the file writable
342 ;; if necessary (using `cvs-edit' if requested).
343 (and editable
(not (eq (vc-mcvs-checkout-model (list file
)) 'implicit
))
345 (vc-mcvs-command nil
0 file
"edit")
346 (set-file-modes file
(logior (file-modes file
) 128))
347 (if (equal file buffer-file-name
) (toggle-read-only -
1))))
348 ;; Check out a particular revision (or recreate the file).
349 (vc-file-setprop file
'vc-working-revision nil
)
350 (apply 'vc-mcvs-command nil
0 file
353 ;; default for verbose checkout: clear the sticky tag so
354 ;; that the actual update will get the head of the trunk
355 (if (or (not rev
) (string= rev
""))
360 (defun vc-mcvs-rename-file (old new
)
361 (vc-mcvs-command nil
0 new
"move" (file-relative-name old
)))
363 (defun vc-mcvs-revert (file &optional contents-done
)
364 "Revert FILE to the working revision it was based on."
365 (vc-default-revert 'MCVS file contents-done
)
366 (unless (eq (vc-mcvs-checkout-model (list file
)) 'implicit
)
368 (vc-mcvs-command nil
0 file
"unedit")
369 ;; Make the file read-only by switching off all w-bits
370 (set-file-modes file
(logand (file-modes file
) 3950)))))
372 (defun vc-mcvs-merge (file first-revision
&optional second-revision
)
373 "Merge changes into current working copy of FILE.
374 The changes are between FIRST-REVISION and SECOND-REVISION."
375 (vc-mcvs-command nil
0 file
377 (concat "-j" first-revision
)
378 (concat "-j" second-revision
))
379 (vc-file-setprop file
'vc-state
'edited
)
380 (with-current-buffer (get-buffer "*vc*")
381 (goto-char (point-min))
382 (if (re-search-forward "conflicts during merge" nil t
)
384 0))) ; signal success
386 (defun vc-mcvs-merge-news (file)
387 "Merge in any new changes made to FILE."
388 (message "Merging changes into %s..." file
)
389 ;; (vc-file-setprop file 'vc-working-revision nil)
390 (vc-file-setprop file
'vc-checkout-time
0)
391 (vc-mcvs-command nil
0 file
"update")
392 ;; Analyze the merge result reported by Meta-CVS, and set
393 ;; file properties accordingly.
394 (with-current-buffer (get-buffer "*vc*")
395 (goto-char (point-min))
396 ;; get new working revision
397 (if (re-search-forward
398 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t
)
399 (vc-file-setprop file
'vc-working-revision
(match-string 1))
400 (vc-file-setprop file
'vc-working-revision nil
))
403 (if (eq (buffer-size) 0)
404 0 ;; there were no news; indicate success
405 (if (re-search-forward
406 (concat "^\\([CMUP] \\)?"
408 "\\( already contains the differences between \\)?")
411 ;; Merge successful, we are in sync with repository now
412 ((or (match-string 2)
413 (string= (match-string 1) "U ")
414 (string= (match-string 1) "P "))
415 (vc-file-setprop file
'vc-state
'up-to-date
)
416 (vc-file-setprop file
'vc-checkout-time
417 (nth 5 (file-attributes file
)))
418 0);; indicate success to the caller
419 ;; Merge successful, but our own changes are still in the file
420 ((string= (match-string 1) "M ")
421 (vc-file-setprop file
'vc-state
'edited
)
422 0);; indicate success to the caller
423 ;; Conflicts detected!
425 (vc-file-setprop file
'vc-state
'edited
)
426 1);; signal the error to the caller
428 (pop-to-buffer "*vc*")
429 (error "Couldn't analyze mcvs update result")))
430 (message "Merging changes into %s...done" file
))))
432 (defun vc-mcvs-modify-change-comment (files rev comment
)
433 "Modify the change comments for FILES on a specified REV.
434 Will fail unless you have administrative privileges on the repo."
435 (vc-mcvs-command nil
0 files
"rcs" (concat "-m" comment
":" rev
)))
439 ;;; History functions
442 (defun vc-mcvs-print-log (files &optional buffer
)
443 "Get change log associated with FILES."
444 (let ((default-directory (vc-mcvs-root (car files
))))
445 ;; Run the command from the root dir so that `mcvs filt' returns
446 ;; valid relative names.
449 (if (vc-stay-local-p files
) 'async
0)
452 (defun vc-mcvs-diff (files &optional oldvers newvers buffer
)
453 "Get a difference report using Meta-CVS between two revisions of FILES."
454 (let* ((async (and (not vc-disable-async-diff
)
455 (vc-stay-local-p files
)))
456 ;; Run the command from the root dir so that `mcvs filt' returns
457 ;; valid relative names.
458 (default-directory (vc-mcvs-root (car files
)))
460 (apply 'vc-mcvs-command
(or buffer
"*vc-diff*")
463 (and oldvers
(concat "-r" oldvers
))
464 (and newvers
(concat "-r" newvers
))
465 (vc-switches 'MCVS
'diff
))))
466 (if async
1 status
))) ; async diff, pessimistic assumption.
468 (defun vc-mcvs-annotate-command (file buffer
&optional revision
)
469 "Execute \"mcvs annotate\" on FILE, inserting the contents in BUFFER.
470 Optional arg REVISION is a revision to annotate from."
473 (if (vc-stay-local-p file
) 'async
0)
474 file
"annotate" (if revision
(concat "-r" revision
)))
475 (with-current-buffer buffer
476 (goto-char (point-min))
477 (re-search-forward "^[0-9]")
478 (delete-region (point-min) (1- (point)))))
480 (defalias 'vc-mcvs-annotate-current-time
'vc-cvs-annotate-current-time
)
481 (defalias 'vc-mcvs-annotate-time
'vc-cvs-annotate-time
)
487 (defun vc-mcvs-create-snapshot (dir name branchp
)
488 "Assign to DIR's current revision a given NAME.
489 If BRANCHP is non-nil, the name is created as a branch (and the current
490 workspace is immediately moved to that new branch)."
492 (vc-mcvs-command nil
0 dir
"tag" "-c" name
)
493 (vc-mcvs-command nil
0 dir
"branch" name
)
494 (vc-mcvs-command nil
0 dir
"switch" name
)))
496 (defun vc-mcvs-retrieve-snapshot (dir name update
)
497 "Retrieve a snapshot at and below DIR.
498 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
499 If UPDATE is non-nil, then update (resynch) any affected buffers."
500 (with-current-buffer (get-buffer-create "*vc*")
501 (let ((default-directory dir
)
504 (if (or (not name
) (string= name
""))
505 (vc-mcvs-command t
0 nil
"update")
506 (vc-mcvs-command t
0 nil
"update" "-r" name
)
507 (setq sticky-tag name
))
509 (goto-char (point-min))
511 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
512 (let* ((file (expand-file-name (match-string 2) dir
))
513 (state (match-string 1))
514 (buffer (find-buffer-visiting file
)))
517 ((or (string= state
"U")
519 (vc-file-setprop file
'vc-state
'up-to-date
)
520 (vc-file-setprop file
'vc-working-revision nil
)
521 (vc-file-setprop file
'vc-checkout-time
522 (nth 5 (file-attributes file
))))
523 ((or (string= state
"M")
525 (vc-file-setprop file
'vc-state
'edited
)
526 (vc-file-setprop file
'vc-working-revision nil
)
527 (vc-file-setprop file
'vc-checkout-time
0)))
528 (vc-file-setprop file
'vc-mcvs-sticky-tag sticky-tag
)
529 (vc-resynch-buffer file t t
))))
530 (forward-line 1))))))
537 (defalias 'vc-mcvs-make-version-backups-p
'vc-stay-local-p
538 "Return non-nil if version backups should be made for FILE.")
539 (defalias 'vc-mcvs-check-headers
'vc-cvs-check-headers
)
543 ;;; Internal functions
546 (defun vc-mcvs-command (buffer okstatus file
&rest flags
)
547 "A wrapper around `vc-do-command' for use in vc-mcvs.el.
548 The difference to vc-do-command is that this function always invokes `mcvs',
549 and that it passes `vc-mcvs-global-switches' to it before FLAGS."
550 (let ((args (append '("--error-terminate")
551 (if (stringp vc-mcvs-global-switches
)
552 (cons vc-mcvs-global-switches flags
)
553 (append vc-mcvs-global-switches flags
)))))
554 (if (not (member (car flags
) '("diff" "log" "status")))
555 ;; No need to filter: do it the easy way.
556 (apply 'vc-do-command buffer okstatus
"mcvs" file args
)
557 ;; We need to filter the output.
558 ;; The output of the filter uses filenames relative to the root,
559 ;; so we need to change the default-directory.
560 ;; (assert (equal default-directory (vc-mcvs-root file)))
562 buffer okstatus
"sh" nil
"-c"
565 'shell-quote-argument
566 (append (remq nil args
)
567 (if file
(list (file-relative-name file
))))
571 (defun vc-mcvs-repository-hostname (dirname)
572 (vc-cvs-repository-hostname (vc-mcvs-root dirname
)))
574 (defun vc-mcvs-dir-state-heuristic (dir)
575 "Find the Meta-CVS state of all files in DIR, using only local information."
577 (vc-cvs-get-entries dir
)
578 (goto-char (point-min))
580 ;; Meta-MCVS-removed files are not taken under VC control.
581 (when (looking-at "/\\([^/]*\\)/[^/-]")
582 (let ((file (expand-file-name (match-string 1) dir
)))
583 (unless (vc-file-getprop file
'vc-state
)
584 (vc-cvs-parse-entry file t
))))
587 (defalias 'vc-mcvs-valid-symbolic-tag-name-p
'vc-cvs-valid-symbolic-tag-name-p
)
588 (defalias 'vc-mcvs-valid-revision-number-p
'vc-cvs-valid-revision-number-p
)
592 ;; arch-tag: a39c7c1c-5247-429d-88df-dd7187d2e704
593 ;;; vc-mcvs.el ends here