1 ;;; vc-cvs.el --- non-resident support for CVS version-control
3 ;; Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 (eval-when-compile (require 'cl) (require 'vc))
30 ;; Clear up the cache to force vc-call to check again and discover
31 ;; new functions when we reload this file.
32 (put 'CVS 'vc-functions nil)
34 ;;; Properties of the backend.
36 (defun vc-cvs-revision-granularity () 'file)
38 (defun vc-cvs-checkout-model (files)
39 "CVS-specific version of `vc-checkout-model'."
40 (if (getenv "CVSREAD")
42 (let* ((file (if (consp files) (car files) files))
43 (attrib (file-attributes file)))
44 (or (vc-file-getprop file 'vc-checkout-model)
46 file 'vc-checkout-model
47 (if (and attrib ;; don't check further if FILE doesn't exist
48 ;; If the file is not writable (despite CVSREAD being
49 ;; undefined), this is probably because the file is being
50 ;; "watched" by other developers.
51 ;; (If vc-mistrust-permissions was t, we actually shouldn't
52 ;; trust this, but there is no other way to learn this from
53 ;; CVS at the moment (version 1.9).)
54 (string-match "r-..-..-." (nth 8 attrib)))
59 ;;; Customization options
62 (defcustom vc-cvs-global-switches nil
63 "*Global switches to pass to any CVS command."
64 :type '(choice (const :tag "None" nil)
65 (string :tag "Argument String")
66 (repeat :tag "Argument List"
72 (defcustom vc-cvs-register-switches nil
73 "*Extra switches for registering a file into CVS.
74 A string or list of strings passed to the checkin program by
76 :type '(choice (const :tag "None" nil)
77 (string :tag "Argument String")
78 (repeat :tag "Argument List"
84 (defcustom vc-cvs-diff-switches nil
85 "*A string or list of strings specifying extra switches for cvs diff under VC."
86 :type '(choice (const :tag "None" nil)
87 (string :tag "Argument String")
88 (repeat :tag "Argument List"
94 (defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
95 "*Header keywords to be inserted by `vc-insert-headers'."
97 :type '(repeat string)
100 (defcustom vc-cvs-use-edit t
101 "*Non-nil means to use `cvs edit' to \"check out\" a file.
102 This is only meaningful if you don't use the implicit checkout model
103 \(i.e. if you have $CVSREAD set)."
108 (defcustom vc-cvs-stay-local t
109 "*Non-nil means use local operations when possible for remote repositories.
110 This avoids slow queries over the network and instead uses heuristics
111 and past information to determine the current status of a file.
113 The value can also be a regular expression or list of regular
114 expressions to match against the host name of a repository; then VC
115 only stays local for hosts that match it. Alternatively, the value
116 can be a list of regular expressions where the first element is the
117 symbol `except'; then VC always stays local except for hosts matched
118 by these regular expressions."
119 :type '(choice (const :tag "Always stay local" t)
120 (const :tag "Don't stay local" nil)
121 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
122 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
123 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
124 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
128 (defcustom vc-cvs-sticky-date-format-string "%c"
129 "*Format string for mode-line display of sticky date.
130 Format is according to `format-time-string'. Only used if
131 `vc-cvs-sticky-tag-display' is t."
136 (defcustom vc-cvs-sticky-tag-display t
137 "*Specify the mode-line display of sticky tags.
138 Value t means default display, nil means no display at all. If the
139 value is a function or macro, it is called with the sticky tag and
140 its' type as parameters, in that order. TYPE can have three different
141 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
142 string) and `date' (TAG is a date as returned by `encode-time'). The
143 return value of the function or macro will be displayed as a string.
145 Here's an example that will display the formatted date for sticky
146 dates and the word \"Sticky\" for sticky tag names and revisions.
149 (cond ((eq type 'date) (format-time-string
150 vc-cvs-sticky-date-format-string tag))
151 ((eq type 'revision-number) \"Sticky\")
152 ((eq type 'symbolic-name) \"Sticky\")))
154 Here's an example that will abbreviate to the first character only,
155 any text before the first occurrence of `-' for sticky symbolic tags.
156 If the sticky tag is a revision number, the word \"Sticky\" is
157 displayed. Date and time is displayed for sticky dates.
160 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
161 ((eq type 'revision-number) \"Sticky\")
162 ((eq type 'symbolic-name)
165 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
166 (concat (substring (match-string 1 tag) 0 1) \":\"
167 (substring (match-string 2 tag) 1 nil)))
168 (error tag))))) ; Fall-back to given tag name.
170 See also variable `vc-cvs-sticky-date-format-string'."
171 :type '(choice boolean function)
176 ;;; Internal variables
181 ;;; State-querying functions
184 ;;;###autoload (defun vc-cvs-registered (f)
185 ;;;###autoload (when (file-readable-p (expand-file-name
186 ;;;###autoload "CVS/Entries" (file-name-directory f)))
187 ;;;###autoload (load "vc-cvs")
188 ;;;###autoload (vc-cvs-registered f)))
190 (defun vc-cvs-registered (file)
191 "Check if FILE is CVS registered."
192 (let ((dirname (or (file-name-directory file) ""))
193 (basename (file-name-nondirectory file))
194 ;; make sure that the file name is searched case-sensitively
195 (case-fold-search nil))
196 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
197 (or (string= basename "")
199 (vc-cvs-get-entries dirname)
200 (goto-char (point-min))
201 (cond ((re-search-forward
202 (concat "^/" (regexp-quote basename) "/[^/]") nil t)
204 (vc-cvs-parse-entry file)
209 (defun vc-cvs-state (file)
210 "CVS-specific version of `vc-state'."
211 (if (vc-stay-local-p file)
212 (let ((state (vc-file-getprop file 'vc-state)))
213 ;; If we should stay local, use the heuristic but only if
214 ;; we don't have a more precise state already available.
215 (if (memq state '(up-to-date edited nil))
216 (vc-cvs-state-heuristic file)
219 (cd (file-name-directory file))
220 (vc-cvs-command t 0 file "status")
221 (vc-cvs-parse-status t))))
223 (defun vc-cvs-state-heuristic (file)
224 "CVS-specific state heuristic."
225 ;; If the file has not changed since checkout, consider it `up-to-date'.
226 ;; Otherwise consider it `edited'.
227 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
228 (lastmod (nth 5 (file-attributes file))))
230 ((equal checkout-time lastmod) 'up-to-date)
231 ((string= (vc-working-revision file) "0") 'added)
232 ((null checkout-time) 'unregistered)
235 (defun vc-cvs-working-revision (file)
236 "CVS-specific version of `vc-working-revision'."
237 ;; There is no need to consult RCS headers under CVS, because we
238 ;; get the workfile version for free when we recognize that a file
239 ;; is registered in CVS.
240 (vc-cvs-registered file)
241 (vc-file-getprop file 'vc-working-revision))
243 (defun vc-cvs-mode-line-string (file)
244 "Return string for placement into the modeline for FILE.
245 Compared to the default implementation, this function does two things:
246 Handle the special case of a CVS file that is added but not yet
247 committed and support display of sticky tags."
248 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
251 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
253 (get-text-property 0 'help-echo def-ml))
256 (if (zerop (length sticky-tag))
258 (setq help-echo (format "%s on the '%s' branch"
259 help-echo sticky-tag))
260 (concat string "[" sticky-tag "]"))
261 'help-echo help-echo)))
265 ;;; State-changing functions
268 (defun vc-cvs-register (files &optional rev comment)
269 "Register FILES into the CVS version-control system.
270 COMMENT can be used to provide an initial description of FILES.
272 `vc-register-switches' and `vc-cvs-register-switches' are passed to
273 the CVS command (in that order)."
274 ;; Register the directories if needed.
277 (and (not (vc-cvs-responsible-p file))
278 (vc-cvs-could-register file)
279 (push (directory-file-name (file-name-directory file)) dirs)))
280 (if dirs (vc-cvs-register dirs)))
281 (apply 'vc-cvs-command nil 0 files
283 (and comment (string-match "[^\t\n ]" comment)
284 (concat "-m" comment))
285 (vc-switches 'CVS 'register)))
287 (defun vc-cvs-responsible-p (file)
288 "Return non-nil if CVS thinks it is responsible for FILE."
289 (file-directory-p (expand-file-name "CVS"
290 (if (file-directory-p file)
292 (file-name-directory file)))))
294 (defun vc-cvs-could-register (file)
295 "Return non-nil if FILE could be registered in CVS.
296 This is only possible if CVS is managing FILE's directory or one of
299 (while (and (stringp dir)
300 (not (equal dir (setq dir (file-name-directory dir))))
302 (setq dir (if (file-directory-p
303 (expand-file-name "CVS/Entries" dir))
304 t (directory-file-name dir))))
307 (defun vc-cvs-checkin (files rev comment)
308 "CVS-specific version of `vc-backend-checkin'."
309 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
310 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
311 (error "%s is not a valid symbolic tag name" rev)
312 ;; If the input revison is a valid symbolic tag name, we create it
313 ;; as a branch, commit and switch to it.
314 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
315 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
316 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
318 (let ((status (apply 'vc-cvs-command nil 1 files
319 "ci" (if rev (concat "-r" rev))
320 (concat "-m" comment)
321 (vc-switches 'CVS 'checkin))))
323 (goto-char (point-min))
324 (when (not (zerop status))
325 ;; Check checkin problem.
327 ((re-search-forward "Up-to-date check failed" nil t)
328 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
330 (error "%s" (substitute-command-keys
331 (concat "Up-to-date check failed: "
332 "type \\[vc-next-action] to merge in changes"))))
334 (pop-to-buffer (current-buffer))
335 (goto-char (point-min))
336 (shrink-window-if-larger-than-buffer)
337 (error "Check-in failed"))))
338 ;; Single-file commit? Then update the revision by parsing the buffer.
339 ;; Otherwise we can't necessarily tell what goes with what; clear
340 ;; its properties so they have to be refetched.
341 (if (= (length files) 1)
343 (car files) 'vc-working-revision
344 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
345 (mapc 'vc-file-clearprops files))
346 ;; Anyway, forget the checkout model of the file, because we might have
347 ;; guessed wrong when we found the file. After commit, we can
348 ;; tell it from the permissions of the file (see
349 ;; vc-cvs-checkout-model).
350 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
353 ;; if this was an explicit check-in (does not include creation of
354 ;; a branch), remove the sticky tag.
355 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
356 (vc-cvs-command nil 0 files "update" "-A"))))
358 (defun vc-cvs-find-revision (file rev buffer)
359 (apply 'vc-cvs-command
361 "-Q" ; suppress diagnostic output
363 (and rev (not (string= rev ""))
366 (vc-switches 'CVS 'checkout)))
368 (defun vc-cvs-checkout (file &optional editable rev)
369 "Checkout a revision of FILE into the working area.
370 EDITABLE non-nil means that the file should be writable.
371 REV is the revision to check out."
372 (message "Checking out %s..." file)
373 ;; Change buffers to get local value of vc-checkout-switches.
374 (with-current-buffer (or (get-file-buffer file) (current-buffer))
375 (if (and (file-exists-p file) (not rev))
376 ;; If no revision was specified, just make the file writable
377 ;; if necessary (using `cvs-edit' if requested).
378 (and editable (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
380 (vc-cvs-command nil 0 file "edit")
381 (set-file-modes file (logior (file-modes file) 128))
382 (if (equal file buffer-file-name) (toggle-read-only -1))))
383 ;; Check out a particular revision (or recreate the file).
384 (vc-file-setprop file 'vc-working-revision nil)
385 (apply 'vc-cvs-command nil 0 file
390 ;; default for verbose checkout: clear the
391 ;; sticky tag so that the actual update will
392 ;; get the head of the trunk
396 (vc-switches 'CVS 'checkout)))
398 (message "Checking out %s...done" file))
400 (defun vc-cvs-delete-file (file)
401 (vc-cvs-command nil 0 file "remove" "-f"))
403 (defun vc-cvs-revert (file &optional contents-done)
404 "Revert FILE to the working revision on which it was based."
405 (vc-default-revert 'CVS file contents-done)
406 (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
408 (vc-cvs-command nil 0 file "unedit")
409 ;; Make the file read-only by switching off all w-bits
410 (set-file-modes file (logand (file-modes file) 3950)))))
412 (defun vc-cvs-merge (file first-revision &optional second-revision)
413 "Merge changes into current working copy of FILE.
414 The changes are between FIRST-REVISION and SECOND-REVISION."
415 (vc-cvs-command nil 0 file
417 (concat "-j" first-revision)
418 (concat "-j" second-revision))
419 (vc-file-setprop file 'vc-state 'edited)
420 (with-current-buffer (get-buffer "*vc*")
421 (goto-char (point-min))
422 (if (re-search-forward "conflicts during merge" nil t)
424 (vc-file-setprop file 'vc-state 'conflict)
427 (vc-file-setprop file 'vc-state 'edited)
431 (defun vc-cvs-merge-news (file)
432 "Merge in any new changes made to FILE."
433 (message "Merging changes into %s..." file)
434 ;; (vc-file-setprop file 'vc-working-revision nil)
435 (vc-file-setprop file 'vc-checkout-time 0)
436 (vc-cvs-command nil nil file "update")
437 ;; Analyze the merge result reported by CVS, and set
438 ;; file properties accordingly.
439 (with-current-buffer (get-buffer "*vc*")
440 (goto-char (point-min))
441 ;; get new working revision
442 (if (re-search-forward
443 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
444 (vc-file-setprop file 'vc-working-revision (match-string 1))
445 (vc-file-setprop file 'vc-working-revision nil))
448 (if (eq (buffer-size) 0)
449 0 ;; there were no news; indicate success
450 (if (re-search-forward
451 (concat "^\\([CMUP] \\)?"
452 (regexp-quote (file-name-nondirectory file))
453 "\\( already contains the differences between \\)?")
456 ;; Merge successful, we are in sync with repository now
457 ((or (match-string 2)
458 (string= (match-string 1) "U ")
459 (string= (match-string 1) "P "))
460 (vc-file-setprop file 'vc-state 'up-to-date)
461 (vc-file-setprop file 'vc-checkout-time
462 (nth 5 (file-attributes file)))
463 0);; indicate success to the caller
464 ;; Merge successful, but our own changes are still in the file
465 ((string= (match-string 1) "M ")
466 (vc-file-setprop file 'vc-state 'edited)
467 0);; indicate success to the caller
468 ;; Conflicts detected!
470 (vc-file-setprop file 'vc-state 'conflict)
471 1);; signal the error to the caller
473 (pop-to-buffer "*vc*")
474 (error "Couldn't analyze cvs update result")))
475 (message "Merging changes into %s...done" file))))
477 (defun vc-cvs-modify-change-comment (files rev comment)
478 "Modify the change comments for FILES on a specified REV.
479 Will fail unless you have administrative privileges on the repo."
480 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
483 ;;; History functions
486 (defun vc-cvs-print-log (files &optional buffer)
487 "Get change logs associated with FILES."
488 ;; It's just the catenation of the individual logs.
491 (if (vc-stay-local-p files) 'async 0)
494 (defun vc-cvs-comment-history (file)
495 "Get comment history of a file."
496 (vc-call-backend 'RCS 'comment-history file))
498 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
499 "Get a difference report using CVS between two revisions of FILE."
500 (let* ((async (and (not vc-disable-async-diff)
501 (vc-stay-local-p files)))
502 (invoke-cvs-diff-list nil)
504 ;; Look through the file list and see if any files have backups
505 ;; that can be used to do a plain "diff" instead of "cvs diff".
509 (when (or (not ov) (string-equal ov ""))
510 (setq ov (vc-working-revision file)))
511 (when (string-equal nv "")
513 (let ((file-oldvers (vc-version-backup-file file ov))
514 (file-newvers (if (not nv)
516 (vc-version-backup-file file nv)))
517 (coding-system-for-read (vc-coding-system-for-diff file)))
518 (if (and file-oldvers file-newvers)
520 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
521 (append (if (listp diff-switches)
523 (list diff-switches))
524 (if (listp vc-diff-switches)
526 (list vc-diff-switches))
527 (list (file-relative-name file-oldvers)
528 (file-relative-name file-newvers))))
530 (push file invoke-cvs-diff-list)))))
531 (when invoke-cvs-diff-list
532 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
534 invoke-cvs-diff-list "diff"
535 (and oldvers (concat "-r" oldvers))
536 (and newvers (concat "-r" newvers))
537 (vc-switches 'CVS 'diff))))
538 (if async 1 status))) ; async diff, pessimistic assumption
540 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
542 (defun vc-cvs-annotate-process-filter (process string)
543 (setq string (concat (process-get process 'output) string))
544 (if (not (string-match vc-cvs-annotate-first-line-re string))
545 ;; Still waiting for the first real line.
546 (process-put process 'output string)
547 (let ((vc-filter (process-get process 'vc-filter)))
548 (set-process-filter process vc-filter)
549 (funcall vc-filter process (substring string (match-beginning 0))))))
551 (defun vc-cvs-annotate-command (file buffer &optional revision)
552 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
553 Optional arg REVISION is a revision to annotate from."
554 (vc-cvs-command buffer
555 (if (vc-stay-local-p file)
558 (if revision (concat "-r" revision)))
559 ;; Strip the leading few lines.
560 (let ((proc (get-buffer-process buffer)))
562 ;; If running asynchronously, use a process filter.
564 (process-put proc 'vc-filter (process-filter proc))
565 (set-process-filter proc 'vc-cvs-annotate-process-filter))
566 (with-current-buffer buffer
567 (goto-char (point-min))
568 (re-search-forward vc-cvs-annotate-first-line-re)
569 (delete-region (point-min) (1- (point)))))))
571 (declare-function vc-annotate-convert-time "vc-annotate" (time))
573 (defun vc-cvs-annotate-current-time ()
574 "Return the current time, based at midnight of the current day, and
575 encoded as fractional days."
576 (vc-annotate-convert-time
577 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
579 (defun vc-cvs-annotate-time ()
580 "Return the time of the next annotation (as fraction of days)
581 systime, or nil if there is none."
583 (cache (get-text-property bol 'vc-cvs-annotate-time))
584 (inhibit-read-only t)
585 (inhibit-modification-hooks t))
589 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
590 (let ((day (string-to-number (match-string 1)))
591 (month (cdr (assq (intern (match-string 2))
592 '((Jan . 1) (Feb . 2) (Mar . 3)
593 (Apr . 4) (May . 5) (Jun . 6)
594 (Jul . 7) (Aug . 8) (Sep . 9)
595 (Oct . 10) (Nov . 11) (Dec . 12)))))
596 (year (let ((tmp (string-to-number (match-string 3))))
597 ;; Years 0..68 are 2000..2068.
598 ;; Years 69..99 are 1969..1999.
599 (+ (cond ((> 69 tmp) 2000)
604 bol (1+ bol) 'vc-cvs-annotate-time
606 ;; Position at end makes for nicer overlay result.
607 ;; Don't put actual buffer pos here, but only relative
608 ;; distance, so we don't ever move backward in the
609 ;; goto-char below, even if the text is moved.
610 (- (match-end 0) (match-beginning 0))
611 (vc-annotate-convert-time
612 (encode-time 0 0 0 day month year))))))))
614 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
615 (cdr cache)))) ; days (float)
617 (defun vc-cvs-annotate-extract-revision-at-line ()
620 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
621 (line-end-position) t)
622 (match-string-no-properties 1)
629 (defun vc-cvs-create-tag (dir name branchp)
630 "Assign to DIR's current revision a given NAME.
631 If BRANCHP is non-nil, the name is created as a branch (and the current
632 workspace is immediately moved to that new branch)."
633 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
634 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
636 (defun vc-cvs-retrieve-tag (dir name update)
637 "Retrieve a tag at and below DIR.
638 NAME is the name of the tag; if it is empty, do a `cvs update'.
639 If UPDATE is non-nil, then update (resynch) any affected buffers."
640 (with-current-buffer (get-buffer-create "*vc*")
641 (let ((default-directory dir)
644 (if (or (not name) (string= name ""))
645 (vc-cvs-command t 0 nil "update")
646 (vc-cvs-command t 0 nil "update" "-r" name)
647 (setq sticky-tag name))
649 (goto-char (point-min))
651 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
652 (let* ((file (expand-file-name (match-string 2) dir))
653 (state (match-string 1))
654 (buffer (find-buffer-visiting file)))
657 ((or (string= state "U")
659 (vc-file-setprop file 'vc-state 'up-to-date)
660 (vc-file-setprop file 'vc-working-revision nil)
661 (vc-file-setprop file 'vc-checkout-time
662 (nth 5 (file-attributes file))))
663 ((or (string= state "M")
665 (vc-file-setprop file 'vc-state 'edited)
666 (vc-file-setprop file 'vc-working-revision nil)
667 (vc-file-setprop file 'vc-checkout-time 0)))
668 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
669 (vc-resynch-buffer file t t))))
670 (forward-line 1))))))
677 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
678 "Return non-nil if version backups should be made for FILE.")
680 (defun vc-cvs-check-headers ()
681 "Check if the current file has any headers in it."
683 (goto-char (point-min))
684 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
685 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
689 ;;; Internal functions
692 (defun vc-cvs-command (buffer okstatus files &rest flags)
693 "A wrapper around `vc-do-command' for use in vc-cvs.el.
694 The difference to vc-do-command is that this function always invokes `cvs',
695 and that it passes `vc-cvs-global-switches' to it before FLAGS."
696 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
697 (if (stringp vc-cvs-global-switches)
698 (cons vc-cvs-global-switches flags)
699 (append vc-cvs-global-switches
702 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
704 (defun vc-cvs-repository-hostname (dirname)
705 "Hostname of the CVS server associated to workarea DIRNAME."
706 (let ((rootname (expand-file-name "CVS/Root" dirname)))
707 (when (file-readable-p rootname)
709 (let ((coding-system-for-read
710 (or file-name-coding-system
711 default-file-name-coding-system)))
712 (vc-insert-file rootname))
713 (goto-char (point-min))
714 (nth 2 (vc-cvs-parse-root
715 (buffer-substring (point)
716 (line-end-position))))))))
718 (defun vc-cvs-parse-root (root)
719 "Split CVS ROOT specification string into a list of fields.
720 A CVS root specification of the form
721 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
722 is converted to a normalized record with the following structure:
723 \(METHOD USER HOSTNAME CVS-ROOT).
724 The default METHOD for a CVS root of the form
727 The default METHOD for a CVS root of the form
728 [USER@]HOSTNAME:/path/to/repository
730 For an empty string, nil is returned (invalid CVS root)."
731 ;; Split CVS root into colon separated fields (0-4).
732 ;; The `x:' makes sure, that leading colons are not lost;
733 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
734 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
735 (len (length root-list))
736 ;; All syntactic varieties will get a proper METHOD.
743 ;; Simple PATH => method `local'
745 (cons nil root-list)))
747 ;; [USER@]HOST:PATH => method `ext'
748 (and (not (equal (car root-list) ""))
749 (cons "ext" root-list)))
752 (cons (cadr root-list)
753 (cons nil (cddr root-list))))
755 ;; :METHOD:[USER@]HOST:PATH
758 (let ((method (car root-list))
759 (uhost (or (cadr root-list) ""))
760 (root (nth 2 root-list))
763 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
764 (setq user (match-string 1 uhost)
765 host (match-string 2 uhost))
770 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
772 (equal method "local")
773 (setq root (concat host ":" root) host))
774 ;; Normalize CVS root record
775 (list method user host root)))))
777 ;; XXX: This does not work correctly for subdirectories. "cvs status"
778 ;; information is context sensitive, it contains lines like:
779 ;; cvs status: Examining DIRNAME
780 ;; and the file entries after that don't show the full path.
781 ;; Because of this VC directory listings only show changed files
782 ;; at the top level for CVS.
783 (defun vc-cvs-parse-status (&optional full)
784 "Parse output of \"cvs status\" command in the current buffer.
785 Set file properties accordingly. Unless FULL is t, parse only
786 essential information. Note that this can never set the 'ignored
788 (let (file status missing)
789 (goto-char (point-min))
790 (while (looking-at "? \\(.*\\)")
791 (setq file (expand-file-name (match-string 1)))
792 (vc-file-setprop file 'vc-state 'unregistered)
794 (when (re-search-forward "^File: " nil t)
795 (when (setq missing (looking-at "no file "))
796 (goto-char (match-end 0)))
798 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
799 (setq file (expand-file-name (match-string 1)))
800 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
801 (match-string 1) "Unknown"))
804 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
805 \[\t ]+\\([0-9.]+\\)"
807 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
811 ((string-match "Up-to-date" status)
812 (vc-file-setprop file 'vc-checkout-time
813 (nth 5 (file-attributes file)))
815 ((string-match "Locally Modified" status) 'edited)
816 ((string-match "Needs Merge" status) 'needs-merge)
817 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
818 (if missing 'missing 'needs-update))
819 ((string-match "Locally Added" status) 'added)
820 ((string-match "Locally Removed" status) 'removed)
821 ((string-match "File had conflicts " status) 'conflict)
822 ((string-match "Unknown" status) 'unregistered)
825 (defun vc-cvs-after-dir-status (update-function)
826 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
827 ;; This needs a lot of testing.
834 (subdir default-directory))
835 (goto-char (point-min))
837 ;; Look for either a file entry, an unregistered file, or a
840 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: \\(Examining\\|nothing\\) .*\n\\)"
842 ;; FIXME: get rid of narrowing here.
843 (narrow-to-region (match-beginning 0) (match-end 0))
844 (goto-char (point-min))
846 (when (looking-at "cvs status: Examining \\(.+\\)")
847 (setq subdir (expand-file-name (match-string 1))))
848 ;; Unregistered files
849 (while (looking-at "? \\(.*\\)")
850 (setq file (file-relative-name
851 (expand-file-name (match-string 1) subdir)))
852 (push (list file 'unregistered) result)
854 (when (looking-at "cvs status: nothing known about")
855 ;; We asked about a non existent file. The output looks like this:
857 ;; cvs status: nothing known about `lisp/v.diff'
858 ;; ===================================================================
859 ;; File: no file v.diff Status: Unknown
861 ;; Working revision: No entry for v.diff
862 ;; Repository revision: No revision control file
865 ;; Due to narrowing in this iteration we only see the "cvs
866 ;; status:" line, so just set a flag so that we can ignore the
867 ;; file in the next iteration.
868 (setq ignore-next t))
870 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
871 (setq missing (match-string 1))
872 (setq file (file-relative-name
873 (expand-file-name (match-string 2) subdir)))
874 (setq status-str (match-string 3))
877 ((string-match "Up-to-date" status-str) 'up-to-date)
878 ((string-match "Locally Modified" status-str) 'edited)
879 ((string-match "Needs Merge" status-str) 'needs-merge)
880 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
881 (if missing 'missing 'needs-update))
882 ((string-match "Locally Added" status-str) 'added)
883 ((string-match "Locally Removed" status-str) 'removed)
884 ((string-match "File had conflicts " status-str) 'conflict)
885 ((string-match "Unknown" status-str) 'unregistered)
888 (setq ignore-next nil)
889 (unless (eq status 'up-to-date)
890 (push (list file status) result))))
891 (goto-char (point-max))
893 (funcall update-function result))
894 ;; Alternative implementation: use the "update" command instead of
895 ;; the "status" command.
896 ;; (let ((result nil)
897 ;; (translation '((?? . unregistered)
901 ;; (?P . needs-merge)
903 ;; (?U . needs-update))))
904 ;; (goto-char (point-min))
905 ;; (while (not (eobp))
906 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
907 ;; (push (list (match-string 1)
908 ;; (cdr (assoc (char-after) translation)))
911 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
913 ;; ;; cvs update: warning: FILENAME was lost
915 ;; (push (list (match-string 1) 'missing) result)
916 ;; ;; Skip the "U" line
918 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
919 ;; (push (list (match-string 1) 'unregistered) result))))
921 ;; (funcall update-function result)))
924 ;; Based on vc-cvs-dir-state-heuristic from Emacs 22.
925 (defun vc-cvs-dir-status-heuristic (dir update-function &optional basedir)
926 "Find the CVS state of all files in DIR, using only local information."
927 (let (file basename status result dirlist)
929 (vc-cvs-get-entries dir)
930 (goto-char (point-min))
932 (if (looking-at "D/\\([^/]*\\)////")
933 (push (expand-file-name (match-string 1) dir) dirlist)
934 ;; CVS-removed files are not taken under VC control.
935 (when (looking-at "/\\([^/]*\\)/[^/-]")
936 (setq basename (match-string 1)
937 file (expand-file-name basename dir)
938 status (or (vc-file-getprop file 'vc-state)
939 (vc-cvs-parse-entry file t)))
940 (unless (eq status 'up-to-date)
941 (push (list (if basedir
942 (file-relative-name file basedir)
946 (dolist (subdir dirlist)
947 (setq result (append result
948 (vc-cvs-dir-status-heuristic subdir nil
951 (funcall update-function result))))
953 (defun vc-cvs-dir-status (dir update-function)
954 "Create a list of conses (file . state) for DIR."
955 ;; FIXME check all files in DIR instead?
956 (if (vc-stay-local-p dir)
957 (vc-cvs-dir-status-heuristic dir update-function)
958 (vc-cvs-command (current-buffer) 'async dir "-f" "status")
959 ;; Alternative implementation: use the "update" command instead of
960 ;; the "status" command.
961 ;; (vc-cvs-command (current-buffer) 'async
962 ;; (file-relative-name dir)
963 ;; "-f" "-n" "update" "-d" "-P")
965 `(vc-cvs-after-dir-status (quote ,update-function)))))
967 (defun vc-cvs-dir-status-files (dir files default-state update-function)
968 "Create a list of conses (file . state) for DIR."
969 (apply 'vc-cvs-command (current-buffer) 'async dir "-f" "status" files)
971 `(vc-cvs-after-dir-status (quote ,update-function))))
973 (defun vc-cvs-file-to-string (file)
974 "Read the content of FILE and return it as a string."
977 (insert-file-contents file)
978 (goto-char (point-min))
979 (buffer-substring (point) (point-max)))
982 (defun vc-cvs-status-extra-headers (dir)
983 "Extract and represent per-directory properties of a CVS working copy."
987 (insert-file-contents "CVS/Root")
988 (goto-char (point-min))
989 (and (looking-at ":ext:") (delete-char 5))
990 (buffer-substring (point) (1- (point-max))))
995 (insert-file-contents "CVS/Repository")
996 (goto-char (point-min))
997 (re-search-forward "[^/\n]*" nil t)
998 (concat (match-string 0) "\n"))
1002 (concat (propertize "Module : " 'face 'font-lock-type-face)
1003 (propertize module 'face 'font-lock-variable-name-face)))
1006 (concat (propertize "Repository : " 'face 'font-lock-type-face)
1007 (propertize repo 'face 'font-lock-variable-name-face)))
1009 ;; In CVS, branch is a per-file property, not a per-directory property.
1010 ;; We can't really do this here without making dangerous assumptions.
1011 ;;(propertize "Branch: " 'face 'font-lock-type-face)
1012 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
1013 ;; 'face 'font-lock-warning-face)
1016 (defun vc-cvs-get-entries (dir)
1017 "Insert the CVS/Entries file from below DIR into the current buffer.
1018 This function ensures that the correct coding system is used for that,
1019 which may not be the one that is used for the files' contents.
1020 CVS/Entries should only be accessed through this function."
1021 (let ((coding-system-for-read (or file-name-coding-system
1022 default-file-name-coding-system)))
1023 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
1025 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
1026 "Return non-nil if TAG is a valid symbolic tag name."
1027 ;; According to the CVS manual, a valid symbolic tag must start with
1028 ;; an uppercase or lowercase letter and can contain uppercase and
1029 ;; lowercase letters, digits, `-', and `_'.
1030 (and (string-match "^[a-zA-Z]" tag)
1031 (not (string-match "[^a-z0-9A-Z-_]" tag))))
1033 (defun vc-cvs-valid-revision-number-p (tag)
1034 "Return non-nil if TAG is a valid revision number."
1035 (and (string-match "^[0-9]" tag)
1036 (not (string-match "[^0-9.]" tag))))
1038 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
1039 "Parse and return the sticky tag as a string.
1040 `match-data' is protected."
1041 (let ((data (match-data))
1043 (type (cond ((string= match-type "D") 'date)
1044 ((string= match-type "T")
1045 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
1052 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1055 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1057 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1058 (month (string-to-number (match-string 2 match-tag)))
1059 (day (string-to-number (match-string 3 match-tag)))
1060 (hour (string-to-number (match-string 4 match-tag)))
1061 (min (string-to-number (match-string 5 match-tag)))
1062 (sec (string-to-number (match-string 6 match-tag)))
1063 ;; Years 0..68 are 2000..2068.
1064 ;; Years 69..99 are 1969..1999.
1065 (year (+ (cond ((> 69 year-tmp) 2000)
1066 ((> 100 year-tmp) 1900)
1069 (setq tag (encode-time sec min hour day month year))))
1070 ;; Sticky Tag name or revision number
1071 ((eq type 'symbolic-name) (setq tag match-tag))
1072 ((eq type 'revision-number) (setq tag match-tag))
1073 ;; Default is no sticky tag at all
1075 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1076 ((eq vc-cvs-sticky-tag-display t)
1077 (cond ((eq type 'date) (format-time-string
1078 vc-cvs-sticky-date-format-string
1080 ((eq type 'symbolic-name) tag)
1081 ((eq type 'revision-number) tag)
1083 ((functionp vc-cvs-sticky-tag-display)
1084 (funcall vc-cvs-sticky-tag-display tag type))
1087 (set-match-data data))))
1089 (defun vc-cvs-parse-entry (file &optional set-state)
1090 "Parse a line from CVS/Entries.
1091 Compare modification time to that of the FILE, set file properties
1092 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1095 ;; entry for a "locally added" file (not yet committed)
1096 ((looking-at "/[^/]+/0/")
1097 (vc-file-setprop file 'vc-checkout-time 0)
1098 (vc-file-setprop file 'vc-working-revision "0")
1099 (if set-state (vc-file-setprop file 'vc-state 'added)))
1105 ;; timestamp and optional conflict field
1110 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1111 "\\(.*\\)")) ;Sticky tag
1112 (vc-file-setprop file 'vc-working-revision (match-string 1))
1113 (vc-file-setprop file 'vc-cvs-sticky-tag
1114 (vc-cvs-parse-sticky-tag (match-string 4)
1116 ;; Compare checkout time and modification time.
1117 ;; This is intentionally different from the algorithm that CVS uses
1118 ;; (which is based on textual comparison), because there can be problems
1119 ;; generating a time string that looks exactly like the one from CVS.
1120 (let ((mtime (nth 5 (file-attributes file))))
1121 (require 'parse-time)
1123 (parse-time-string (concat (match-string 2) " +0000"))))
1124 (cond ((and (not (string-match "\\+" (match-string 2)))
1126 (equal mtime (apply 'encode-time parsed-time)))
1127 (vc-file-setprop file 'vc-checkout-time mtime)
1128 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1130 (vc-file-setprop file 'vc-checkout-time 0)
1131 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
1133 ;; Completion of revision names.
1134 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1135 ;; `cvs log' so I can list all the revision numbers rather than only
1138 (defun vc-cvs-revision-table (file)
1139 (let ((default-directory (file-name-directory file))
1142 (vc-cvs-command t nil file "log")
1143 (goto-char (point-min))
1144 (when (re-search-forward "^symbolic names:\n" nil t)
1145 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1146 (push (cons (match-string 1) (match-string 2)) res)
1148 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1149 (push (match-string 1) res))
1152 (defun vc-cvs-revision-completion-table (files)
1153 (lexical-let ((files files)
1155 (setq table (lazy-completion-table
1156 table (lambda () (vc-cvs-revision-table (car files)))))
1162 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
1163 ;;; vc-cvs.el ends here