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