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