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