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