(vc-(m)cvs-create-repo): Remove.
[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 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 2, 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 ;;;
39 ;;; Customization options
40 ;;;
41
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)
51
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)
63
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)
73
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)
79
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)
87
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.
92
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)
107
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)
115
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.
124
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.
127
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\")))
133
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.
138
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.
149
150 See also variable `vc-cvs-sticky-date-format-string'."
151 :type '(choice boolean function)
152 :version "22.1"
153 :group 'vc)
154
155 ;;;
156 ;;; Internal variables
157 ;;;
158
159
160 ;;;
161 ;;; State-querying functions
162 ;;;
163
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)))
169
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 ;; CVS-removed files are not taken under VC control.
183 (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
184 (beginning-of-line)
185 (vc-cvs-parse-entry file)
186 t)
187 (t nil)))
188 nil)))
189
190 (defun vc-cvs-state (file)
191 "CVS-specific version of `vc-state'."
192 (if (vc-stay-local-p file)
193 (let ((state (vc-file-getprop file 'vc-state)))
194 ;; If we should stay local, use the heuristic but only if
195 ;; we don't have a more precise state already available.
196 (if (memq state '(up-to-date edited nil))
197 (vc-cvs-state-heuristic file)
198 state))
199 (with-temp-buffer
200 (cd (file-name-directory file))
201 (vc-cvs-command t 0 file "status")
202 (vc-cvs-parse-status t))))
203
204 (defun vc-cvs-state-heuristic (file)
205 "CVS-specific state heuristic."
206 ;; If the file has not changed since checkout, consider it `up-to-date'.
207 ;; Otherwise consider it `edited'.
208 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
209 (lastmod (nth 5 (file-attributes file))))
210 (if (equal checkout-time lastmod)
211 'up-to-date
212 'edited)))
213
214 (defun vc-cvs-dir-state (dir)
215 "Find the CVS state of all files in DIR."
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 (vc-cvs-command t 0 nil "status" "-l")
225 (goto-char (point-min))
226 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
227 (narrow-to-region (match-beginning 0) (match-end 0))
228 (vc-cvs-parse-status)
229 (goto-char (point-max))
230 (widen)))))))
231
232 (defun vc-cvs-workfile-version (file)
233 "CVS-specific version of `vc-workfile-version'."
234 ;; There is no need to consult RCS headers under CVS, because we
235 ;; get the workfile version for free when we recognize that a file
236 ;; is registered in CVS.
237 (vc-cvs-registered file)
238 (vc-file-getprop file 'vc-workfile-version))
239
240 (defun vc-cvs-checkout-model (file)
241 "CVS-specific version of `vc-checkout-model'."
242 (if (getenv "CVSREAD")
243 'announce
244 (let ((attrib (file-attributes file)))
245 (if (and attrib ;; don't check further if FILE doesn't exist
246 ;; If the file is not writable (despite CVSREAD being
247 ;; undefined), this is probably because the file is being
248 ;; "watched" by other developers.
249 ;; (If vc-mistrust-permissions was t, we actually shouldn't
250 ;; trust this, but there is no other way to learn this from CVS
251 ;; at the moment (version 1.9).)
252 (string-match "r-..-..-." (nth 8 attrib)))
253 'announce
254 'implicit))))
255
256 (defun vc-cvs-mode-line-string (file)
257 "Return string for placement into the modeline for FILE.
258 Compared to the default implementation, this function does two things:
259 Handle the special case of a CVS file that is added but not yet
260 committed and support display of sticky tags."
261 (let ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
262 (string (if (string= (vc-workfile-version file) "0")
263 ;; A file that is added but not yet committed.
264 "CVS @@"
265 (vc-default-mode-line-string 'CVS file))))
266 (if (zerop (length sticky-tag))
267 string
268 (concat string "[" sticky-tag "]"))))
269
270 (defun vc-cvs-dired-state-info (file)
271 "CVS-specific version of `vc-dired-state-info'."
272 (let ((cvs-state (vc-state file)))
273 (cond ((eq cvs-state 'edited)
274 (if (equal (vc-workfile-version file) "0")
275 "(added)" "(modified)"))
276 ((eq cvs-state 'needs-patch) "(patch)")
277 ((eq cvs-state 'needs-merge) "(merge)"))))
278
279
280 ;;;
281 ;;; State-changing functions
282 ;;;
283
284 (defun vc-cvs-register (files &optional rev comment)
285 "Register FILES into the CVS version-control system.
286 COMMENT can be used to provide an initial description of FILES.
287
288 `vc-register-switches' and `vc-cvs-register-switches' are passed to
289 the CVS command (in that order)."
290 (when (and (not (vc-cvs-responsible-p file))
291 (vc-cvs-could-register file))
292 ;; Register the directory if needed.
293 (vc-cvs-register (directory-file-name (file-name-directory file))))
294 (apply 'vc-cvs-command nil 0 files
295 "add"
296 (and comment (string-match "[^\t\n ]" comment)
297 (concat "-m" comment))
298 (vc-switches 'CVS 'register)))
299
300 (defun vc-cvs-responsible-p (file)
301 "Return non-nil if CVS thinks it is responsible for FILE."
302 (file-directory-p (expand-file-name "CVS"
303 (if (file-directory-p file)
304 file
305 (file-name-directory file)))))
306
307 (defun vc-cvs-could-register (file)
308 "Return non-nil if FILE could be registered in CVS.
309 This is only possible if CVS is managing FILE's directory or one of
310 its parents."
311 (let ((dir file))
312 (while (and (stringp dir)
313 (not (equal dir (setq dir (file-name-directory dir))))
314 dir)
315 (setq dir (if (file-directory-p
316 (expand-file-name "CVS/Entries" dir))
317 t (directory-file-name dir))))
318 (eq dir t)))
319
320 (defun vc-cvs-checkin (files rev comment)
321 "CVS-specific version of `vc-backend-checkin'."
322 (unless (or (not rev) (vc-cvs-valid-version-number-p rev))
323 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
324 (error "%s is not a valid symbolic tag name" rev)
325 ;; If the input revison is a valid symbolic tag name, we create it
326 ;; as a branch, commit and switch to it.
327 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
328 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
329 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
330 files)))
331 (let ((status (apply 'vc-cvs-command nil 1 files
332 "ci" (if rev (concat "-r" rev))
333 (concat "-m" comment)
334 (vc-switches 'CVS 'checkin))))
335 (set-buffer "*vc*")
336 (goto-char (point-min))
337 (when (not (zerop status))
338 ;; Check checkin problem.
339 (cond
340 ((re-search-forward "Up-to-date check failed" nil t)
341 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
342 files)
343 (error (substitute-command-keys
344 (concat "Up-to-date check failed: "
345 "type \\[vc-next-action] to merge in changes"))))
346 (t
347 (pop-to-buffer (current-buffer))
348 (goto-char (point-min))
349 (shrink-window-if-larger-than-buffer)
350 (error "Check-in failed"))))
351 ;; Single-file commit? Then update the version by parsing the buffer.
352 ;; Otherwise we can't necessarily tell what goes with what; clear
353 ;; its properties so they have to be refetched.
354 (if (= (length files) 1)
355 (vc-file-setprop
356 (car files) 'vc-workfile-version
357 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
358 (mapc (lambda (file) (vc-file-clearprops file)) files))
359 ;; Anyway, forget the checkout model of the file, because we might have
360 ;; guessed wrong when we found the file. After commit, we can
361 ;; tell it from the permissions of the file (see
362 ;; vc-cvs-checkout-model).
363 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
364 files)
365
366 ;; if this was an explicit check-in (does not include creation of
367 ;; a branch), remove the sticky tag.
368 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
369 (vc-cvs-command nil 0 files "update" "-A"))))
370
371 (defun vc-cvs-find-version (file rev buffer)
372 (apply 'vc-cvs-command
373 buffer 0 file
374 "-Q" ; suppress diagnostic output
375 "update"
376 (and rev (not (string= rev ""))
377 (concat "-r" rev))
378 "-p"
379 (vc-switches 'CVS 'checkout)))
380
381 (defun vc-cvs-checkout (file &optional editable rev)
382 "Checkout a revision of FILE into the working area.
383 EDITABLE non-nil means that the file should be writable.
384 REV is the revision to check out."
385 (message "Checking out %s..." file)
386 ;; Change buffers to get local value of vc-checkout-switches.
387 (with-current-buffer (or (get-file-buffer file) (current-buffer))
388 (if (and (file-exists-p file) (not rev))
389 ;; If no revision was specified, just make the file writable
390 ;; if necessary (using `cvs-edit' if requested).
391 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
392 (if vc-cvs-use-edit
393 (vc-cvs-command nil 0 file "edit")
394 (set-file-modes file (logior (file-modes file) 128))
395 (if (equal file buffer-file-name) (toggle-read-only -1))))
396 ;; Check out a particular version (or recreate the file).
397 (vc-file-setprop file 'vc-workfile-version nil)
398 (apply 'vc-cvs-command nil 0 file
399 (and editable "-w")
400 "update"
401 (when rev
402 (unless (eq rev t)
403 ;; default for verbose checkout: clear the
404 ;; sticky tag so that the actual update will
405 ;; get the head of the trunk
406 (if (string= rev "")
407 "-A"
408 (concat "-r" rev))))
409 (vc-switches 'CVS 'checkout)))
410 (vc-mode-line file))
411 (message "Checking out %s...done" file))
412
413 (defun vc-cvs-delete-file (file)
414 (vc-cvs-command nil 0 file "remove" "-f")
415 (vc-cvs-command nil 0 file "commit" "-mRemoved."))
416
417 (defun vc-cvs-revert (file &optional contents-done)
418 "Revert FILE to the version on which it was based."
419 (vc-default-revert 'CVS file contents-done)
420 (unless (eq (vc-checkout-model file) 'implicit)
421 (if vc-cvs-use-edit
422 (vc-cvs-command nil 0 file "unedit")
423 ;; Make the file read-only by switching off all w-bits
424 (set-file-modes file (logand (file-modes file) 3950)))))
425
426 (defun vc-cvs-merge (file first-version &optional second-version)
427 "Merge changes into current working copy of FILE.
428 The changes are between FIRST-VERSION and SECOND-VERSION."
429 (vc-cvs-command nil 0 file
430 "update" "-kk"
431 (concat "-j" first-version)
432 (concat "-j" second-version))
433 (vc-file-setprop file 'vc-state 'edited)
434 (with-current-buffer (get-buffer "*vc*")
435 (goto-char (point-min))
436 (if (re-search-forward "conflicts during merge" nil t)
437 1 ; signal error
438 0))) ; signal success
439
440 (defun vc-cvs-merge-news (file)
441 "Merge in any new changes made to FILE."
442 (message "Merging changes into %s..." file)
443 ;; (vc-file-setprop file 'vc-workfile-version nil)
444 (vc-file-setprop file 'vc-checkout-time 0)
445 (vc-cvs-command nil 0 file "update")
446 ;; Analyze the merge result reported by CVS, and set
447 ;; file properties accordingly.
448 (with-current-buffer (get-buffer "*vc*")
449 (goto-char (point-min))
450 ;; get new workfile version
451 (if (re-search-forward
452 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
453 (vc-file-setprop file 'vc-workfile-version (match-string 1))
454 (vc-file-setprop file 'vc-workfile-version nil))
455 ;; get file status
456 (prog1
457 (if (eq (buffer-size) 0)
458 0 ;; there were no news; indicate success
459 (if (re-search-forward
460 (concat "^\\([CMUP] \\)?"
461 (regexp-quote (file-name-nondirectory file))
462 "\\( already contains the differences between \\)?")
463 nil t)
464 (cond
465 ;; Merge successful, we are in sync with repository now
466 ((or (match-string 2)
467 (string= (match-string 1) "U ")
468 (string= (match-string 1) "P "))
469 (vc-file-setprop file 'vc-state 'up-to-date)
470 (vc-file-setprop file 'vc-checkout-time
471 (nth 5 (file-attributes file)))
472 0);; indicate success to the caller
473 ;; Merge successful, but our own changes are still in the file
474 ((string= (match-string 1) "M ")
475 (vc-file-setprop file 'vc-state 'edited)
476 0);; indicate success to the caller
477 ;; Conflicts detected!
478 (t
479 (vc-file-setprop file 'vc-state 'edited)
480 1);; signal the error to the caller
481 )
482 (pop-to-buffer "*vc*")
483 (error "Couldn't analyze cvs update result")))
484 (message "Merging changes into %s...done" file))))
485
486
487 ;;;
488 ;;; History functions
489 ;;;
490
491 (defun vc-cvs-print-log (files &optional buffer)
492 "Get change log associated with FILE."
493 (vc-cvs-command
494 buffer
495 (if (and (vc-stay-local-p files) (fboundp 'start-process)) 'async 0)
496 files "log"))
497
498 (defun vc-cvs-wash-log ()
499 "Remove all non-comment information from log output."
500 (vc-call-backend 'RCS 'wash-log)
501 nil)
502
503 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
504 "Get a difference report using CVS between two versions of FILE."
505 (let* ((async (and (not vc-disable-async-diff)
506 (vc-stay-local-p files)
507 (fboundp 'start-process)))
508 (status (apply 'vc-cvs-command (or buffer "*vc-diff*")
509 (if async 'async 1)
510 files "diff"
511 (and oldvers (concat "-r" oldvers))
512 (and newvers (concat "-r" newvers))
513 (vc-switches 'CVS 'diff))))
514 (if async 1 status))) ; async diff, pessimistic assumption
515
516 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
517 "Diff all files at and below DIR."
518 (with-current-buffer "*vc-diff*"
519 (setq default-directory dir)
520 (if (vc-stay-local-p dir)
521 ;; local diff: do it filewise, and only for files that are modified
522 (vc-file-tree-walk
523 dir
524 (lambda (f)
525 (vc-exec-after
526 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
527 ;; possible optimization: fetch the state of all files
528 ;; in the tree via vc-cvs-dir-state-heuristic
529 (unless (vc-up-to-date-p ',f)
530 (message "Looking at %s" ',f)
531 (vc-diff-internal ',f ',rev1 ',rev2))))))
532 ;; cvs diff: use a single call for the entire tree
533 (let ((coding-system-for-read
534 (or coding-system-for-read 'undecided)))
535 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
536 (and rev1 (concat "-r" rev1))
537 (and rev2 (concat "-r" rev2))
538 (vc-switches 'CVS 'diff))))))
539
540 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
541
542 (defun vc-cvs-annotate-process-filter (process string)
543 (setq string (concat (process-get process 'output) string))
544 (if (not (string-match vc-cvs-annotate-first-line-re string))
545 ;; Still waiting for the first real line.
546 (process-put process 'output string)
547 (let ((vc-filter (process-get process 'vc-filter)))
548 (set-process-filter process vc-filter)
549 (funcall vc-filter process (substring string (match-beginning 0))))))
550
551 (defun vc-cvs-annotate-command (file buffer &optional version)
552 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
553 Optional arg VERSION is a version to annotate from."
554 (vc-cvs-command buffer
555 (if (and (vc-stay-local-p file) (fboundp 'start-process))
556 'async 0)
557 file "annotate"
558 (if version (concat "-r" version)))
559 ;; Strip the leading few lines.
560 (let ((proc (get-buffer-process buffer)))
561 (if proc
562 ;; If running asynchronously, use a process filter.
563 (progn
564 (process-put proc 'vc-filter (process-filter proc))
565 (set-process-filter proc 'vc-cvs-annotate-process-filter))
566 (with-current-buffer buffer
567 (goto-char (point-min))
568 (re-search-forward vc-cvs-annotate-first-line-re)
569 (delete-region (point-min) (1- (point)))))))
570
571 (defun vc-cvs-annotate-current-time ()
572 "Return the current time, based at midnight of the current day, and
573 encoded as fractional days."
574 (vc-annotate-convert-time
575 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
576
577 (defun vc-cvs-annotate-time ()
578 "Return the time of the next annotation (as fraction of days)
579 systime, or nil if there is none."
580 (let* ((bol (point))
581 (cache (get-text-property bol 'vc-cvs-annotate-time))
582 buffer-read-only)
583 (cond
584 (cache)
585 ((looking-at
586 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
587 (let ((day (string-to-number (match-string 1)))
588 (month (cdr (assq (intern (match-string 2))
589 '((Jan . 1) (Feb . 2) (Mar . 3)
590 (Apr . 4) (May . 5) (Jun . 6)
591 (Jul . 7) (Aug . 8) (Sep . 9)
592 (Oct . 10) (Nov . 11) (Dec . 12)))))
593 (year (let ((tmp (string-to-number (match-string 3))))
594 ;; Years 0..68 are 2000..2068.
595 ;; Years 69..99 are 1969..1999.
596 (+ (cond ((> 69 tmp) 2000)
597 ((> 100 tmp) 1900)
598 (t 0))
599 tmp))))
600 (put-text-property
601 bol (1+ bol) 'vc-cvs-annotate-time
602 (setq cache (cons
603 ;; Position at end makes for nicer overlay result.
604 (match-end 0)
605 (vc-annotate-convert-time
606 (encode-time 0 0 0 day month year))))))))
607 (when cache
608 (goto-char (car cache)) ; fontify from here to eol
609 (cdr cache)))) ; days (float)
610
611 (defun vc-cvs-annotate-extract-revision-at-line ()
612 (save-excursion
613 (beginning-of-line)
614 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
615 (line-end-position) t)
616 (match-string-no-properties 1)
617 nil)))
618
619 ;;;
620 ;;; Snapshot system
621 ;;;
622
623 (defun vc-cvs-create-snapshot (dir name branchp)
624 "Assign to DIR's current version a given NAME.
625 If BRANCHP is non-nil, the name is created as a branch (and the current
626 workspace is immediately moved to that new branch)."
627 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
628 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
629
630 (defun vc-cvs-retrieve-snapshot (dir name update)
631 "Retrieve a snapshot at and below DIR.
632 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
633 If UPDATE is non-nil, then update (resynch) any affected buffers."
634 (with-current-buffer (get-buffer-create "*vc*")
635 (let ((default-directory dir)
636 (sticky-tag))
637 (erase-buffer)
638 (if (or (not name) (string= name ""))
639 (vc-cvs-command t 0 nil "update")
640 (vc-cvs-command t 0 nil "update" "-r" name)
641 (setq sticky-tag name))
642 (when update
643 (goto-char (point-min))
644 (while (not (eobp))
645 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
646 (let* ((file (expand-file-name (match-string 2) dir))
647 (state (match-string 1))
648 (buffer (find-buffer-visiting file)))
649 (when buffer
650 (cond
651 ((or (string= state "U")
652 (string= state "P"))
653 (vc-file-setprop file 'vc-state 'up-to-date)
654 (vc-file-setprop file 'vc-workfile-version nil)
655 (vc-file-setprop file 'vc-checkout-time
656 (nth 5 (file-attributes file))))
657 ((or (string= state "M")
658 (string= state "C"))
659 (vc-file-setprop file 'vc-state 'edited)
660 (vc-file-setprop file 'vc-workfile-version nil)
661 (vc-file-setprop file 'vc-checkout-time 0)))
662 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
663 (vc-resynch-buffer file t t))))
664 (forward-line 1))))))
665
666
667 ;;;
668 ;;; Miscellaneous
669 ;;;
670
671 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
672 "Return non-nil if version backups should be made for FILE.")
673
674 (defun vc-cvs-check-headers ()
675 "Check if the current file has any headers in it."
676 (save-excursion
677 (goto-char (point-min))
678 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
679 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
680
681
682 ;;;
683 ;;; Internal functions
684 ;;;
685
686 (defun vc-cvs-command (buffer okstatus files &rest flags)
687 "A wrapper around `vc-do-command' for use in vc-cvs.el.
688 The difference to vc-do-command is that this function always invokes `cvs',
689 and that it passes `vc-cvs-global-switches' to it before FLAGS."
690 (apply 'vc-do-command buffer okstatus "cvs" files
691 (if (stringp vc-cvs-global-switches)
692 (cons vc-cvs-global-switches flags)
693 (append vc-cvs-global-switches
694 flags))))
695
696 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
697
698 (defun vc-cvs-repository-hostname (dirname)
699 "Hostname of the CVS server associated to workarea DIRNAME."
700 (let ((rootname (expand-file-name "CVS/Root" dirname)))
701 (when (file-readable-p rootname)
702 (with-temp-buffer
703 (let ((coding-system-for-read
704 (or file-name-coding-system
705 default-file-name-coding-system)))
706 (vc-insert-file rootname))
707 (goto-char (point-min))
708 (nth 2 (vc-cvs-parse-root
709 (buffer-substring (point)
710 (line-end-position))))))))
711
712 (defun vc-cvs-parse-root (root)
713 "Split CVS ROOT specification string into a list of fields.
714 A CVS root specification of the form
715 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
716 is converted to a normalized record with the following structure:
717 \(METHOD USER HOSTNAME CVS-ROOT).
718 The default METHOD for a CVS root of the form
719 /path/to/repository
720 is `local'.
721 The default METHOD for a CVS root of the form
722 [USER@]HOSTNAME:/path/to/repository
723 is `ext'.
724 For an empty string, nil is returned (invalid CVS root)."
725 ;; Split CVS root into colon separated fields (0-4).
726 ;; The `x:' makes sure, that leading colons are not lost;
727 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
728 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
729 (len (length root-list))
730 ;; All syntactic varieties will get a proper METHOD.
731 (root-list
732 (cond
733 ((= len 0)
734 ;; Invalid CVS root
735 nil)
736 ((= len 1)
737 ;; Simple PATH => method `local'
738 (cons "local"
739 (cons nil root-list)))
740 ((= len 2)
741 ;; [USER@]HOST:PATH => method `ext'
742 (and (not (equal (car root-list) ""))
743 (cons "ext" root-list)))
744 ((= len 3)
745 ;; :METHOD:PATH
746 (cons (cadr root-list)
747 (cons nil (cddr root-list))))
748 (t
749 ;; :METHOD:[USER@]HOST:PATH
750 (cdr root-list)))))
751 (if root-list
752 (let ((method (car root-list))
753 (uhost (or (cadr root-list) ""))
754 (root (nth 2 root-list))
755 user host)
756 ;; Split USER@HOST
757 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
758 (setq user (match-string 1 uhost)
759 host (match-string 2 uhost))
760 (setq host uhost))
761 ;; Remove empty HOST
762 (and (equal host "")
763 (setq host))
764 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
765 (and host
766 (equal method "local")
767 (setq root (concat host ":" root) host))
768 ;; Normalize CVS root record
769 (list method user host root)))))
770
771 (defun vc-cvs-parse-status (&optional full)
772 "Parse output of \"cvs status\" command in the current buffer.
773 Set file properties accordingly. Unless FULL is t, parse only
774 essential information."
775 (let (file status)
776 (goto-char (point-min))
777 (if (re-search-forward "^File: " nil t)
778 (cond
779 ((looking-at "no file") nil)
780 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
781 (setq file (expand-file-name (match-string 1)))
782 (vc-file-setprop file 'vc-backend 'CVS)
783 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
784 (setq status "Unknown")
785 (setq status (match-string 1)))
786 (if (and full
787 (re-search-forward
788 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
789 \[\t ]+\\([0-9.]+\\)"
790 nil t))
791 (vc-file-setprop file 'vc-latest-version (match-string 2)))
792 (vc-file-setprop
793 file 'vc-state
794 (cond
795 ((string-match "Up-to-date" status)
796 (vc-file-setprop file 'vc-checkout-time
797 (nth 5 (file-attributes file)))
798 'up-to-date)
799 ((string-match "Locally Modified" status) 'edited)
800 ((string-match "Needs Merge" status) 'needs-merge)
801 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
802 (t 'edited))))))))
803
804 (defun vc-cvs-dir-state-heuristic (dir)
805 "Find the CVS state of all files in DIR, using only local information."
806 (with-temp-buffer
807 (vc-cvs-get-entries dir)
808 (goto-char (point-min))
809 (while (not (eobp))
810 ;; CVS-removed files are not taken under VC control.
811 (when (looking-at "/\\([^/]*\\)/[^/-]")
812 (let ((file (expand-file-name (match-string 1) dir)))
813 (unless (vc-file-getprop file 'vc-state)
814 (vc-cvs-parse-entry file t))))
815 (forward-line 1))))
816
817 (defun vc-cvs-get-entries (dir)
818 "Insert the CVS/Entries file from below DIR into the current buffer.
819 This function ensures that the correct coding system is used for that,
820 which may not be the one that is used for the files' contents.
821 CVS/Entries should only be accessed through this function."
822 (let ((coding-system-for-read (or file-name-coding-system
823 default-file-name-coding-system)))
824 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
825
826 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
827 "Return non-nil if TAG is a valid symbolic tag name."
828 ;; According to the CVS manual, a valid symbolic tag must start with
829 ;; an uppercase or lowercase letter and can contain uppercase and
830 ;; lowercase letters, digits, `-', and `_'.
831 (and (string-match "^[a-zA-Z]" tag)
832 (not (string-match "[^a-z0-9A-Z-_]" tag))))
833
834 (defun vc-cvs-valid-version-number-p (tag)
835 "Return non-nil if TAG is a valid version number."
836 (and (string-match "^[0-9]" tag)
837 (not (string-match "[^0-9.]" tag))))
838
839 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
840 "Parse and return the sticky tag as a string.
841 `match-data' is protected."
842 (let ((data (match-data))
843 (tag)
844 (type (cond ((string= match-type "D") 'date)
845 ((string= match-type "T")
846 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
847 'symbolic-name
848 'revision-number))
849 (t nil))))
850 (unwind-protect
851 (progn
852 (cond
853 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
854 ((eq type 'date)
855 (string-match
856 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
857 match-tag)
858 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
859 (month (string-to-number (match-string 2 match-tag)))
860 (day (string-to-number (match-string 3 match-tag)))
861 (hour (string-to-number (match-string 4 match-tag)))
862 (min (string-to-number (match-string 5 match-tag)))
863 (sec (string-to-number (match-string 6 match-tag)))
864 ;; Years 0..68 are 2000..2068.
865 ;; Years 69..99 are 1969..1999.
866 (year (+ (cond ((> 69 year-tmp) 2000)
867 ((> 100 year-tmp) 1900)
868 (t 0))
869 year-tmp)))
870 (setq tag (encode-time sec min hour day month year))))
871 ;; Sticky Tag name or revision number
872 ((eq type 'symbolic-name) (setq tag match-tag))
873 ((eq type 'revision-number) (setq tag match-tag))
874 ;; Default is no sticky tag at all
875 (t nil))
876 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
877 ((eq vc-cvs-sticky-tag-display t)
878 (cond ((eq type 'date) (format-time-string
879 vc-cvs-sticky-date-format-string
880 tag))
881 ((eq type 'symbolic-name) tag)
882 ((eq type 'revision-number) tag)
883 (t nil)))
884 ((functionp vc-cvs-sticky-tag-display)
885 (funcall vc-cvs-sticky-tag-display tag type))
886 (t nil)))
887
888 (set-match-data data))))
889
890 (defun vc-cvs-parse-entry (file &optional set-state)
891 "Parse a line from CVS/Entries.
892 Compare modification time to that of the FILE, set file properties
893 accordingly. However, `vc-state' is set only if optional arg SET-STATE
894 is non-nil."
895 (cond
896 ;; entry for a "locally added" file (not yet committed)
897 ((looking-at "/[^/]+/0/")
898 (vc-file-setprop file 'vc-checkout-time 0)
899 (vc-file-setprop file 'vc-workfile-version "0")
900 (if set-state (vc-file-setprop file 'vc-state 'edited)))
901 ;; normal entry
902 ((looking-at
903 (concat "/[^/]+"
904 ;; revision
905 "/\\([^/]*\\)"
906 ;; timestamp and optional conflict field
907 "/\\([^/]*\\)/"
908 ;; options
909 "\\([^/]*\\)/"
910 ;; sticky tag
911 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
912 "\\(.*\\)")) ;Sticky tag
913 (vc-file-setprop file 'vc-workfile-version (match-string 1))
914 (vc-file-setprop file 'vc-cvs-sticky-tag
915 (vc-cvs-parse-sticky-tag (match-string 4)
916 (match-string 5)))
917 ;; Compare checkout time and modification time.
918 ;; This is intentionally different from the algorithm that CVS uses
919 ;; (which is based on textual comparison), because there can be problems
920 ;; generating a time string that looks exactly like the one from CVS.
921 (let ((mtime (nth 5 (file-attributes file))))
922 (require 'parse-time)
923 (let ((parsed-time
924 (parse-time-string (concat (match-string 2) " +0000"))))
925 (cond ((and (not (string-match "\\+" (match-string 2)))
926 (car parsed-time)
927 (equal mtime (apply 'encode-time parsed-time)))
928 (vc-file-setprop file 'vc-checkout-time mtime)
929 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
930 (t
931 (vc-file-setprop file 'vc-checkout-time 0)
932 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
933
934 ;; Completion of revision names.
935 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
936 ;; `cvs log' so I can list all the revision numbers rather than only
937 ;; tag names.
938
939 (defun vc-cvs-revision-table (file)
940 (let ((default-directory (file-name-directory file))
941 (res nil))
942 (with-temp-buffer
943 (vc-cvs-command t nil file "log")
944 (goto-char (point-min))
945 (when (re-search-forward "^symbolic names:\n" nil t)
946 (while (looking-at "^ \\(.*\\): \\(.*\\)")
947 (push (cons (match-string 1) (match-string 2)) res)
948 (forward-line 1)))
949 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
950 (push (match-string 1) res))
951 res)))
952
953 (defun vc-cvs-revision-completion-table (file)
954 (lexical-let ((file file)
955 table)
956 (setq table (lazy-completion-table
957 table (lambda () (vc-cvs-revision-table file))))
958 table))
959
960
961 (provide 'vc-cvs)
962
963 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
964 ;;; vc-cvs.el ends here