]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-cvs.el
Merge branch 'map'
[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 "%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 ;; vc-cvs-checkin used to take a 'rev' second argument that allowed
336 ;; checking in onto a specified branch tip rather than the current
337 ;; default branch, but nothing in the entire rest of VC exercised
338 ;; this code. Removing it simplifies the backend interface for all
339 ;; modes.
340 ;;
341 ;; Here's the setup code preserved in amber, in case the logic needs
342 ;; to be broken out into a method someday; (if rev (concat "-r" rev))
343 ;; used to be part of the switches passed to vc-cvs-command.
344 ;;
345 ;; (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
346 ;; (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
347 ;; (error "%s is not a valid symbolic tag name" rev)
348 ;; ;; If the input revision is a valid symbolic tag name, we create it
349 ;; ;; as a branch, commit and switch to it.
350 ;; (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
351 ;; (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
352 ;; (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
353 ;; files)))
354 ;;
355 ;; The following postamble cleaned up after the branch change:
356 ;;
357 ;; ;; if this was an explicit check-in (does not include creation of
358 ;; ;; a branch), remove the sticky tag.
359 ;; (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
360 ;; (vc-cvs-command nil 0 files "update" "-A"))))
361 ;; files)))
362 ;;
363 (defun vc-cvs-checkin (files comment)
364 "CVS-specific version of `vc-backend-checkin'."
365 (let ((status (apply 'vc-cvs-command nil 1 files
366 "ci" (concat "-m" comment)
367 (vc-switches 'CVS 'checkin))))
368 (set-buffer "*vc*")
369 (goto-char (point-min))
370 (when (not (zerop status))
371 ;; Check checkin problem.
372 (cond
373 ((re-search-forward "Up-to-date check failed" nil t)
374 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
375 files)
376 (error "%s" (substitute-command-keys
377 (concat "Up-to-date check failed: "
378 "type \\[vc-next-action] to merge in changes"))))
379 (t
380 (pop-to-buffer (current-buffer))
381 (goto-char (point-min))
382 (shrink-window-if-larger-than-buffer)
383 (error "Check-in failed"))))
384 ;; Single-file commit? Then update the revision by parsing the buffer.
385 ;; Otherwise we can't necessarily tell what goes with what; clear
386 ;; its properties so they have to be refetched.
387 (if (= (length files) 1)
388 (vc-file-setprop
389 (car files) 'vc-working-revision
390 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
391 (mapc 'vc-file-clearprops files))
392 ;; Anyway, forget the checkout model of the file, because we might have
393 ;; guessed wrong when we found the file. After commit, we can
394 ;; tell it from the permissions of the file (see
395 ;; vc-cvs-checkout-model).
396 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
397 files)))
398
399 (defun vc-cvs-find-revision (file rev buffer)
400 (apply 'vc-cvs-command
401 buffer 0 file
402 "-Q" ; suppress diagnostic output
403 "update"
404 (and rev (not (string= rev ""))
405 (concat "-r" rev))
406 "-p"
407 (vc-switches 'CVS 'checkout)))
408
409 (defun vc-cvs-checkout (file &optional rev)
410 "Checkout a revision of FILE into the working area.
411 REV is the revision to check out."
412 (message "Checking out %s..." file)
413 ;; Change buffers to get local value of vc-checkout-switches.
414 (with-current-buffer (or (get-file-buffer file) (current-buffer))
415 (if (and (file-exists-p file) (not rev))
416 ;; If no revision was specified, just make the file writable
417 ;; if necessary (using `cvs-edit' if requested).
418 (and (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
419 (if vc-cvs-use-edit
420 (vc-cvs-command nil 0 file "edit")
421 (set-file-modes file (logior (file-modes file) 128))
422 (if (equal file buffer-file-name) (read-only-mode -1))))
423 ;; Check out a particular revision (or recreate the file).
424 (vc-file-setprop file 'vc-working-revision nil)
425 (apply 'vc-cvs-command nil 0 file
426 "-w"
427 "update"
428 (when rev
429 (unless (eq rev t)
430 ;; default for verbose checkout: clear the
431 ;; sticky tag so that the actual update will
432 ;; get the head of the trunk
433 (if (string= rev "")
434 "-A"
435 (concat "-r" rev))))
436 (vc-switches 'CVS 'checkout)))
437 (vc-mode-line file 'CVS))
438 (message "Checking out %s...done" file))
439
440 (defun vc-cvs-delete-file (file)
441 (vc-cvs-command nil 0 file "remove" "-f"))
442
443 (autoload 'vc-default-revert "vc")
444
445 (defun vc-cvs-revert (file &optional contents-done)
446 "Revert FILE to the working revision on which it was based."
447 (vc-default-revert 'CVS file contents-done)
448 (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
449 (if vc-cvs-use-edit
450 (vc-cvs-command nil 0 file "unedit")
451 ;; Make the file read-only by switching off all w-bits
452 (set-file-modes file (logand (file-modes file) 3950)))))
453
454 (defun vc-cvs-merge-file (file)
455 "Accept a file merge request, prompting for revisions."
456 (let* ((first-revision
457 (vc-read-revision
458 (concat "Merge " file
459 " from branch or revision "
460 "(default news on current branch): ")
461 (list file)
462 'CVS))
463 second-revision
464 status)
465 (cond
466 ((string= first-revision "")
467 (setq status (vc-cvs-merge-news file)))
468 (t
469 (if (not (vc-branch-p first-revision))
470 (setq second-revision
471 (vc-read-revision
472 "Second revision: "
473 (list file) 'CVS nil
474 (concat (vc-branch-part first-revision) ".")))
475 ;; We want to merge an entire branch. Set revisions
476 ;; accordingly, so that vc-cvs-merge understands us.
477 (setq second-revision first-revision)
478 ;; first-revision must be the starting point of the branch
479 (setq first-revision (vc-branch-part first-revision)))
480 (setq status (vc-cvs-merge file first-revision second-revision))))
481 status))
482
483 (defun vc-cvs-merge (file first-revision &optional second-revision)
484 "Merge changes into current working copy of FILE.
485 The changes are between FIRST-REVISION and SECOND-REVISION."
486 (vc-cvs-command nil 0 file
487 "update" "-kk"
488 (concat "-j" first-revision)
489 (concat "-j" second-revision))
490 (vc-file-setprop file 'vc-state 'edited)
491 (with-current-buffer (get-buffer "*vc*")
492 (goto-char (point-min))
493 (if (re-search-forward "conflicts during merge" nil t)
494 (progn
495 (vc-file-setprop file 'vc-state 'conflict)
496 ;; signal error
497 1)
498 (vc-file-setprop file 'vc-state 'edited)
499 ;; signal success
500 0)))
501
502 (defun vc-cvs-merge-news (file)
503 "Merge in any new changes made to FILE."
504 (message "Merging changes into %s..." file)
505 ;; (vc-file-setprop file 'vc-working-revision nil)
506 (vc-file-setprop file 'vc-checkout-time 0)
507 (vc-cvs-command nil nil file "update")
508 ;; Analyze the merge result reported by CVS, and set
509 ;; file properties accordingly.
510 (with-current-buffer (get-buffer "*vc*")
511 (goto-char (point-min))
512 ;; get new working revision
513 (if (re-search-forward
514 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
515 (vc-file-setprop file 'vc-working-revision (match-string 1))
516 (vc-file-setprop file 'vc-working-revision nil))
517 ;; get file status
518 (prog1
519 (if (eq (buffer-size) 0)
520 0 ;; there were no news; indicate success
521 (if (re-search-forward
522 (concat "^\\([CMUP] \\)?"
523 (regexp-quote
524 (substring file (1+ (length (expand-file-name
525 "." default-directory)))))
526 "\\( already contains the differences between \\)?")
527 nil t)
528 (cond
529 ;; Merge successful, we are in sync with repository now
530 ((or (match-string 2)
531 (string= (match-string 1) "U ")
532 (string= (match-string 1) "P "))
533 (vc-file-setprop file 'vc-state 'up-to-date)
534 (vc-file-setprop file 'vc-checkout-time
535 (nth 5 (file-attributes file)))
536 0);; indicate success to the caller
537 ;; Merge successful, but our own changes are still in the file
538 ((string= (match-string 1) "M ")
539 (vc-file-setprop file 'vc-state 'edited)
540 0);; indicate success to the caller
541 ;; Conflicts detected!
542 (t
543 (vc-file-setprop file 'vc-state 'conflict)
544 1);; signal the error to the caller
545 )
546 (pop-to-buffer "*vc*")
547 (error "Couldn't analyze cvs update result")))
548 (message "Merging changes into %s...done" file))))
549
550 (defun vc-cvs-modify-change-comment (files rev comment)
551 "Modify the change comments for FILES on a specified REV.
552 Will fail unless you have administrative privileges on the repo."
553 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
554
555 ;;;
556 ;;; History functions
557 ;;;
558
559 (declare-function vc-rcs-print-log-cleanup "vc-rcs" ())
560 ;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
561 (declare-function vc-exec-after "vc-dispatcher" (code))
562
563 (defun vc-cvs-print-log (files buffer &optional _shortlog _start-revision limit)
564 "Print commit log associated with FILES into specified BUFFER.
565 Remaining arguments are ignored."
566 (require 'vc-rcs)
567 ;; It's just the catenation of the individual logs.
568 (vc-cvs-command
569 buffer
570 (if (vc-cvs-stay-local-p files) 'async 0)
571 files "log")
572 (with-current-buffer buffer
573 (vc-run-delayed (vc-rcs-print-log-cleanup)))
574 (when limit 'limit-unsupported))
575
576 (defun vc-cvs-comment-history (file)
577 "Get comment history of a file."
578 (vc-call-backend 'RCS 'comment-history file))
579
580 (autoload 'vc-version-backup-file "vc")
581 (declare-function vc-coding-system-for-diff "vc" (file))
582
583 (defun vc-cvs-diff (files &optional oldvers newvers buffer async)
584 "Get a difference report using CVS between two revisions of FILE."
585 (let* (process-file-side-effects
586 (async (and async (vc-cvs-stay-local-p files)))
587 (invoke-cvs-diff-list nil)
588 status)
589 ;; Look through the file list and see if any files have backups
590 ;; that can be used to do a plain "diff" instead of "cvs diff".
591 (dolist (file files)
592 (let ((ov oldvers)
593 (nv newvers))
594 (when (or (not ov) (string-equal ov ""))
595 (setq ov (vc-working-revision file)))
596 (when (string-equal nv "")
597 (setq nv nil))
598 (let ((file-oldvers (vc-version-backup-file file ov))
599 (file-newvers (if (not nv)
600 file
601 (vc-version-backup-file file nv)))
602 (coding-system-for-read (vc-coding-system-for-diff file)))
603 (if (and file-oldvers file-newvers)
604 (progn
605 ;; This used to append diff-switches and vc-diff-switches,
606 ;; which was consistent with the vc-diff-switches doc at that
607 ;; time, but not with the actual behavior of any other VC diff.
608 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
609 ;; Not a CVS diff, does not use vc-cvs-diff-switches.
610 (append (vc-switches nil 'diff)
611 (list (file-relative-name file-oldvers)
612 (file-relative-name file-newvers))))
613 (setq status 0))
614 (push file invoke-cvs-diff-list)))))
615 (when invoke-cvs-diff-list
616 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
617 (if async 'async 1)
618 invoke-cvs-diff-list "diff"
619 (and oldvers (concat "-r" oldvers))
620 (and newvers (concat "-r" newvers))
621 (vc-switches 'CVS 'diff))))
622 (if async 1 status))) ; async diff, pessimistic assumption
623
624 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
625
626 (defun vc-cvs-annotate-process-filter (filter process string)
627 (setq string (concat (process-get process 'output) string))
628 (if (not (string-match vc-cvs-annotate-first-line-re string))
629 ;; Still waiting for the first real line.
630 (process-put process 'output string)
631 (remove-function (process-filter process) #'vc-cvs-annotate-process-filter)
632 (funcall filter process (substring string (match-beginning 0)))))
633
634 (defun vc-cvs-annotate-command (file buffer &optional revision)
635 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
636 Optional arg REVISION is a revision to annotate from."
637 (apply #'vc-cvs-command buffer
638 (if (vc-cvs-stay-local-p file)
639 'async 0)
640 file "annotate"
641 (append (vc-switches 'cvs 'annotate)
642 (if revision (list (concat "-r" revision)))))
643 ;; Strip the leading few lines.
644 (let ((proc (get-buffer-process buffer)))
645 (if proc
646 ;; If running asynchronously, use a process filter.
647 (add-function :around (process-filter proc)
648 #'vc-cvs-annotate-process-filter)
649 (with-current-buffer buffer
650 (goto-char (point-min))
651 (re-search-forward vc-cvs-annotate-first-line-re)
652 (delete-region (point-min) (1- (point)))))))
653
654 (declare-function vc-annotate-convert-time "vc-annotate" (&optional time))
655
656 (defun vc-cvs-annotate-current-time ()
657 "Return the current time, based at midnight of the current day, and
658 encoded as fractional days."
659 (vc-annotate-convert-time
660 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time)))))
661
662 (defun vc-cvs-annotate-time ()
663 "Return the time of the next annotation (as fraction of days)
664 systime, or nil if there is none."
665 (let* ((bol (point))
666 (cache (get-text-property bol 'vc-cvs-annotate-time))
667 (inhibit-read-only t)
668 (inhibit-modification-hooks t))
669 (cond
670 (cache)
671 ((looking-at
672 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
673 (let ((day (string-to-number (match-string 1)))
674 (month (cdr (assq (intern (match-string 2))
675 '((Jan . 1) (Feb . 2) (Mar . 3)
676 (Apr . 4) (May . 5) (Jun . 6)
677 (Jul . 7) (Aug . 8) (Sep . 9)
678 (Oct . 10) (Nov . 11) (Dec . 12)))))
679 (year (let ((tmp (string-to-number (match-string 3))))
680 ;; Years 0..68 are 2000..2068.
681 ;; Years 69..99 are 1969..1999.
682 (+ (cond ((> 69 tmp) 2000)
683 ((> 100 tmp) 1900)
684 (t 0))
685 tmp))))
686 (put-text-property
687 bol (1+ bol) 'vc-cvs-annotate-time
688 (setq cache (cons
689 ;; Position at end makes for nicer overlay result.
690 ;; Don't put actual buffer pos here, but only relative
691 ;; distance, so we don't ever move backward in the
692 ;; goto-char below, even if the text is moved.
693 (- (match-end 0) (match-beginning 0))
694 (vc-annotate-convert-time
695 (encode-time 0 0 0 day month year))))))))
696 (when cache
697 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
698 (cdr cache)))) ; days (float)
699
700 (defun vc-cvs-annotate-extract-revision-at-line ()
701 (save-excursion
702 (beginning-of-line)
703 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
704 (line-end-position) t)
705 (match-string-no-properties 1)
706 nil)))
707
708 (defun vc-cvs-previous-revision (file rev)
709 (vc-call-backend 'RCS 'previous-revision file rev))
710
711 (defun vc-cvs-next-revision (file rev)
712 (vc-call-backend 'RCS 'next-revision file rev))
713
714 ;; FIXME: This should probably be replaced by code using cvs2cl.
715 (defun vc-cvs-update-changelog (files)
716 (vc-call-backend 'RCS 'update-changelog files))
717
718 ;;;
719 ;;; Tag system
720 ;;;
721
722 (defun vc-cvs-create-tag (dir name branchp)
723 "Assign to DIR's current revision a given NAME.
724 If BRANCHP is non-nil, the name is created as a branch (and the current
725 workspace is immediately moved to that new branch)."
726 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
727 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
728
729 ;; Follows vc-cvs-command, which uses vc-do-command from vc-dispatcher.
730 (declare-function vc-resynch-buffer "vc-dispatcher"
731 (file &optional keep noquery reset-vc-info))
732
733 (defun vc-cvs-retrieve-tag (dir name update)
734 "Retrieve a tag at and below DIR.
735 NAME is the name of the tag; if it is empty, do a `cvs update'.
736 If UPDATE is non-nil, then update (resynch) any affected buffers."
737 (with-current-buffer (get-buffer-create "*vc*")
738 (let ((default-directory dir)
739 (sticky-tag))
740 (erase-buffer)
741 (if (or (not name) (string= name ""))
742 (vc-cvs-command t 0 nil "update")
743 (vc-cvs-command t 0 nil "update" "-r" name)
744 (setq sticky-tag name))
745 (when update
746 (goto-char (point-min))
747 (while (not (eobp))
748 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
749 (let* ((file (expand-file-name (match-string 2) dir))
750 (state (match-string 1))
751 (buffer (find-buffer-visiting file)))
752 (when buffer
753 (cond
754 ((or (string= state "U")
755 (string= state "P"))
756 (vc-file-setprop file 'vc-state 'up-to-date)
757 (vc-file-setprop file 'vc-working-revision nil)
758 (vc-file-setprop file 'vc-checkout-time
759 (nth 5 (file-attributes file))))
760 ((or (string= state "M")
761 (string= state "C"))
762 (vc-file-setprop file 'vc-state 'edited)
763 (vc-file-setprop file 'vc-working-revision nil)
764 (vc-file-setprop file 'vc-checkout-time 0)))
765 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
766 (vc-resynch-buffer file t t))))
767 (forward-line 1))))))
768
769
770 ;;;
771 ;;; Miscellaneous
772 ;;;
773
774 (defun vc-cvs-make-version-backups-p (file)
775 "Return non-nil if version backups should be made for FILE."
776 (vc-cvs-stay-local-p file))
777
778 (defun vc-cvs-check-headers ()
779 "Check if the current file has any headers in it."
780 (save-excursion
781 (goto-char (point-min))
782 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
783 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
784
785
786 ;;;
787 ;;; Internal functions
788 ;;;
789
790 (defun vc-cvs-command (buffer okstatus files &rest flags)
791 "A wrapper around `vc-do-command' for use in vc-cvs.el.
792 The difference to vc-do-command is that this function always invokes `cvs',
793 and that it passes `vc-cvs-global-switches' to it before FLAGS."
794 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
795 (if (stringp vc-cvs-global-switches)
796 (cons vc-cvs-global-switches flags)
797 (append vc-cvs-global-switches
798 flags))))
799
800 (defun vc-cvs-stay-local-p (file)
801 "Return non-nil if VC should stay local when handling FILE.
802 If FILE is a list of files, return non-nil if any of them
803 individually should stay local."
804 (if (listp file)
805 (delq nil (mapcar (lambda (arg) (vc-cvs-stay-local-p arg)) file))
806 (let* ((sym (vc-make-backend-sym 'CVS 'stay-local))
807 (stay-local (if (boundp sym) (symbol-value sym) vc-stay-local)))
808 (if (symbolp stay-local) stay-local
809 (let ((dirname (if (file-directory-p file)
810 (directory-file-name file)
811 (file-name-directory file))))
812 (eq 'yes
813 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
814 (vc-file-setprop
815 dirname 'vc-cvs-stay-local-p
816 (let ((hostname (vc-cvs-repository-hostname dirname)))
817 (if (not hostname)
818 'no
819 (let ((default t))
820 (if (eq (car-safe stay-local) 'except)
821 (setq default nil stay-local (cdr stay-local)))
822 (when (consp stay-local)
823 (setq stay-local
824 (mapconcat 'identity stay-local "\\|")))
825 (if (if (string-match stay-local hostname)
826 default (not default))
827 'yes 'no))))))))))))
828
829 (defun vc-cvs-repository-hostname (dirname)
830 "Hostname of the CVS server associated to workarea DIRNAME."
831 (let ((rootname (expand-file-name "CVS/Root" dirname)))
832 (when (file-readable-p rootname)
833 (with-temp-buffer
834 (let ((coding-system-for-read
835 (or file-name-coding-system
836 default-file-name-coding-system)))
837 (vc-insert-file rootname))
838 (goto-char (point-min))
839 (nth 2 (vc-cvs-parse-root
840 (buffer-substring (point)
841 (line-end-position))))))))
842
843 (defun vc-cvs-parse-uhp (path)
844 "parse user@host/path into (user@host /path)"
845 (if (string-match "\\([^/]+\\)\\(/.*\\)" path)
846 (list (match-string 1 path) (match-string 2 path))
847 (list nil path)))
848
849 (defun vc-cvs-parse-root (root)
850 "Split CVS ROOT specification string into a list of fields.
851 A CVS root specification of the form
852 [:METHOD:][[USER@]HOSTNAME]:?/path/to/repository
853 is converted to a normalized record with the following structure:
854 \(METHOD USER HOSTNAME CVS-ROOT).
855 The default METHOD for a CVS root of the form
856 /path/to/repository
857 is `local'.
858 The default METHOD for a CVS root of the form
859 [USER@]HOSTNAME:/path/to/repository
860 is `ext'.
861 For an empty string, nil is returned (invalid CVS root)."
862 ;; Split CVS root into colon separated fields (0-4).
863 ;; The `x:' makes sure, that leading colons are not lost;
864 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
865 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
866 (len (length root-list))
867 ;; All syntactic varieties will get a proper METHOD.
868 (root-list
869 (cond
870 ((= len 0)
871 ;; Invalid CVS root
872 nil)
873 ((= len 1)
874 (let ((uhp (vc-cvs-parse-uhp (car root-list))))
875 (cons (if (car uhp) "ext" "local") uhp)))
876 ((= len 2)
877 ;; [USER@]HOST:PATH => method `ext'
878 (and (not (equal (car root-list) ""))
879 (cons "ext" root-list)))
880 ((= len 3)
881 ;; :METHOD:PATH or :METHOD:USER@HOSTNAME/PATH
882 (cons (cadr root-list)
883 (vc-cvs-parse-uhp (nth 2 root-list))))
884 (t
885 ;; :METHOD:[USER@]HOST:PATH
886 (cdr root-list)))))
887 (if root-list
888 (let ((method (car root-list))
889 (uhost (or (cadr root-list) ""))
890 (root (nth 2 root-list))
891 user host)
892 ;; Split USER@HOST
893 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
894 (setq user (match-string 1 uhost)
895 host (match-string 2 uhost))
896 (setq host uhost))
897 ;; Remove empty HOST
898 (and (equal host "")
899 (setq host))
900 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
901 (and host
902 (equal method "local")
903 (setq root (concat host ":" root) host))
904 ;; Normalize CVS root record
905 (list method user host root)))))
906
907 ;; XXX: This does not work correctly for subdirectories. "cvs status"
908 ;; information is context sensitive, it contains lines like:
909 ;; cvs status: Examining DIRNAME
910 ;; and the file entries after that don't show the full path.
911 ;; Because of this VC directory listings only show changed files
912 ;; at the top level for CVS.
913 (defun vc-cvs-parse-status (&optional full)
914 "Parse output of \"cvs status\" command in the current buffer.
915 Set file properties accordingly. Unless FULL is t, parse only
916 essential information. Note that this can never set the 'ignored
917 state."
918 (let (file status missing)
919 (goto-char (point-min))
920 (while (looking-at "? \\(.*\\)")
921 (setq file (expand-file-name (match-string 1)))
922 (vc-file-setprop file 'vc-state 'unregistered)
923 (forward-line 1))
924 (when (re-search-forward "^File: " nil t)
925 (when (setq missing (looking-at "no file "))
926 (goto-char (match-end 0)))
927 (cond
928 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
929 (setq file (expand-file-name (match-string 1)))
930 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
931 (match-string 1) "Unknown"))
932 (when (and full
933 (re-search-forward
934 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
935 \[\t ]+\\([0-9.]+\\)"
936 nil t))
937 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
938 (vc-file-setprop
939 file 'vc-state
940 (cond
941 ((string-match "Up-to-date" status)
942 (vc-file-setprop file 'vc-checkout-time
943 (nth 5 (file-attributes file)))
944 'up-to-date)
945 ((string-match "Locally Modified" status) 'edited)
946 ((string-match "Needs Merge" status) 'needs-merge)
947 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
948 (if missing 'missing 'needs-update))
949 ((string-match "Locally Added" status) 'added)
950 ((string-match "Locally Removed" status) 'removed)
951 ((string-match "File had conflicts " status) 'conflict)
952 ((string-match "Unknown" status) 'unregistered)
953 (t 'edited))))))))
954
955 (defun vc-cvs-after-dir-status (update-function)
956 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
957 ;; This needs a lot of testing.
958 (let ((status nil)
959 (status-str nil)
960 (file nil)
961 (result nil)
962 (missing nil)
963 (ignore-next nil)
964 (subdir default-directory))
965 (goto-char (point-min))
966 (while
967 ;; Look for either a file entry, an unregistered file, or a
968 ;; directory change.
969 (re-search-forward
970 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: \\(Examining\\|nothing\\) .*\n\\)"
971 nil t)
972 ;; FIXME: get rid of narrowing here.
973 (narrow-to-region (match-beginning 0) (match-end 0))
974 (goto-char (point-min))
975 ;; The subdir
976 (when (looking-at "cvs status: Examining \\(.+\\)")
977 (setq subdir (expand-file-name (match-string 1))))
978 ;; Unregistered files
979 (while (looking-at "? \\(.*\\)")
980 (setq file (file-relative-name
981 (expand-file-name (match-string 1) subdir)))
982 (push (list file 'unregistered) result)
983 (forward-line 1))
984 (when (looking-at "cvs status: nothing known about")
985 ;; We asked about a non existent file. The output looks like this:
986
987 ;; cvs status: nothing known about `lisp/v.diff'
988 ;; ===================================================================
989 ;; File: no file v.diff Status: Unknown
990 ;;
991 ;; Working revision: No entry for v.diff
992 ;; Repository revision: No revision control file
993 ;;
994
995 ;; Due to narrowing in this iteration we only see the "cvs
996 ;; status:" line, so just set a flag so that we can ignore the
997 ;; file in the next iteration.
998 (setq ignore-next t))
999 ;; A file entry.
1000 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
1001 (setq missing (match-string 1))
1002 (setq file (file-relative-name
1003 (expand-file-name (match-string 2) subdir)))
1004 (setq status-str (match-string 3))
1005 (setq status
1006 (cond
1007 ((string-match "Up-to-date" status-str) 'up-to-date)
1008 ((string-match "Locally Modified" status-str) 'edited)
1009 ((string-match "Needs Merge" status-str) 'needs-merge)
1010 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
1011 (if missing 'missing 'needs-update))
1012 ((string-match "Locally Added" status-str) 'added)
1013 ((string-match "Locally Removed" status-str) 'removed)
1014 ((string-match "File had conflicts " status-str) 'conflict)
1015 ((string-match "Unknown" status-str) 'unregistered)
1016 (t 'edited)))
1017 (if ignore-next
1018 (setq ignore-next nil)
1019 (unless (eq status 'up-to-date)
1020 (push (list file status) result))))
1021 (goto-char (point-max))
1022 (widen))
1023 (funcall update-function result))
1024 ;; Alternative implementation: use the "update" command instead of
1025 ;; the "status" command.
1026 ;; (let ((result nil)
1027 ;; (translation '((?? . unregistered)
1028 ;; (?A . added)
1029 ;; (?C . conflict)
1030 ;; (?M . edited)
1031 ;; (?P . needs-merge)
1032 ;; (?R . removed)
1033 ;; (?U . needs-update))))
1034 ;; (goto-char (point-min))
1035 ;; (while (not (eobp))
1036 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
1037 ;; (push (list (match-string 1)
1038 ;; (cdr (assoc (char-after) translation)))
1039 ;; result)
1040 ;; (cond
1041 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
1042 ;; ;; Format is:
1043 ;; ;; cvs update: warning: FILENAME was lost
1044 ;; ;; U FILENAME
1045 ;; (push (list (match-string 1) 'missing) result)
1046 ;; ;; Skip the "U" line
1047 ;; (forward-line 1))
1048 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
1049 ;; (push (list (match-string 1) 'unregistered) result))))
1050 ;; (forward-line 1))
1051 ;; (funcall update-function result)))
1052 )
1053
1054 ;; Based on vc-cvs-dir-state-heuristic from Emacs 22.
1055 ;; FIXME does not mention unregistered files.
1056 (defun vc-cvs-dir-status-heuristic (dir update-function &optional basedir)
1057 "Find the CVS state of all files in DIR, using only local information."
1058 (let (file basename status result dirlist)
1059 (with-temp-buffer
1060 (vc-cvs-get-entries dir)
1061 (goto-char (point-min))
1062 (while (not (eobp))
1063 (if (looking-at "D/\\([^/]*\\)////")
1064 (push (expand-file-name (match-string 1) dir) dirlist)
1065 ;; CVS-removed files are not taken under VC control.
1066 (when (looking-at "/\\([^/]*\\)/[^/-]")
1067 (setq basename (match-string 1)
1068 file (expand-file-name basename dir)
1069 status (or (vc-file-getprop file 'vc-state)
1070 (vc-cvs-parse-entry file t)))
1071 (unless (eq status 'up-to-date)
1072 (push (list (if basedir
1073 (file-relative-name file basedir)
1074 basename)
1075 status) result))))
1076 (forward-line 1)))
1077 (dolist (subdir dirlist)
1078 (setq result (append result
1079 (vc-cvs-dir-status-heuristic subdir nil
1080 (or basedir dir)))))
1081 (if basedir result
1082 (funcall update-function result))))
1083
1084 (defun vc-cvs-dir-status-files (dir files update-function)
1085 "Create a list of conses (file . state) for FILES in DIR.
1086 Query all files in DIR if files is nil."
1087 (let ((local (vc-cvs-stay-local-p dir)))
1088 (if (and (not files) local (not (eq local 'only-file)))
1089 (vc-cvs-dir-status-heuristic dir update-function)
1090 (if (not files) (setq files (vc-expand-dirs (list dir) 'CVS)))
1091 (vc-cvs-command (current-buffer) 'async files "-f" "status")
1092 ;; Alternative implementation: use the "update" command instead of
1093 ;; the "status" command.
1094 ;; (vc-cvs-command (current-buffer) 'async
1095 ;; (file-relative-name dir)
1096 ;; "-f" "-n" "update" "-d" "-P")
1097 (vc-run-delayed
1098 (vc-cvs-after-dir-status update-function)))))
1099
1100 (defun vc-cvs-file-to-string (file)
1101 "Read the content of FILE and return it as a string."
1102 (condition-case nil
1103 (with-temp-buffer
1104 (insert-file-contents file)
1105 (goto-char (point-min))
1106 (buffer-substring (point) (point-max)))
1107 (file-error nil)))
1108
1109 (defun vc-cvs-dir-extra-headers (_dir)
1110 "Extract and represent per-directory properties of a CVS working copy."
1111 (let ((repo
1112 (condition-case nil
1113 (with-temp-buffer
1114 (insert-file-contents "CVS/Root")
1115 (goto-char (point-min))
1116 (and (looking-at ":ext:") (delete-char 5))
1117 (concat (buffer-substring (point) (1- (point-max))) "\n"))
1118 (file-error nil)))
1119 (module
1120 (condition-case nil
1121 (with-temp-buffer
1122 (insert-file-contents "CVS/Repository")
1123 (goto-char (point-min))
1124 (skip-chars-forward "^\n")
1125 (concat (buffer-substring (point-min) (point)) "\n"))
1126 (file-error nil))))
1127 (concat
1128 (cond (repo
1129 (concat (propertize "Repository : " 'face 'font-lock-type-face)
1130 (propertize repo 'face 'font-lock-variable-name-face)))
1131 (t ""))
1132 (cond (module
1133 (concat (propertize "Module : " 'face 'font-lock-type-face)
1134 (propertize module 'face 'font-lock-variable-name-face)))
1135 (t ""))
1136 (if (file-readable-p "CVS/Tag")
1137 (let ((tag (vc-cvs-file-to-string "CVS/Tag")))
1138 (cond
1139 ((string-match "\\`T" tag)
1140 (concat (propertize "Tag : " 'face 'font-lock-type-face)
1141 (propertize (substring tag 1)
1142 'face 'font-lock-variable-name-face)))
1143 ((string-match "\\`D" tag)
1144 (concat (propertize "Date : " 'face 'font-lock-type-face)
1145 (propertize (substring tag 1)
1146 'face 'font-lock-variable-name-face)))
1147 (t ""))))
1148
1149 ;; In CVS, branch is a per-file property, not a per-directory property.
1150 ;; We can't really do this here without making dangerous assumptions.
1151 ;;(propertize "Branch: " 'face 'font-lock-type-face)
1152 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
1153 ;; 'face 'font-lock-warning-face)
1154 )))
1155
1156 (defun vc-cvs-get-entries (dir)
1157 "Insert the CVS/Entries file from below DIR into the current buffer.
1158 This function ensures that the correct coding system is used for that,
1159 which may not be the one that is used for the files' contents.
1160 CVS/Entries should only be accessed through this function."
1161 (let ((coding-system-for-read (or file-name-coding-system
1162 default-file-name-coding-system)))
1163 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
1164
1165 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
1166 "Return non-nil if TAG is a valid symbolic tag name."
1167 ;; According to the CVS manual, a valid symbolic tag must start with
1168 ;; an uppercase or lowercase letter and can contain uppercase and
1169 ;; lowercase letters, digits, `-', and `_'.
1170 (and (string-match "^[a-zA-Z]" tag)
1171 (not (string-match "[^a-z0-9A-Z-_]" tag))))
1172
1173 (defun vc-cvs-valid-revision-number-p (tag)
1174 "Return non-nil if TAG is a valid revision number."
1175 (and (string-match "^[0-9]" tag)
1176 (not (string-match "[^0-9.]" tag))))
1177
1178 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
1179 "Parse and return the sticky tag as a string.
1180 `match-data' is protected."
1181 (let ((data (match-data))
1182 (tag)
1183 (type (cond ((string= match-type "D") 'date)
1184 ((string= match-type "T")
1185 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
1186 'symbolic-name
1187 'revision-number))
1188 (t nil))))
1189 (unwind-protect
1190 (progn
1191 (cond
1192 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1193 ((eq type 'date)
1194 (string-match
1195 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1196 match-tag)
1197 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1198 (month (string-to-number (match-string 2 match-tag)))
1199 (day (string-to-number (match-string 3 match-tag)))
1200 (hour (string-to-number (match-string 4 match-tag)))
1201 (min (string-to-number (match-string 5 match-tag)))
1202 (sec (string-to-number (match-string 6 match-tag)))
1203 ;; Years 0..68 are 2000..2068.
1204 ;; Years 69..99 are 1969..1999.
1205 (year (+ (cond ((> 69 year-tmp) 2000)
1206 ((> 100 year-tmp) 1900)
1207 (t 0))
1208 year-tmp)))
1209 (setq tag (encode-time sec min hour day month year))))
1210 ;; Sticky Tag name or revision number
1211 ((eq type 'symbolic-name) (setq tag match-tag))
1212 ((eq type 'revision-number) (setq tag match-tag))
1213 ;; Default is no sticky tag at all
1214 (t nil))
1215 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1216 ((eq vc-cvs-sticky-tag-display t)
1217 (cond ((eq type 'date) (format-time-string
1218 vc-cvs-sticky-date-format-string
1219 tag))
1220 ((eq type 'symbolic-name) tag)
1221 ((eq type 'revision-number) tag)
1222 (t nil)))
1223 ((functionp vc-cvs-sticky-tag-display)
1224 (funcall vc-cvs-sticky-tag-display tag type))
1225 (t nil)))
1226
1227 (set-match-data data))))
1228
1229 (defun vc-cvs-parse-entry (file &optional set-state)
1230 "Parse a line from CVS/Entries.
1231 Compare modification time to that of the FILE, set file properties
1232 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1233 is non-nil."
1234 (cond
1235 ;; entry for a "locally added" file (not yet committed)
1236 ((looking-at "/[^/]+/0/")
1237 (vc-file-setprop file 'vc-checkout-time 0)
1238 (vc-file-setprop file 'vc-working-revision "0")
1239 (if set-state (vc-file-setprop file 'vc-state 'added)))
1240 ;; normal entry
1241 ((looking-at
1242 (concat "/[^/]+"
1243 ;; revision
1244 "/\\([^/]*\\)"
1245 ;; timestamp and optional conflict field
1246 "/\\([^/]*\\)/"
1247 ;; options
1248 "\\([^/]*\\)/"
1249 ;; sticky tag
1250 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1251 "\\(.*\\)")) ;Sticky tag
1252 (vc-file-setprop file 'vc-working-revision (match-string 1))
1253 (vc-file-setprop file 'vc-cvs-sticky-tag
1254 (vc-cvs-parse-sticky-tag (match-string 4)
1255 (match-string 5)))
1256 ;; Compare checkout time and modification time.
1257 ;; This is intentionally different from the algorithm that CVS uses
1258 ;; (which is based on textual comparison), because there can be problems
1259 ;; generating a time string that looks exactly like the one from CVS.
1260 (let* ((time (match-string 2))
1261 (mtime (nth 5 (file-attributes file)))
1262 (parsed-time (progn (require 'parse-time)
1263 (parse-time-string (concat time " +0000")))))
1264 (cond ((and (not (string-match "\\+" time))
1265 (car parsed-time)
1266 ;; Compare just the seconds part of the file time,
1267 ;; since CVS file time stamp resolution is just 1 second.
1268 (let ((ptime (apply 'encode-time parsed-time)))
1269 (and (eq (car mtime) (car ptime))
1270 (eq (cadr mtime) (cadr ptime)))))
1271 (vc-file-setprop file 'vc-checkout-time mtime)
1272 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1273 (t
1274 (vc-file-setprop file 'vc-checkout-time 0)
1275 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
1276
1277 ;; Completion of revision names.
1278 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1279 ;; `cvs log' so I can list all the revision numbers rather than only
1280 ;; tag names.
1281
1282 (defun vc-cvs-revision-table (file)
1283 (let (process-file-side-effects
1284 (default-directory (file-name-directory file))
1285 (res nil))
1286 (with-temp-buffer
1287 (vc-cvs-command t nil file "log")
1288 (goto-char (point-min))
1289 (when (re-search-forward "^symbolic names:\n" nil t)
1290 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1291 (push (cons (match-string 1) (match-string 2)) res)
1292 (forward-line 1)))
1293 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1294 (push (match-string 1) res))
1295 res)))
1296
1297 (defun vc-cvs-revision-completion-table (files)
1298 (letrec ((table (lazy-completion-table
1299 table (lambda () (vc-cvs-revision-table (car files))))))
1300 table))
1301
1302 (defun vc-cvs-find-admin-dir (file)
1303 "Return the administrative directory of FILE."
1304 (vc-find-root file "CVS"))
1305
1306 (defun vc-cvs-ignore (file &optional _directory _remove)
1307 "Ignore FILE under CVS."
1308 (vc-cvs-append-to-ignore (file-name-directory file) file))
1309
1310 (defun vc-cvs-append-to-ignore (dir str &optional old-dir)
1311 "In DIR, add STR to the .cvsignore file.
1312 If OLD-DIR is non-nil, then this is a directory that we don't want
1313 to hear about anymore."
1314 (with-current-buffer
1315 (find-file-noselect (expand-file-name ".cvsignore" dir))
1316 (when (ignore-errors
1317 (and buffer-read-only
1318 (eq 'CVS (vc-backend buffer-file-name))
1319 (not (vc-editable-p buffer-file-name))))
1320 ;; CVSREAD=on special case
1321 (vc-checkout buffer-file-name t))
1322 (goto-char (point-max))
1323 (unless (bolp) (insert "\n"))
1324 (insert str (if old-dir "/\n" "\n"))
1325 ;; FIXME this is a pcvs variable.
1326 (if (bound-and-true-p cvs-sort-ignore-file)
1327 (sort-lines nil (point-min) (point-max)))
1328 (save-buffer)))
1329
1330 (provide 'vc-cvs)
1331
1332 ;;; vc-cvs.el ends here