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