]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-cvs.el
e21aece85aef688e80b419c45fdb30dc62b18ee7
[gnu-emacs] / lisp / vc / vc-cvs.el
1 ;;; vc-cvs.el --- non-resident support for CVS version-control -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1995, 1998-2015 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'vc))
29
30 ;; Clear up the cache to force vc-call to check again and discover
31 ;; new functions when we reload this file.
32 (put 'CVS 'vc-functions nil)
33
34 ;;; Properties of the backend.
35
36 (defun vc-cvs-revision-granularity () 'file)
37
38 (defun vc-cvs-checkout-model (files)
39 "CVS-specific version of `vc-checkout-model'."
40 (if (getenv "CVSREAD")
41 'announce
42 (let* ((file (if (consp files) (car files) files))
43 (attrib (file-attributes file)))
44 (or (vc-file-getprop file 'vc-checkout-model)
45 (vc-file-setprop
46 file 'vc-checkout-model
47 (if (and attrib ;; don't check further if FILE doesn't exist
48 ;; If the file is not writable (despite CVSREAD being
49 ;; undefined), this is probably because the file is being
50 ;; "watched" by other developers.
51 ;; (We actually shouldn't trust this, but there is
52 ;; no other way to learn this from CVS at the
53 ;; moment (version 1.9).)
54 (string-match "r-..-..-." (nth 8 attrib)))
55 'announce
56 'implicit))))))
57
58 ;;;
59 ;;; Customization options
60 ;;;
61
62 (defgroup vc-cvs nil
63 "VC CVS backend."
64 :version "24.1"
65 :group 'vc)
66
67 (defcustom vc-cvs-global-switches nil
68 "Global switches to pass to any CVS command."
69 :type '(choice (const :tag "None" nil)
70 (string :tag "Argument String")
71 (repeat :tag "Argument List"
72 :value ("")
73 string))
74 :version "22.1"
75 :group 'vc-cvs)
76
77 (defcustom vc-cvs-register-switches nil
78 "Switches for registering a file into CVS.
79 A string or list of strings passed to the checkin program by
80 \\[vc-register]. If nil, use the value of `vc-register-switches'.
81 If t, use no switches."
82 :type '(choice (const :tag "Unspecified" nil)
83 (const :tag "None" t)
84 (string :tag "Argument String")
85 (repeat :tag "Argument List" :value ("") string))
86 :version "21.1"
87 :group 'vc-cvs)
88
89 (defcustom vc-cvs-diff-switches nil
90 "String or list of strings specifying switches for CVS diff under VC.
91 If nil, use the value of `vc-diff-switches'. If t, use no switches."
92 :type '(choice (const :tag "Unspecified" nil)
93 (const :tag "None" t)
94 (string :tag "Argument String")
95 (repeat :tag "Argument List" :value ("") string))
96 :version "21.1"
97 :group 'vc-cvs)
98
99 (defcustom vc-cvs-annotate-switches nil
100 "String or list of strings specifying switches for cvs annotate under VC.
101 If nil, use the value of `vc-annotate-switches'. If t, use no
102 switches."
103 :type '(choice (const :tag "Unspecified" nil)
104 (const :tag "None" t)
105 (string :tag "Argument String")
106 (repeat :tag "Argument List" :value ("") string))
107 :version "25.1"
108 :group 'vc-cvs)
109
110 (defcustom vc-cvs-header '("$Id\ $")
111 "Header keywords to be inserted by `vc-insert-headers'."
112 :version "24.1" ; no longer consult the obsolete vc-header-alist
113 :type '(repeat string)
114 :group 'vc-cvs)
115
116 (defcustom vc-cvs-use-edit t
117 "Non-nil means to use `cvs edit' to \"check out\" a file.
118 This is only meaningful if you don't use the implicit checkout model
119 \(i.e. if you have $CVSREAD set)."
120 :type 'boolean
121 :version "21.1"
122 :group 'vc-cvs)
123
124 (defcustom vc-stay-local 'only-file
125 "Non-nil means use local operations when possible for remote repositories.
126 This avoids slow queries over the network and instead uses heuristics
127 and past information to determine the current status of a file.
128
129 If value is the symbol `only-file', `vc-dir' will connect to the
130 server, but heuristics will be used to determine the status for
131 all other VC operations.
132
133 The value can also be a regular expression or list of regular
134 expressions to match against the host name of a repository; then VC
135 only stays local for hosts that match it. Alternatively, the value
136 can be a list of regular expressions where the first element is the
137 symbol `except'; then VC always stays local except for hosts matched
138 by these regular expressions."
139 :type '(choice (const :tag "Always stay local" t)
140 (const :tag "Only for file operations" only-file)
141 (const :tag "Don't stay local" nil)
142 (list :format "\nExamine hostname and %v"
143 :tag "Examine hostname ..."
144 (set :format "%v" :inline t
145 (const :format "%t" :tag "don't" except))
146 (regexp :format " stay local,\n%t: %v"
147 :tag "if it matches")
148 (repeat :format "%v%i\n" :inline t (regexp :tag "or"))))
149 :version "23.1"
150 :group 'vc-cvs)
151
152 (defcustom vc-cvs-sticky-date-format-string "%c"
153 "Format string for mode-line display of sticky date.
154 Format is according to `format-time-string'. Only used if
155 `vc-cvs-sticky-tag-display' is t."
156 :type '(string)
157 :version "22.1"
158 :group 'vc-cvs)
159
160 (defcustom vc-cvs-sticky-tag-display t
161 "Specify the mode-line display of sticky tags.
162 Value t means default display, nil means no display at all. If the
163 value is a function or macro, it is called with the sticky tag and
164 its type as parameters, in that order. TYPE can have three different
165 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
166 string) and `date' (TAG is a date as returned by `encode-time'). The
167 return value of the function or macro will be displayed as a string.
168
169 Here's an example that will display the formatted date for sticky
170 dates and the word \"Sticky\" for sticky tag names and revisions.
171
172 (lambda (tag type)
173 (cond ((eq type \\='date) (format-time-string
174 vc-cvs-sticky-date-format-string tag))
175 ((eq type \\='revision-number) \"Sticky\")
176 ((eq type \\='symbolic-name) \"Sticky\")))
177
178 Here's an example that will abbreviate to the first character only,
179 any text before the first occurrence of `-' for sticky symbolic tags.
180 If the sticky tag is a revision number, the word \"Sticky\" is
181 displayed. Date and time is displayed for sticky dates.
182
183 (lambda (tag type)
184 (cond ((eq type \\='date) (format-time-string \"%Y%m%d %H:%M\" tag))
185 ((eq type \\='revision-number) \"Sticky\")
186 ((eq type \\='symbolic-name)
187 (condition-case nil
188 (progn
189 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
190 (concat (substring (match-string 1 tag) 0 1) \":\"
191 (substring (match-string 2 tag) 1 nil)))
192 (error tag))))) ; Fall-back to given tag name.
193
194 See also variable `vc-cvs-sticky-date-format-string'."
195 :type '(choice boolean function)
196 :version "22.1"
197 :group 'vc-cvs)
198
199 ;;;
200 ;;; Internal variables
201 ;;;
202
203
204 ;;;
205 ;;; State-querying functions
206 ;;;
207
208 ;;;###autoload(defun vc-cvs-registered (f)
209 ;;;###autoload "Return non-nil if file F is registered with CVS."
210 ;;;###autoload (when (file-readable-p (expand-file-name
211 ;;;###autoload "CVS/Entries" (file-name-directory f)))
212 ;;;###autoload (load "vc-cvs" nil t)
213 ;;;###autoload (vc-cvs-registered f)))
214
215 (defun vc-cvs-registered (file)
216 "Check if FILE is CVS registered."
217 (let ((dirname (or (file-name-directory file) ""))
218 (basename (file-name-nondirectory file))
219 ;; make sure that the file name is searched case-sensitively
220 (case-fold-search nil))
221 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
222 (or (string= basename "")
223 (with-temp-buffer
224 (vc-cvs-get-entries dirname)
225 (goto-char (point-min))
226 (cond ((re-search-forward
227 (concat "^/" (regexp-quote basename) "/[^/]") nil t)
228 (beginning-of-line)
229 (vc-cvs-parse-entry file)
230 t)
231 (t nil))))
232 nil)))
233
234 (defun vc-cvs-state (file)
235 "CVS-specific version of `vc-state'."
236 (if (vc-cvs-stay-local-p file)
237 (let ((state (vc-file-getprop file 'vc-state)))
238 ;; If we should stay local, use the heuristic but only if
239 ;; we don't have a more precise state already available.
240 (if (memq state '(up-to-date edited nil))
241 (vc-cvs-state-heuristic file)
242 state))
243 (with-temp-buffer
244 (cd (file-name-directory file))
245 (let (process-file-side-effects)
246 (vc-cvs-command t 0 file "status"))
247 (vc-cvs-parse-status t))))
248
249 (defun vc-cvs-state-heuristic (file)
250 "CVS-specific state heuristic."
251 ;; If the file has not changed since checkout, consider it `up-to-date'.
252 ;; Otherwise consider it `edited'.
253 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
254 (lastmod (nth 5 (file-attributes file))))
255 (cond
256 ((equal checkout-time lastmod) 'up-to-date)
257 ((string= (vc-working-revision file) "0") 'added)
258 ((null checkout-time) 'unregistered)
259 (t 'edited))))
260
261 (defun vc-cvs-working-revision (file)
262 "CVS-specific version of `vc-working-revision'."
263 ;; There is no need to consult RCS headers under CVS, because we
264 ;; get the workfile version for free when we recognize that a file
265 ;; is registered in CVS.
266 (vc-cvs-registered file)
267 (vc-file-getprop file 'vc-working-revision))
268
269 (defun vc-cvs-mode-line-string (file)
270 "Return a string for `vc-mode-line' to put in the mode line for FILE.
271 Compared to the default implementation, this function does two things:
272 Handle the special case of a CVS file that is added but not yet
273 committed and support display of sticky tags."
274 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
275 help-echo
276 (string
277 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
278 (setq help-echo
279 (get-text-property 0 'help-echo def-ml))
280 def-ml)))
281 (propertize
282 (if (zerop (length sticky-tag))
283 string
284 (setq help-echo (format-message "%s on the `%s' branch"
285 help-echo sticky-tag))
286 (concat string "[" sticky-tag "]"))
287 'help-echo help-echo)))
288
289
290 ;;;
291 ;;; State-changing functions
292 ;;;
293
294 (autoload 'vc-switches "vc")
295
296 (defun vc-cvs-register (files &optional comment)
297 "Register FILES into the CVS version-control system.
298 COMMENT can be used to provide an initial description of FILES.
299 Passes either `vc-cvs-register-switches' or `vc-register-switches'
300 to the CVS command."
301 ;; Register the directories if needed.
302 (let (dirs)
303 (dolist (file files)
304 (and (not (vc-cvs-responsible-p file))
305 (vc-cvs-could-register file)
306 (push (directory-file-name (file-name-directory file)) dirs)))
307 (if dirs (vc-cvs-register dirs)))
308 (apply 'vc-cvs-command nil 0 files
309 "add"
310 (and comment (string-match "[^\t\n ]" comment)
311 (concat "-m" comment))
312 (vc-switches 'CVS 'register)))
313
314 (defun vc-cvs-responsible-p (file)
315 "Return non-nil if CVS thinks it is responsible for FILE."
316 (file-directory-p (expand-file-name "CVS"
317 (if (file-directory-p file)
318 file
319 (file-name-directory file)))))
320
321 (defun vc-cvs-could-register (file)
322 "Return non-nil if FILE could be registered in CVS.
323 This is only possible if CVS is managing FILE's directory or one of
324 its parents."
325 (let ((dir file))
326 (while (and (stringp dir)
327 (not (equal dir (setq dir (file-name-directory dir))))
328 dir)
329 (setq dir (if (file-exists-p
330 (expand-file-name "CVS/Entries" dir))
331 t
332 (directory-file-name dir))))
333 (eq dir t)))
334
335 (defun vc-cvs-checkin (files comment &optional rev)
336 "CVS-specific version of `vc-backend-checkin'."
337 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
338 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
339 (error "%s is not a valid symbolic tag name" rev)
340 ;; If the input revision is a valid symbolic tag name, we create it
341 ;; as a branch, commit and switch to it.
342 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
343 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
344 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
345 files)))
346 (let ((status (apply 'vc-cvs-command nil 1 files
347 "ci" (if rev (concat "-r" rev))
348 (concat "-m" comment)
349 (vc-switches 'CVS 'checkin))))
350 (set-buffer "*vc*")
351 (goto-char (point-min))
352 (when (not (zerop status))
353 ;; Check checkin problem.
354 (cond
355 ((re-search-forward "Up-to-date check failed" nil t)
356 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
357 files)
358 (error "%s" (substitute-command-keys
359 (concat "Up-to-date check failed: "
360 "type \\[vc-next-action] to merge in changes"))))
361 (t
362 (pop-to-buffer (current-buffer))
363 (goto-char (point-min))
364 (shrink-window-if-larger-than-buffer)
365 (error "Check-in failed"))))
366 ;; Single-file commit? Then update the revision by parsing the buffer.
367 ;; Otherwise we can't necessarily tell what goes with what; clear
368 ;; its properties so they have to be refetched.
369 (if (= (length files) 1)
370 (vc-file-setprop
371 (car files) 'vc-working-revision
372 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
373 (mapc 'vc-file-clearprops files))
374 ;; Anyway, forget the checkout model of the file, because we might have
375 ;; guessed wrong when we found the file. After commit, we can
376 ;; tell it from the permissions of the file (see
377 ;; vc-cvs-checkout-model).
378 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
379 files)
380 ;; if this was an explicit check-in (does not include creation of
381 ;; a branch), remove the sticky tag.
382 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
383 (vc-cvs-command nil 0 files "update" "-A"))))
384
385 (defun vc-cvs-find-revision (file rev buffer)
386 (apply 'vc-cvs-command
387 buffer 0 file
388 "-Q" ; suppress diagnostic output
389 "update"
390 (and rev (not (string= rev ""))
391 (concat "-r" rev))
392 "-p"
393 (vc-switches 'CVS 'checkout)))
394
395 (defun vc-cvs-checkout (file &optional rev)
396 "Checkout a revision of FILE into the working area.
397 REV is the revision to check out."
398 (message "Checking out %s..." file)
399 ;; Change buffers to get local value of vc-checkout-switches.
400 (with-current-buffer (or (get-file-buffer file) (current-buffer))
401 (if (and (file-exists-p file) (not rev))
402 ;; If no revision was specified, just make the file writable
403 ;; if necessary (using `cvs-edit' if requested).
404 (and (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
405 (if vc-cvs-use-edit
406 (vc-cvs-command nil 0 file "edit")
407 (set-file-modes file (logior (file-modes file) 128))
408 (if (equal file buffer-file-name) (read-only-mode -1))))
409 ;; Check out a particular revision (or recreate the file).
410 (vc-file-setprop file 'vc-working-revision nil)
411 (apply 'vc-cvs-command nil 0 file
412 "-w"
413 "update"
414 (when rev
415 (unless (eq rev t)
416 ;; default for verbose checkout: clear the
417 ;; sticky tag so that the actual update will
418 ;; get the head of the trunk
419 (if (string= rev "")
420 "-A"
421 (concat "-r" rev))))
422 (vc-switches 'CVS 'checkout)))
423 (vc-mode-line file 'CVS))
424 (message "Checking out %s...done" file))
425
426 (defun vc-cvs-delete-file (file)
427 (vc-cvs-command nil 0 file "remove" "-f"))
428
429 (autoload 'vc-default-revert "vc")
430
431 (defun vc-cvs-revert (file &optional contents-done)
432 "Revert FILE to the working revision on which it was based."
433 (vc-default-revert 'CVS file contents-done)
434 (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
435 (if vc-cvs-use-edit
436 (vc-cvs-command nil 0 file "unedit")
437 ;; Make the file read-only by switching off all w-bits
438 (set-file-modes file (logand (file-modes file) 3950)))))
439
440 (defun vc-cvs-merge-file (file)
441 "Accept a file merge request, prompting for revisions."
442 (let* ((first-revision
443 (vc-read-revision
444 (concat "Merge " file
445 " from branch or revision "
446 "(default news on current branch): ")
447 (list file)
448 'CVS))
449 second-revision
450 status)
451 (cond
452 ((string= first-revision "")
453 (setq status (vc-cvs-merge-news file)))
454 (t
455 (if (not (vc-branch-p first-revision))
456 (setq second-revision
457 (vc-read-revision
458 "Second revision: "
459 (list file) 'CVS nil
460 (concat (vc-branch-part first-revision) ".")))
461 ;; We want to merge an entire branch. Set revisions
462 ;; accordingly, so that vc-cvs-merge understands us.
463 (setq second-revision first-revision)
464 ;; first-revision must be the starting point of the branch
465 (setq first-revision (vc-branch-part first-revision)))
466 (setq status (vc-cvs-merge file first-revision second-revision))))
467 status))
468
469 (defun vc-cvs-merge (file first-revision &optional second-revision)
470 "Merge changes into current working copy of FILE.
471 The changes are between FIRST-REVISION and SECOND-REVISION."
472 (vc-cvs-command nil 0 file
473 "update" "-kk"
474 (concat "-j" first-revision)
475 (concat "-j" second-revision))
476 (vc-file-setprop file 'vc-state 'edited)
477 (with-current-buffer (get-buffer "*vc*")
478 (goto-char (point-min))
479 (if (re-search-forward "conflicts during merge" nil t)
480 (progn
481 (vc-file-setprop file 'vc-state 'conflict)
482 ;; signal error
483 1)
484 (vc-file-setprop file 'vc-state 'edited)
485 ;; signal success
486 0)))
487
488 (defun vc-cvs-merge-news (file)
489 "Merge in any new changes made to FILE."
490 (message "Merging changes into %s..." file)
491 ;; (vc-file-setprop file 'vc-working-revision nil)
492 (vc-file-setprop file 'vc-checkout-time 0)
493 (vc-cvs-command nil nil file "update")
494 ;; Analyze the merge result reported by CVS, and set
495 ;; file properties accordingly.
496 (with-current-buffer (get-buffer "*vc*")
497 (goto-char (point-min))
498 ;; get new working revision
499 (if (re-search-forward
500 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
501 (vc-file-setprop file 'vc-working-revision (match-string 1))
502 (vc-file-setprop file 'vc-working-revision nil))
503 ;; get file status
504 (prog1
505 (if (eq (buffer-size) 0)
506 0 ;; there were no news; indicate success
507 (if (re-search-forward
508 (concat "^\\([CMUP] \\)?"
509 (regexp-quote
510 (substring file (1+ (length (expand-file-name
511 "." default-directory)))))
512 "\\( already contains the differences between \\)?")
513 nil t)
514 (cond
515 ;; Merge successful, we are in sync with repository now
516 ((or (match-string 2)
517 (string= (match-string 1) "U ")
518 (string= (match-string 1) "P "))
519 (vc-file-setprop file 'vc-state 'up-to-date)
520 (vc-file-setprop file 'vc-checkout-time
521 (nth 5 (file-attributes file)))
522 0);; indicate success to the caller
523 ;; Merge successful, but our own changes are still in the file
524 ((string= (match-string 1) "M ")
525 (vc-file-setprop file 'vc-state 'edited)
526 0);; indicate success to the caller
527 ;; Conflicts detected!
528 (t
529 (vc-file-setprop file 'vc-state 'conflict)
530 1);; signal the error to the caller
531 )
532 (pop-to-buffer "*vc*")
533 (error "Couldn't analyze cvs update result")))
534 (message "Merging changes into %s...done" file))))
535
536 (defun vc-cvs-modify-change-comment (files rev comment)
537 "Modify the change comments for FILES on a specified REV.
538 Will fail unless you have administrative privileges on the repo."
539 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
540
541 ;;;
542 ;;; History functions
543 ;;;
544
545 (declare-function vc-rcs-print-log-cleanup "vc-rcs" ())
546 ;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
547 (declare-function vc-exec-after "vc-dispatcher" (code))
548
549 (defun vc-cvs-print-log (files buffer &optional _shortlog _start-revision limit)
550 "Print commit log associated with FILES into specified BUFFER.
551 Remaining arguments are ignored."
552 (require 'vc-rcs)
553 ;; It's just the catenation of the individual logs.
554 (vc-cvs-command
555 buffer
556 (if (vc-cvs-stay-local-p files) 'async 0)
557 files "log")
558 (with-current-buffer buffer
559 (vc-run-delayed (vc-rcs-print-log-cleanup)))
560 (when limit 'limit-unsupported))
561
562 (defun vc-cvs-comment-history (file)
563 "Get comment history of a file."
564 (vc-call-backend 'RCS 'comment-history file))
565
566 (autoload 'vc-version-backup-file "vc")
567 (declare-function vc-coding-system-for-diff "vc" (file))
568
569 (defun vc-cvs-diff (files &optional oldvers newvers buffer async)
570 "Get a difference report using CVS between two revisions of FILE."
571 (let* (process-file-side-effects
572 (async (and async (vc-cvs-stay-local-p files)))
573 (invoke-cvs-diff-list nil)
574 status)
575 ;; Look through the file list and see if any files have backups
576 ;; that can be used to do a plain "diff" instead of "cvs diff".
577 (dolist (file files)
578 (let ((ov oldvers)
579 (nv newvers))
580 (when (or (not ov) (string-equal ov ""))
581 (setq ov (vc-working-revision file)))
582 (when (string-equal nv "")
583 (setq nv nil))
584 (let ((file-oldvers (vc-version-backup-file file ov))
585 (file-newvers (if (not nv)
586 file
587 (vc-version-backup-file file nv)))
588 (coding-system-for-read (vc-coding-system-for-diff file)))
589 (if (and file-oldvers file-newvers)
590 (progn
591 ;; This used to append diff-switches and vc-diff-switches,
592 ;; which was consistent with the vc-diff-switches doc at that
593 ;; time, but not with the actual behavior of any other VC diff.
594 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
595 ;; Not a CVS diff, does not use vc-cvs-diff-switches.
596 (append (vc-switches nil 'diff)
597 (list (file-relative-name file-oldvers)
598 (file-relative-name file-newvers))))
599 (setq status 0))
600 (push file invoke-cvs-diff-list)))))
601 (when invoke-cvs-diff-list
602 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
603 (if async 'async 1)
604 invoke-cvs-diff-list "diff"
605 (and oldvers (concat "-r" oldvers))
606 (and newvers (concat "-r" newvers))
607 (vc-switches 'CVS 'diff))))
608 (if async 1 status))) ; async diff, pessimistic assumption
609
610 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
611
612 (defun vc-cvs-annotate-process-filter (filter process string)
613 (setq string (concat (process-get process 'output) string))
614 (if (not (string-match vc-cvs-annotate-first-line-re string))
615 ;; Still waiting for the first real line.
616 (process-put process 'output string)
617 (remove-function (process-filter process) #'vc-cvs-annotate-process-filter)
618 (funcall filter process (substring string (match-beginning 0)))))
619
620 (defun vc-cvs-annotate-command (file buffer &optional revision)
621 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
622 Optional arg REVISION is a revision to annotate from."
623 (apply #'vc-cvs-command buffer
624 (if (vc-cvs-stay-local-p file)
625 'async 0)
626 file "annotate"
627 (append (vc-switches 'cvs 'annotate)
628 (if revision (list (concat "-r" revision)))))
629 ;; Strip the leading few lines.
630 (let ((proc (get-buffer-process buffer)))
631 (if proc
632 ;; If running asynchronously, use a process filter.
633 (add-function :around (process-filter proc)
634 #'vc-cvs-annotate-process-filter)
635 (with-current-buffer buffer
636 (goto-char (point-min))
637 (re-search-forward vc-cvs-annotate-first-line-re)
638 (delete-region (point-min) (1- (point)))))))
639
640 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
641
642 (defun vc-cvs-annotate-current-time ()
643 "Return the current time, based at midnight of the current day, and
644 encoded as fractional days."
645 (vc-annotate-convert-time
646 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time)))))
647
648 (defun vc-cvs-annotate-time ()
649 "Return the time of the next annotation (as fraction of days)
650 systime, or nil if there is none."
651 (let* ((bol (point))
652 (cache (get-text-property bol 'vc-cvs-annotate-time))
653 (inhibit-read-only t)
654 (inhibit-modification-hooks t))
655 (cond
656 (cache)
657 ((looking-at
658 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
659 (let ((day (string-to-number (match-string 1)))
660 (month (cdr (assq (intern (match-string 2))
661 '((Jan . 1) (Feb . 2) (Mar . 3)
662 (Apr . 4) (May . 5) (Jun . 6)
663 (Jul . 7) (Aug . 8) (Sep . 9)
664 (Oct . 10) (Nov . 11) (Dec . 12)))))
665 (year (let ((tmp (string-to-number (match-string 3))))
666 ;; Years 0..68 are 2000..2068.
667 ;; Years 69..99 are 1969..1999.
668 (+ (cond ((> 69 tmp) 2000)
669 ((> 100 tmp) 1900)
670 (t 0))
671 tmp))))
672 (put-text-property
673 bol (1+ bol) 'vc-cvs-annotate-time
674 (setq cache (cons
675 ;; Position at end makes for nicer overlay result.
676 ;; Don't put actual buffer pos here, but only relative
677 ;; distance, so we don't ever move backward in the
678 ;; goto-char below, even if the text is moved.
679 (- (match-end 0) (match-beginning 0))
680 (vc-annotate-convert-time
681 (encode-time 0 0 0 day month year))))))))
682 (when cache
683 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
684 (cdr cache)))) ; days (float)
685
686 (defun vc-cvs-annotate-extract-revision-at-line ()
687 (save-excursion
688 (beginning-of-line)
689 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
690 (line-end-position) t)
691 (match-string-no-properties 1)
692 nil)))
693
694 (defun vc-cvs-previous-revision (file rev)
695 (vc-call-backend 'RCS 'previous-revision file rev))
696
697 (defun vc-cvs-next-revision (file rev)
698 (vc-call-backend 'RCS 'next-revision file rev))
699
700 ;; FIXME: This should probably be replaced by code using cvs2cl.
701 (defun vc-cvs-update-changelog (files)
702 (vc-call-backend 'RCS 'update-changelog files))
703
704 ;;;
705 ;;; Tag system
706 ;;;
707
708 (defun vc-cvs-create-tag (dir name branchp)
709 "Assign to DIR's current revision a given NAME.
710 If BRANCHP is non-nil, the name is created as a branch (and the current
711 workspace is immediately moved to that new branch)."
712 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
713 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
714
715 ;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
716 (declare-function vc-resynch-buffer "vc-dispatcher"
717 (file &optional keep noquery reset-vc-info))
718
719 (defun vc-cvs-retrieve-tag (dir name update)
720 "Retrieve a tag at and below DIR.
721 NAME is the name of the tag; if it is empty, do a `cvs update'.
722 If UPDATE is non-nil, then update (resynch) any affected buffers."
723 (with-current-buffer (get-buffer-create "*vc*")
724 (let ((default-directory dir)
725 (sticky-tag))
726 (erase-buffer)
727 (if (or (not name) (string= name ""))
728 (vc-cvs-command t 0 nil "update")
729 (vc-cvs-command t 0 nil "update" "-r" name)
730 (setq sticky-tag name))
731 (when update
732 (goto-char (point-min))
733 (while (not (eobp))
734 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
735 (let* ((file (expand-file-name (match-string 2) dir))
736 (state (match-string 1))
737 (buffer (find-buffer-visiting file)))
738 (when buffer
739 (cond
740 ((or (string= state "U")
741 (string= state "P"))
742 (vc-file-setprop file 'vc-state 'up-to-date)
743 (vc-file-setprop file 'vc-working-revision nil)
744 (vc-file-setprop file 'vc-checkout-time
745 (nth 5 (file-attributes file))))
746 ((or (string= state "M")
747 (string= state "C"))
748 (vc-file-setprop file 'vc-state 'edited)
749 (vc-file-setprop file 'vc-working-revision nil)
750 (vc-file-setprop file 'vc-checkout-time 0)))
751 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
752 (vc-resynch-buffer file t t))))
753 (forward-line 1))))))
754
755
756 ;;;
757 ;;; Miscellaneous
758 ;;;
759
760 (defun vc-cvs-make-version-backups-p (file)
761 "Return non-nil if version backups should be made for FILE."
762 (vc-cvs-stay-local-p file))
763
764 (defun vc-cvs-check-headers ()
765 "Check if the current file has any headers in it."
766 (save-excursion
767 (goto-char (point-min))
768 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
769 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
770
771
772 ;;;
773 ;;; Internal functions
774 ;;;
775
776 (defun vc-cvs-command (buffer okstatus files &rest flags)
777 "A wrapper around `vc-do-command' for use in vc-cvs.el.
778 The difference to vc-do-command is that this function always invokes `cvs',
779 and that it passes `vc-cvs-global-switches' to it before FLAGS."
780 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
781 (if (stringp vc-cvs-global-switches)
782 (cons vc-cvs-global-switches flags)
783 (append vc-cvs-global-switches
784 flags))))
785
786 (defun vc-cvs-stay-local-p (file)
787 "Return non-nil if VC should stay local when handling FILE.
788 If FILE is a list of files, return non-nil if any of them
789 individually should stay local."
790 (if (listp file)
791 (delq nil (mapcar (lambda (arg) (vc-cvs-stay-local-p arg)) file))
792 (let* ((sym (vc-make-backend-sym 'CVS 'stay-local))
793 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
794 (if (symbolp stay-local) stay-local
795 (let ((dirname (if (file-directory-p file)
796 (directory-file-name file)
797 (file-name-directory file))))
798 (eq 'yes
799 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
800 (vc-file-setprop
801 dirname 'vc-cvs-stay-local-p
802 (let ((hostname (vc-cvs-repository-hostname dirname)))
803 (if (not hostname)
804 'no
805 (let ((default t))
806 (if (eq (car-safe stay-local) 'except)
807 (setq default nil stay-local (cdr stay-local)))
808 (when (consp stay-local)
809 (setq stay-local
810 (mapconcat 'identity stay-local "\\|")))
811 (if (if (string-match stay-local hostname)
812 default (not default))
813 'yes 'no))))))))))))
814
815 (defun vc-cvs-repository-hostname (dirname)
816 "Hostname of the CVS server associated to workarea DIRNAME."
817 (let ((rootname (expand-file-name "CVS/Root" dirname)))
818 (when (file-readable-p rootname)
819 (with-temp-buffer
820 (let ((coding-system-for-read
821 (or file-name-coding-system
822 default-file-name-coding-system)))
823 (vc-insert-file rootname))
824 (goto-char (point-min))
825 (nth 2 (vc-cvs-parse-root
826 (buffer-substring (point)
827 (line-end-position))))))))
828
829 (defun vc-cvs-parse-uhp (path)
830 "parse user@host/path into (user@host /path)"
831 (if (string-match "\\([^/]+\\)\\(/.*\\)" path)
832 (list (match-string 1 path) (match-string 2 path))
833 (list nil path)))
834
835 (defun vc-cvs-parse-root (root)
836 "Split CVS ROOT specification string into a list of fields.
837 A CVS root specification of the form
838 [:METHOD:][[USER@]HOSTNAME]:?/path/to/repository
839 is converted to a normalized record with the following structure:
840 \(METHOD USER HOSTNAME CVS-ROOT).
841 The default METHOD for a CVS root of the form
842 /path/to/repository
843 is `local'.
844 The default METHOD for a CVS root of the form
845 [USER@]HOSTNAME:/path/to/repository
846 is `ext'.
847 For an empty string, nil is returned (invalid CVS root)."
848 ;; Split CVS root into colon separated fields (0-4).
849 ;; The `x:' makes sure, that leading colons are not lost;
850 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
851 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
852 (len (length root-list))
853 ;; All syntactic varieties will get a proper METHOD.
854 (root-list
855 (cond
856 ((= len 0)
857 ;; Invalid CVS root
858 nil)
859 ((= len 1)
860 (let ((uhp (vc-cvs-parse-uhp (car root-list))))
861 (cons (if (car uhp) "ext" "local") uhp)))
862 ((= len 2)
863 ;; [USER@]HOST:PATH => method `ext'
864 (and (not (equal (car root-list) ""))
865 (cons "ext" root-list)))
866 ((= len 3)
867 ;; :METHOD:PATH or :METHOD:USER@HOSTNAME/PATH
868 (cons (cadr root-list)
869 (vc-cvs-parse-uhp (nth 2 root-list))))
870 (t
871 ;; :METHOD:[USER@]HOST:PATH
872 (cdr root-list)))))
873 (if root-list
874 (let ((method (car root-list))
875 (uhost (or (cadr root-list) ""))
876 (root (nth 2 root-list))
877 user host)
878 ;; Split USER@HOST
879 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
880 (setq user (match-string 1 uhost)
881 host (match-string 2 uhost))
882 (setq host uhost))
883 ;; Remove empty HOST
884 (and (equal host "")
885 (setq host nil))
886 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
887 (and host
888 (equal method "local")
889 (setq root (concat host ":" root) host nil))
890 ;; Normalize CVS root record
891 (list method user host root)))))
892
893 ;; XXX: This does not work correctly for subdirectories. "cvs status"
894 ;; information is context sensitive, it contains lines like:
895 ;; cvs status: Examining DIRNAME
896 ;; and the file entries after that don't show the full path.
897 ;; Because of this VC directory listings only show changed files
898 ;; at the top level for CVS.
899 (defun vc-cvs-parse-status (&optional full)
900 "Parse output of \"cvs status\" command in the current buffer.
901 Set file properties accordingly. Unless FULL is t, parse only
902 essential information. Note that this can never set the `ignored'
903 state."
904 (let (file status missing)
905 (goto-char (point-min))
906 (while (looking-at "? \\(.*\\)")
907 (setq file (expand-file-name (match-string 1)))
908 (vc-file-setprop file 'vc-state 'unregistered)
909 (forward-line 1))
910 (when (re-search-forward "^File: " nil t)
911 (when (setq missing (looking-at "no file "))
912 (goto-char (match-end 0)))
913 (cond
914 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
915 (setq file (expand-file-name (match-string 1)))
916 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
917 (match-string 1) "Unknown"))
918 (when (and full
919 (re-search-forward
920 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
921 [\t ]+\\([0-9.]+\\)"
922 nil t))
923 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
924 (vc-file-setprop
925 file 'vc-state
926 (cond
927 ((string-match "Up-to-date" status)
928 (vc-file-setprop file 'vc-checkout-time
929 (nth 5 (file-attributes file)))
930 'up-to-date)
931 ((string-match "Locally Modified" status) 'edited)
932 ((string-match "Needs Merge" status) 'needs-merge)
933 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
934 (if missing 'missing 'needs-update))
935 ((string-match "Locally Added" status) 'added)
936 ((string-match "Locally Removed" status) 'removed)
937 ((string-match "File had conflicts " status) 'conflict)
938 ((string-match "Unknown" status) 'unregistered)
939 (t 'edited))))))))
940
941 (defun vc-cvs-after-dir-status (update-function)
942 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
943 ;; This needs a lot of testing.
944 (let ((status nil)
945 (status-str nil)
946 (file nil)
947 (result nil)
948 (missing nil)
949 (ignore-next nil)
950 (subdir default-directory))
951 (goto-char (point-min))
952 (while
953 ;; Look for either a file entry, an unregistered file, or a
954 ;; directory change.
955 (re-search-forward
956 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: \\(Examining\\|nothing\\) .*\n\\)"
957 nil t)
958 ;; FIXME: get rid of narrowing here.
959 (narrow-to-region (match-beginning 0) (match-end 0))
960 (goto-char (point-min))
961 ;; The subdir
962 (when (looking-at "cvs status: Examining \\(.+\\)")
963 (setq subdir (expand-file-name (match-string 1))))
964 ;; Unregistered files
965 (while (looking-at "? \\(.*\\)")
966 (setq file (file-relative-name
967 (expand-file-name (match-string 1) subdir)))
968 (push (list file 'unregistered) result)
969 (forward-line 1))
970 (when (looking-at "cvs status: nothing known about")
971 ;; We asked about a non existent file. The output looks like this:
972
973 ;; cvs status: nothing known about `lisp/v.diff'
974 ;; ===================================================================
975 ;; File: no file v.diff Status: Unknown
976 ;;
977 ;; Working revision: No entry for v.diff
978 ;; Repository revision: No revision control file
979 ;;
980
981 ;; Due to narrowing in this iteration we only see the "cvs
982 ;; status:" line, so just set a flag so that we can ignore the
983 ;; file in the next iteration.
984 (setq ignore-next t))
985 ;; A file entry.
986 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
987 (setq missing (match-string 1))
988 (setq file (file-relative-name
989 (expand-file-name (match-string 2) subdir)))
990 (setq status-str (match-string 3))
991 (setq status
992 (cond
993 ((string-match "Up-to-date" status-str) 'up-to-date)
994 ((string-match "Locally Modified" status-str) 'edited)
995 ((string-match "Needs Merge" status-str) 'needs-merge)
996 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
997 (if missing 'missing 'needs-update))
998 ((string-match "Locally Added" status-str) 'added)
999 ((string-match "Locally Removed" status-str) 'removed)
1000 ((string-match "File had conflicts " status-str) 'conflict)
1001 ((string-match "Unknown" status-str) 'unregistered)
1002 (t 'edited)))
1003 (if ignore-next
1004 (setq ignore-next nil)
1005 (unless (eq status 'up-to-date)
1006 (push (list file status) result))))
1007 (goto-char (point-max))
1008 (widen))
1009 (funcall update-function result))
1010 ;; Alternative implementation: use the "update" command instead of
1011 ;; the "status" command.
1012 ;; (let ((result nil)
1013 ;; (translation '((?? . unregistered)
1014 ;; (?A . added)
1015 ;; (?C . conflict)
1016 ;; (?M . edited)
1017 ;; (?P . needs-merge)
1018 ;; (?R . removed)
1019 ;; (?U . needs-update))))
1020 ;; (goto-char (point-min))
1021 ;; (while (not (eobp))
1022 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
1023 ;; (push (list (match-string 1)
1024 ;; (cdr (assoc (char-after) translation)))
1025 ;; result)
1026 ;; (cond
1027 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
1028 ;; ;; Format is:
1029 ;; ;; cvs update: warning: FILENAME was lost
1030 ;; ;; U FILENAME
1031 ;; (push (list (match-string 1) 'missing) result)
1032 ;; ;; Skip the "U" line
1033 ;; (forward-line 1))
1034 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
1035 ;; (push (list (match-string 1) 'unregistered) result))))
1036 ;; (forward-line 1))
1037 ;; (funcall update-function result)))
1038 )
1039
1040 ;; Based on vc-cvs-dir-state-heuristic from Emacs 22.
1041 ;; FIXME does not mention unregistered files.
1042 (defun vc-cvs-dir-status-heuristic (dir update-function &optional basedir)
1043 "Find the CVS state of all files in DIR, using only local information."
1044 (let (file basename status result dirlist)
1045 (with-temp-buffer
1046 (vc-cvs-get-entries dir)
1047 (goto-char (point-min))
1048 (while (not (eobp))
1049 (if (looking-at "D/\\([^/]*\\)////")
1050 (push (expand-file-name (match-string 1) dir) dirlist)
1051 ;; CVS-removed files are not taken under VC control.
1052 (when (looking-at "/\\([^/]*\\)/[^/-]")
1053 (setq basename (match-string 1)
1054 file (expand-file-name basename dir)
1055 status (or (vc-file-getprop file 'vc-state)
1056 (vc-cvs-parse-entry file t)))
1057 (unless (eq status 'up-to-date)
1058 (push (list (if basedir
1059 (file-relative-name file basedir)
1060 basename)
1061 status) result))))
1062 (forward-line 1)))
1063 (dolist (subdir dirlist)
1064 (setq result (append result
1065 (vc-cvs-dir-status-heuristic subdir nil
1066 (or basedir dir)))))
1067 (if basedir result
1068 (funcall update-function result))))
1069
1070 (defun vc-cvs-dir-status-files (dir files update-function)
1071 "Create a list of conses (file . state) for FILES in DIR.
1072 Query all files in DIR if files is nil."
1073 (let ((local (vc-cvs-stay-local-p dir)))
1074 (if (and (not files) local (not (eq local 'only-file)))
1075 (vc-cvs-dir-status-heuristic dir update-function)
1076 (if (not files) (setq files (vc-expand-dirs (list dir) 'CVS)))
1077 (vc-cvs-command (current-buffer) 'async files "-f" "status")
1078 ;; Alternative implementation: use the "update" command instead of
1079 ;; the "status" command.
1080 ;; (vc-cvs-command (current-buffer) 'async
1081 ;; (file-relative-name dir)
1082 ;; "-f" "-n" "update" "-d" "-P")
1083 (vc-run-delayed
1084 (vc-cvs-after-dir-status update-function)))))
1085
1086 (defun vc-cvs-file-to-string (file)
1087 "Read the content of FILE and return it as a string."
1088 (condition-case nil
1089 (with-temp-buffer
1090 (insert-file-contents file)
1091 (goto-char (point-min))
1092 (buffer-substring (point) (point-max)))
1093 (file-error nil)))
1094
1095 (defun vc-cvs-dir-extra-headers (_dir)
1096 "Extract and represent per-directory properties of a CVS working copy."
1097 (let ((repo
1098 (condition-case nil
1099 (with-temp-buffer
1100 (insert-file-contents "CVS/Root")
1101 (goto-char (point-min))
1102 (and (looking-at ":ext:") (delete-char 5))
1103 (concat (buffer-substring (point) (1- (point-max))) "\n"))
1104 (file-error nil)))
1105 (module
1106 (condition-case nil
1107 (with-temp-buffer
1108 (insert-file-contents "CVS/Repository")
1109 (goto-char (point-min))
1110 (skip-chars-forward "^\n")
1111 (concat (buffer-substring (point-min) (point)) "\n"))
1112 (file-error nil))))
1113 (concat
1114 (cond (repo
1115 (concat (propertize "Repository : " 'face 'font-lock-type-face)
1116 (propertize repo 'face 'font-lock-variable-name-face)))
1117 (t ""))
1118 (cond (module
1119 (concat (propertize "Module : " 'face 'font-lock-type-face)
1120 (propertize module 'face 'font-lock-variable-name-face)))
1121 (t ""))
1122 (if (file-readable-p "CVS/Tag")
1123 (let ((tag (vc-cvs-file-to-string "CVS/Tag")))
1124 (cond
1125 ((string-match "\\`T" tag)
1126 (concat (propertize "Tag : " 'face 'font-lock-type-face)
1127 (propertize (substring tag 1)
1128 'face 'font-lock-variable-name-face)))
1129 ((string-match "\\`D" tag)
1130 (concat (propertize "Date : " 'face 'font-lock-type-face)
1131 (propertize (substring tag 1)
1132 'face 'font-lock-variable-name-face)))
1133 (t ""))))
1134
1135 ;; In CVS, branch is a per-file property, not a per-directory property.
1136 ;; We can't really do this here without making dangerous assumptions.
1137 ;;(propertize "Branch: " 'face 'font-lock-type-face)
1138 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
1139 ;; 'face 'font-lock-warning-face)
1140 )))
1141
1142 (defun vc-cvs-get-entries (dir)
1143 "Insert the CVS/Entries file from below DIR into the current buffer.
1144 This function ensures that the correct coding system is used for that,
1145 which may not be the one that is used for the files' contents.
1146 CVS/Entries should only be accessed through this function."
1147 (let ((coding-system-for-read (or file-name-coding-system
1148 default-file-name-coding-system)))
1149 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
1150
1151 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
1152 "Return non-nil if TAG is a valid symbolic tag name."
1153 ;; According to the CVS manual, a valid symbolic tag must start with
1154 ;; an uppercase or lowercase letter and can contain uppercase and
1155 ;; lowercase letters, digits, `-', and `_'.
1156 (and (string-match "^[a-zA-Z]" tag)
1157 (not (string-match "[^a-z0-9A-Z-_]" tag))))
1158
1159 (defun vc-cvs-valid-revision-number-p (tag)
1160 "Return non-nil if TAG is a valid revision number."
1161 (and (string-match "^[0-9]" tag)
1162 (not (string-match "[^0-9.]" tag))))
1163
1164 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
1165 "Parse and return the sticky tag as a string.
1166 `match-data' is protected."
1167 (let ((data (match-data))
1168 (tag)
1169 (type (cond ((string= match-type "D") 'date)
1170 ((string= match-type "T")
1171 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
1172 'symbolic-name
1173 'revision-number))
1174 (t nil))))
1175 (unwind-protect
1176 (progn
1177 (cond
1178 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1179 ((eq type 'date)
1180 (string-match
1181 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1182 match-tag)
1183 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1184 (month (string-to-number (match-string 2 match-tag)))
1185 (day (string-to-number (match-string 3 match-tag)))
1186 (hour (string-to-number (match-string 4 match-tag)))
1187 (min (string-to-number (match-string 5 match-tag)))
1188 (sec (string-to-number (match-string 6 match-tag)))
1189 ;; Years 0..68 are 2000..2068.
1190 ;; Years 69..99 are 1969..1999.
1191 (year (+ (cond ((> 69 year-tmp) 2000)
1192 ((> 100 year-tmp) 1900)
1193 (t 0))
1194 year-tmp)))
1195 (setq tag (encode-time sec min hour day month year))))
1196 ;; Sticky Tag name or revision number
1197 ((eq type 'symbolic-name) (setq tag match-tag))
1198 ((eq type 'revision-number) (setq tag match-tag))
1199 ;; Default is no sticky tag at all
1200 (t nil))
1201 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1202 ((eq vc-cvs-sticky-tag-display t)
1203 (cond ((eq type 'date) (format-time-string
1204 vc-cvs-sticky-date-format-string
1205 tag))
1206 ((eq type 'symbolic-name) tag)
1207 ((eq type 'revision-number) tag)
1208 (t nil)))
1209 ((functionp vc-cvs-sticky-tag-display)
1210 (funcall vc-cvs-sticky-tag-display tag type))
1211 (t nil)))
1212
1213 (set-match-data data))))
1214
1215 (defun vc-cvs-parse-entry (file &optional set-state)
1216 "Parse a line from CVS/Entries.
1217 Compare modification time to that of the FILE, set file properties
1218 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1219 is non-nil."
1220 (cond
1221 ;; entry for a "locally added" file (not yet committed)
1222 ((looking-at "/[^/]+/0/")
1223 (vc-file-setprop file 'vc-checkout-time 0)
1224 (vc-file-setprop file 'vc-working-revision "0")
1225 (if set-state (vc-file-setprop file 'vc-state 'added)))
1226 ;; normal entry
1227 ((looking-at
1228 (concat "/[^/]+"
1229 ;; revision
1230 "/\\([^/]*\\)"
1231 ;; timestamp and optional conflict field
1232 "/\\([^/]*\\)/"
1233 ;; options
1234 "\\([^/]*\\)/"
1235 ;; sticky tag
1236 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1237 "\\(.*\\)")) ;Sticky tag
1238 (vc-file-setprop file 'vc-working-revision (match-string 1))
1239 (vc-file-setprop file 'vc-cvs-sticky-tag
1240 (vc-cvs-parse-sticky-tag (match-string 4)
1241 (match-string 5)))
1242 ;; Compare checkout time and modification time.
1243 ;; This is intentionally different from the algorithm that CVS uses
1244 ;; (which is based on textual comparison), because there can be problems
1245 ;; generating a time string that looks exactly like the one from CVS.
1246 (let* ((time (match-string 2))
1247 (mtime (nth 5 (file-attributes file)))
1248 (parsed-time (progn (require 'parse-time)
1249 (parse-time-string (concat time " +0000")))))
1250 (cond ((and (not (string-match "\\+" time))
1251 (car parsed-time)
1252 ;; Compare just the seconds part of the file time,
1253 ;; since CVS file time stamp resolution is just 1 second.
1254 (let ((ptime (apply 'encode-time parsed-time)))
1255 (and (eq (car mtime) (car ptime))
1256 (eq (cadr mtime) (cadr ptime)))))
1257 (vc-file-setprop file 'vc-checkout-time mtime)
1258 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1259 (t
1260 (vc-file-setprop file 'vc-checkout-time 0)
1261 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
1262
1263 ;; Completion of revision names.
1264 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1265 ;; `cvs log' so I can list all the revision numbers rather than only
1266 ;; tag names.
1267
1268 (defun vc-cvs-revision-table (file)
1269 (let (process-file-side-effects
1270 (default-directory (file-name-directory file))
1271 (res nil))
1272 (with-temp-buffer
1273 (vc-cvs-command t nil file "log")
1274 (goto-char (point-min))
1275 (when (re-search-forward "^symbolic names:\n" nil t)
1276 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1277 (push (cons (match-string 1) (match-string 2)) res)
1278 (forward-line 1)))
1279 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1280 (push (match-string 1) res))
1281 res)))
1282
1283 (defun vc-cvs-revision-completion-table (files)
1284 (letrec ((table (lazy-completion-table
1285 table (lambda () (vc-cvs-revision-table (car files))))))
1286 table))
1287
1288 (defun vc-cvs-find-admin-dir (file)
1289 "Return the administrative directory of FILE."
1290 (vc-find-root file "CVS"))
1291
1292 (defun vc-cvs-ignore (file &optional _directory _remove)
1293 "Ignore FILE under CVS."
1294 (vc-cvs-append-to-ignore (file-name-directory file) file))
1295
1296 (defun vc-cvs-append-to-ignore (dir str &optional old-dir)
1297 "In DIR, add STR to the .cvsignore file.
1298 If OLD-DIR is non-nil, then this is a directory that we don't want
1299 to hear about anymore."
1300 (with-current-buffer
1301 (find-file-noselect (expand-file-name ".cvsignore" dir))
1302 (when (ignore-errors
1303 (and buffer-read-only
1304 (eq 'CVS (vc-backend buffer-file-name))
1305 (not (vc-editable-p buffer-file-name))))
1306 ;; CVSREAD=on special case
1307 (vc-checkout buffer-file-name t))
1308 (goto-char (point-max))
1309 (unless (bolp) (insert "\n"))
1310 (insert str (if old-dir "/\n" "\n"))
1311 ;; FIXME this is a pcvs variable.
1312 (if (bound-and-true-p cvs-sort-ignore-file)
1313 (sort-lines nil (point-min) (point-max)))
1314 (save-buffer)))
1315
1316 (provide 'vc-cvs)
1317
1318 ;;; vc-cvs.el ends here