(Custom-reset-current): Don't test for default-boundp.
[emacs.git] / lisp / vc-cvs.el
blob5ed447ee8b54338714e8da9567951cfac60ff510
1 ;;; vc-cvs.el --- non-resident support for CVS version-control
3 ;; Copyright (C) 1995,98,99,2000,2001 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.44 2002/10/08 15:38:28 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.
88 The value can also be a regular expression to match against the host name
89 of a repository; then VC only stays local for hosts that match it."
90 :type '(choice (const :tag "Always stay local" t)
91 (string :tag "Host regexp")
92 (const :tag "Don't stay local" nil))
93 :version "21.1"
94 :group 'vc)
96 (defcustom vc-cvs-sticky-date-format-string "%c"
97 "*Format string for mode-line display of sticky date.
98 Format is according to `format-time-string'. Only used if
99 `vc-cvs-sticky-tag-display' is t."
100 :type '(string)
101 :version "21.4"
102 :group 'vc)
104 (defcustom vc-cvs-sticky-tag-display t
105 "*Specify the mode-line display of sticky tags.
106 Value t means default display, nil means no display at all. If the
107 value is a function or macro, it is called with the sticky tag and
108 its' type as parameters, in that order. TYPE can have three different
109 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
110 string) and `date' (TAG is a date as returned by `encode-time'). The
111 return value of the function or macro will be displayed as a string.
113 Here's an example that will display the formatted date for sticky
114 dates and the word \"Sticky\" for sticky tag names and revisions.
116 (lambda (tag type)
117 (cond ((eq type 'date) (format-time-string
118 vc-cvs-sticky-date-format-string tag))
119 ((eq type 'revision-number) \"Sticky\")
120 ((eq type 'symbolic-name) \"Sticky\")))
122 Here's an example that will abbreviate to the first character only,
123 any text before the first occurence of `-' for sticky symbolic tags.
124 If the sticky tag is a revision number, the word \"Sticky\" is
125 displayed. Date and time is displayed for sticky dates.
127 (lambda (tag type)
128 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
129 ((eq type 'revision-number) \"Sticky\")
130 ((eq type 'symbolic-name)
131 (condition-case nil
132 (progn
133 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
134 (concat (substring (match-string 1 tag) 0 1) \":\"
135 (substring (match-string 2 tag) 1 nil)))
136 (error tag))))) ; Fall-back to given tag name.
138 See also variable `vc-cvs-sticky-date-format-string'."
139 :type '(choice boolean function)
140 :version "21.4"
141 :group 'vc)
144 ;;; Internal variables
147 (defvar vc-cvs-local-month-numbers
148 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
149 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
150 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
151 "Local association list of month numbers.")
155 ;;; State-querying functions
158 ;;;###autoload (defun vc-cvs-registered (f)
159 ;;;###autoload (when (file-readable-p (expand-file-name
160 ;;;###autoload "CVS/Entries" (file-name-directory f)))
161 ;;;###autoload (require 'vc-cvs)
162 ;;;###autoload (vc-cvs-registered f)))
164 (defun vc-cvs-registered (file)
165 "Check if FILE is CVS registered."
166 (let ((dirname (or (file-name-directory file) ""))
167 (basename (file-name-nondirectory file))
168 ;; make sure that the file name is searched case-sensitively
169 (case-fold-search nil))
170 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
171 (with-temp-buffer
172 (vc-insert-file (expand-file-name "CVS/Entries" dirname))
173 (goto-char (point-min))
174 (cond
175 ((re-search-forward
176 ;; CVS-removed files are not taken under VC control.
177 (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
178 (beginning-of-line)
179 (vc-cvs-parse-entry file)
181 (t nil)))
182 nil)))
184 (defun vc-cvs-state (file)
185 "CVS-specific version of `vc-state'."
186 (if (vc-cvs-stay-local-p file)
187 (let ((state (vc-file-getprop file 'vc-state)))
188 ;; If we should stay local, use the heuristic but only if
189 ;; we don't have a more precise state already available.
190 (if (memq state '(up-to-date edited))
191 (vc-cvs-state-heuristic file)
192 state))
193 (with-temp-buffer
194 (cd (file-name-directory file))
195 (vc-cvs-command t 0 file "status")
196 (vc-cvs-parse-status t))))
198 (defun vc-cvs-state-heuristic (file)
199 "CVS-specific state heuristic."
200 ;; If the file has not changed since checkout, consider it `up-to-date'.
201 ;; Otherwise consider it `edited'.
202 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
203 (lastmod (nth 5 (file-attributes file))))
204 (if (equal checkout-time lastmod)
205 'up-to-date
206 'edited)))
208 (defun vc-cvs-dir-state (dir)
209 "Find the CVS state of all files in DIR."
210 (if (vc-cvs-stay-local-p dir)
211 (vc-cvs-dir-state-heuristic dir)
212 (let ((default-directory dir))
213 ;; Don't specify DIR in this command, the default-directory is
214 ;; enough. Otherwise it might fail with remote repositories.
215 (with-temp-buffer
216 (vc-cvs-command t 0 nil "status" "-l")
217 (goto-char (point-min))
218 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
219 (narrow-to-region (match-beginning 0) (match-end 0))
220 (vc-cvs-parse-status)
221 (goto-char (point-max))
222 (widen))))))
224 (defun vc-cvs-workfile-version (file)
225 "CVS-specific version of `vc-workfile-version'."
226 ;; There is no need to consult RCS headers under CVS, because we
227 ;; get the workfile version for free when we recognize that a file
228 ;; is registered in CVS.
229 (vc-cvs-registered file)
230 (vc-file-getprop file 'vc-workfile-version))
232 (defun vc-cvs-checkout-model (file)
233 "CVS-specific version of `vc-checkout-model'."
234 (if (or (getenv "CVSREAD")
235 ;; If the file is not writable (despite CVSREAD being
236 ;; undefined), this is probably because the file is being
237 ;; "watched" by other developers.
238 ;; (If vc-mistrust-permissions was t, we actually shouldn't
239 ;; trust this, but there is no other way to learn this from CVS
240 ;; at the moment (version 1.9).)
241 (string-match "r-..-..-." (nth 8 (file-attributes file))))
242 'announce
243 'implicit))
245 (defun vc-cvs-mode-line-string (file)
246 "Return string for placement into the modeline for FILE.
247 Compared to the default implementation, this function does two things:
248 Handle the special case of a CVS file that is added but not yet
249 committed and support display of sticky tags."
250 (let* ((state (vc-state file))
251 (rev (vc-workfile-version file))
252 (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
253 (sticky-tag-printable (and sticky-tag
254 (not (string= sticky-tag ""))
255 (concat "[" sticky-tag "]"))))
256 (cond ((string= rev "0")
257 ;; A file that is added but not yet committed.
258 "CVS @@")
259 ((or (eq state 'up-to-date)
260 (eq state 'needs-patch))
261 (concat "CVS-" rev sticky-tag-printable))
262 ((stringp state)
263 (concat "CVS:" state ":" rev sticky-tag-printable))
265 ;; Not just for the 'edited state, but also a fallback
266 ;; for all other states. Think about different symbols
267 ;; for 'needs-patch and 'needs-merge.
268 (concat "CVS:" rev sticky-tag-printable)))))
270 (defun vc-cvs-dired-state-info (file)
271 "CVS-specific version of `vc-dired-state-info'."
272 (let* ((cvs-state (vc-state file))
273 (state (cond ((eq cvs-state 'edited) "modified")
274 ((eq cvs-state 'needs-patch) "patch")
275 ((eq cvs-state 'needs-merge) "merge")
276 ;; FIXME: those two states cannot occur right now
277 ((eq cvs-state 'unlocked-changes) "conflict")
278 ((eq cvs-state 'locally-added) "added")
280 (if state (concat "(" state ")"))))
284 ;;; State-changing functions
287 (defun vc-cvs-register (file &optional rev comment)
288 "Register FILE into the CVS version-control system.
289 COMMENT can be used to provide an initial description of FILE.
291 `vc-register-switches' and `vc-cvs-register-switches' are passed to
292 the CVS command (in that order)."
293 (let ((switches (append
294 (if (stringp vc-register-switches)
295 (list vc-register-switches)
296 vc-register-switches)
297 (if (stringp vc-cvs-register-switches)
298 (list vc-cvs-register-switches)
299 vc-cvs-register-switches))))
301 (apply 'vc-cvs-command nil 0 file
302 "add"
303 (and comment (string-match "[^\t\n ]" comment)
304 (concat "-m" comment))
305 switches)))
307 (defun vc-cvs-responsible-p (file)
308 "Return non-nil if CVS thinks it is responsible for FILE."
309 (file-directory-p (expand-file-name "CVS"
310 (if (file-directory-p file)
311 file
312 (file-name-directory file)))))
314 (defun vc-cvs-could-register (file)
315 "Return non-nil if FILE could be registered in CVS.
316 This is only possible if CVS is responsible for FILE's directory."
317 (vc-cvs-responsible-p file))
319 (defun vc-cvs-checkin (file rev comment)
320 "CVS-specific version of `vc-backend-checkin'."
321 (let ((switches (if (stringp vc-checkin-switches)
322 (list vc-checkin-switches)
323 vc-checkin-switches))
324 status)
325 (if (or (not rev) (vc-cvs-valid-version-number-p rev))
326 (setq status (apply 'vc-cvs-command nil 1 file
327 "ci" (if rev (concat "-r" rev))
328 (concat "-m" comment)
329 switches))
330 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
331 (error "%s is not a valid symbolic tag name" rev)
332 ;; If the input revison is a valid symbolic tag name, we create it
333 ;; as a branch, commit and switch to it.
334 (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
335 (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
336 (setq status (apply 'vc-cvs-command nil 1 file
337 "ci"
338 (concat "-m" comment)
339 switches))
340 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
341 (set-buffer "*vc*")
342 (goto-char (point-min))
343 (when (not (zerop status))
344 ;; Check checkin problem.
345 (cond
346 ((re-search-forward "Up-to-date check failed" nil t)
347 (vc-file-setprop file 'vc-state 'needs-merge)
348 (error (substitute-command-keys
349 (concat "Up-to-date check failed: "
350 "type \\[vc-next-action] to merge in changes"))))
352 (pop-to-buffer (current-buffer))
353 (goto-char (point-min))
354 (shrink-window-if-larger-than-buffer)
355 (error "Check-in failed"))))
356 ;; Update file properties
357 (vc-file-setprop
358 file 'vc-workfile-version
359 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
360 ;; Forget the checkout model of the file, because we might have
361 ;; guessed wrong when we found the file. After commit, we can
362 ;; tell it from the permissions of the file (see
363 ;; vc-cvs-checkout-model).
364 (vc-file-setprop file 'vc-checkout-model nil)
366 ;; if this was an explicit check-in (does not include creation of
367 ;; a branch), remove the sticky tag.
368 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
369 (vc-cvs-command nil 0 file "update" "-A"))))
371 (defun vc-cvs-find-version (file rev buffer)
372 (apply 'vc-cvs-command
373 buffer 0 file
374 "-Q" ; suppress diagnostic output
375 "update"
376 (and rev (not (string= rev ""))
377 (concat "-r" rev))
378 "-p"
379 (if (stringp vc-checkout-switches)
380 (list vc-checkout-switches)
381 vc-checkout-switches)))
383 (defun vc-cvs-checkout (file &optional editable rev workfile)
384 "Retrieve a revision of FILE into a WORKFILE.
385 EDITABLE non-nil means that the file should be writable.
386 REV is the revision to check out into WORKFILE."
387 (let ((filename (or workfile file))
388 (file-buffer (get-file-buffer file))
389 switches)
390 (message "Checking out %s..." filename)
391 (save-excursion
392 ;; Change buffers to get local value of vc-checkout-switches.
393 (if file-buffer (set-buffer file-buffer))
394 (setq switches (if (stringp vc-checkout-switches)
395 (list vc-checkout-switches)
396 vc-checkout-switches))
397 ;; Save this buffer's default-directory
398 ;; and use save-excursion to make sure it is restored
399 ;; in the same buffer it was saved in.
400 (let ((default-directory default-directory))
401 (save-excursion
402 ;; Adjust the default-directory so that the check-out creates
403 ;; the file in the right place.
404 (setq default-directory (file-name-directory filename))
405 (if workfile
406 (let ((failed t)
407 (backup-name (if (string= file workfile)
408 (car (find-backup-file-name filename)))))
409 (when backup-name
410 (copy-file filename backup-name
411 'ok-if-already-exists 'keep-date)
412 (unless (file-writable-p filename)
413 (set-file-modes filename
414 (logior (file-modes filename) 128))))
415 (unwind-protect
416 (progn
417 (let ((coding-system-for-read 'no-conversion)
418 (coding-system-for-write 'no-conversion))
419 (with-temp-file filename
420 (apply 'vc-cvs-command
421 (current-buffer) 0 file
422 "-Q" ; suppress diagnostic output
423 "update"
424 (and rev (not (string= rev ""))
425 (concat "-r" rev))
426 "-p"
427 switches)))
428 (setq failed nil))
429 (if failed
430 (if backup-name
431 (rename-file backup-name filename
432 'ok-if-already-exists)
433 (if (file-exists-p filename)
434 (delete-file filename)))
435 (and backup-name
436 (not vc-make-backup-files)
437 (delete-file backup-name)))))
438 (if (and (file-exists-p file) (not rev))
439 ;; If no revision was specified, just make the file writable
440 ;; if necessary (using `cvs-edit' if requested).
441 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
442 (if vc-cvs-use-edit
443 (vc-cvs-command nil 0 file "edit")
444 (set-file-modes file (logior (file-modes file) 128))
445 (if file-buffer (toggle-read-only -1))))
446 ;; Check out a particular version (or recreate the file).
447 (vc-file-setprop file 'vc-workfile-version nil)
448 (apply 'vc-cvs-command nil 0 file
449 (and editable
450 (or (not (file-exists-p file))
451 (not (eq (vc-cvs-checkout-model file)
452 'implicit)))
453 "-w")
454 "update"
455 ;; default for verbose checkout: clear the sticky tag so
456 ;; that the actual update will get the head of the trunk
457 (if (or (not rev) (string= rev ""))
458 "-A"
459 (concat "-r" rev))
460 switches))))
461 (vc-mode-line file)
462 (message "Checking out %s...done" filename)))))
464 (defun vc-cvs-revert (file &optional contents-done)
465 "Revert FILE to the version it was based on."
466 (unless contents-done
467 ;; Check out via standard output (caused by the final argument
468 ;; FILE below), so that no sticky tag is set.
469 (vc-cvs-checkout file nil (vc-workfile-version file) file))
470 (unless (eq (vc-checkout-model file) 'implicit)
471 (if vc-cvs-use-edit
472 (vc-cvs-command nil 0 file "unedit")
473 ;; Make the file read-only by switching off all w-bits
474 (set-file-modes file (logand (file-modes file) 3950)))))
476 (defun vc-cvs-merge (file first-version &optional second-version)
477 "Merge changes into current working copy of FILE.
478 The changes are between FIRST-VERSION and SECOND-VERSION."
479 (vc-cvs-command nil 0 file
480 "update" "-kk"
481 (concat "-j" first-version)
482 (concat "-j" second-version))
483 (vc-file-setprop file 'vc-state 'edited)
484 (save-excursion
485 (set-buffer (get-buffer "*vc*"))
486 (goto-char (point-min))
487 (if (re-search-forward "conflicts during merge" nil t)
488 1 ; signal error
489 0))) ; signal success
491 (defun vc-cvs-merge-news (file)
492 "Merge in any new changes made to FILE."
493 (message "Merging changes into %s..." file)
494 (save-excursion
495 ;; (vc-file-setprop file 'vc-workfile-version nil)
496 (vc-file-setprop file 'vc-checkout-time 0)
497 (vc-cvs-command nil 0 file "update")
498 ;; Analyze the merge result reported by CVS, and set
499 ;; file properties accordingly.
500 (set-buffer (get-buffer "*vc*"))
501 (goto-char (point-min))
502 ;; get new workfile version
503 (if (re-search-forward (concat "^Merging differences between "
504 "[01234567890.]* and "
505 "\\([01234567890.]*\\) into")
506 nil t)
507 (vc-file-setprop file 'vc-workfile-version (match-string 1))
508 (vc-file-setprop file 'vc-workfile-version nil))
509 ;; get file status
510 (prog1
511 (if (eq (buffer-size) 0)
512 0 ;; there were no news; indicate success
513 (if (re-search-forward
514 (concat "^\\([CMUP] \\)?"
515 (regexp-quote (file-name-nondirectory file))
516 "\\( already contains the differences between \\)?")
517 nil t)
518 (cond
519 ;; Merge successful, we are in sync with repository now
520 ((or (match-string 2)
521 (string= (match-string 1) "U ")
522 (string= (match-string 1) "P "))
523 (vc-file-setprop file 'vc-state 'up-to-date)
524 (vc-file-setprop file 'vc-checkout-time
525 (nth 5 (file-attributes file)))
526 0);; indicate success to the caller
527 ;; Merge successful, but our own changes are still in the file
528 ((string= (match-string 1) "M ")
529 (vc-file-setprop file 'vc-state 'edited)
530 0);; indicate success to the caller
531 ;; Conflicts detected!
533 (vc-file-setprop file 'vc-state 'edited)
534 1);; signal the error to the caller
536 (pop-to-buffer "*vc*")
537 (error "Couldn't analyze cvs update result")))
538 (message "Merging changes into %s...done" file))))
542 ;;; History functions
545 (defun vc-cvs-print-log (file)
546 "Get change log associated with FILE."
547 (vc-cvs-command
549 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
550 file "log"))
552 (defun vc-cvs-diff (file &optional oldvers newvers)
553 "Get a difference report using CVS between two versions of FILE."
554 (let (options status (diff-switches-list (vc-diff-switches-list 'CVS)))
555 (if (string= (vc-workfile-version file) "0")
556 ;; This file is added but not yet committed; there is no master file.
557 (if (or oldvers newvers)
558 (error "No revisions of %s exist" file)
559 ;; We regard this as "changed".
560 ;; Diff it against /dev/null.
561 ;; Note: this is NOT a "cvs diff".
562 (apply 'vc-do-command "*vc-diff*"
563 1 "diff" file
564 (append diff-switches-list '("/dev/null"))))
565 (setq status
566 (apply 'vc-cvs-command "*vc-diff*"
567 (if (and (vc-cvs-stay-local-p file)
568 (fboundp 'start-process))
569 'async
571 file "diff"
572 (and oldvers (concat "-r" oldvers))
573 (and newvers (concat "-r" newvers))
574 diff-switches-list))
575 (if (vc-cvs-stay-local-p file)
576 1 ;; async diff, pessimistic assumption
577 status))))
579 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
580 "Diff all files at and below DIR."
581 (with-current-buffer "*vc-diff*"
582 (setq default-directory dir)
583 (if (vc-cvs-stay-local-p dir)
584 ;; local diff: do it filewise, and only for files that are modified
585 (vc-file-tree-walk
587 (lambda (f)
588 (vc-exec-after
589 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
590 ;; possible optimization: fetch the state of all files
591 ;; in the tree via vc-cvs-dir-state-heuristic
592 (unless (vc-up-to-date-p ',f)
593 (message "Looking at %s" ',f)
594 (vc-diff-internal ',f ',rev1 ',rev2))))))
595 ;; cvs diff: use a single call for the entire tree
596 (let ((coding-system-for-read
597 (or coding-system-for-read 'undecided)))
598 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
599 (and rev1 (concat "-r" rev1))
600 (and rev2 (concat "-r" rev2))
601 (vc-diff-switches-list 'CVS))))))
603 (defun vc-cvs-annotate-command (file buffer &optional version)
604 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
605 Optional arg VERSION is a version to annotate from."
606 (vc-cvs-command buffer 0 file "annotate" (if version
607 (concat "-r" version))))
609 (defun vc-cvs-annotate-current-time ()
610 "Return the current time, based at midnight of the current day, and
611 encoded as fractional days."
612 (vc-annotate-convert-time
613 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
615 (defun vc-cvs-annotate-time ()
616 "Return the time of the next annotation (as fraction of days)
617 systime, or nil if there is none."
618 (let ((time-stamp
619 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
620 (if (looking-at time-stamp)
621 (progn
622 (let* ((day (string-to-number (match-string 1)))
623 (month (cdr (assoc (match-string 2)
624 vc-cvs-local-month-numbers)))
625 (year-tmp (string-to-number (match-string 3)))
626 ;; Years 0..68 are 2000..2068.
627 ;; Years 69..99 are 1969..1999.
628 (year (+ (cond ((> 69 year-tmp) 2000)
629 ((> 100 year-tmp) 1900)
630 (t 0))
631 year-tmp)))
632 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
633 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
634 ;; If we did not look directly at an annotation, there might be
635 ;; some further down. This is the case if we are positioned at
636 ;; the very top of the buffer, for instance.
637 (if (re-search-forward time-stamp nil t)
638 (progn
639 (beginning-of-line nil)
640 (vc-cvs-annotate-time))))))
643 ;;; Snapshot system
646 (defun vc-cvs-create-snapshot (dir name branchp)
647 "Assign to DIR's current version a given NAME.
648 If BRANCHP is non-nil, the name is created as a branch (and the current
649 workspace is immediately moved to that new branch)."
650 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
651 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
653 (defun vc-cvs-retrieve-snapshot (dir name update)
654 "Retrieve a snapshot at and below DIR.
655 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
656 If UPDATE is non-nil, then update (resynch) any affected buffers."
657 (with-current-buffer (get-buffer-create "*vc*")
658 (let ((default-directory dir)
659 (sticky-tag))
660 (erase-buffer)
661 (if (or (not name) (string= name ""))
662 (vc-cvs-command t 0 nil "update")
663 (vc-cvs-command t 0 nil "update" "-r" name)
664 (setq sticky-tag name))
665 (when update
666 (goto-char (point-min))
667 (while (not (eobp))
668 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
669 (let* ((file (expand-file-name (match-string 2) dir))
670 (state (match-string 1))
671 (buffer (find-buffer-visiting file)))
672 (when buffer
673 (cond
674 ((or (string= state "U")
675 (string= state "P"))
676 (vc-file-setprop file 'vc-state 'up-to-date)
677 (vc-file-setprop file 'vc-workfile-version nil)
678 (vc-file-setprop file 'vc-checkout-time
679 (nth 5 (file-attributes file))))
680 ((or (string= state "M")
681 (string= state "C"))
682 (vc-file-setprop file 'vc-state 'edited)
683 (vc-file-setprop file 'vc-workfile-version nil)
684 (vc-file-setprop file 'vc-checkout-time 0)))
685 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
686 (vc-resynch-buffer file t t))))
687 (forward-line 1))))))
691 ;;; Miscellaneous
694 (defun vc-cvs-make-version-backups-p (file)
695 "Return non-nil if version backups should be made for FILE."
696 (vc-cvs-stay-local-p file))
698 (defun vc-cvs-check-headers ()
699 "Check if the current file has any headers in it."
700 (save-excursion
701 (goto-char (point-min))
702 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
703 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
707 ;;; Internal functions
710 (defun vc-cvs-command (buffer okstatus file &rest flags)
711 "A wrapper around `vc-do-command' for use in vc-cvs.el.
712 The difference to vc-do-command is that this function always invokes `cvs',
713 and that it passes `vc-cvs-global-switches' to it before FLAGS."
714 (apply 'vc-do-command buffer okstatus "cvs" file
715 (if (stringp vc-cvs-global-switches)
716 (cons vc-cvs-global-switches flags)
717 (append vc-cvs-global-switches
718 flags))))
720 (defun vc-cvs-stay-local-p (file)
721 "Return non-nil if VC should stay local when handling FILE."
722 (if vc-cvs-stay-local
723 (let* ((dirname (if (file-directory-p file)
724 (directory-file-name file)
725 (file-name-directory file)))
726 (prop
727 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
728 (let ((rootname (expand-file-name "CVS/Root" dirname)))
729 (vc-file-setprop
730 dirname 'vc-cvs-stay-local-p
731 (when (file-readable-p rootname)
732 (with-temp-buffer
733 (vc-insert-file rootname)
734 (goto-char (point-min))
735 (if (looking-at "\\([^:]*\\):")
736 (if (not (stringp vc-cvs-stay-local))
737 'yes
738 (let ((hostname (match-string 1)))
739 (if (string-match vc-cvs-stay-local hostname)
740 'yes
741 'no)))
742 'no))))))))
743 (if (eq prop 'yes) t nil))))
745 (defun vc-cvs-parse-status (&optional full)
746 "Parse output of \"cvs status\" command in the current buffer.
747 Set file properties accordingly. Unless FULL is t, parse only
748 essential information."
749 (let (file status)
750 (goto-char (point-min))
751 (if (re-search-forward "^File: " nil t)
752 (cond
753 ((looking-at "no file") nil)
754 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
755 (setq file (expand-file-name (match-string 1)))
756 (vc-file-setprop file 'vc-backend 'CVS)
757 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
758 (setq status "Unknown")
759 (setq status (match-string 1)))
760 (if (and full
761 (re-search-forward
762 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
763 \[\t ]+\\([0-9.]+\\)"
764 nil t))
765 (vc-file-setprop file 'vc-latest-version (match-string 2)))
766 (vc-file-setprop
767 file 'vc-state
768 (cond
769 ((string-match "Up-to-date" status)
770 (vc-file-setprop file 'vc-checkout-time
771 (nth 5 (file-attributes file)))
772 'up-to-date)
773 ((string-match "Locally Modified" status) 'edited)
774 ((string-match "Needs Merge" status) 'needs-merge)
775 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
776 (t 'edited))))))))
778 (defun vc-cvs-dir-state-heuristic (dir)
779 "Find the CVS state of all files in DIR, using only local information."
780 (with-temp-buffer
781 (vc-insert-file (expand-file-name "CVS/Entries" dir))
782 (goto-char (point-min))
783 (while (not (eobp))
784 ;; CVS-removed files are not taken under VC control.
785 (when (looking-at "/\\([^/]*\\)/[^/-]")
786 (let ((file (expand-file-name (match-string 1) dir)))
787 (unless (vc-file-getprop file 'vc-state)
788 (vc-cvs-parse-entry file t))))
789 (forward-line 1))))
792 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
793 "Return non-nil if TAG is a valid symbolic tag name."
794 ;; According to the CVS manual, a valid symbolic tag must start with
795 ;; an uppercase or lowercase letter and can contain uppercase and
796 ;; lowercase letters, digits, `-', and `_'.
797 (and (string-match "^[a-zA-Z]" tag)
798 (not (string-match "[^a-z0-9A-Z-_]" tag))))
800 (defun vc-cvs-valid-version-number-p (tag)
801 "Return non-nil if TAG is a valid version number."
802 (and (string-match "^[0-9]" tag)
803 (not (string-match "[^0-9.]" tag))))
805 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
806 "Parse and return the sticky tag as a string.
807 `match-data' is protected."
808 (let ((data (match-data))
809 (tag)
810 (type (cond ((string= match-type "D") 'date)
811 ((string= match-type "T")
812 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
813 'symbolic-name
814 'revision-number))
815 (t nil))))
816 (unwind-protect
817 (progn
818 (cond
819 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
820 ((eq type 'date)
821 (string-match
822 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
823 match-tag)
824 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
825 (month (string-to-number (match-string 2 match-tag)))
826 (day (string-to-number (match-string 3 match-tag)))
827 (hour (string-to-number (match-string 4 match-tag)))
828 (min (string-to-number (match-string 5 match-tag)))
829 (sec (string-to-number (match-string 6 match-tag)))
830 ;; Years 0..68 are 2000..2068.
831 ;; Years 69..99 are 1969..1999.
832 (year (+ (cond ((> 69 year-tmp) 2000)
833 ((> 100 year-tmp) 1900)
834 (t 0))
835 year-tmp)))
836 (setq tag (encode-time sec min hour day month year))))
837 ;; Sticky Tag name or revision number
838 ((eq type 'symbolic-name) (setq tag match-tag))
839 ((eq type 'revision-number) (setq tag match-tag))
840 ;; Default is no sticky tag at all
841 (t nil))
842 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
843 ((eq vc-cvs-sticky-tag-display t)
844 (cond ((eq type 'date) (format-time-string
845 vc-cvs-sticky-date-format-string
846 tag))
847 ((eq type 'symbolic-name) tag)
848 ((eq type 'revision-number) tag)
849 (t nil)))
850 ((functionp vc-cvs-sticky-tag-display)
851 (funcall vc-cvs-sticky-tag-display tag type))
852 (t nil)))
854 (set-match-data data))))
856 (defun vc-cvs-parse-entry (file &optional set-state)
857 "Parse a line from CVS/Entries.
858 Compare modification time to that of the FILE, set file properties
859 accordingly. However, `vc-state' is set only if optional arg SET-STATE
860 is non-nil."
861 (cond
862 ;; entry for a "locally added" file (not yet committed)
863 ((looking-at "/[^/]+/0/")
864 (vc-file-setprop file 'vc-checkout-time 0)
865 (vc-file-setprop file 'vc-workfile-version "0")
866 (if set-state (vc-file-setprop file 'vc-state 'edited)))
867 ;; normal entry
868 ((looking-at
869 (concat "/[^/]+"
870 ;; revision
871 "/\\([^/]*\\)"
872 ;; timestamp
873 "/\\([^/]*\\)"
874 ;; optional conflict field
875 "\\(+[^/]*\\)?/"
876 ;; options
877 "\\([^/]*\\)/"
878 ;; sticky tag
879 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
880 "\\(.*\\)")) ;Sticky tag
881 (vc-file-setprop file 'vc-workfile-version (match-string 1))
882 (vc-file-setprop file 'vc-cvs-sticky-tag
883 (vc-cvs-parse-sticky-tag (match-string 5) (match-string 6)))
884 ;; compare checkout time and modification time
885 (let ((mtime (nth 5 (file-attributes file)))
886 (system-time-locale "C"))
887 (cond ((equal (format-time-string "%c" mtime 'utc) (match-string 2))
888 (vc-file-setprop file 'vc-checkout-time mtime)
889 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
891 (vc-file-setprop file 'vc-checkout-time 0)
892 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
894 (provide 'vc-cvs)
896 ;;; vc-cvs.el ends here