(help-add-fundoc-usage): Allow arglist to be a string.
[emacs.git] / lisp / vc-cvs.el
blob474d4ca733050bddba0280d8cf5601a22ede9e61
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.55 2003/04/23 12:49:25 spiegel 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* ((state (vc-state file))
261 (rev (vc-workfile-version file))
262 (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
263 (sticky-tag-printable (and sticky-tag
264 (not (string= sticky-tag ""))
265 (concat "[" sticky-tag "]"))))
266 (cond ((string= rev "0")
267 ;; A file that is added but not yet committed.
268 "CVS @@")
269 ((or (eq state 'up-to-date)
270 (eq state 'needs-patch))
271 (concat "CVS-" rev sticky-tag-printable))
272 ((stringp state)
273 (concat "CVS:" state ":" rev sticky-tag-printable))
275 ;; Not just for the 'edited state, but also a fallback
276 ;; for all other states. Think about different symbols
277 ;; for 'needs-patch and 'needs-merge.
278 (concat "CVS:" rev sticky-tag-printable)))))
280 (defun vc-cvs-dired-state-info (file)
281 "CVS-specific version of `vc-dired-state-info'."
282 (let* ((cvs-state (vc-state file))
283 (state (cond ((eq cvs-state 'edited) "modified")
284 ((eq cvs-state 'needs-patch) "patch")
285 ((eq cvs-state 'needs-merge) "merge")
286 ;; FIXME: those two states cannot occur right now
287 ((eq cvs-state 'unlocked-changes) "conflict")
288 ((eq cvs-state 'locally-added) "added")
290 (if state (concat "(" state ")"))))
294 ;;; State-changing functions
297 (defun vc-cvs-register (file &optional rev comment)
298 "Register FILE into the CVS version-control system.
299 COMMENT can be used to provide an initial description of FILE.
301 `vc-register-switches' and `vc-cvs-register-switches' are passed to
302 the CVS command (in that order)."
303 (let ((switches (append
304 (if (stringp vc-register-switches)
305 (list vc-register-switches)
306 vc-register-switches)
307 (if (stringp vc-cvs-register-switches)
308 (list vc-cvs-register-switches)
309 vc-cvs-register-switches))))
311 (apply 'vc-cvs-command nil 0 file
312 "add"
313 (and comment (string-match "[^\t\n ]" comment)
314 (concat "-m" comment))
315 switches)))
317 (defun vc-cvs-responsible-p (file)
318 "Return non-nil if CVS thinks it is responsible for FILE."
319 (file-directory-p (expand-file-name "CVS"
320 (if (file-directory-p file)
321 file
322 (file-name-directory file)))))
324 (defalias 'vc-cvs-could-register 'vc-cvs-responsible-p
325 "Return non-nil if FILE could be registered in CVS.
326 This is only possible if CVS is responsible for FILE's directory.")
328 (defun vc-cvs-checkin (file rev comment)
329 "CVS-specific version of `vc-backend-checkin'."
330 (let ((switches (if (stringp vc-checkin-switches)
331 (list vc-checkin-switches)
332 vc-checkin-switches))
333 status)
334 (if (or (not rev) (vc-cvs-valid-version-number-p rev))
335 (setq status (apply 'vc-cvs-command nil 1 file
336 "ci" (if rev (concat "-r" rev))
337 (concat "-m" comment)
338 switches))
339 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
340 (error "%s is not a valid symbolic tag name" rev)
341 ;; If the input revison is a valid symbolic tag name, we create it
342 ;; as a branch, commit and switch to it.
343 (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
344 (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
345 (setq status (apply 'vc-cvs-command nil 1 file
346 "ci"
347 (concat "-m" comment)
348 switches))
349 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
350 (set-buffer "*vc*")
351 (goto-char (point-min))
352 (when (not (zerop status))
353 ;; Check checkin problem.
354 (cond
355 ((re-search-forward "Up-to-date check failed" nil t)
356 (vc-file-setprop file 'vc-state 'needs-merge)
357 (error (substitute-command-keys
358 (concat "Up-to-date check failed: "
359 "type \\[vc-next-action] to merge in changes"))))
361 (pop-to-buffer (current-buffer))
362 (goto-char (point-min))
363 (shrink-window-if-larger-than-buffer)
364 (error "Check-in failed"))))
365 ;; Update file properties
366 (vc-file-setprop
367 file 'vc-workfile-version
368 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
369 ;; Forget the checkout model of the file, because we might have
370 ;; guessed wrong when we found the file. After commit, we can
371 ;; tell it from the permissions of the file (see
372 ;; vc-cvs-checkout-model).
373 (vc-file-setprop file 'vc-checkout-model nil)
375 ;; if this was an explicit check-in (does not include creation of
376 ;; a branch), remove the sticky tag.
377 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
378 (vc-cvs-command nil 0 file "update" "-A"))))
380 (defun vc-cvs-find-version (file rev buffer)
381 (apply 'vc-cvs-command
382 buffer 0 file
383 "-Q" ; suppress diagnostic output
384 "update"
385 (and rev (not (string= rev ""))
386 (concat "-r" rev))
387 "-p"
388 (if (stringp vc-checkout-switches)
389 (list vc-checkout-switches)
390 vc-checkout-switches)))
392 (defun vc-cvs-checkout (file &optional editable rev workfile)
393 "Retrieve a revision of FILE into a WORKFILE.
394 EDITABLE non-nil means that the file should be writable.
395 REV is the revision to check out into WORKFILE."
396 (let ((filename (or workfile file))
397 (file-buffer (get-file-buffer file))
398 switches)
399 (message "Checking out %s..." filename)
400 (save-excursion
401 ;; Change buffers to get local value of vc-checkout-switches.
402 (if file-buffer (set-buffer file-buffer))
403 (setq switches (if (stringp vc-checkout-switches)
404 (list vc-checkout-switches)
405 vc-checkout-switches))
406 ;; Save this buffer's default-directory
407 ;; and use save-excursion to make sure it is restored
408 ;; in the same buffer it was saved in.
409 (let ((default-directory default-directory))
410 (save-excursion
411 ;; Adjust the default-directory so that the check-out creates
412 ;; the file in the right place.
413 (setq default-directory (file-name-directory filename))
414 (if workfile
415 (let ((failed t)
416 (backup-name (if (string= file workfile)
417 (car (find-backup-file-name filename)))))
418 (when backup-name
419 (copy-file filename backup-name
420 'ok-if-already-exists 'keep-date)
421 (unless (file-writable-p filename)
422 (set-file-modes filename
423 (logior (file-modes filename) 128))))
424 (unwind-protect
425 (progn
426 (let ((coding-system-for-read 'no-conversion)
427 (coding-system-for-write 'no-conversion))
428 (with-temp-file filename
429 (apply 'vc-cvs-command
430 (current-buffer) 0 file
431 "-Q" ; suppress diagnostic output
432 "update"
433 (and (stringp rev)
434 (not (string= rev ""))
435 (concat "-r" rev))
436 "-p"
437 switches)))
438 (setq failed nil))
439 (if failed
440 (if backup-name
441 (rename-file backup-name filename
442 'ok-if-already-exists)
443 (if (file-exists-p filename)
444 (delete-file filename)))
445 (and backup-name
446 (not vc-make-backup-files)
447 (delete-file backup-name)))))
448 (if (and (file-exists-p file) (not rev))
449 ;; If no revision was specified, just make the file writable
450 ;; if necessary (using `cvs-edit' if requested).
451 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
452 (if vc-cvs-use-edit
453 (vc-cvs-command nil 0 file "edit")
454 (set-file-modes file (logior (file-modes file) 128))
455 (if file-buffer (toggle-read-only -1))))
456 ;; Check out a particular version (or recreate the file).
457 (vc-file-setprop file 'vc-workfile-version nil)
458 (apply 'vc-cvs-command nil 0 file
459 (and editable
460 (or (not (file-exists-p file))
461 (not (eq (vc-cvs-checkout-model file)
462 'implicit)))
463 "-w")
464 "update"
465 ;; default for verbose checkout: clear the sticky tag so
466 ;; that the actual update will get the head of the trunk
467 (if (or (not rev) (eq rev t) (string= rev ""))
468 "-A"
469 (concat "-r" rev))
470 switches))))
471 (vc-mode-line file)
472 (message "Checking out %s...done" filename)))))
474 (defun vc-cvs-revert (file &optional contents-done)
475 "Revert FILE to the version it was based on."
476 (unless contents-done
477 ;; Check out via standard output (caused by the final argument
478 ;; FILE below), so that no sticky tag is set.
479 (vc-cvs-checkout file nil (vc-workfile-version file) file))
480 (unless (eq (vc-checkout-model file) 'implicit)
481 (if vc-cvs-use-edit
482 (vc-cvs-command nil 0 file "unedit")
483 ;; Make the file read-only by switching off all w-bits
484 (set-file-modes file (logand (file-modes file) 3950)))))
486 (defun vc-cvs-merge (file first-version &optional second-version)
487 "Merge changes into current working copy of FILE.
488 The changes are between FIRST-VERSION and SECOND-VERSION."
489 (vc-cvs-command nil 0 file
490 "update" "-kk"
491 (concat "-j" first-version)
492 (concat "-j" second-version))
493 (vc-file-setprop file 'vc-state 'edited)
494 (with-current-buffer (get-buffer "*vc*")
495 (goto-char (point-min))
496 (if (re-search-forward "conflicts during merge" nil t)
497 1 ; signal error
498 0))) ; signal success
500 (defun vc-cvs-merge-news (file)
501 "Merge in any new changes made to FILE."
502 (message "Merging changes into %s..." file)
503 ;; (vc-file-setprop file 'vc-workfile-version nil)
504 (vc-file-setprop file 'vc-checkout-time 0)
505 (vc-cvs-command nil 0 file "update")
506 ;; Analyze the merge result reported by CVS, and set
507 ;; file properties accordingly.
508 (with-current-buffer (get-buffer "*vc*")
509 (goto-char (point-min))
510 ;; get new workfile version
511 (if (re-search-forward
512 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
513 (vc-file-setprop file 'vc-workfile-version (match-string 1))
514 (vc-file-setprop file 'vc-workfile-version nil))
515 ;; get file status
516 (prog1
517 (if (eq (buffer-size) 0)
518 0 ;; there were no news; indicate success
519 (if (re-search-forward
520 (concat "^\\([CMUP] \\)?"
521 (regexp-quote (file-name-nondirectory file))
522 "\\( already contains the differences between \\)?")
523 nil t)
524 (cond
525 ;; Merge successful, we are in sync with repository now
526 ((or (match-string 2)
527 (string= (match-string 1) "U ")
528 (string= (match-string 1) "P "))
529 (vc-file-setprop file 'vc-state 'up-to-date)
530 (vc-file-setprop file 'vc-checkout-time
531 (nth 5 (file-attributes file)))
532 0);; indicate success to the caller
533 ;; Merge successful, but our own changes are still in the file
534 ((string= (match-string 1) "M ")
535 (vc-file-setprop file 'vc-state 'edited)
536 0);; indicate success to the caller
537 ;; Conflicts detected!
539 (vc-file-setprop file 'vc-state 'edited)
540 1);; signal the error to the caller
542 (pop-to-buffer "*vc*")
543 (error "Couldn't analyze cvs update result")))
544 (message "Merging changes into %s...done" file))))
548 ;;; History functions
551 (defun vc-cvs-print-log (file)
552 "Get change log associated with FILE."
553 (vc-cvs-command
555 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
556 file "log"))
558 (defun vc-cvs-diff (file &optional oldvers newvers)
559 "Get a difference report using CVS between two versions of FILE."
560 (let (status (diff-switches-list (vc-diff-switches-list 'CVS)))
561 (if (string= (vc-workfile-version file) "0")
562 ;; This file is added but not yet committed; there is no master file.
563 (if (or oldvers newvers)
564 (error "No revisions of %s exist" file)
565 ;; We regard this as "changed".
566 ;; Diff it against /dev/null.
567 ;; Note: this is NOT a "cvs diff".
568 (apply 'vc-do-command "*vc-diff*"
569 1 "diff" file
570 (append diff-switches-list '("/dev/null"))))
571 (setq status
572 (apply 'vc-cvs-command "*vc-diff*"
573 (if (and (vc-cvs-stay-local-p file)
574 (fboundp 'start-process))
575 'async
577 file "diff"
578 (and oldvers (concat "-r" oldvers))
579 (and newvers (concat "-r" newvers))
580 diff-switches-list))
581 (if (vc-cvs-stay-local-p file)
582 1 ;; async diff, pessimistic assumption
583 status))))
585 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
586 "Diff all files at and below DIR."
587 (with-current-buffer "*vc-diff*"
588 (setq default-directory dir)
589 (if (vc-cvs-stay-local-p dir)
590 ;; local diff: do it filewise, and only for files that are modified
591 (vc-file-tree-walk
593 (lambda (f)
594 (vc-exec-after
595 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
596 ;; possible optimization: fetch the state of all files
597 ;; in the tree via vc-cvs-dir-state-heuristic
598 (unless (vc-up-to-date-p ',f)
599 (message "Looking at %s" ',f)
600 (vc-diff-internal ',f ',rev1 ',rev2))))))
601 ;; cvs diff: use a single call for the entire tree
602 (let ((coding-system-for-read
603 (or coding-system-for-read 'undecided)))
604 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
605 (and rev1 (concat "-r" rev1))
606 (and rev2 (concat "-r" rev2))
607 (vc-diff-switches-list 'CVS))))))
609 (defun vc-cvs-annotate-command (file buffer &optional version)
610 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
611 Optional arg VERSION is a version to annotate from."
612 (vc-cvs-command buffer 0 file "annotate" (if version
613 (concat "-r" version))))
615 (defun vc-cvs-annotate-current-time ()
616 "Return the current time, based at midnight of the current day, and
617 encoded as fractional days."
618 (vc-annotate-convert-time
619 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
621 (defun vc-cvs-annotate-time ()
622 "Return the time of the next annotation (as fraction of days)
623 systime, or nil if there is none."
624 (let ((time-stamp
625 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
626 (if (looking-at time-stamp)
627 (progn
628 (let* ((day (string-to-number (match-string 1)))
629 (month (cdr (assoc (match-string 2)
630 vc-cvs-local-month-numbers)))
631 (year-tmp (string-to-number (match-string 3)))
632 ;; Years 0..68 are 2000..2068.
633 ;; Years 69..99 are 1969..1999.
634 (year (+ (cond ((> 69 year-tmp) 2000)
635 ((> 100 year-tmp) 1900)
636 (t 0))
637 year-tmp)))
638 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
639 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
640 ;; If we did not look directly at an annotation, there might be
641 ;; some further down. This is the case if we are positioned at
642 ;; the very top of the buffer, for instance.
643 (if (re-search-forward time-stamp nil t)
644 (progn
645 (beginning-of-line nil)
646 (vc-cvs-annotate-time))))))
649 ;;; Snapshot system
652 (defun vc-cvs-create-snapshot (dir name branchp)
653 "Assign to DIR's current version a given NAME.
654 If BRANCHP is non-nil, the name is created as a branch (and the current
655 workspace is immediately moved to that new branch)."
656 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
657 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
659 (defun vc-cvs-retrieve-snapshot (dir name update)
660 "Retrieve a snapshot at and below DIR.
661 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
662 If UPDATE is non-nil, then update (resynch) any affected buffers."
663 (with-current-buffer (get-buffer-create "*vc*")
664 (let ((default-directory dir)
665 (sticky-tag))
666 (erase-buffer)
667 (if (or (not name) (string= name ""))
668 (vc-cvs-command t 0 nil "update")
669 (vc-cvs-command t 0 nil "update" "-r" name)
670 (setq sticky-tag name))
671 (when update
672 (goto-char (point-min))
673 (while (not (eobp))
674 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
675 (let* ((file (expand-file-name (match-string 2) dir))
676 (state (match-string 1))
677 (buffer (find-buffer-visiting file)))
678 (when buffer
679 (cond
680 ((or (string= state "U")
681 (string= state "P"))
682 (vc-file-setprop file 'vc-state 'up-to-date)
683 (vc-file-setprop file 'vc-workfile-version nil)
684 (vc-file-setprop file 'vc-checkout-time
685 (nth 5 (file-attributes file))))
686 ((or (string= state "M")
687 (string= state "C"))
688 (vc-file-setprop file 'vc-state 'edited)
689 (vc-file-setprop file 'vc-workfile-version nil)
690 (vc-file-setprop file 'vc-checkout-time 0)))
691 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
692 (vc-resynch-buffer file t t))))
693 (forward-line 1))))))
697 ;;; Miscellaneous
700 (defalias 'vc-cvs-make-version-backups-p 'vc-cvs-stay-local-p
701 "Return non-nil if version backups should be made for FILE.")
703 (defun vc-cvs-check-headers ()
704 "Check if the current file has any headers in it."
705 (save-excursion
706 (goto-char (point-min))
707 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
708 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
712 ;;; Internal functions
715 (defun vc-cvs-command (buffer okstatus file &rest flags)
716 "A wrapper around `vc-do-command' for use in vc-cvs.el.
717 The difference to vc-do-command is that this function always invokes `cvs',
718 and that it passes `vc-cvs-global-switches' to it before FLAGS."
719 (apply 'vc-do-command buffer okstatus "cvs" file
720 (if (stringp vc-cvs-global-switches)
721 (cons vc-cvs-global-switches flags)
722 (append vc-cvs-global-switches
723 flags))))
725 (defun vc-cvs-stay-local-p (file)
726 "Return non-nil if VC should stay local when handling FILE.
727 See `vc-cvs-stay-local'."
728 (if vc-cvs-stay-local
729 (let* ((dirname (if (file-directory-p file)
730 (directory-file-name file)
731 (file-name-directory file)))
732 (prop
733 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
734 (let ((rootname (expand-file-name "CVS/Root" dirname)))
735 (vc-file-setprop
736 dirname 'vc-cvs-stay-local-p
737 (when (file-readable-p rootname)
738 (with-temp-buffer
739 (vc-insert-file rootname)
740 (goto-char (point-min))
741 (looking-at "\\([^\n]*\\)")
742 (let* ((cvs-root-members
743 (vc-cvs-parse-root (match-string 1)))
744 (hostname (nth 2 cvs-root-members)))
745 (if (not hostname)
747 (let ((stay-local t) rx)
748 (cond
749 ;; vc-cvs-stay-local: rx
750 ((stringp vc-cvs-stay-local)
751 (setq rx vc-cvs-stay-local))
752 ;; vc-cvs-stay-local: '( [except] rx ... )
753 ((consp vc-cvs-stay-local)
754 (setq rx (mapconcat
755 (function
756 (lambda (elt)
757 elt))
758 (if (not (eq (car vc-cvs-stay-local)
759 'except))
760 vc-cvs-stay-local
761 (setq stay-local nil)
762 (cdr vc-cvs-stay-local))
763 "\\|"))))
764 (if (not rx)
765 'yes
766 (if (not (string-match rx hostname))
767 (setq stay-local (not stay-local)))
768 (if stay-local
769 'yes
770 'no))))))))))))
771 (if (eq prop 'yes) t nil))))
773 (defun vc-cvs-parse-root ( root )
774 "Split CVS ROOT specification string into a list of fields.
775 A CVS root specification of the form
776 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
777 is converted to a normalized record with the following structure:
778 \(METHOD USER HOSTNAME CVS-ROOT).
779 The default METHOD for a CVS root of the form
780 /path/to/repository
781 is `local'.
782 The default METHOD for a CVS root of the form
783 [USER@]HOSTNAME:/path/to/repository
784 is `ext'.
785 For an empty string, nil is returned (illegal CVS root)."
786 ;; Split CVS root into colon separated fields (0-4).
787 ;; The `x:' makes sure, that leading colons are not lost;
788 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
789 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
790 (len (length root-list))
791 ;; All syntactic varieties will get a proper METHOD.
792 (root-list
793 (cond
794 ((= len 0)
795 ;; Invalid CVS root
796 nil)
797 ((= len 1)
798 ;; Simple PATH => method `local'
799 (cons "local"
800 (cons nil root-list)))
801 ((= len 2)
802 ;; [USER@]HOST:PATH => method `ext'
803 (and (not (equal (car root-list) ""))
804 (cons "ext" root-list)))
805 ((= len 3)
806 ;; :METHOD:PATH
807 (cons (cadr root-list)
808 (cons nil (cddr root-list))))
810 ;; :METHOD:[USER@]HOST:PATH
811 (cdr root-list)))))
812 (if root-list
813 (let ((method (car root-list))
814 (uhost (or (cadr root-list) ""))
815 (root (nth 2 root-list))
816 user host)
817 ;; Split USER@HOST
818 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
819 (setq user (match-string 1 uhost)
820 host (match-string 2 uhost))
821 (setq host uhost))
822 ;; Remove empty HOST
823 (and (equal host "")
824 (setq host))
825 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
826 (and host
827 (equal method "local")
828 (setq root (concat host ":" root) host))
829 ;; Normalize CVS root record
830 (list method user host root)))))
832 (defun vc-cvs-parse-status (&optional full)
833 "Parse output of \"cvs status\" command in the current buffer.
834 Set file properties accordingly. Unless FULL is t, parse only
835 essential information."
836 (let (file status)
837 (goto-char (point-min))
838 (if (re-search-forward "^File: " nil t)
839 (cond
840 ((looking-at "no file") nil)
841 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
842 (setq file (expand-file-name (match-string 1)))
843 (vc-file-setprop file 'vc-backend 'CVS)
844 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
845 (setq status "Unknown")
846 (setq status (match-string 1)))
847 (if (and full
848 (re-search-forward
849 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
850 \[\t ]+\\([0-9.]+\\)"
851 nil t))
852 (vc-file-setprop file 'vc-latest-version (match-string 2)))
853 (vc-file-setprop
854 file 'vc-state
855 (cond
856 ((string-match "Up-to-date" status)
857 (vc-file-setprop file 'vc-checkout-time
858 (nth 5 (file-attributes file)))
859 'up-to-date)
860 ((string-match "Locally Modified" status) 'edited)
861 ((string-match "Needs Merge" status) 'needs-merge)
862 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
863 (t 'edited))))))))
865 (defun vc-cvs-dir-state-heuristic (dir)
866 "Find the CVS state of all files in DIR, using only local information."
867 (with-temp-buffer
868 (vc-cvs-get-entries dir)
869 (goto-char (point-min))
870 (while (not (eobp))
871 ;; CVS-removed files are not taken under VC control.
872 (when (looking-at "/\\([^/]*\\)/[^/-]")
873 (let ((file (expand-file-name (match-string 1) dir)))
874 (unless (vc-file-getprop file 'vc-state)
875 (vc-cvs-parse-entry file t))))
876 (forward-line 1))))
878 (defun vc-cvs-get-entries (dir)
879 "Insert the CVS/Entries file from below DIR into the current buffer.
880 This function ensures that the correct coding system is used for that,
881 which may not be the one that is used for the files' contents.
882 CVS/Entries should only be accessed through this function."
883 (let ((coding-system-for-read (or file-name-coding-system
884 default-file-name-coding-system)))
885 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
887 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
888 "Return non-nil if TAG is a valid symbolic tag name."
889 ;; According to the CVS manual, a valid symbolic tag must start with
890 ;; an uppercase or lowercase letter and can contain uppercase and
891 ;; lowercase letters, digits, `-', and `_'.
892 (and (string-match "^[a-zA-Z]" tag)
893 (not (string-match "[^a-z0-9A-Z-_]" tag))))
895 (defun vc-cvs-valid-version-number-p (tag)
896 "Return non-nil if TAG is a valid version number."
897 (and (string-match "^[0-9]" tag)
898 (not (string-match "[^0-9.]" tag))))
900 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
901 "Parse and return the sticky tag as a string.
902 `match-data' is protected."
903 (let ((data (match-data))
904 (tag)
905 (type (cond ((string= match-type "D") 'date)
906 ((string= match-type "T")
907 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
908 'symbolic-name
909 'revision-number))
910 (t nil))))
911 (unwind-protect
912 (progn
913 (cond
914 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
915 ((eq type 'date)
916 (string-match
917 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
918 match-tag)
919 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
920 (month (string-to-number (match-string 2 match-tag)))
921 (day (string-to-number (match-string 3 match-tag)))
922 (hour (string-to-number (match-string 4 match-tag)))
923 (min (string-to-number (match-string 5 match-tag)))
924 (sec (string-to-number (match-string 6 match-tag)))
925 ;; Years 0..68 are 2000..2068.
926 ;; Years 69..99 are 1969..1999.
927 (year (+ (cond ((> 69 year-tmp) 2000)
928 ((> 100 year-tmp) 1900)
929 (t 0))
930 year-tmp)))
931 (setq tag (encode-time sec min hour day month year))))
932 ;; Sticky Tag name or revision number
933 ((eq type 'symbolic-name) (setq tag match-tag))
934 ((eq type 'revision-number) (setq tag match-tag))
935 ;; Default is no sticky tag at all
936 (t nil))
937 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
938 ((eq vc-cvs-sticky-tag-display t)
939 (cond ((eq type 'date) (format-time-string
940 vc-cvs-sticky-date-format-string
941 tag))
942 ((eq type 'symbolic-name) tag)
943 ((eq type 'revision-number) tag)
944 (t nil)))
945 ((functionp vc-cvs-sticky-tag-display)
946 (funcall vc-cvs-sticky-tag-display tag type))
947 (t nil)))
949 (set-match-data data))))
951 (defun vc-cvs-parse-entry (file &optional set-state)
952 "Parse a line from CVS/Entries.
953 Compare modification time to that of the FILE, set file properties
954 accordingly. However, `vc-state' is set only if optional arg SET-STATE
955 is non-nil."
956 (cond
957 ;; entry for a "locally added" file (not yet committed)
958 ((looking-at "/[^/]+/0/")
959 (vc-file-setprop file 'vc-checkout-time 0)
960 (vc-file-setprop file 'vc-workfile-version "0")
961 (if set-state (vc-file-setprop file 'vc-state 'edited)))
962 ;; normal entry
963 ((looking-at
964 (concat "/[^/]+"
965 ;; revision
966 "/\\([^/]*\\)"
967 ;; timestamp and optional conflict field
968 "/\\([^/]*\\)/"
969 ;; options
970 "\\([^/]*\\)/"
971 ;; sticky tag
972 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
973 "\\(.*\\)")) ;Sticky tag
974 (vc-file-setprop file 'vc-workfile-version (match-string 1))
975 (vc-file-setprop file 'vc-cvs-sticky-tag
976 (vc-cvs-parse-sticky-tag (match-string 4) (match-string 5)))
977 ;; compare checkout time and modification time
978 (let* ((mtime (nth 5 (file-attributes file)))
979 (system-time-locale "C")
980 (mtstr (format-time-string "%c" mtime 'utc)))
981 ;; Solaris sometimes uses "Wed Sep 05" instead of "Wed Sep 5".
982 ;; See "grep '[^a-z_]ctime' cvs/src/*.c" for reference.
983 (if (= (aref mtstr 8) ?0)
984 (setq mtstr (concat (substring mtstr 0 8) " " (substring mtstr 9))))
985 (cond ((equal mtstr (match-string 2))
986 (vc-file-setprop file 'vc-checkout-time mtime)
987 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
989 (vc-file-setprop file 'vc-checkout-time 0)
990 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
992 (provide 'vc-cvs)
994 ;;; vc-cvs.el ends here