Tom Tromey <tromey at redhat.com>
[emacs.git] / lisp / vc-cvs.el
blob67830b48fd5feb74116ca99ce8548fb572757f63
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 (file)
242 "CVS-specific version of `vc-checkout-model'."
243 (if (getenv "CVSREAD")
244 'announce
245 (let ((attrib (file-attributes file)))
246 (if (and attrib ;; don't check further if FILE doesn't exist
247 ;; If the file is not writable (despite CVSREAD being
248 ;; undefined), this is probably because the file is being
249 ;; "watched" by other developers.
250 ;; (If vc-mistrust-permissions was t, we actually shouldn't
251 ;; trust this, but there is no other way to learn this from CVS
252 ;; at the moment (version 1.9).)
253 (string-match "r-..-..-." (nth 8 attrib)))
254 'announce
255 'implicit))))
257 (defun vc-cvs-mode-line-string (file)
258 "Return string for placement into the modeline for FILE.
259 Compared to the default implementation, this function does two things:
260 Handle the special case of a CVS file that is added but not yet
261 committed and support display of sticky tags."
262 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
263 help-echo
264 (string
265 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
266 (setq help-echo
267 (get-text-property 0 'help-echo def-ml))
268 def-ml)))
269 (propertize
270 (if (zerop (length sticky-tag))
271 string
272 (setq help-echo (format "%s on the '%s' branch"
273 help-echo sticky-tag))
274 (concat string "[" sticky-tag "]"))
275 'help-echo help-echo)))
279 ;;; State-changing functions
282 (defun vc-cvs-register (files &optional rev comment)
283 "Register FILES into the CVS version-control system.
284 COMMENT can be used to provide an initial description of FILES.
286 `vc-register-switches' and `vc-cvs-register-switches' are passed to
287 the CVS command (in that order)."
288 ;; Register the directories if needed.
289 (let (dirs)
290 (dolist (file files)
291 (and (not (vc-cvs-responsible-p file))
292 (vc-cvs-could-register file)
293 (push (directory-file-name (file-name-directory file)) dirs)))
294 (if dirs (vc-cvs-register dirs)))
295 (apply 'vc-cvs-command nil 0 files
296 "add"
297 (and comment (string-match "[^\t\n ]" comment)
298 (concat "-m" comment))
299 (vc-switches 'CVS 'register)))
301 (defun vc-cvs-responsible-p (file)
302 "Return non-nil if CVS thinks it is responsible for FILE."
303 (file-directory-p (expand-file-name "CVS"
304 (if (file-directory-p file)
305 file
306 (file-name-directory file)))))
308 (defun vc-cvs-could-register (file)
309 "Return non-nil if FILE could be registered in CVS.
310 This is only possible if CVS is managing FILE's directory or one of
311 its parents."
312 (let ((dir file))
313 (while (and (stringp dir)
314 (not (equal dir (setq dir (file-name-directory dir))))
315 dir)
316 (setq dir (if (file-directory-p
317 (expand-file-name "CVS/Entries" dir))
318 t (directory-file-name dir))))
319 (eq dir t)))
321 (defun vc-cvs-checkin (files rev comment)
322 "CVS-specific version of `vc-backend-checkin'."
323 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
324 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
325 (error "%s is not a valid symbolic tag name" rev)
326 ;; If the input revison is a valid symbolic tag name, we create it
327 ;; as a branch, commit and switch to it.
328 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
329 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
330 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
331 files)))
332 (let ((status (apply 'vc-cvs-command nil 1 files
333 "ci" (if rev (concat "-r" rev))
334 (concat "-m" comment)
335 (vc-switches 'CVS 'checkin))))
336 (set-buffer "*vc*")
337 (goto-char (point-min))
338 (when (not (zerop status))
339 ;; Check checkin problem.
340 (cond
341 ((re-search-forward "Up-to-date check failed" nil t)
342 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
343 files)
344 (error "%s" (substitute-command-keys
345 (concat "Up-to-date check failed: "
346 "type \\[vc-next-action] to merge in changes"))))
348 (pop-to-buffer (current-buffer))
349 (goto-char (point-min))
350 (shrink-window-if-larger-than-buffer)
351 (error "Check-in failed"))))
352 ;; Single-file commit? Then update the revision by parsing the buffer.
353 ;; Otherwise we can't necessarily tell what goes with what; clear
354 ;; its properties so they have to be refetched.
355 (if (= (length files) 1)
356 (vc-file-setprop
357 (car files) 'vc-working-revision
358 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
359 (mapc (lambda (file) (vc-file-clearprops file)) files))
360 ;; Anyway, forget the checkout model of the file, because we might have
361 ;; guessed wrong when we found the file. After commit, we can
362 ;; tell it from the permissions of the file (see
363 ;; vc-cvs-checkout-model).
364 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
365 files)
367 ;; if this was an explicit check-in (does not include creation of
368 ;; a branch), remove the sticky tag.
369 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
370 (vc-cvs-command nil 0 files "update" "-A"))))
372 (defun vc-cvs-find-revision (file rev buffer)
373 (apply 'vc-cvs-command
374 buffer 0 file
375 "-Q" ; suppress diagnostic output
376 "update"
377 (and rev (not (string= rev ""))
378 (concat "-r" rev))
379 "-p"
380 (vc-switches 'CVS 'checkout)))
382 (defun vc-cvs-checkout (file &optional editable rev)
383 "Checkout a revision of FILE into the working area.
384 EDITABLE non-nil means that the file should be writable.
385 REV is the revision to check out."
386 (message "Checking out %s..." file)
387 ;; Change buffers to get local value of vc-checkout-switches.
388 (with-current-buffer (or (get-file-buffer file) (current-buffer))
389 (if (and (file-exists-p file) (not rev))
390 ;; If no revision was specified, just make the file writable
391 ;; if necessary (using `cvs-edit' if requested).
392 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
393 (if vc-cvs-use-edit
394 (vc-cvs-command nil 0 file "edit")
395 (set-file-modes file (logior (file-modes file) 128))
396 (if (equal file buffer-file-name) (toggle-read-only -1))))
397 ;; Check out a particular revision (or recreate the file).
398 (vc-file-setprop file 'vc-working-revision nil)
399 (apply 'vc-cvs-command nil 0 file
400 (and editable "-w")
401 "update"
402 (when rev
403 (unless (eq rev t)
404 ;; default for verbose checkout: clear the
405 ;; sticky tag so that the actual update will
406 ;; get the head of the trunk
407 (if (string= rev "")
408 "-A"
409 (concat "-r" rev))))
410 (vc-switches 'CVS 'checkout)))
411 (vc-mode-line file))
412 (message "Checking out %s...done" file))
414 (defun vc-cvs-delete-file (file)
415 (vc-cvs-command nil 0 file "remove" "-f")
416 (vc-cvs-command nil 0 file "commit" "-mRemoved."))
418 (defun vc-cvs-revert (file &optional contents-done)
419 "Revert FILE to the working revision on which it was based."
420 (vc-default-revert 'CVS file contents-done)
421 (unless (eq (vc-checkout-model file) 'implicit)
422 (if vc-cvs-use-edit
423 (vc-cvs-command nil 0 file "unedit")
424 ;; Make the file read-only by switching off all w-bits
425 (set-file-modes file (logand (file-modes file) 3950)))))
427 (defun vc-cvs-merge (file first-revision &optional second-revision)
428 "Merge changes into current working copy of FILE.
429 The changes are between FIRST-REVISION and SECOND-REVISION."
430 (vc-cvs-command nil 0 file
431 "update" "-kk"
432 (concat "-j" first-revision)
433 (concat "-j" second-revision))
434 (vc-file-setprop file 'vc-state 'edited)
435 (with-current-buffer (get-buffer "*vc*")
436 (goto-char (point-min))
437 (if (re-search-forward "conflicts during merge" nil t)
438 (progn
439 (vc-file-setprop file 'vc-state 'conflict)
440 ;; signal error
442 (vc-file-setprop file 'vc-state 'edited)
443 ;; signal success
444 0)))
446 (defun vc-cvs-merge-news (file)
447 "Merge in any new changes made to FILE."
448 (message "Merging changes into %s..." file)
449 ;; (vc-file-setprop file 'vc-working-revision nil)
450 (vc-file-setprop file 'vc-checkout-time 0)
451 (vc-cvs-command nil nil file "update")
452 ;; Analyze the merge result reported by CVS, and set
453 ;; file properties accordingly.
454 (with-current-buffer (get-buffer "*vc*")
455 (goto-char (point-min))
456 ;; get new working revision
457 (if (re-search-forward
458 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
459 (vc-file-setprop file 'vc-working-revision (match-string 1))
460 (vc-file-setprop file 'vc-working-revision nil))
461 ;; get file status
462 (prog1
463 (if (eq (buffer-size) 0)
464 0 ;; there were no news; indicate success
465 (if (re-search-forward
466 (concat "^\\([CMUP] \\)?"
467 (regexp-quote (file-name-nondirectory file))
468 "\\( already contains the differences between \\)?")
469 nil t)
470 (cond
471 ;; Merge successful, we are in sync with repository now
472 ((or (match-string 2)
473 (string= (match-string 1) "U ")
474 (string= (match-string 1) "P "))
475 (vc-file-setprop file 'vc-state 'up-to-date)
476 (vc-file-setprop file 'vc-checkout-time
477 (nth 5 (file-attributes file)))
478 0);; indicate success to the caller
479 ;; Merge successful, but our own changes are still in the file
480 ((string= (match-string 1) "M ")
481 (vc-file-setprop file 'vc-state 'edited)
482 0);; indicate success to the caller
483 ;; Conflicts detected!
485 (vc-file-setprop file 'vc-state 'conflict)
486 1);; signal the error to the caller
488 (pop-to-buffer "*vc*")
489 (error "Couldn't analyze cvs update result")))
490 (message "Merging changes into %s...done" file))))
492 (defun vc-cvs-modify-change-comment (files rev comment)
493 "Modify the change comments for FILES on a specified REV.
494 Will fail unless you have administrative privileges on the repo."
495 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
498 ;;; History functions
501 (defun vc-cvs-print-log (files &optional buffer)
502 "Get change logs associated with FILES."
503 ;; It's just the catenation of the individual logs.
504 (vc-cvs-command
505 buffer
506 (if (vc-stay-local-p files) 'async 0)
507 files "log"))
509 (defun vc-cvs-wash-log ()
510 "Remove all non-comment information from log output."
511 (vc-call-backend 'RCS 'wash-log)
512 nil)
514 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
515 "Get a difference report using CVS between two revisions of FILE."
516 (let* ((async (and (not vc-disable-async-diff)
517 (vc-stay-local-p files)))
518 (invoke-cvs-diff-list nil)
519 status)
520 ;; Look through the file list and see if any files have backups
521 ;; that can be used to do a plain "diff" instead of "cvs diff".
522 (dolist (file files)
523 (let ((ov oldvers)
524 (nv newvers))
525 (when (or (not ov) (string-equal ov ""))
526 (setq ov (vc-working-revision file)))
527 (when (string-equal nv "")
528 (setq nv nil))
529 (let ((file-oldvers (vc-version-backup-file file ov))
530 (file-newvers (if (not nv)
531 file
532 (vc-version-backup-file file nv)))
533 (coding-system-for-read (vc-coding-system-for-diff file)))
534 (if (and file-oldvers file-newvers)
535 (progn
536 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
537 (append (if (listp diff-switches)
538 diff-switches
539 (list diff-switches))
540 (if (listp vc-diff-switches)
541 vc-diff-switches
542 (list vc-diff-switches))
543 (list (file-relative-name file-oldvers)
544 (file-relative-name file-newvers))))
545 (setq status 0))
546 (push file invoke-cvs-diff-list)))))
547 (when invoke-cvs-diff-list
548 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
549 (if async 'async 1)
550 invoke-cvs-diff-list "diff"
551 (and oldvers (concat "-r" oldvers))
552 (and newvers (concat "-r" newvers))
553 (vc-switches 'CVS 'diff))))
554 (if async 1 status))) ; async diff, pessimistic assumption
556 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
558 (defun vc-cvs-annotate-process-filter (process string)
559 (setq string (concat (process-get process 'output) string))
560 (if (not (string-match vc-cvs-annotate-first-line-re string))
561 ;; Still waiting for the first real line.
562 (process-put process 'output string)
563 (let ((vc-filter (process-get process 'vc-filter)))
564 (set-process-filter process vc-filter)
565 (funcall vc-filter process (substring string (match-beginning 0))))))
567 (defun vc-cvs-annotate-command (file buffer &optional revision)
568 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
569 Optional arg REVISION is a revision to annotate from."
570 (vc-cvs-command buffer
571 (if (vc-stay-local-p file)
572 'async 0)
573 file "annotate"
574 (if revision (concat "-r" revision)))
575 ;; Strip the leading few lines.
576 (let ((proc (get-buffer-process buffer)))
577 (if proc
578 ;; If running asynchronously, use a process filter.
579 (progn
580 (process-put proc 'vc-filter (process-filter proc))
581 (set-process-filter proc 'vc-cvs-annotate-process-filter))
582 (with-current-buffer buffer
583 (goto-char (point-min))
584 (re-search-forward vc-cvs-annotate-first-line-re)
585 (delete-region (point-min) (1- (point)))))))
587 (defun vc-cvs-annotate-current-time ()
588 "Return the current time, based at midnight of the current day, and
589 encoded as fractional days."
590 (vc-annotate-convert-time
591 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
593 (defun vc-cvs-annotate-time ()
594 "Return the time of the next annotation (as fraction of days)
595 systime, or nil if there is none."
596 (let* ((bol (point))
597 (cache (get-text-property bol 'vc-cvs-annotate-time))
598 (inhibit-read-only t)
599 (inhibit-modification-hooks t))
600 (cond
601 (cache)
602 ((looking-at
603 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
604 (let ((day (string-to-number (match-string 1)))
605 (month (cdr (assq (intern (match-string 2))
606 '((Jan . 1) (Feb . 2) (Mar . 3)
607 (Apr . 4) (May . 5) (Jun . 6)
608 (Jul . 7) (Aug . 8) (Sep . 9)
609 (Oct . 10) (Nov . 11) (Dec . 12)))))
610 (year (let ((tmp (string-to-number (match-string 3))))
611 ;; Years 0..68 are 2000..2068.
612 ;; Years 69..99 are 1969..1999.
613 (+ (cond ((> 69 tmp) 2000)
614 ((> 100 tmp) 1900)
615 (t 0))
616 tmp))))
617 (put-text-property
618 bol (1+ bol) 'vc-cvs-annotate-time
619 (setq cache (cons
620 ;; Position at end makes for nicer overlay result.
621 ;; Don't put actual buffer pos here, but only relative
622 ;; distance, so we don't ever move backward in the
623 ;; goto-char below, even if the text is moved.
624 (- (match-end 0) (match-beginning 0))
625 (vc-annotate-convert-time
626 (encode-time 0 0 0 day month year))))))))
627 (when cache
628 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
629 (cdr cache)))) ; days (float)
631 (defun vc-cvs-annotate-extract-revision-at-line ()
632 (save-excursion
633 (beginning-of-line)
634 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
635 (line-end-position) t)
636 (match-string-no-properties 1)
637 nil)))
640 ;;; Snapshot system
643 (defun vc-cvs-create-snapshot (dir name branchp)
644 "Assign to DIR's current revision a given NAME.
645 If BRANCHP is non-nil, the name is created as a branch (and the current
646 workspace is immediately moved to that new branch)."
647 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
648 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
650 (defun vc-cvs-retrieve-snapshot (dir name update)
651 "Retrieve a snapshot at and below DIR.
652 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
653 If UPDATE is non-nil, then update (resynch) any affected buffers."
654 (with-current-buffer (get-buffer-create "*vc*")
655 (let ((default-directory dir)
656 (sticky-tag))
657 (erase-buffer)
658 (if (or (not name) (string= name ""))
659 (vc-cvs-command t 0 nil "update")
660 (vc-cvs-command t 0 nil "update" "-r" name)
661 (setq sticky-tag name))
662 (when update
663 (goto-char (point-min))
664 (while (not (eobp))
665 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
666 (let* ((file (expand-file-name (match-string 2) dir))
667 (state (match-string 1))
668 (buffer (find-buffer-visiting file)))
669 (when buffer
670 (cond
671 ((or (string= state "U")
672 (string= state "P"))
673 (vc-file-setprop file 'vc-state 'up-to-date)
674 (vc-file-setprop file 'vc-working-revision nil)
675 (vc-file-setprop file 'vc-checkout-time
676 (nth 5 (file-attributes file))))
677 ((or (string= state "M")
678 (string= state "C"))
679 (vc-file-setprop file 'vc-state 'edited)
680 (vc-file-setprop file 'vc-working-revision nil)
681 (vc-file-setprop file 'vc-checkout-time 0)))
682 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
683 (vc-resynch-buffer file t t))))
684 (forward-line 1))))))
688 ;;; Miscellaneous
691 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
692 "Return non-nil if version backups should be made for FILE.")
694 (defun vc-cvs-check-headers ()
695 "Check if the current file has any headers in it."
696 (save-excursion
697 (goto-char (point-min))
698 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
699 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
703 ;;; Internal functions
706 (defun vc-cvs-root (dir)
707 (vc-find-root dir "CVS" t))
709 (defun vc-cvs-command (buffer okstatus files &rest flags)
710 "A wrapper around `vc-do-command' for use in vc-cvs.el.
711 The difference to vc-do-command is that this function always invokes `cvs',
712 and that it passes `vc-cvs-global-switches' to it before FLAGS."
713 (apply 'vc-do-command buffer okstatus "cvs" files
714 (if (stringp vc-cvs-global-switches)
715 (cons vc-cvs-global-switches flags)
716 (append vc-cvs-global-switches
717 flags))))
719 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
721 (defun vc-cvs-repository-hostname (dirname)
722 "Hostname of the CVS server associated to workarea DIRNAME."
723 (let ((rootname (expand-file-name "CVS/Root" dirname)))
724 (when (file-readable-p rootname)
725 (with-temp-buffer
726 (let ((coding-system-for-read
727 (or file-name-coding-system
728 default-file-name-coding-system)))
729 (vc-insert-file rootname))
730 (goto-char (point-min))
731 (nth 2 (vc-cvs-parse-root
732 (buffer-substring (point)
733 (line-end-position))))))))
735 (defun vc-cvs-parse-root (root)
736 "Split CVS ROOT specification string into a list of fields.
737 A CVS root specification of the form
738 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
739 is converted to a normalized record with the following structure:
740 \(METHOD USER HOSTNAME CVS-ROOT).
741 The default METHOD for a CVS root of the form
742 /path/to/repository
743 is `local'.
744 The default METHOD for a CVS root of the form
745 [USER@]HOSTNAME:/path/to/repository
746 is `ext'.
747 For an empty string, nil is returned (invalid CVS root)."
748 ;; Split CVS root into colon separated fields (0-4).
749 ;; The `x:' makes sure, that leading colons are not lost;
750 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
751 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
752 (len (length root-list))
753 ;; All syntactic varieties will get a proper METHOD.
754 (root-list
755 (cond
756 ((= len 0)
757 ;; Invalid CVS root
758 nil)
759 ((= len 1)
760 ;; Simple PATH => method `local'
761 (cons "local"
762 (cons nil root-list)))
763 ((= len 2)
764 ;; [USER@]HOST:PATH => method `ext'
765 (and (not (equal (car root-list) ""))
766 (cons "ext" root-list)))
767 ((= len 3)
768 ;; :METHOD:PATH
769 (cons (cadr root-list)
770 (cons nil (cddr root-list))))
772 ;; :METHOD:[USER@]HOST:PATH
773 (cdr root-list)))))
774 (if root-list
775 (let ((method (car root-list))
776 (uhost (or (cadr root-list) ""))
777 (root (nth 2 root-list))
778 user host)
779 ;; Split USER@HOST
780 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
781 (setq user (match-string 1 uhost)
782 host (match-string 2 uhost))
783 (setq host uhost))
784 ;; Remove empty HOST
785 (and (equal host "")
786 (setq host))
787 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
788 (and host
789 (equal method "local")
790 (setq root (concat host ":" root) host))
791 ;; Normalize CVS root record
792 (list method user host root)))))
794 ;; XXX: This does not work correctly for subdirectories. "cvs status"
795 ;; information is context sensitive, it contains lines like:
796 ;; cvs status: Examining DIRNAME
797 ;; and the file entries after that don't show the full path.
798 ;; Because of this vc-dired only shows changed files at the top level
799 ;; for CVS.
800 (defun vc-cvs-parse-status (&optional full)
801 "Parse output of \"cvs status\" command in the current buffer.
802 Set file properties accordingly. Unless FULL is t, parse only
803 essential information. Note that this can never set the 'ignored
804 state."
805 (let (file status missing)
806 (goto-char (point-min))
807 (while (looking-at "? \\(.*\\)")
808 (setq file (expand-file-name (match-string 1)))
809 (vc-file-setprop file 'vc-state 'unregistered)
810 (forward-line 1))
811 (when (re-search-forward "^File: " nil t)
812 (when (setq missing (looking-at "no file "))
813 (goto-char (match-end 0)))
814 (cond
815 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
816 (setq file (expand-file-name (match-string 1)))
817 (vc-file-setprop file 'vc-backend 'CVS)
818 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
819 (setq status "Unknown")
820 (setq status (match-string 1)))
821 (when (and full
822 (re-search-forward
823 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
824 \[\t ]+\\([0-9.]+\\)"
825 nil t))
826 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
827 (vc-file-setprop
828 file 'vc-state
829 (cond
830 ((string-match "Up-to-date" status)
831 (vc-file-setprop file 'vc-checkout-time
832 (nth 5 (file-attributes file)))
833 'up-to-date)
834 ((string-match "Locally Modified" status) 'edited)
835 ((string-match "Needs Merge" status) 'needs-merge)
836 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
837 (if missing 'missing 'needs-patch))
838 ((string-match "Locally Added" status) 'added)
839 ((string-match "Locally Removed" status) 'removed)
840 ((string-match "File had conflicts " status) 'conflict)
841 (t 'edited))))))))
843 (defun vc-cvs-dir-state-heuristic (dir)
844 "Find the CVS state of all files in DIR, using only local information."
845 (with-temp-buffer
846 (vc-cvs-get-entries dir)
847 (goto-char (point-min))
848 (while (not (eobp))
849 ;; CVS-removed files are not taken under VC control.
850 (when (looking-at "/\\([^/]*\\)/[^/-]")
851 (let ((file (expand-file-name (match-string 1) dir)))
852 (unless (vc-file-getprop file 'vc-state)
853 (vc-cvs-parse-entry file t))))
854 (forward-line 1))))
856 ;; XXX Experimental function for the vc-dired replacement.
857 (defun vc-cvs-after-dir-status (update-function)
858 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
859 ;; It needs a lot of testing.
860 (let ((status nil)
861 (status-str nil)
862 (file nil)
863 (result nil)
864 (missing nil)
865 (subdir default-directory))
866 (goto-char (point-min))
867 (while
868 ;; Look for either a file entry, an unregistered file, or a
869 ;; directory change.
870 (re-search-forward
871 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: Examining .*\n\\)"
872 nil t)
873 ;; XXX: get rid of narrowing here.
874 (narrow-to-region (match-beginning 0) (match-end 0))
875 (goto-char (point-min))
876 ;; The subdir
877 (when (looking-at "cvs status: Examining \\(.+\\)")
878 (setq subdir (expand-file-name (match-string 1))))
879 ;; Unregistered files
880 (while (looking-at "? \\(.*\\)")
881 (setq file (file-relative-name
882 (expand-file-name (match-string 1) subdir)))
883 (push (list file 'unregistered) result)
884 (forward-line 1))
885 ;; A file entry.
886 (when (re-search-forward "^File: " nil t)
887 (when (setq missing (looking-at "no file "))
888 (goto-char (match-end 0)))
889 (cond
890 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
891 (setq file (file-relative-name
892 (expand-file-name (match-string 1) subdir)))
893 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
894 (push (list file 'unregistered) result)
895 (setq status-str (match-string 1))
896 (setq status
897 (cond
898 ((string-match "Up-to-date" status-str) 'up-to-date)
899 ((string-match "Locally Modified" status-str) 'edited)
900 ((string-match "Needs Merge" status-str) 'needs-merge)
901 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
902 (if missing 'missing 'needs-patch))
903 ((string-match "Locally Added" status-str) 'added)
904 ((string-match "Locally Removed" status-str) 'removed)
905 ((string-match "File had conflicts " status-str) 'conflict)
906 (t 'edited)))
907 (unless (eq status 'up-to-date)
908 (push (list file status) result))))))
909 (goto-char (point-max))
910 (widen))
911 (funcall update-function result))
912 ;; Alternative implementation: use the "update" command instead of
913 ;; the "status" command.
914 ;; (let ((result nil)
915 ;; (translation '((?? . unregistered)
916 ;; (?A . added)
917 ;; (?C . conflict)
918 ;; (?M . edited)
919 ;; (?P . needs-merge)
920 ;; (?R . removed)
921 ;; (?U . needs-patch))))
922 ;; (goto-char (point-min))
923 ;; (while (not (eobp))
924 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
925 ;; (push (list (match-string 1)
926 ;; (cdr (assoc (char-after) translation)))
927 ;; result)
928 ;; (cond
929 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
930 ;; ;; Format is:
931 ;; ;; cvs update: warning: FILENAME was lost
932 ;; ;; U FILENAME
933 ;; (push (list (match-string 1) 'missing) result)
934 ;; ;; Skip the "U" line
935 ;; (forward-line 1))
936 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
937 ;; (push (list (match-string 1) 'unregistered) result))))
938 ;; (forward-line 1))
939 ;; (funcall update-function result)))
942 (defun vc-cvs-dir-status (dir update-function)
943 "Create a list of conses (file . state) for DIR."
944 (vc-cvs-command (current-buffer) 'async dir "status")
945 ;; Alternative implementation: use the "update" command instead of
946 ;; the "status" command.
947 ;; (vc-cvs-command (current-buffer) 'async
948 ;; (file-relative-name dir)
949 ;; "-f" "-n" "update" "-d" "-P")
950 (vc-exec-after
951 `(vc-cvs-after-dir-status (quote ,update-function))))
953 (defun vc-cvs-status-extra-headers (dir)
954 (concat
955 ;; FIXME: see how PCL-CVS gets the data to print all these
956 (propertize "Module : " 'face 'font-lock-type-face)
957 (propertize "ADD CODE TO PRINT THE MODULE\n"
958 'face 'font-lock-warning-face)
959 (propertize "Repository : " 'face 'font-lock-type-face)
960 (propertize "ADD CODE TO PRINT THE REPOSITORY\n"
961 'face 'font-lock-warning-face)
962 (propertize "Branch : " 'face 'font-lock-type-face)
963 (propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
964 'face 'font-lock-warning-face)))
966 (defun vc-cvs-get-entries (dir)
967 "Insert the CVS/Entries file from below DIR into the current buffer.
968 This function ensures that the correct coding system is used for that,
969 which may not be the one that is used for the files' contents.
970 CVS/Entries should only be accessed through this function."
971 (let ((coding-system-for-read (or file-name-coding-system
972 default-file-name-coding-system)))
973 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
975 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
976 "Return non-nil if TAG is a valid symbolic tag name."
977 ;; According to the CVS manual, a valid symbolic tag must start with
978 ;; an uppercase or lowercase letter and can contain uppercase and
979 ;; lowercase letters, digits, `-', and `_'.
980 (and (string-match "^[a-zA-Z]" tag)
981 (not (string-match "[^a-z0-9A-Z-_]" tag))))
983 (defun vc-cvs-valid-revision-number-p (tag)
984 "Return non-nil if TAG is a valid revision number."
985 (and (string-match "^[0-9]" tag)
986 (not (string-match "[^0-9.]" tag))))
988 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
989 "Parse and return the sticky tag as a string.
990 `match-data' is protected."
991 (let ((data (match-data))
992 (tag)
993 (type (cond ((string= match-type "D") 'date)
994 ((string= match-type "T")
995 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
996 'symbolic-name
997 'revision-number))
998 (t nil))))
999 (unwind-protect
1000 (progn
1001 (cond
1002 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1003 ((eq type 'date)
1004 (string-match
1005 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1006 match-tag)
1007 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1008 (month (string-to-number (match-string 2 match-tag)))
1009 (day (string-to-number (match-string 3 match-tag)))
1010 (hour (string-to-number (match-string 4 match-tag)))
1011 (min (string-to-number (match-string 5 match-tag)))
1012 (sec (string-to-number (match-string 6 match-tag)))
1013 ;; Years 0..68 are 2000..2068.
1014 ;; Years 69..99 are 1969..1999.
1015 (year (+ (cond ((> 69 year-tmp) 2000)
1016 ((> 100 year-tmp) 1900)
1017 (t 0))
1018 year-tmp)))
1019 (setq tag (encode-time sec min hour day month year))))
1020 ;; Sticky Tag name or revision number
1021 ((eq type 'symbolic-name) (setq tag match-tag))
1022 ((eq type 'revision-number) (setq tag match-tag))
1023 ;; Default is no sticky tag at all
1024 (t nil))
1025 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1026 ((eq vc-cvs-sticky-tag-display t)
1027 (cond ((eq type 'date) (format-time-string
1028 vc-cvs-sticky-date-format-string
1029 tag))
1030 ((eq type 'symbolic-name) tag)
1031 ((eq type 'revision-number) tag)
1032 (t nil)))
1033 ((functionp vc-cvs-sticky-tag-display)
1034 (funcall vc-cvs-sticky-tag-display tag type))
1035 (t nil)))
1037 (set-match-data data))))
1039 (defun vc-cvs-parse-entry (file &optional set-state)
1040 "Parse a line from CVS/Entries.
1041 Compare modification time to that of the FILE, set file properties
1042 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1043 is non-nil."
1044 (cond
1045 ;; entry for a "locally added" file (not yet committed)
1046 ((looking-at "/[^/]+/0/")
1047 (vc-file-setprop file 'vc-backend 'CVS)
1048 (vc-file-setprop file 'vc-checkout-time 0)
1049 (vc-file-setprop file 'vc-working-revision "0")
1050 (if set-state (vc-file-setprop file 'vc-state 'added)))
1051 ;; normal entry
1052 ((looking-at
1053 (concat "/[^/]+"
1054 ;; revision
1055 "/\\([^/]*\\)"
1056 ;; timestamp and optional conflict field
1057 "/\\([^/]*\\)/"
1058 ;; options
1059 "\\([^/]*\\)/"
1060 ;; sticky tag
1061 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1062 "\\(.*\\)")) ;Sticky tag
1063 (vc-file-setprop file 'vc-backend 'CVS)
1064 (vc-file-setprop file 'vc-working-revision (match-string 1))
1065 (vc-file-setprop file 'vc-cvs-sticky-tag
1066 (vc-cvs-parse-sticky-tag (match-string 4)
1067 (match-string 5)))
1068 ;; Compare checkout time and modification time.
1069 ;; This is intentionally different from the algorithm that CVS uses
1070 ;; (which is based on textual comparison), because there can be problems
1071 ;; generating a time string that looks exactly like the one from CVS.
1072 (let ((mtime (nth 5 (file-attributes file))))
1073 (require 'parse-time)
1074 (let ((parsed-time
1075 (parse-time-string (concat (match-string 2) " +0000"))))
1076 (cond ((and (not (string-match "\\+" (match-string 2)))
1077 (car parsed-time)
1078 (equal mtime (apply 'encode-time parsed-time)))
1079 (vc-file-setprop file 'vc-checkout-time mtime)
1080 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1082 (vc-file-setprop file 'vc-checkout-time 0)
1083 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
1085 ;; Completion of revision names.
1086 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1087 ;; `cvs log' so I can list all the revision numbers rather than only
1088 ;; tag names.
1090 (defun vc-cvs-revision-table (file)
1091 (let ((default-directory (file-name-directory file))
1092 (res nil))
1093 (with-temp-buffer
1094 (vc-cvs-command t nil file "log")
1095 (goto-char (point-min))
1096 (when (re-search-forward "^symbolic names:\n" nil t)
1097 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1098 (push (cons (match-string 1) (match-string 2)) res)
1099 (forward-line 1)))
1100 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1101 (push (match-string 1) res))
1102 res)))
1104 (defun vc-cvs-revision-completion-table (files)
1105 (lexical-let ((files files)
1106 table)
1107 (setq table (lazy-completion-table
1108 table (lambda () (vc-cvs-revision-table (car files)))))
1109 table))
1112 (provide 'vc-cvs)
1114 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
1115 ;;; vc-cvs.el ends here