(Info-select-node): Doc fix.
[emacs.git] / lisp / vc-cvs.el
blobe2374c0e7f41cf3de96b40204a7f2d2ed9bcbf73
1 ;;; vc-cvs.el --- non-resident support for CVS version-control
3 ;; Copyright (C) 1995,98,99,2000,2001,2002 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; $Id: vc-cvs.el,v 1.60 2003/05/09 14:32:01 monnier Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;;; Code:
31 (eval-when-compile
32 (require 'vc))
34 ;;;
35 ;;; Customization options
36 ;;;
38 (defcustom vc-cvs-global-switches nil
39 "*Global switches to pass to any CVS command."
40 :type '(choice (const :tag "None" nil)
41 (string :tag "Argument String")
42 (repeat :tag "Argument List"
43 :value ("")
44 string))
45 :version "21.4"
46 :group 'vc)
48 (defcustom vc-cvs-register-switches nil
49 "*Extra switches for registering a file into CVS.
50 A string or list of strings passed to the checkin program by
51 \\[vc-register]."
52 :type '(choice (const :tag "None" nil)
53 (string :tag "Argument String")
54 (repeat :tag "Argument List"
55 :value ("")
56 string))
57 :version "21.1"
58 :group 'vc)
60 (defcustom vc-cvs-diff-switches nil
61 "*A string or list of strings specifying extra switches for cvs diff under VC."
62 :type '(choice (const :tag "None" nil)
63 (string :tag "Argument String")
64 (repeat :tag "Argument List"
65 :value ("")
66 string))
67 :version "21.1"
68 :group 'vc)
70 (defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
71 "*Header keywords to be inserted by `vc-insert-headers'."
72 :version "21.1"
73 :type '(repeat string)
74 :group 'vc)
76 (defcustom vc-cvs-use-edit t
77 "*Non-nil means to use `cvs edit' to \"check out\" a file.
78 This is only meaningful if you don't use the implicit checkout model
79 \(i.e. if you have $CVSREAD set)."
80 :type 'boolean
81 :version "21.1"
82 :group 'vc)
84 (defcustom vc-cvs-stay-local t
85 "*Non-nil means use local operations when possible for remote repositories.
86 This avoids slow queries over the network and instead uses heuristics
87 and past information to determine the current status of a file.
89 The value can also be a regular expression or list of regular
90 expressions to match against the host name of a repository; then VC
91 only stays local for hosts that match it. Alternatively, the value
92 can be a list of regular expressions where the first element is the
93 symbol `except'; then VC always stays local except for hosts matched
94 by these regular expressions."
95 :type '(choice (const :tag "Always stay local" t)
96 (const :tag "Don't stay local" nil)
97 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
98 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
99 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
100 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
101 :version "21.1"
102 :group 'vc)
104 (defcustom vc-cvs-sticky-date-format-string "%c"
105 "*Format string for mode-line display of sticky date.
106 Format is according to `format-time-string'. Only used if
107 `vc-cvs-sticky-tag-display' is t."
108 :type '(string)
109 :version "21.4"
110 :group 'vc)
112 (defcustom vc-cvs-sticky-tag-display t
113 "*Specify the mode-line display of sticky tags.
114 Value t means default display, nil means no display at all. If the
115 value is a function or macro, it is called with the sticky tag and
116 its' type as parameters, in that order. TYPE can have three different
117 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
118 string) and `date' (TAG is a date as returned by `encode-time'). The
119 return value of the function or macro will be displayed as a string.
121 Here's an example that will display the formatted date for sticky
122 dates and the word \"Sticky\" for sticky tag names and revisions.
124 (lambda (tag type)
125 (cond ((eq type 'date) (format-time-string
126 vc-cvs-sticky-date-format-string tag))
127 ((eq type 'revision-number) \"Sticky\")
128 ((eq type 'symbolic-name) \"Sticky\")))
130 Here's an example that will abbreviate to the first character only,
131 any text before the first occurrence of `-' for sticky symbolic tags.
132 If the sticky tag is a revision number, the word \"Sticky\" is
133 displayed. Date and time is displayed for sticky dates.
135 (lambda (tag type)
136 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
137 ((eq type 'revision-number) \"Sticky\")
138 ((eq type 'symbolic-name)
139 (condition-case nil
140 (progn
141 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
142 (concat (substring (match-string 1 tag) 0 1) \":\"
143 (substring (match-string 2 tag) 1 nil)))
144 (error tag))))) ; Fall-back to given tag name.
146 See also variable `vc-cvs-sticky-date-format-string'."
147 :type '(choice boolean function)
148 :version "21.4"
149 :group 'vc)
152 ;;; Internal variables
155 (defvar vc-cvs-local-month-numbers
156 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
157 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
158 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
159 "Local association list of month numbers.")
163 ;;; State-querying functions
166 ;;;###autoload (defun vc-cvs-registered (f)
167 ;;;###autoload (when (file-readable-p (expand-file-name
168 ;;;###autoload "CVS/Entries" (file-name-directory f)))
169 ;;;###autoload (load "vc-cvs")
170 ;;;###autoload (vc-cvs-registered f)))
172 (defun vc-cvs-registered (file)
173 "Check if FILE is CVS registered."
174 (let ((dirname (or (file-name-directory file) ""))
175 (basename (file-name-nondirectory file))
176 ;; make sure that the file name is searched case-sensitively
177 (case-fold-search nil))
178 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
179 (with-temp-buffer
180 (vc-cvs-get-entries dirname)
181 (goto-char (point-min))
182 (cond
183 ((re-search-forward
184 ;; CVS-removed files are not taken under VC control.
185 (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
186 (beginning-of-line)
187 (vc-cvs-parse-entry file)
189 (t nil)))
190 nil)))
192 (defun vc-cvs-state (file)
193 "CVS-specific version of `vc-state'."
194 (if (vc-cvs-stay-local-p file)
195 (let ((state (vc-file-getprop file 'vc-state)))
196 ;; If we should stay local, use the heuristic but only if
197 ;; we don't have a more precise state already available.
198 (if (memq state '(up-to-date edited))
199 (vc-cvs-state-heuristic file)
200 state))
201 (with-temp-buffer
202 (cd (file-name-directory file))
203 (vc-cvs-command t 0 file "status")
204 (vc-cvs-parse-status t))))
206 (defun vc-cvs-state-heuristic (file)
207 "CVS-specific state heuristic."
208 ;; If the file has not changed since checkout, consider it `up-to-date'.
209 ;; Otherwise consider it `edited'.
210 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
211 (lastmod (nth 5 (file-attributes file))))
212 (if (equal checkout-time lastmod)
213 'up-to-date
214 'edited)))
216 (defun vc-cvs-dir-state (dir)
217 "Find the CVS state of all files in DIR."
218 ;; if DIR is not under CVS control, don't do anything.
219 (when (file-readable-p (expand-file-name "CVS/Entries" dir))
220 (if (vc-cvs-stay-local-p dir)
221 (vc-cvs-dir-state-heuristic dir)
222 (let ((default-directory dir))
223 ;; Don't specify DIR in this command, the default-directory is
224 ;; enough. Otherwise it might fail with remote repositories.
225 (with-temp-buffer
226 (vc-cvs-command t 0 nil "status" "-l")
227 (goto-char (point-min))
228 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
229 (narrow-to-region (match-beginning 0) (match-end 0))
230 (vc-cvs-parse-status)
231 (goto-char (point-max))
232 (widen)))))))
234 (defun vc-cvs-workfile-version (file)
235 "CVS-specific version of `vc-workfile-version'."
236 ;; There is no need to consult RCS headers under CVS, because we
237 ;; get the workfile version for free when we recognize that a file
238 ;; is registered in CVS.
239 (vc-cvs-registered file)
240 (vc-file-getprop file 'vc-workfile-version))
242 (defun vc-cvs-checkout-model (file)
243 "CVS-specific version of `vc-checkout-model'."
244 (if (or (getenv "CVSREAD")
245 ;; If the file is not writable (despite CVSREAD being
246 ;; undefined), this is probably because the file is being
247 ;; "watched" by other developers.
248 ;; (If vc-mistrust-permissions was t, we actually shouldn't
249 ;; trust this, but there is no other way to learn this from CVS
250 ;; at the moment (version 1.9).)
251 (string-match "r-..-..-." (nth 8 (file-attributes file))))
252 'announce
253 'implicit))
255 (defun vc-cvs-mode-line-string (file)
256 "Return string for placement into the modeline for FILE.
257 Compared to the default implementation, this function does two things:
258 Handle the special case of a CVS file that is added but not yet
259 committed and support display of sticky tags."
260 (let ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
261 (string (if (string= (vc-workfile-version file) "0")
262 ;; A file that is added but not yet committed.
263 "CVS @@"
264 (vc-default-mode-line-string 'CVS file))))
265 (if (zerop (length sticky-tag))
266 string
267 (concat string "[" sticky-tag "]"))))
269 (defun vc-cvs-dired-state-info (file)
270 "CVS-specific version of `vc-dired-state-info'."
271 (let ((cvs-state (vc-state file)))
272 (cond ((eq cvs-state 'edited)
273 (if (equal (vc-workfile-version file) "0")
274 "(added)" "(modified)"))
275 ((eq cvs-state 'needs-patch) "(patch)")
276 ((eq cvs-state 'needs-merge) "(merge)"))))
280 ;;; State-changing functions
283 (defun vc-cvs-register (file &optional rev comment)
284 "Register FILE into the CVS version-control system.
285 COMMENT can be used to provide an initial description of FILE.
287 `vc-register-switches' and `vc-cvs-register-switches' are passed to
288 the CVS command (in that order)."
289 (apply 'vc-cvs-command nil 0 file
290 "add"
291 (and comment (string-match "[^\t\n ]" comment)
292 (concat "-m" comment))
293 (vc-switches 'CVS 'register)))
295 (defun vc-cvs-responsible-p (file)
296 "Return non-nil if CVS thinks it is responsible for FILE."
297 (file-directory-p (expand-file-name "CVS"
298 (if (file-directory-p file)
299 file
300 (file-name-directory file)))))
302 (defalias 'vc-cvs-could-register 'vc-cvs-responsible-p
303 "Return non-nil if FILE could be registered in CVS.
304 This is only possible if CVS is responsible for FILE's directory.")
306 (defun vc-cvs-checkin (file rev comment)
307 "CVS-specific version of `vc-backend-checkin'."
308 (unless (or (not rev) (vc-cvs-valid-version-number-p rev))
309 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
310 (error "%s is not a valid symbolic tag name" rev)
311 ;; If the input revison is a valid symbolic tag name, we create it
312 ;; as a branch, commit and switch to it.
313 (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
314 (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
315 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
316 (let ((status (apply 'vc-cvs-command nil 1 file
317 "ci" (if rev (concat "-r" rev))
318 (concat "-m" comment)
319 (vc-switches 'CVS 'checkin))))
320 (set-buffer "*vc*")
321 (goto-char (point-min))
322 (when (not (zerop status))
323 ;; Check checkin problem.
324 (cond
325 ((re-search-forward "Up-to-date check failed" nil t)
326 (vc-file-setprop file 'vc-state 'needs-merge)
327 (error (substitute-command-keys
328 (concat "Up-to-date check failed: "
329 "type \\[vc-next-action] to merge in changes"))))
331 (pop-to-buffer (current-buffer))
332 (goto-char (point-min))
333 (shrink-window-if-larger-than-buffer)
334 (error "Check-in failed"))))
335 ;; Update file properties
336 (vc-file-setprop
337 file 'vc-workfile-version
338 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
339 ;; Forget the checkout model of the file, because we might have
340 ;; guessed wrong when we found the file. After commit, we can
341 ;; tell it from the permissions of the file (see
342 ;; vc-cvs-checkout-model).
343 (vc-file-setprop file 'vc-checkout-model nil)
345 ;; if this was an explicit check-in (does not include creation of
346 ;; a branch), remove the sticky tag.
347 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
348 (vc-cvs-command nil 0 file "update" "-A"))))
350 (defun vc-cvs-find-version (file rev buffer)
351 (apply 'vc-cvs-command
352 buffer 0 file
353 "-Q" ; suppress diagnostic output
354 "update"
355 (and rev (not (string= rev ""))
356 (concat "-r" rev))
357 "-p"
358 (vc-switches 'CVS 'checkout)))
360 (defun vc-cvs-checkout (file &optional editable rev workfile)
361 "Retrieve a revision of FILE into a WORKFILE.
362 EDITABLE non-nil means that the file should be writable.
363 REV is the revision to check out into WORKFILE."
364 (let ((filename (or workfile file))
365 (file-buffer (get-file-buffer file))
366 switches)
367 (message "Checking out %s..." filename)
368 (save-excursion
369 ;; Change buffers to get local value of vc-checkout-switches.
370 (if file-buffer (set-buffer file-buffer))
371 (setq switches (vc-switches 'CVS 'checkout))
372 ;; Save this buffer's default-directory
373 ;; and use save-excursion to make sure it is restored
374 ;; in the same buffer it was saved in.
375 (let ((default-directory default-directory))
376 (save-excursion
377 ;; Adjust the default-directory so that the check-out creates
378 ;; the file in the right place.
379 (setq default-directory (file-name-directory filename))
380 (if workfile
381 (let ((failed t)
382 (backup-name (if (string= file workfile)
383 (car (find-backup-file-name filename)))))
384 (when backup-name
385 (copy-file filename backup-name
386 'ok-if-already-exists 'keep-date)
387 (unless (file-writable-p filename)
388 (set-file-modes filename
389 (logior (file-modes filename) 128))))
390 (unwind-protect
391 (progn
392 (let ((coding-system-for-read 'no-conversion)
393 (coding-system-for-write 'no-conversion))
394 (with-temp-file filename
395 (apply 'vc-cvs-command
396 (current-buffer) 0 file
397 "-Q" ; suppress diagnostic output
398 "update"
399 (and (stringp rev)
400 (not (string= rev ""))
401 (concat "-r" rev))
402 "-p"
403 switches)))
404 (setq failed nil))
405 (if failed
406 (if backup-name
407 (rename-file backup-name filename
408 'ok-if-already-exists)
409 (if (file-exists-p filename)
410 (delete-file filename)))
411 (and backup-name
412 (not vc-make-backup-files)
413 (delete-file backup-name)))))
414 (if (and (file-exists-p file) (not rev))
415 ;; If no revision was specified, just make the file writable
416 ;; if necessary (using `cvs-edit' if requested).
417 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
418 (if vc-cvs-use-edit
419 (vc-cvs-command nil 0 file "edit")
420 (set-file-modes file (logior (file-modes file) 128))
421 (if file-buffer (toggle-read-only -1))))
422 ;; Check out a particular version (or recreate the file).
423 (vc-file-setprop file 'vc-workfile-version nil)
424 (apply 'vc-cvs-command nil 0 file
425 (and editable
426 (or (not (file-exists-p file))
427 (not (eq (vc-cvs-checkout-model file)
428 'implicit)))
429 "-w")
430 "update"
431 (when rev
432 (unless (eq rev t)
433 ;; default for verbose checkout: clear the
434 ;; sticky tag so that the actual update will
435 ;; get the head of the trunk
436 (if (string= rev "")
437 "-A"
438 (concat "-r" rev))))
439 switches))))
440 (vc-mode-line file)
441 (message "Checking out %s...done" filename)))))
443 (defun vc-cvs-delete-file (file)
444 (vc-cvs-command nil 0 file "remove" "-f"))
446 (defun vc-cvs-rename-file (old new)
447 ;; CVS doesn't know how to move files, so we just remove&add.
448 (condition-case nil
449 (add-name-to-file old new)
450 (error (rename-file old new)))
451 (vc-cvs-delete-file old)
452 (with-current-buffer (find-file-noselect new)
453 (vc-register)))
455 (defun vc-cvs-revert (file &optional contents-done)
456 "Revert FILE to the version it was based on."
457 (unless contents-done
458 ;; Check out via standard output (caused by the final argument
459 ;; FILE below), so that no sticky tag is set.
460 (vc-cvs-checkout file nil (vc-workfile-version file) file))
461 (unless (eq (vc-checkout-model file) 'implicit)
462 (if vc-cvs-use-edit
463 (vc-cvs-command nil 0 file "unedit")
464 ;; Make the file read-only by switching off all w-bits
465 (set-file-modes file (logand (file-modes file) 3950)))))
467 (defun vc-cvs-merge (file first-version &optional second-version)
468 "Merge changes into current working copy of FILE.
469 The changes are between FIRST-VERSION and SECOND-VERSION."
470 (vc-cvs-command nil 0 file
471 "update" "-kk"
472 (concat "-j" first-version)
473 (concat "-j" second-version))
474 (vc-file-setprop file 'vc-state 'edited)
475 (with-current-buffer (get-buffer "*vc*")
476 (goto-char (point-min))
477 (if (re-search-forward "conflicts during merge" nil t)
478 1 ; signal error
479 0))) ; signal success
481 (defun vc-cvs-merge-news (file)
482 "Merge in any new changes made to FILE."
483 (message "Merging changes into %s..." file)
484 ;; (vc-file-setprop file 'vc-workfile-version nil)
485 (vc-file-setprop file 'vc-checkout-time 0)
486 (vc-cvs-command nil 0 file "update")
487 ;; Analyze the merge result reported by CVS, and set
488 ;; file properties accordingly.
489 (with-current-buffer (get-buffer "*vc*")
490 (goto-char (point-min))
491 ;; get new workfile version
492 (if (re-search-forward
493 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
494 (vc-file-setprop file 'vc-workfile-version (match-string 1))
495 (vc-file-setprop file 'vc-workfile-version nil))
496 ;; get file status
497 (prog1
498 (if (eq (buffer-size) 0)
499 0 ;; there were no news; indicate success
500 (if (re-search-forward
501 (concat "^\\([CMUP] \\)?"
502 (regexp-quote (file-name-nondirectory file))
503 "\\( already contains the differences between \\)?")
504 nil t)
505 (cond
506 ;; Merge successful, we are in sync with repository now
507 ((or (match-string 2)
508 (string= (match-string 1) "U ")
509 (string= (match-string 1) "P "))
510 (vc-file-setprop file 'vc-state 'up-to-date)
511 (vc-file-setprop file 'vc-checkout-time
512 (nth 5 (file-attributes file)))
513 0);; indicate success to the caller
514 ;; Merge successful, but our own changes are still in the file
515 ((string= (match-string 1) "M ")
516 (vc-file-setprop file 'vc-state 'edited)
517 0);; indicate success to the caller
518 ;; Conflicts detected!
520 (vc-file-setprop file 'vc-state 'edited)
521 1);; signal the error to the caller
523 (pop-to-buffer "*vc*")
524 (error "Couldn't analyze cvs update result")))
525 (message "Merging changes into %s...done" file))))
529 ;;; History functions
532 (defun vc-cvs-print-log (file)
533 "Get change log associated with FILE."
534 (vc-cvs-command
536 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
537 file "log"))
539 (defun vc-cvs-diff (file &optional oldvers newvers)
540 "Get a difference report using CVS between two versions of FILE."
541 (if (string= (vc-workfile-version file) "0")
542 ;; This file is added but not yet committed; there is no master file.
543 (if (or oldvers newvers)
544 (error "No revisions of %s exist" file)
545 ;; We regard this as "changed".
546 ;; Diff it against /dev/null.
547 ;; Note: this is NOT a "cvs diff".
548 (apply 'vc-do-command "*vc-diff*"
549 1 "diff" file
550 (append (vc-switches nil 'diff) '("/dev/null")))
551 ;; Even if it's empty, it's locally modified.
553 (let* ((async (and (vc-cvs-stay-local-p file) (fboundp 'start-process)))
554 (status (apply 'vc-cvs-command "*vc-diff*"
555 (if async 'async 1)
556 file "diff"
557 (and oldvers (concat "-r" oldvers))
558 (and newvers (concat "-r" newvers))
559 (vc-switches 'CVS 'diff))))
560 (if async 1 status)))) ; async diff, pessimistic assumption
562 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
563 "Diff all files at and below DIR."
564 (with-current-buffer "*vc-diff*"
565 (setq default-directory dir)
566 (if (vc-cvs-stay-local-p dir)
567 ;; local diff: do it filewise, and only for files that are modified
568 (vc-file-tree-walk
570 (lambda (f)
571 (vc-exec-after
572 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
573 ;; possible optimization: fetch the state of all files
574 ;; in the tree via vc-cvs-dir-state-heuristic
575 (unless (vc-up-to-date-p ',f)
576 (message "Looking at %s" ',f)
577 (vc-diff-internal ',f ',rev1 ',rev2))))))
578 ;; cvs diff: use a single call for the entire tree
579 (let ((coding-system-for-read
580 (or coding-system-for-read 'undecided)))
581 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
582 (and rev1 (concat "-r" rev1))
583 (and rev2 (concat "-r" rev2))
584 (vc-switches 'CVS 'diff))))))
586 (defun vc-cvs-annotate-command (file buffer &optional version)
587 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
588 Optional arg VERSION is a version to annotate from."
589 (vc-cvs-command buffer 0 file "annotate" (if version (concat "-r" version))))
591 (defun vc-cvs-annotate-current-time ()
592 "Return the current time, based at midnight of the current day, and
593 encoded as fractional days."
594 (vc-annotate-convert-time
595 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
597 (defun vc-cvs-annotate-time ()
598 "Return the time of the next annotation (as fraction of days)
599 systime, or nil if there is none."
600 (let ((time-stamp
601 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
602 (if (looking-at time-stamp)
603 (progn
604 (let* ((day (string-to-number (match-string 1)))
605 (month (cdr (assoc (match-string 2)
606 vc-cvs-local-month-numbers)))
607 (year-tmp (string-to-number (match-string 3)))
608 ;; Years 0..68 are 2000..2068.
609 ;; Years 69..99 are 1969..1999.
610 (year (+ (cond ((> 69 year-tmp) 2000)
611 ((> 100 year-tmp) 1900)
612 (t 0))
613 year-tmp)))
614 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
615 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
616 ;; If we did not look directly at an annotation, there might be
617 ;; some further down. This is the case if we are positioned at
618 ;; the very top of the buffer, for instance.
619 (if (re-search-forward time-stamp nil t)
620 (progn
621 (beginning-of-line nil)
622 (vc-cvs-annotate-time))))))
625 ;;; Snapshot system
628 (defun vc-cvs-create-snapshot (dir name branchp)
629 "Assign to DIR's current version a given NAME.
630 If BRANCHP is non-nil, the name is created as a branch (and the current
631 workspace is immediately moved to that new branch)."
632 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
633 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
635 (defun vc-cvs-retrieve-snapshot (dir name update)
636 "Retrieve a snapshot at and below DIR.
637 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
638 If UPDATE is non-nil, then update (resynch) any affected buffers."
639 (with-current-buffer (get-buffer-create "*vc*")
640 (let ((default-directory dir)
641 (sticky-tag))
642 (erase-buffer)
643 (if (or (not name) (string= name ""))
644 (vc-cvs-command t 0 nil "update")
645 (vc-cvs-command t 0 nil "update" "-r" name)
646 (setq sticky-tag name))
647 (when update
648 (goto-char (point-min))
649 (while (not (eobp))
650 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
651 (let* ((file (expand-file-name (match-string 2) dir))
652 (state (match-string 1))
653 (buffer (find-buffer-visiting file)))
654 (when buffer
655 (cond
656 ((or (string= state "U")
657 (string= state "P"))
658 (vc-file-setprop file 'vc-state 'up-to-date)
659 (vc-file-setprop file 'vc-workfile-version nil)
660 (vc-file-setprop file 'vc-checkout-time
661 (nth 5 (file-attributes file))))
662 ((or (string= state "M")
663 (string= state "C"))
664 (vc-file-setprop file 'vc-state 'edited)
665 (vc-file-setprop file 'vc-workfile-version nil)
666 (vc-file-setprop file 'vc-checkout-time 0)))
667 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
668 (vc-resynch-buffer file t t))))
669 (forward-line 1))))))
673 ;;; Miscellaneous
676 (defalias 'vc-cvs-make-version-backups-p 'vc-cvs-stay-local-p
677 "Return non-nil if version backups should be made for FILE.")
679 (defun vc-cvs-check-headers ()
680 "Check if the current file has any headers in it."
681 (save-excursion
682 (goto-char (point-min))
683 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
684 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
688 ;;; Internal functions
691 (defun vc-cvs-command (buffer okstatus file &rest flags)
692 "A wrapper around `vc-do-command' for use in vc-cvs.el.
693 The difference to vc-do-command is that this function always invokes `cvs',
694 and that it passes `vc-cvs-global-switches' to it before FLAGS."
695 (apply 'vc-do-command buffer okstatus "cvs" file
696 (if (stringp vc-cvs-global-switches)
697 (cons vc-cvs-global-switches flags)
698 (append vc-cvs-global-switches
699 flags))))
701 (defun vc-cvs-stay-local-p (file)
702 "Return non-nil if VC should stay local when handling FILE.
703 See `vc-cvs-stay-local'."
704 (when vc-cvs-stay-local
705 (let* ((dirname (if (file-directory-p file)
706 (directory-file-name file)
707 (file-name-directory file)))
708 (prop
709 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
710 (vc-file-setprop
711 dirname 'vc-cvs-stay-local-p
712 (let ((rootname (expand-file-name "CVS/Root" dirname)))
713 (when (file-readable-p rootname)
714 (with-temp-buffer
715 (let ((coding-system-for-read
716 (or file-name-coding-system
717 default-file-name-coding-system)))
718 (vc-insert-file rootname))
719 (goto-char (point-min))
720 (let* ((cvs-root-members
721 (vc-cvs-parse-root
722 (buffer-substring (point)
723 (line-end-position))))
724 (hostname (nth 2 cvs-root-members)))
725 (if (not hostname)
727 (let* ((stay-local t)
729 (cond
730 ;; vc-cvs-stay-local: rx
731 ((stringp vc-cvs-stay-local)
732 vc-cvs-stay-local)
733 ;; vc-cvs-stay-local: '( [except] rx ... )
734 ((consp vc-cvs-stay-local)
735 (mapconcat
736 'identity
737 (if (not (eq (car vc-cvs-stay-local)
738 'except))
739 vc-cvs-stay-local
740 (setq stay-local nil)
741 (cdr vc-cvs-stay-local))
742 "\\|")))))
743 (if (not rx)
744 'yes
745 (if (not (string-match rx hostname))
746 (setq stay-local (not stay-local)))
747 (if stay-local
748 'yes
749 'no))))))))))))
750 (if (eq prop 'yes) t nil))))
752 (defun vc-cvs-parse-root (root)
753 "Split CVS ROOT specification string into a list of fields.
754 A CVS root specification of the form
755 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
756 is converted to a normalized record with the following structure:
757 \(METHOD USER HOSTNAME CVS-ROOT).
758 The default METHOD for a CVS root of the form
759 /path/to/repository
760 is `local'.
761 The default METHOD for a CVS root of the form
762 [USER@]HOSTNAME:/path/to/repository
763 is `ext'.
764 For an empty string, nil is returned (illegal CVS root)."
765 ;; Split CVS root into colon separated fields (0-4).
766 ;; The `x:' makes sure, that leading colons are not lost;
767 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
768 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
769 (len (length root-list))
770 ;; All syntactic varieties will get a proper METHOD.
771 (root-list
772 (cond
773 ((= len 0)
774 ;; Invalid CVS root
775 nil)
776 ((= len 1)
777 ;; Simple PATH => method `local'
778 (cons "local"
779 (cons nil root-list)))
780 ((= len 2)
781 ;; [USER@]HOST:PATH => method `ext'
782 (and (not (equal (car root-list) ""))
783 (cons "ext" root-list)))
784 ((= len 3)
785 ;; :METHOD:PATH
786 (cons (cadr root-list)
787 (cons nil (cddr root-list))))
789 ;; :METHOD:[USER@]HOST:PATH
790 (cdr root-list)))))
791 (if root-list
792 (let ((method (car root-list))
793 (uhost (or (cadr root-list) ""))
794 (root (nth 2 root-list))
795 user host)
796 ;; Split USER@HOST
797 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
798 (setq user (match-string 1 uhost)
799 host (match-string 2 uhost))
800 (setq host uhost))
801 ;; Remove empty HOST
802 (and (equal host "")
803 (setq host))
804 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
805 (and host
806 (equal method "local")
807 (setq root (concat host ":" root) host))
808 ;; Normalize CVS root record
809 (list method user host root)))))
811 (defun vc-cvs-parse-status (&optional full)
812 "Parse output of \"cvs status\" command in the current buffer.
813 Set file properties accordingly. Unless FULL is t, parse only
814 essential information."
815 (let (file status)
816 (goto-char (point-min))
817 (if (re-search-forward "^File: " nil t)
818 (cond
819 ((looking-at "no file") nil)
820 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
821 (setq file (expand-file-name (match-string 1)))
822 (vc-file-setprop file 'vc-backend 'CVS)
823 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
824 (setq status "Unknown")
825 (setq status (match-string 1)))
826 (if (and full
827 (re-search-forward
828 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
829 \[\t ]+\\([0-9.]+\\)"
830 nil t))
831 (vc-file-setprop file 'vc-latest-version (match-string 2)))
832 (vc-file-setprop
833 file 'vc-state
834 (cond
835 ((string-match "Up-to-date" status)
836 (vc-file-setprop file 'vc-checkout-time
837 (nth 5 (file-attributes file)))
838 'up-to-date)
839 ((string-match "Locally Modified" status) 'edited)
840 ((string-match "Needs Merge" status) 'needs-merge)
841 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
842 (t 'edited))))))))
844 (defun vc-cvs-dir-state-heuristic (dir)
845 "Find the CVS state of all files in DIR, using only local information."
846 (with-temp-buffer
847 (vc-cvs-get-entries dir)
848 (goto-char (point-min))
849 (while (not (eobp))
850 ;; CVS-removed files are not taken under VC control.
851 (when (looking-at "/\\([^/]*\\)/[^/-]")
852 (let ((file (expand-file-name (match-string 1) dir)))
853 (unless (vc-file-getprop file 'vc-state)
854 (vc-cvs-parse-entry file t))))
855 (forward-line 1))))
857 (defun vc-cvs-get-entries (dir)
858 "Insert the CVS/Entries file from below DIR into the current buffer.
859 This function ensures that the correct coding system is used for that,
860 which may not be the one that is used for the files' contents.
861 CVS/Entries should only be accessed through this function."
862 (let ((coding-system-for-read (or file-name-coding-system
863 default-file-name-coding-system)))
864 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
866 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
867 "Return non-nil if TAG is a valid symbolic tag name."
868 ;; According to the CVS manual, a valid symbolic tag must start with
869 ;; an uppercase or lowercase letter and can contain uppercase and
870 ;; lowercase letters, digits, `-', and `_'.
871 (and (string-match "^[a-zA-Z]" tag)
872 (not (string-match "[^a-z0-9A-Z-_]" tag))))
874 (defun vc-cvs-valid-version-number-p (tag)
875 "Return non-nil if TAG is a valid version number."
876 (and (string-match "^[0-9]" tag)
877 (not (string-match "[^0-9.]" tag))))
879 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
880 "Parse and return the sticky tag as a string.
881 `match-data' is protected."
882 (let ((data (match-data))
883 (tag)
884 (type (cond ((string= match-type "D") 'date)
885 ((string= match-type "T")
886 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
887 'symbolic-name
888 'revision-number))
889 (t nil))))
890 (unwind-protect
891 (progn
892 (cond
893 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
894 ((eq type 'date)
895 (string-match
896 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
897 match-tag)
898 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
899 (month (string-to-number (match-string 2 match-tag)))
900 (day (string-to-number (match-string 3 match-tag)))
901 (hour (string-to-number (match-string 4 match-tag)))
902 (min (string-to-number (match-string 5 match-tag)))
903 (sec (string-to-number (match-string 6 match-tag)))
904 ;; Years 0..68 are 2000..2068.
905 ;; Years 69..99 are 1969..1999.
906 (year (+ (cond ((> 69 year-tmp) 2000)
907 ((> 100 year-tmp) 1900)
908 (t 0))
909 year-tmp)))
910 (setq tag (encode-time sec min hour day month year))))
911 ;; Sticky Tag name or revision number
912 ((eq type 'symbolic-name) (setq tag match-tag))
913 ((eq type 'revision-number) (setq tag match-tag))
914 ;; Default is no sticky tag at all
915 (t nil))
916 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
917 ((eq vc-cvs-sticky-tag-display t)
918 (cond ((eq type 'date) (format-time-string
919 vc-cvs-sticky-date-format-string
920 tag))
921 ((eq type 'symbolic-name) tag)
922 ((eq type 'revision-number) tag)
923 (t nil)))
924 ((functionp vc-cvs-sticky-tag-display)
925 (funcall vc-cvs-sticky-tag-display tag type))
926 (t nil)))
928 (set-match-data data))))
930 (defun vc-cvs-parse-entry (file &optional set-state)
931 "Parse a line from CVS/Entries.
932 Compare modification time to that of the FILE, set file properties
933 accordingly. However, `vc-state' is set only if optional arg SET-STATE
934 is non-nil."
935 (cond
936 ;; entry for a "locally added" file (not yet committed)
937 ((looking-at "/[^/]+/0/")
938 (vc-file-setprop file 'vc-checkout-time 0)
939 (vc-file-setprop file 'vc-workfile-version "0")
940 (if set-state (vc-file-setprop file 'vc-state 'edited)))
941 ;; normal entry
942 ((looking-at
943 (concat "/[^/]+"
944 ;; revision
945 "/\\([^/]*\\)"
946 ;; timestamp and optional conflict field
947 "/\\([^/]*\\)/"
948 ;; options
949 "\\([^/]*\\)/"
950 ;; sticky tag
951 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
952 "\\(.*\\)")) ;Sticky tag
953 (vc-file-setprop file 'vc-workfile-version (match-string 1))
954 (vc-file-setprop file 'vc-cvs-sticky-tag
955 (vc-cvs-parse-sticky-tag (match-string 4) (match-string 5)))
956 ;; compare checkout time and modification time
957 (let* ((mtime (nth 5 (file-attributes file)))
958 (system-time-locale "C")
959 (mtstr (format-time-string "%c" mtime 'utc)))
960 ;; Solaris sometimes uses "Wed Sep 05" instead of "Wed Sep 5".
961 ;; See "grep '[^a-z_]ctime' cvs/src/*.c" for reference.
962 (if (= (aref mtstr 8) ?0)
963 (setq mtstr (concat (substring mtstr 0 8) " " (substring mtstr 9))))
964 (cond ((equal mtstr (match-string 2))
965 (vc-file-setprop file 'vc-checkout-time mtime)
966 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
968 (vc-file-setprop file 'vc-checkout-time 0)
969 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
971 (provide 'vc-cvs)
973 ;;; vc-cvs.el ends here