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