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