*** empty log message ***
[emacs.git] / lisp / vc-cvs.el
blob337170ab8963fdcaf1ef809d8ee6ca9887498211
1 ;;; vc-cvs.el --- non-resident support for CVS version-control
3 ;; Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; $Id$
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;;; Code:
32 (eval-when-compile (require 'cl) (require 'vc))
34 ;; Clear up the cache to force vc-call to check again and discover
35 ;; new functions when we reload this file.
36 (put 'CVS 'vc-functions nil)
38 ;;;
39 ;;; Customization options
40 ;;;
42 (defcustom vc-cvs-global-switches nil
43 "*Global switches to pass to any CVS command."
44 :type '(choice (const :tag "None" nil)
45 (string :tag "Argument String")
46 (repeat :tag "Argument List"
47 :value ("")
48 string))
49 :version "22.1"
50 :group 'vc)
52 (defcustom vc-cvs-register-switches nil
53 "*Extra switches for registering a file into CVS.
54 A string or list of strings passed to the checkin program by
55 \\[vc-register]."
56 :type '(choice (const :tag "None" nil)
57 (string :tag "Argument String")
58 (repeat :tag "Argument List"
59 :value ("")
60 string))
61 :version "21.1"
62 :group 'vc)
64 (defcustom vc-cvs-diff-switches nil
65 "*A string or list of strings specifying extra switches for cvs diff under VC."
66 :type '(choice (const :tag "None" nil)
67 (string :tag "Argument String")
68 (repeat :tag "Argument List"
69 :value ("")
70 string))
71 :version "21.1"
72 :group 'vc)
74 (defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
75 "*Header keywords to be inserted by `vc-insert-headers'."
76 :version "21.1"
77 :type '(repeat string)
78 :group 'vc)
80 (defcustom vc-cvs-use-edit t
81 "*Non-nil means to use `cvs edit' to \"check out\" a file.
82 This is only meaningful if you don't use the implicit checkout model
83 \(i.e. if you have $CVSREAD set)."
84 :type 'boolean
85 :version "21.1"
86 :group 'vc)
88 (defcustom vc-cvs-stay-local t
89 "*Non-nil means use local operations when possible for remote repositories.
90 This avoids slow queries over the network and instead uses heuristics
91 and past information to determine the current status of a file.
93 The value can also be a regular expression or list of regular
94 expressions to match against the host name of a repository; then VC
95 only stays local for hosts that match it. Alternatively, the value
96 can be a list of regular expressions where the first element is the
97 symbol `except'; then VC always stays local except for hosts matched
98 by these regular expressions."
99 :type '(choice (const :tag "Always stay local" t)
100 (const :tag "Don't stay local" nil)
101 (list :format "\nExamine hostname and %v" :tag "Examine hostname ..."
102 (set :format "%v" :inline t (const :format "%t" :tag "don't" except))
103 (regexp :format " stay local,\n%t: %v" :tag "if it matches")
104 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
105 :version "21.1"
106 :group 'vc)
108 (defcustom vc-cvs-sticky-date-format-string "%c"
109 "*Format string for mode-line display of sticky date.
110 Format is according to `format-time-string'. Only used if
111 `vc-cvs-sticky-tag-display' is t."
112 :type '(string)
113 :version "22.1"
114 :group 'vc)
116 (defcustom vc-cvs-sticky-tag-display t
117 "*Specify the mode-line display of sticky tags.
118 Value t means default display, nil means no display at all. If the
119 value is a function or macro, it is called with the sticky tag and
120 its' type as parameters, in that order. TYPE can have three different
121 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
122 string) and `date' (TAG is a date as returned by `encode-time'). The
123 return value of the function or macro will be displayed as a string.
125 Here's an example that will display the formatted date for sticky
126 dates and the word \"Sticky\" for sticky tag names and revisions.
128 (lambda (tag type)
129 (cond ((eq type 'date) (format-time-string
130 vc-cvs-sticky-date-format-string tag))
131 ((eq type 'revision-number) \"Sticky\")
132 ((eq type 'symbolic-name) \"Sticky\")))
134 Here's an example that will abbreviate to the first character only,
135 any text before the first occurrence of `-' for sticky symbolic tags.
136 If the sticky tag is a revision number, the word \"Sticky\" is
137 displayed. Date and time is displayed for sticky dates.
139 (lambda (tag type)
140 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
141 ((eq type 'revision-number) \"Sticky\")
142 ((eq type 'symbolic-name)
143 (condition-case nil
144 (progn
145 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
146 (concat (substring (match-string 1 tag) 0 1) \":\"
147 (substring (match-string 2 tag) 1 nil)))
148 (error tag))))) ; Fall-back to given tag name.
150 See also variable `vc-cvs-sticky-date-format-string'."
151 :type '(choice boolean function)
152 :version "22.1"
153 :group 'vc)
156 ;;; Internal variables
161 ;;; State-querying functions
164 ;;;###autoload (defun vc-cvs-registered (f)
165 ;;;###autoload (when (file-readable-p (expand-file-name
166 ;;;###autoload "CVS/Entries" (file-name-directory f)))
167 ;;;###autoload (load "vc-cvs")
168 ;;;###autoload (vc-cvs-registered f)))
170 (defun vc-cvs-registered (file)
171 "Check if FILE is CVS registered."
172 (let ((dirname (or (file-name-directory file) ""))
173 (basename (file-name-nondirectory file))
174 ;; make sure that the file name is searched case-sensitively
175 (case-fold-search nil))
176 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
177 (with-temp-buffer
178 (vc-cvs-get-entries dirname)
179 (goto-char (point-min))
180 (cond
181 ((re-search-forward
182 ;; CVS-removed files are not taken under VC control.
183 (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
184 (beginning-of-line)
185 (vc-cvs-parse-entry file)
187 (t nil)))
188 nil)))
190 (defun vc-cvs-state (file)
191 "CVS-specific version of `vc-state'."
192 (if (vc-stay-local-p file)
193 (let ((state (vc-file-getprop file 'vc-state)))
194 ;; If we should stay local, use the heuristic but only if
195 ;; we don't have a more precise state already available.
196 (if (memq state '(up-to-date edited nil))
197 (vc-cvs-state-heuristic file)
198 state))
199 (with-temp-buffer
200 (cd (file-name-directory file))
201 (vc-cvs-command t 0 file "status")
202 (vc-cvs-parse-status t))))
204 (defun vc-cvs-state-heuristic (file)
205 "CVS-specific state heuristic."
206 ;; If the file has not changed since checkout, consider it `up-to-date'.
207 ;; Otherwise consider it `edited'.
208 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
209 (lastmod (nth 5 (file-attributes file))))
210 (if (equal checkout-time lastmod)
211 'up-to-date
212 'edited)))
214 (defun vc-cvs-dir-state (dir)
215 "Find the CVS state of all files in DIR and subdirectories."
216 ;; if DIR is not under CVS control, don't do anything.
217 (when (file-readable-p (expand-file-name "CVS/Entries" dir))
218 (if (vc-stay-local-p dir)
219 (vc-cvs-dir-state-heuristic dir)
220 (let ((default-directory dir))
221 ;; Don't specify DIR in this command, the default-directory is
222 ;; enough. Otherwise it might fail with remote repositories.
223 (with-temp-buffer
224 (vc-cvs-command t 0 nil "status")
225 (goto-char (point-min))
226 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
227 (narrow-to-region (match-beginning 0) (match-end 0))
228 (vc-cvs-parse-status)
229 (goto-char (point-max))
230 (widen)))))))
232 (defun vc-cvs-working-revision (file)
233 "CVS-specific version of `vc-working-revision'."
234 ;; There is no need to consult RCS headers under CVS, because we
235 ;; get the workfile version for free when we recognize that a file
236 ;; is registered in CVS.
237 (vc-cvs-registered file)
238 (vc-file-getprop file 'vc-working-revision))
240 (defun vc-cvs-checkout-model (file)
241 "CVS-specific version of `vc-checkout-model'."
242 (if (getenv "CVSREAD")
243 'announce
244 (let ((attrib (file-attributes file)))
245 (if (and attrib ;; don't check further if FILE doesn't exist
246 ;; If the file is not writable (despite CVSREAD being
247 ;; undefined), this is probably because the file is being
248 ;; "watched" by other developers.
249 ;; (If vc-mistrust-permissions was t, we actually shouldn't
250 ;; trust this, but there is no other way to learn this from CVS
251 ;; at the moment (version 1.9).)
252 (string-match "r-..-..-." (nth 8 attrib)))
253 'announce
254 'implicit))))
256 (defun vc-cvs-mode-line-string (file)
257 "Return string for placement into the modeline for FILE.
258 Compared to the default implementation, this function does two things:
259 Handle the special case of a CVS file that is added but not yet
260 committed and support display of sticky tags."
261 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
262 help-echo
263 (string
264 (if (string= (vc-working-revision file) "0")
265 ;; A file that is added but not yet committed.
266 (progn
267 (setq help-echo "Added file (needs commit) under CVS")
268 "CVS @@")
269 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
270 (setq help-echo
271 (get-text-property 0 'help-echo def-ml))
272 def-ml))))
273 (propertize
274 (if (zerop (length sticky-tag))
275 string
276 (setq help-echo (format "%s on the '%s' branch"
277 help-echo sticky-tag))
278 (concat string "[" sticky-tag "]"))
279 'help-echo help-echo)))
281 (defun vc-cvs-dired-state-info (file)
282 "CVS-specific version of `vc-dired-state-info'."
283 (let ((cvs-state (vc-state file)))
284 (cond ((eq cvs-state 'edited)
285 (if (equal (vc-working-revision file) "0")
286 "(added)" "(modified)"))
287 ((eq cvs-state 'needs-patch) "(patch)")
288 ((eq cvs-state 'needs-merge) "(merge)"))))
292 ;;; State-changing functions
295 (defun vc-cvs-register (files &optional rev comment)
296 "Register FILES into the CVS version-control system.
297 COMMENT can be used to provide an initial description of FILES.
299 `vc-register-switches' and `vc-cvs-register-switches' are passed to
300 the CVS command (in that order)."
301 (when (and (not (vc-cvs-responsible-p file))
302 (vc-cvs-could-register file))
303 ;; Register the directory if needed.
304 (vc-cvs-register (directory-file-name (file-name-directory file))))
305 (apply 'vc-cvs-command nil 0 files
306 "add"
307 (and comment (string-match "[^\t\n ]" comment)
308 (concat "-m" comment))
309 (vc-switches 'CVS 'register)))
311 (defun vc-cvs-responsible-p (file)
312 "Return non-nil if CVS thinks it is responsible for FILE."
313 (file-directory-p (expand-file-name "CVS"
314 (if (file-directory-p file)
315 file
316 (file-name-directory file)))))
318 (defun vc-cvs-could-register (file)
319 "Return non-nil if FILE could be registered in CVS.
320 This is only possible if CVS is managing FILE's directory or one of
321 its parents."
322 (let ((dir file))
323 (while (and (stringp dir)
324 (not (equal dir (setq dir (file-name-directory dir))))
325 dir)
326 (setq dir (if (file-directory-p
327 (expand-file-name "CVS/Entries" dir))
328 t (directory-file-name dir))))
329 (eq dir t)))
331 (defun vc-cvs-checkin (files rev comment)
332 "CVS-specific version of `vc-backend-checkin'."
333 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
334 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
335 (error "%s is not a valid symbolic tag name" rev)
336 ;; If the input revison is a valid symbolic tag name, we create it
337 ;; as a branch, commit and switch to it.
338 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
339 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
340 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
341 files)))
342 (let ((status (apply 'vc-cvs-command nil 1 files
343 "ci" (if rev (concat "-r" rev))
344 (concat "-m" comment)
345 (vc-switches 'CVS 'checkin))))
346 (set-buffer "*vc*")
347 (goto-char (point-min))
348 (when (not (zerop status))
349 ;; Check checkin problem.
350 (cond
351 ((re-search-forward "Up-to-date check failed" nil t)
352 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
353 files)
354 (error "%s" (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 ;; Single-file commit? Then update the revision by parsing the buffer.
363 ;; Otherwise we can't necessarily tell what goes with what; clear
364 ;; its properties so they have to be refetched.
365 (if (= (length files) 1)
366 (vc-file-setprop
367 (car files) 'vc-working-revision
368 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
369 (mapc (lambda (file) (vc-file-clearprops file)) files))
370 ;; Anyway, forget the checkout model of the file, because we might have
371 ;; guessed wrong when we found the file. After commit, we can
372 ;; tell it from the permissions of the file (see
373 ;; vc-cvs-checkout-model).
374 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
375 files)
377 ;; if this was an explicit check-in (does not include creation of
378 ;; a branch), remove the sticky tag.
379 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
380 (vc-cvs-command nil 0 files "update" "-A"))))
382 (defun vc-cvs-find-revision (file rev buffer)
383 (apply 'vc-cvs-command
384 buffer 0 file
385 "-Q" ; suppress diagnostic output
386 "update"
387 (and rev (not (string= rev ""))
388 (concat "-r" rev))
389 "-p"
390 (vc-switches 'CVS 'checkout)))
392 (defun vc-cvs-checkout (file &optional editable rev)
393 "Checkout a revision of FILE into the working area.
394 EDITABLE non-nil means that the file should be writable.
395 REV is the revision to check out."
396 (message "Checking out %s..." file)
397 ;; Change buffers to get local value of vc-checkout-switches.
398 (with-current-buffer (or (get-file-buffer file) (current-buffer))
399 (if (and (file-exists-p file) (not rev))
400 ;; If no revision was specified, just make the file writable
401 ;; if necessary (using `cvs-edit' if requested).
402 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
403 (if vc-cvs-use-edit
404 (vc-cvs-command nil 0 file "edit")
405 (set-file-modes file (logior (file-modes file) 128))
406 (if (equal file buffer-file-name) (toggle-read-only -1))))
407 ;; Check out a particular revision (or recreate the file).
408 (vc-file-setprop file 'vc-working-revision nil)
409 (apply 'vc-cvs-command nil 0 file
410 (and editable "-w")
411 "update"
412 (when rev
413 (unless (eq rev t)
414 ;; default for verbose checkout: clear the
415 ;; sticky tag so that the actual update will
416 ;; get the head of the trunk
417 (if (string= rev "")
418 "-A"
419 (concat "-r" rev))))
420 (vc-switches 'CVS 'checkout)))
421 (vc-mode-line file))
422 (message "Checking out %s...done" file))
424 (defun vc-cvs-delete-file (file)
425 (vc-cvs-command nil 0 file "remove" "-f")
426 (vc-cvs-command nil 0 file "commit" "-mRemoved."))
428 (defun vc-cvs-revert (file &optional contents-done)
429 "Revert FILE to the working revision on which it was based."
430 (vc-default-revert 'CVS file contents-done)
431 (unless (eq (vc-checkout-model file) 'implicit)
432 (if vc-cvs-use-edit
433 (vc-cvs-command nil 0 file "unedit")
434 ;; Make the file read-only by switching off all w-bits
435 (set-file-modes file (logand (file-modes file) 3950)))))
437 (defun vc-cvs-merge (file first-revision &optional second-revision)
438 "Merge changes into current working copy of FILE.
439 The changes are between FIRST-REVISION and SECOND-REVISION."
440 (vc-cvs-command nil 0 file
441 "update" "-kk"
442 (concat "-j" first-revision)
443 (concat "-j" second-revision))
444 (vc-file-setprop file 'vc-state 'edited)
445 (with-current-buffer (get-buffer "*vc*")
446 (goto-char (point-min))
447 (if (re-search-forward "conflicts during merge" nil t)
448 1 ; signal error
449 0))) ; signal success
451 (defun vc-cvs-merge-news (file)
452 "Merge in any new changes made to FILE."
453 (message "Merging changes into %s..." file)
454 ;; (vc-file-setprop file 'vc-working-revision nil)
455 (vc-file-setprop file 'vc-checkout-time 0)
456 (vc-cvs-command nil 0 file "update")
457 ;; Analyze the merge result reported by CVS, and set
458 ;; file properties accordingly.
459 (with-current-buffer (get-buffer "*vc*")
460 (goto-char (point-min))
461 ;; get new working revision
462 (if (re-search-forward
463 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
464 (vc-file-setprop file 'vc-working-revision (match-string 1))
465 (vc-file-setprop file 'vc-working-revision nil))
466 ;; get file status
467 (prog1
468 (if (eq (buffer-size) 0)
469 0 ;; there were no news; indicate success
470 (if (re-search-forward
471 (concat "^\\([CMUP] \\)?"
472 (regexp-quote (file-name-nondirectory file))
473 "\\( already contains the differences between \\)?")
474 nil t)
475 (cond
476 ;; Merge successful, we are in sync with repository now
477 ((or (match-string 2)
478 (string= (match-string 1) "U ")
479 (string= (match-string 1) "P "))
480 (vc-file-setprop file 'vc-state 'up-to-date)
481 (vc-file-setprop file 'vc-checkout-time
482 (nth 5 (file-attributes file)))
483 0);; indicate success to the caller
484 ;; Merge successful, but our own changes are still in the file
485 ((string= (match-string 1) "M ")
486 (vc-file-setprop file 'vc-state 'edited)
487 0);; indicate success to the caller
488 ;; Conflicts detected!
490 (vc-file-setprop file 'vc-state 'edited)
491 1);; signal the error to the caller
493 (pop-to-buffer "*vc*")
494 (error "Couldn't analyze cvs update result")))
495 (message "Merging changes into %s...done" file))))
499 ;;; History functions
502 (defun vc-cvs-print-log (files &optional buffer)
503 "Get change logs associated with FILES."
504 ;; It's just the catenation of the individual logs.
505 (vc-cvs-command
506 buffer
507 (if (vc-stay-local-p files) 'async 0)
508 files "log"))
510 (defun vc-cvs-wash-log ()
511 "Remove all non-comment information from log output."
512 (vc-call-backend 'RCS 'wash-log)
513 nil)
515 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
516 "Get a difference report using CVS between two revisions of FILE."
517 (let* ((async (and (not vc-disable-async-diff)
518 (vc-stay-local-p files)))
519 (invoke-cvs-diff-list nil)
520 status)
521 ;; Look through the file list and see if any files have backups
522 ;; that can be used to do a plain "diff" instead of "cvs diff".
523 (dolist (file files)
524 (let ((ov oldvers)
525 (nv newvers))
526 (when (or (not ov) (string-equal ov ""))
527 (setq ov (vc-working-revision file)))
528 (when (string-equal nv "")
529 (setq nv nil))
530 (let ((file-oldvers (vc-version-backup-file file ov))
531 (file-newvers (if (not nv)
532 file
533 (vc-version-backup-file file nv)))
534 (coding-system-for-read (vc-coding-system-for-diff file)))
535 (if (and file-oldvers file-newvers)
536 (progn
537 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
538 (append (if (listp diff-switches)
539 diff-switches
540 (list diff-switches))
541 (if (listp vc-diff-switches)
542 vc-diff-switches
543 (list vc-diff-switches))
544 (list (file-relative-name file-oldvers)
545 (file-relative-name file-newvers))))
546 (setq status 0))
547 (push file invoke-cvs-diff-list)))))
548 (when invoke-cvs-diff-list
549 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
550 (if async 'async 1)
551 invoke-cvs-diff-list "diff"
552 (and oldvers (concat "-r" oldvers))
553 (and newvers (concat "-r" newvers))
554 (vc-switches 'CVS 'diff))))
555 (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-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 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
584 (defun vc-cvs-annotate-process-filter (process string)
585 (setq string (concat (process-get process 'output) string))
586 (if (not (string-match vc-cvs-annotate-first-line-re string))
587 ;; Still waiting for the first real line.
588 (process-put process 'output string)
589 (let ((vc-filter (process-get process 'vc-filter)))
590 (set-process-filter process vc-filter)
591 (funcall vc-filter process (substring string (match-beginning 0))))))
593 (defun vc-cvs-annotate-command (file buffer &optional revision)
594 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
595 Optional arg REVISION is a revision to annotate from."
596 (vc-cvs-command buffer
597 (if (vc-stay-local-p file)
598 'async 0)
599 file "annotate"
600 (if revision (concat "-r" revision)))
601 ;; Strip the leading few lines.
602 (let ((proc (get-buffer-process buffer)))
603 (if proc
604 ;; If running asynchronously, use a process filter.
605 (progn
606 (process-put proc 'vc-filter (process-filter proc))
607 (set-process-filter proc 'vc-cvs-annotate-process-filter))
608 (with-current-buffer buffer
609 (goto-char (point-min))
610 (re-search-forward vc-cvs-annotate-first-line-re)
611 (delete-region (point-min) (1- (point)))))))
613 (defun vc-cvs-annotate-current-time ()
614 "Return the current time, based at midnight of the current day, and
615 encoded as fractional days."
616 (vc-annotate-convert-time
617 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
619 (defun vc-cvs-annotate-time ()
620 "Return the time of the next annotation (as fraction of days)
621 systime, or nil if there is none."
622 (let* ((bol (point))
623 (cache (get-text-property bol 'vc-cvs-annotate-time))
624 (inhibit-read-only t)
625 (inhibit-modification-hooks t))
626 (cond
627 (cache)
628 ((looking-at
629 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
630 (let ((day (string-to-number (match-string 1)))
631 (month (cdr (assq (intern (match-string 2))
632 '((Jan . 1) (Feb . 2) (Mar . 3)
633 (Apr . 4) (May . 5) (Jun . 6)
634 (Jul . 7) (Aug . 8) (Sep . 9)
635 (Oct . 10) (Nov . 11) (Dec . 12)))))
636 (year (let ((tmp (string-to-number (match-string 3))))
637 ;; Years 0..68 are 2000..2068.
638 ;; Years 69..99 are 1969..1999.
639 (+ (cond ((> 69 tmp) 2000)
640 ((> 100 tmp) 1900)
641 (t 0))
642 tmp))))
643 (put-text-property
644 bol (1+ bol) 'vc-cvs-annotate-time
645 (setq cache (cons
646 ;; Position at end makes for nicer overlay result.
647 (match-end 0)
648 (vc-annotate-convert-time
649 (encode-time 0 0 0 day month year))))))))
650 (when cache
651 (goto-char (car cache)) ; fontify from here to eol
652 (cdr cache)))) ; days (float)
654 (defun vc-cvs-annotate-extract-revision-at-line ()
655 (save-excursion
656 (beginning-of-line)
657 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
658 (line-end-position) t)
659 (match-string-no-properties 1)
660 nil)))
663 ;;; Snapshot system
666 (defun vc-cvs-create-snapshot (dir name branchp)
667 "Assign to DIR's current revision a given NAME.
668 If BRANCHP is non-nil, the name is created as a branch (and the current
669 workspace is immediately moved to that new branch)."
670 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
671 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
673 (defun vc-cvs-retrieve-snapshot (dir name update)
674 "Retrieve a snapshot at and below DIR.
675 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
676 If UPDATE is non-nil, then update (resynch) any affected buffers."
677 (with-current-buffer (get-buffer-create "*vc*")
678 (let ((default-directory dir)
679 (sticky-tag))
680 (erase-buffer)
681 (if (or (not name) (string= name ""))
682 (vc-cvs-command t 0 nil "update")
683 (vc-cvs-command t 0 nil "update" "-r" name)
684 (setq sticky-tag name))
685 (when update
686 (goto-char (point-min))
687 (while (not (eobp))
688 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
689 (let* ((file (expand-file-name (match-string 2) dir))
690 (state (match-string 1))
691 (buffer (find-buffer-visiting file)))
692 (when buffer
693 (cond
694 ((or (string= state "U")
695 (string= state "P"))
696 (vc-file-setprop file 'vc-state 'up-to-date)
697 (vc-file-setprop file 'vc-working-revision nil)
698 (vc-file-setprop file 'vc-checkout-time
699 (nth 5 (file-attributes file))))
700 ((or (string= state "M")
701 (string= state "C"))
702 (vc-file-setprop file 'vc-state 'edited)
703 (vc-file-setprop file 'vc-working-revision nil)
704 (vc-file-setprop file 'vc-checkout-time 0)))
705 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
706 (vc-resynch-buffer file t t))))
707 (forward-line 1))))))
711 ;;; Miscellaneous
714 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
715 "Return non-nil if version backups should be made for FILE.")
717 (defun vc-cvs-check-headers ()
718 "Check if the current file has any headers in it."
719 (save-excursion
720 (goto-char (point-min))
721 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
722 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
726 ;;; Internal functions
729 (defun vc-cvs-command (buffer okstatus files &rest flags)
730 "A wrapper around `vc-do-command' for use in vc-cvs.el.
731 The difference to vc-do-command is that this function always invokes `cvs',
732 and that it passes `vc-cvs-global-switches' to it before FLAGS."
733 (apply 'vc-do-command buffer okstatus "cvs" files
734 (if (stringp vc-cvs-global-switches)
735 (cons vc-cvs-global-switches flags)
736 (append vc-cvs-global-switches
737 flags))))
739 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
741 (defun vc-cvs-repository-hostname (dirname)
742 "Hostname of the CVS server associated to workarea DIRNAME."
743 (let ((rootname (expand-file-name "CVS/Root" dirname)))
744 (when (file-readable-p rootname)
745 (with-temp-buffer
746 (let ((coding-system-for-read
747 (or file-name-coding-system
748 default-file-name-coding-system)))
749 (vc-insert-file rootname))
750 (goto-char (point-min))
751 (nth 2 (vc-cvs-parse-root
752 (buffer-substring (point)
753 (line-end-position))))))))
755 (defun vc-cvs-parse-root (root)
756 "Split CVS ROOT specification string into a list of fields.
757 A CVS root specification of the form
758 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
759 is converted to a normalized record with the following structure:
760 \(METHOD USER HOSTNAME CVS-ROOT).
761 The default METHOD for a CVS root of the form
762 /path/to/repository
763 is `local'.
764 The default METHOD for a CVS root of the form
765 [USER@]HOSTNAME:/path/to/repository
766 is `ext'.
767 For an empty string, nil is returned (invalid CVS root)."
768 ;; Split CVS root into colon separated fields (0-4).
769 ;; The `x:' makes sure, that leading colons are not lost;
770 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
771 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
772 (len (length root-list))
773 ;; All syntactic varieties will get a proper METHOD.
774 (root-list
775 (cond
776 ((= len 0)
777 ;; Invalid CVS root
778 nil)
779 ((= len 1)
780 ;; Simple PATH => method `local'
781 (cons "local"
782 (cons nil root-list)))
783 ((= len 2)
784 ;; [USER@]HOST:PATH => method `ext'
785 (and (not (equal (car root-list) ""))
786 (cons "ext" root-list)))
787 ((= len 3)
788 ;; :METHOD:PATH
789 (cons (cadr root-list)
790 (cons nil (cddr root-list))))
792 ;; :METHOD:[USER@]HOST:PATH
793 (cdr root-list)))))
794 (if root-list
795 (let ((method (car root-list))
796 (uhost (or (cadr root-list) ""))
797 (root (nth 2 root-list))
798 user host)
799 ;; Split USER@HOST
800 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
801 (setq user (match-string 1 uhost)
802 host (match-string 2 uhost))
803 (setq host uhost))
804 ;; Remove empty HOST
805 (and (equal host "")
806 (setq host))
807 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
808 (and host
809 (equal method "local")
810 (setq root (concat host ":" root) host))
811 ;; Normalize CVS root record
812 (list method user host root)))))
814 (defun vc-cvs-parse-status (&optional full)
815 "Parse output of \"cvs status\" command in the current buffer.
816 Set file properties accordingly. Unless FULL is t, parse only
817 essential information."
818 (let (file status)
819 (goto-char (point-min))
820 (if (re-search-forward "^File: " nil t)
821 (cond
822 ((looking-at "no file") nil)
823 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
824 (setq file (expand-file-name (match-string 1)))
825 (vc-file-setprop file 'vc-backend 'CVS)
826 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
827 (setq status "Unknown")
828 (setq status (match-string 1)))
829 (if (and full
830 (re-search-forward
831 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
832 \[\t ]+\\([0-9.]+\\)"
833 nil t))
834 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
835 (vc-file-setprop
836 file 'vc-state
837 (cond
838 ((string-match "Up-to-date" status)
839 (vc-file-setprop file 'vc-checkout-time
840 (nth 5 (file-attributes file)))
841 'up-to-date)
842 ((string-match "Locally Modified" status) 'edited)
843 ((string-match "Needs Merge" status) 'needs-merge)
844 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
845 (t 'edited))))))))
847 (defun vc-cvs-dir-state-heuristic (dir)
848 "Find the CVS state of all files in DIR, using only local information."
849 (with-temp-buffer
850 (vc-cvs-get-entries dir)
851 (goto-char (point-min))
852 (while (not (eobp))
853 ;; CVS-removed files are not taken under VC control.
854 (when (looking-at "/\\([^/]*\\)/[^/-]")
855 (let ((file (expand-file-name (match-string 1) dir)))
856 (unless (vc-file-getprop file 'vc-state)
857 (vc-cvs-parse-entry file t))))
858 (forward-line 1))))
860 (defun vc-cvs-get-entries (dir)
861 "Insert the CVS/Entries file from below DIR into the current buffer.
862 This function ensures that the correct coding system is used for that,
863 which may not be the one that is used for the files' contents.
864 CVS/Entries should only be accessed through this function."
865 (let ((coding-system-for-read (or file-name-coding-system
866 default-file-name-coding-system)))
867 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
869 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
870 "Return non-nil if TAG is a valid symbolic tag name."
871 ;; According to the CVS manual, a valid symbolic tag must start with
872 ;; an uppercase or lowercase letter and can contain uppercase and
873 ;; lowercase letters, digits, `-', and `_'.
874 (and (string-match "^[a-zA-Z]" tag)
875 (not (string-match "[^a-z0-9A-Z-_]" tag))))
877 (defun vc-cvs-valid-revision-number-p (tag)
878 "Return non-nil if TAG is a valid revision number."
879 (and (string-match "^[0-9]" tag)
880 (not (string-match "[^0-9.]" tag))))
882 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
883 "Parse and return the sticky tag as a string.
884 `match-data' is protected."
885 (let ((data (match-data))
886 (tag)
887 (type (cond ((string= match-type "D") 'date)
888 ((string= match-type "T")
889 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
890 'symbolic-name
891 'revision-number))
892 (t nil))))
893 (unwind-protect
894 (progn
895 (cond
896 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
897 ((eq type 'date)
898 (string-match
899 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
900 match-tag)
901 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
902 (month (string-to-number (match-string 2 match-tag)))
903 (day (string-to-number (match-string 3 match-tag)))
904 (hour (string-to-number (match-string 4 match-tag)))
905 (min (string-to-number (match-string 5 match-tag)))
906 (sec (string-to-number (match-string 6 match-tag)))
907 ;; Years 0..68 are 2000..2068.
908 ;; Years 69..99 are 1969..1999.
909 (year (+ (cond ((> 69 year-tmp) 2000)
910 ((> 100 year-tmp) 1900)
911 (t 0))
912 year-tmp)))
913 (setq tag (encode-time sec min hour day month year))))
914 ;; Sticky Tag name or revision number
915 ((eq type 'symbolic-name) (setq tag match-tag))
916 ((eq type 'revision-number) (setq tag match-tag))
917 ;; Default is no sticky tag at all
918 (t nil))
919 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
920 ((eq vc-cvs-sticky-tag-display t)
921 (cond ((eq type 'date) (format-time-string
922 vc-cvs-sticky-date-format-string
923 tag))
924 ((eq type 'symbolic-name) tag)
925 ((eq type 'revision-number) tag)
926 (t nil)))
927 ((functionp vc-cvs-sticky-tag-display)
928 (funcall vc-cvs-sticky-tag-display tag type))
929 (t nil)))
931 (set-match-data data))))
933 (defun vc-cvs-parse-entry (file &optional set-state)
934 "Parse a line from CVS/Entries.
935 Compare modification time to that of the FILE, set file properties
936 accordingly. However, `vc-state' is set only if optional arg SET-STATE
937 is non-nil."
938 (cond
939 ;; entry for a "locally added" file (not yet committed)
940 ((looking-at "/[^/]+/0/")
941 (vc-file-setprop file 'vc-checkout-time 0)
942 (vc-file-setprop file 'vc-working-revision "0")
943 (if set-state (vc-file-setprop file 'vc-state 'edited)))
944 ;; normal entry
945 ((looking-at
946 (concat "/[^/]+"
947 ;; revision
948 "/\\([^/]*\\)"
949 ;; timestamp and optional conflict field
950 "/\\([^/]*\\)/"
951 ;; options
952 "\\([^/]*\\)/"
953 ;; sticky tag
954 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
955 "\\(.*\\)")) ;Sticky tag
956 (vc-file-setprop file 'vc-working-revision (match-string 1))
957 (vc-file-setprop file 'vc-cvs-sticky-tag
958 (vc-cvs-parse-sticky-tag (match-string 4)
959 (match-string 5)))
960 ;; Compare checkout time and modification time.
961 ;; This is intentionally different from the algorithm that CVS uses
962 ;; (which is based on textual comparison), because there can be problems
963 ;; generating a time string that looks exactly like the one from CVS.
964 (let ((mtime (nth 5 (file-attributes file))))
965 (require 'parse-time)
966 (let ((parsed-time
967 (parse-time-string (concat (match-string 2) " +0000"))))
968 (cond ((and (not (string-match "\\+" (match-string 2)))
969 (car parsed-time)
970 (equal mtime (apply 'encode-time parsed-time)))
971 (vc-file-setprop file 'vc-checkout-time mtime)
972 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
974 (vc-file-setprop file 'vc-checkout-time 0)
975 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
977 ;; Completion of revision names.
978 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
979 ;; `cvs log' so I can list all the revision numbers rather than only
980 ;; tag names.
982 (defun vc-cvs-revision-table (file)
983 (let ((default-directory (file-name-directory file))
984 (res nil))
985 (with-temp-buffer
986 (vc-cvs-command t nil file "log")
987 (goto-char (point-min))
988 (when (re-search-forward "^symbolic names:\n" nil t)
989 (while (looking-at "^ \\(.*\\): \\(.*\\)")
990 (push (cons (match-string 1) (match-string 2)) res)
991 (forward-line 1)))
992 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
993 (push (match-string 1) res))
994 res)))
996 (defun vc-cvs-revision-completion-table (files)
997 (lexical-let ((files files)
998 table)
999 (setq table (lazy-completion-table
1000 table (lambda () (vc-cvs-revision-table (car files)))))
1001 table))
1004 (provide 'vc-cvs)
1006 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
1007 ;;; vc-cvs.el ends here