* viper-ex.el: Patch by Samuel Padgett. Copyright papers received.
[emacs.git] / lisp / vc-cvs.el
blob67c209eb31b9fc75e26a60fde65ff80a40319ed8
1 ;;; vc-cvs.el --- non-resident support for CVS version-control
3 ;; Copyright (C) 1995,98,99,2000,2001 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
8 ;; $Id: vc-cvs.el,v 1.39 2002/04/08 13:38:48 sds Exp $
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
27 ;;; Commentary:
29 ;;; Code:
31 (eval-when-compile
32 (require 'vc))
34 ;;;
35 ;;; Customization options
36 ;;;
38 (defcustom vc-cvs-global-switches nil
39 "*Global switches to pass to any CVS command."
40 :type '(choice (const :tag "None" nil)
41 (string :tag "Argument String")
42 (repeat :tag "Argument List"
43 :value ("")
44 string))
45 :version "21.3"
46 :group 'vc)
48 (defcustom vc-cvs-register-switches nil
49 "*Extra switches for registering a file into CVS.
50 A string or list of strings passed to the checkin program by
51 \\[vc-register]."
52 :type '(choice (const :tag "None" nil)
53 (string :tag "Argument String")
54 (repeat :tag "Argument List"
55 :value ("")
56 string))
57 :version "21.1"
58 :group 'vc)
60 (defcustom vc-cvs-diff-switches nil
61 "*A string or list of strings specifying extra switches for cvs diff under VC."
62 :type '(choice (const :tag "None" nil)
63 (string :tag "Argument String")
64 (repeat :tag "Argument List"
65 :value ("")
66 string))
67 :version "21.1"
68 :group 'vc)
70 (defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
71 "*Header keywords to be inserted by `vc-insert-headers'."
72 :version "21.1"
73 :type '(repeat string)
74 :group 'vc)
76 (defcustom vc-cvs-use-edit t
77 "*Non-nil means to use `cvs edit' to \"check out\" a file.
78 This is only meaningful if you don't use the implicit checkout model
79 \(i.e. if you have $CVSREAD set)."
80 :type 'boolean
81 :version "21.1"
82 :group 'vc)
84 (defcustom vc-cvs-stay-local t
85 "*Non-nil means use local operations when possible for remote repositories.
86 This avoids slow queries over the network and instead uses heuristics
87 and past information to determine the current status of a file.
88 The value can also be a regular expression to match against the host name
89 of a repository; then VC only stays local for hosts that match it."
90 :type '(choice (const :tag "Always stay local" t)
91 (string :tag "Host regexp")
92 (const :tag "Don't stay local" nil))
93 :version "21.1"
94 :group 'vc)
96 (defcustom vc-cvs-sticky-date-format-string "%c"
97 "*Format string for mode-line display of sticky date.
98 Format is according to `format-time-string'. Only used if
99 `vc-cvs-sticky-tag-display' is t."
100 :type '(string)
101 :version "21.3"
102 :group 'vc)
104 (defcustom vc-cvs-sticky-tag-display t
105 "*Specify the mode-line display of sticky tags.
106 Value t means default display, nil means no display at all. If the
107 value is a function or macro, it is called with the sticky tag and
108 its' type as parameters, in that order. TYPE can have three different
109 values: `symbolic-name' (TAG is a string), `revision-number' (TAG is a
110 string) and `date' (TAG is a date as returned by `encode-time'). The
111 return value of the function or macro will be displayed as a string.
113 Here's an example that will display the formatted date for sticky
114 dates and the word \"Sticky\" for sticky tag names and revisions.
116 (lambda (tag type)
117 (cond ((eq type 'date) (format-time-string
118 vc-cvs-sticky-date-format-string tag))
119 ((eq type 'revision-number) \"Sticky\")
120 ((eq type 'symbolic-name) \"Sticky\")))
122 Here's an example that will abbreviate to the first character only,
123 any text before the first occurence of `-' for sticky symbolic tags.
124 If the sticky tag is a revision number, the word \"Sticky\" is
125 displayed. Date and time is displayed for sticky dates.
127 (lambda (tag type)
128 (cond ((eq type 'date) (format-time-string \"%Y%m%d %H:%M\" tag))
129 ((eq type 'revision-number) \"Sticky\")
130 ((eq type 'symbolic-name)
131 (condition-case nil
132 (progn
133 (string-match \"\\\\([^-]*\\\\)\\\\(.*\\\\)\" tag)
134 (concat (substring (match-string 1 tag) 0 1) \":\"
135 (substring (match-string 2 tag) 1 nil)))
136 (error tag))))) ; Fall-back to given tag name.
138 See also variable `vc-cvs-sticky-date-format-string'."
139 :type '(choice boolean function)
140 :version "21.3"
141 :group 'vc)
144 ;;; Internal variables
147 (defvar vc-cvs-local-month-numbers
148 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
149 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
150 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
151 "Local association list of month numbers.")
155 ;;; State-querying functions
158 ;;;###autoload (defun vc-cvs-registered (f)
159 ;;;###autoload (when (file-readable-p (expand-file-name
160 ;;;###autoload "CVS/Entries" (file-name-directory f)))
161 ;;;###autoload (require 'vc-cvs)
162 ;;;###autoload (vc-cvs-registered f)))
164 (defun vc-cvs-registered (file)
165 "Check if FILE is CVS registered."
166 (let ((dirname (or (file-name-directory file) ""))
167 (basename (file-name-nondirectory file))
168 ;; make sure that the file name is searched case-sensitively
169 (case-fold-search nil))
170 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
171 (with-temp-buffer
172 (vc-insert-file (expand-file-name "CVS/Entries" dirname))
173 (goto-char (point-min))
174 (cond
175 ((re-search-forward
176 ;; CVS-removed files are not taken under VC control.
177 (concat "^/" (regexp-quote basename) "/[^/-]") nil t)
178 (beginning-of-line)
179 (vc-cvs-parse-entry file)
181 (t nil)))
182 nil)))
184 (defun vc-cvs-state (file)
185 "CVS-specific version of `vc-state'."
186 (if (vc-cvs-stay-local-p file)
187 (let ((state (vc-file-getprop file 'vc-state)))
188 ;; If we should stay local, use the heuristic but only if
189 ;; we don't have a more precise state already available.
190 (if (memq state '(up-to-date edited))
191 (vc-cvs-state-heuristic file)
192 state))
193 (with-temp-buffer
194 (cd (file-name-directory file))
195 (vc-cvs-command t 0 file "status")
196 (vc-cvs-parse-status t))))
198 (defun vc-cvs-state-heuristic (file)
199 "CVS-specific state heuristic."
200 ;; If the file has not changed since checkout, consider it `up-to-date'.
201 ;; Otherwise consider it `edited'.
202 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
203 (lastmod (nth 5 (file-attributes file))))
204 (if (equal checkout-time lastmod)
205 'up-to-date
206 'edited)))
208 (defun vc-cvs-dir-state (dir)
209 "Find the CVS state of all files in DIR."
210 (if (vc-cvs-stay-local-p dir)
211 (vc-cvs-dir-state-heuristic dir)
212 (let ((default-directory dir))
213 ;; Don't specify DIR in this command, the default-directory is
214 ;; enough. Otherwise it might fail with remote repositories.
215 (with-temp-buffer
216 (vc-cvs-command t 0 nil "status" "-l")
217 (goto-char (point-min))
218 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
219 (narrow-to-region (match-beginning 0) (match-end 0))
220 (vc-cvs-parse-status)
221 (goto-char (point-max))
222 (widen))))))
224 (defun vc-cvs-workfile-version (file)
225 "CVS-specific version of `vc-workfile-version'."
226 ;; There is no need to consult RCS headers under CVS, because we
227 ;; get the workfile version for free when we recognize that a file
228 ;; is registered in CVS.
229 (vc-cvs-registered file)
230 (vc-file-getprop file 'vc-workfile-version))
232 (defun vc-cvs-checkout-model (file)
233 "CVS-specific version of `vc-checkout-model'."
234 (if (or (getenv "CVSREAD")
235 ;; If the file is not writable (despite CVSREAD being
236 ;; undefined), this is probably because the file is being
237 ;; "watched" by other developers.
238 ;; (If vc-mistrust-permissions was t, we actually shouldn't
239 ;; trust this, but there is no other way to learn this from CVS
240 ;; at the moment (version 1.9).)
241 (string-match "r-..-..-." (nth 8 (file-attributes file))))
242 'announce
243 'implicit))
245 (defun vc-cvs-mode-line-string (file)
246 "Return string for placement into the modeline for FILE.
247 Compared to the default implementation, this function does two things:
248 Handle the special case of a CVS file that is added but not yet
249 committed and support display of sticky tags."
250 (let* ((state (vc-state file))
251 (rev (vc-workfile-version file))
252 (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
253 (sticky-tag-printable (and sticky-tag
254 (not (string= sticky-tag ""))
255 (concat "[" sticky-tag "]"))))
256 (cond ((string= rev "0")
257 ;; A file that is added but not yet committed.
258 "CVS @@")
259 ((or (eq state 'up-to-date)
260 (eq state 'needs-patch))
261 (concat "CVS-" rev sticky-tag-printable))
262 ((stringp state)
263 (concat "CVS:" state ":" rev sticky-tag-printable))
265 ;; Not just for the 'edited state, but also a fallback
266 ;; for all other states. Think about different symbols
267 ;; for 'needs-patch and 'needs-merge.
268 (concat "CVS:" rev sticky-tag-printable)))))
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 (state (cond ((eq cvs-state 'edited) "modified")
274 ((eq cvs-state 'needs-patch) "patch")
275 ((eq cvs-state 'needs-merge) "merge")
276 ;; FIXME: those two states cannot occur right now
277 ((eq cvs-state 'unlocked-changes) "conflict")
278 ((eq cvs-state 'locally-added) "added")
280 (if state (concat "(" state ")"))))
284 ;;; State-changing functions
287 (defun vc-cvs-register (file &optional rev comment)
288 "Register FILE into the CVS version-control system.
289 COMMENT can be used to provide an initial description of FILE.
291 `vc-register-switches' and `vc-cvs-register-switches' are passed to
292 the CVS command (in that order)."
293 (let ((switches (append
294 (if (stringp vc-register-switches)
295 (list vc-register-switches)
296 vc-register-switches)
297 (if (stringp vc-cvs-register-switches)
298 (list vc-cvs-register-switches)
299 vc-cvs-register-switches))))
301 (apply 'vc-cvs-command nil 0 file
302 "add"
303 (and comment (string-match "[^\t\n ]" comment)
304 (concat "-m" comment))
305 switches)))
307 (defun vc-cvs-responsible-p (file)
308 "Return non-nil if CVS thinks it is responsible for FILE."
309 (file-directory-p (expand-file-name "CVS"
310 (if (file-directory-p file)
311 file
312 (file-name-directory file)))))
314 (defun vc-cvs-could-register (file)
315 "Return non-nil if FILE could be registered in CVS.
316 This is only possible if CVS is responsible for FILE's directory."
317 (vc-cvs-responsible-p file))
319 (defun vc-cvs-checkin (file rev comment)
320 "CVS-specific version of `vc-backend-checkin'."
321 (let ((switches (if (stringp vc-checkin-switches)
322 (list vc-checkin-switches)
323 vc-checkin-switches))
324 status)
325 (if (or (not rev) (vc-cvs-valid-version-number-p rev))
326 (setq status (apply 'vc-cvs-command nil 1 file
327 "ci" (if rev (concat "-r" rev))
328 (concat "-m" comment)
329 switches))
330 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
331 (error "%s is not a valid symbolic tag name" rev)
332 ;; If the input revison is a valid symbolic tag name, we create it
333 ;; as a branch, commit and switch to it.
334 (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
335 (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
336 (setq status (apply 'vc-cvs-command nil 1 file
337 "ci"
338 (concat "-m" comment)
339 switches))
340 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
341 (set-buffer "*vc*")
342 (goto-char (point-min))
343 (when (not (zerop status))
344 ;; Check checkin problem.
345 (cond
346 ((re-search-forward "Up-to-date check failed" nil t)
347 (vc-file-setprop file 'vc-state 'needs-merge)
348 (error (substitute-command-keys
349 (concat "Up-to-date check failed: "
350 "type \\[vc-next-action] to merge in changes"))))
352 (pop-to-buffer (current-buffer))
353 (goto-char (point-min))
354 (shrink-window-if-larger-than-buffer)
355 (error "Check-in failed"))))
356 ;; Update file properties
357 (vc-file-setprop
358 file 'vc-workfile-version
359 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
360 ;; Forget the checkout model of the file, because we might have
361 ;; guessed wrong when we found the file. After commit, we can
362 ;; tell it from the permissions of the file (see
363 ;; vc-cvs-checkout-model).
364 (vc-file-setprop file 'vc-checkout-model nil)
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 file "update" "-A"))))
371 (defun vc-cvs-checkout (file &optional editable rev workfile)
372 "Retrieve a revision of FILE into a WORKFILE.
373 EDITABLE non-nil means that the file should be writable.
374 REV is the revision to check out into WORKFILE."
375 (let ((filename (or workfile file))
376 (file-buffer (get-file-buffer file))
377 switches)
378 (message "Checking out %s..." filename)
379 (save-excursion
380 ;; Change buffers to get local value of vc-checkout-switches.
381 (if file-buffer (set-buffer file-buffer))
382 (setq switches (if (stringp vc-checkout-switches)
383 (list vc-checkout-switches)
384 vc-checkout-switches))
385 ;; Save this buffer's default-directory
386 ;; and use save-excursion to make sure it is restored
387 ;; in the same buffer it was saved in.
388 (let ((default-directory default-directory))
389 (save-excursion
390 ;; Adjust the default-directory so that the check-out creates
391 ;; the file in the right place.
392 (setq default-directory (file-name-directory filename))
393 (if workfile
394 (let ((failed t)
395 (backup-name (if (string= file workfile)
396 (car (find-backup-file-name filename)))))
397 (when backup-name
398 (copy-file filename backup-name
399 'ok-if-already-exists 'keep-date)
400 (unless (file-writable-p filename)
401 (set-file-modes filename
402 (logior (file-modes filename) 128))))
403 (unwind-protect
404 (progn
405 (let ((coding-system-for-read 'no-conversion)
406 (coding-system-for-write 'no-conversion))
407 (with-temp-file filename
408 (apply 'vc-cvs-command
409 (current-buffer) 0 file
410 "-Q" ; suppress diagnostic output
411 "update"
412 (and rev (not (string= rev ""))
413 (concat "-r" rev))
414 "-p"
415 switches)))
416 (setq failed nil))
417 (if failed
418 (if backup-name
419 (rename-file backup-name filename
420 'ok-if-already-exists)
421 (if (file-exists-p filename)
422 (delete-file filename)))
423 (and backup-name
424 (not vc-make-backup-files)
425 (delete-file backup-name)))))
426 (if (and (file-exists-p file) (not rev))
427 ;; If no revision was specified, just make the file writable
428 ;; if necessary (using `cvs-edit' if requested).
429 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
430 (if vc-cvs-use-edit
431 (vc-cvs-command nil 0 file "edit")
432 (set-file-modes file (logior (file-modes file) 128))
433 (if file-buffer (toggle-read-only -1))))
434 ;; Check out a particular version (or recreate the file).
435 (vc-file-setprop file 'vc-workfile-version nil)
436 (apply 'vc-cvs-command nil 0 file
437 (and editable
438 (or (not (file-exists-p file))
439 (not (eq (vc-cvs-checkout-model file)
440 'implicit)))
441 "-w")
442 "update"
443 ;; default for verbose checkout: clear the sticky tag so
444 ;; that the actual update will get the head of the trunk
445 (if (or (not rev) (string= rev ""))
446 "-A"
447 (concat "-r" rev))
448 switches))))
449 (vc-mode-line file)
450 (message "Checking out %s...done" filename)))))
452 (defun vc-cvs-revert (file &optional contents-done)
453 "Revert FILE to the version it was based on."
454 (unless contents-done
455 ;; Check out via standard output (caused by the final argument
456 ;; FILE below), so that no sticky tag is set.
457 (vc-cvs-checkout file nil (vc-workfile-version file) file))
458 (unless (eq (vc-checkout-model file) 'implicit)
459 (if vc-cvs-use-edit
460 (vc-cvs-command nil 0 file "unedit")
461 ;; Make the file read-only by switching off all w-bits
462 (set-file-modes file (logand (file-modes file) 3950)))))
464 (defun vc-cvs-merge (file first-version &optional second-version)
465 "Merge changes into current working copy of FILE.
466 The changes are between FIRST-VERSION and SECOND-VERSION."
467 (vc-cvs-command nil 0 file
468 "update" "-kk"
469 (concat "-j" first-version)
470 (concat "-j" second-version))
471 (vc-file-setprop file 'vc-state 'edited)
472 (save-excursion
473 (set-buffer (get-buffer "*vc*"))
474 (goto-char (point-min))
475 (if (re-search-forward "conflicts during merge" nil t)
476 1 ; signal error
477 0))) ; signal success
479 (defun vc-cvs-merge-news (file)
480 "Merge in any new changes made to FILE."
481 (message "Merging changes into %s..." file)
482 (save-excursion
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 (set-buffer (get-buffer "*vc*"))
489 (goto-char (point-min))
490 ;; get new workfile version
491 (if (re-search-forward (concat "^Merging differences between "
492 "[01234567890.]* and "
493 "\\([01234567890.]*\\) into")
494 nil t)
495 (vc-file-setprop file 'vc-workfile-version (match-string 1))
496 (vc-file-setprop file 'vc-workfile-version nil))
497 ;; get file status
498 (prog1
499 (if (eq (buffer-size) 0)
500 0 ;; there were no news; indicate success
501 (if (re-search-forward
502 (concat "^\\([CMUP] \\)?"
503 (regexp-quote (file-name-nondirectory file))
504 "\\( already contains the differences between \\)?")
505 nil t)
506 (cond
507 ;; Merge successful, we are in sync with repository now
508 ((or (match-string 2)
509 (string= (match-string 1) "U ")
510 (string= (match-string 1) "P "))
511 (vc-file-setprop file 'vc-state 'up-to-date)
512 (vc-file-setprop file 'vc-checkout-time
513 (nth 5 (file-attributes file)))
514 0);; indicate success to the caller
515 ;; Merge successful, but our own changes are still in the file
516 ((string= (match-string 1) "M ")
517 (vc-file-setprop file 'vc-state 'edited)
518 0);; indicate success to the caller
519 ;; Conflicts detected!
521 (vc-file-setprop file 'vc-state 'edited)
522 1);; signal the error to the caller
524 (pop-to-buffer "*vc*")
525 (error "Couldn't analyze cvs update result")))
526 (message "Merging changes into %s...done" file))))
530 ;;; History functions
533 (defun vc-cvs-print-log (file)
534 "Get change log associated with FILE."
535 (vc-cvs-command
537 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
538 file "log"))
540 (defun vc-cvs-show-log-entry (version)
541 (when (re-search-forward
542 ;; also match some context, for safety
543 (concat "----\nrevision " version
544 "\\(\tlocked by:.*\n\\|\n\\)date: ") nil t)
545 ;; set the display window so that
546 ;; the whole log entry is displayed
547 (let (start end lines)
548 (beginning-of-line) (forward-line -1) (setq start (point))
549 (if (not (re-search-forward "^----*\nrevision" nil t))
550 (setq end (point-max))
551 (beginning-of-line) (forward-line -1) (setq end (point)))
552 (setq lines (count-lines start end))
553 (cond
554 ;; if the global information and this log entry fit
555 ;; into the window, display from the beginning
556 ((< (count-lines (point-min) end) (window-height))
557 (goto-char (point-min))
558 (recenter 0)
559 (goto-char start))
560 ;; if the whole entry fits into the window,
561 ;; display it centered
562 ((< (1+ lines) (window-height))
563 (goto-char start)
564 (recenter (1- (- (/ (window-height) 2) (/ lines 2)))))
565 ;; otherwise (the entry is too large for the window),
566 ;; display from the start
568 (goto-char start)
569 (recenter 0))))))
571 (defun vc-cvs-diff (file &optional oldvers newvers)
572 "Get a difference report using CVS between two versions of FILE."
573 (let (options status (diff-switches-list (vc-diff-switches-list 'CVS)))
574 (if (string= (vc-workfile-version file) "0")
575 ;; This file is added but not yet committed; there is no master file.
576 (if (or oldvers newvers)
577 (error "No revisions of %s exist" file)
578 ;; We regard this as "changed".
579 ;; Diff it against /dev/null.
580 ;; Note: this is NOT a "cvs diff".
581 (apply 'vc-do-command "*vc-diff*"
582 1 "diff" file
583 (append diff-switches-list '("/dev/null"))))
584 (setq status
585 (apply 'vc-cvs-command "*vc-diff*"
586 (if (and (vc-cvs-stay-local-p file)
587 (fboundp 'start-process))
588 'async
590 file "diff"
591 (and oldvers (concat "-r" oldvers))
592 (and newvers (concat "-r" newvers))
593 diff-switches-list))
594 (if (vc-cvs-stay-local-p file)
595 1 ;; async diff, pessimistic assumption
596 status))))
598 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
599 "Diff all files at and below DIR."
600 (with-current-buffer "*vc-diff*"
601 (setq default-directory dir)
602 (if (vc-cvs-stay-local-p dir)
603 ;; local diff: do it filewise, and only for files that are modified
604 (vc-file-tree-walk
606 (lambda (f)
607 (vc-exec-after
608 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
609 ;; possible optimization: fetch the state of all files
610 ;; in the tree via vc-cvs-dir-state-heuristic
611 (unless (vc-up-to-date-p ',f)
612 (message "Looking at %s" ',f)
613 (vc-diff-internal ',f ',rev1 ',rev2))))))
614 ;; cvs diff: use a single call for the entire tree
615 (let ((coding-system-for-read
616 (or coding-system-for-read 'undecided)))
617 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
618 (and rev1 (concat "-r" rev1))
619 (and rev2 (concat "-r" rev2))
620 (vc-diff-switches-list 'CVS))))))
622 (defun vc-cvs-annotate-command (file buffer &optional version)
623 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
624 Optional arg VERSION is a version to annotate from."
625 (vc-cvs-command buffer 0 file "annotate" (if version
626 (concat "-r" version))))
628 (defun vc-cvs-annotate-current-time ()
629 "Return the current time, based at midnight of the current day, and
630 encoded as fractional days."
631 (vc-annotate-convert-time
632 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
634 (defun vc-cvs-annotate-time ()
635 "Return the time of the next annotation (as fraction of days)
636 systime, or nil if there is none."
637 (let ((time-stamp
638 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
639 (if (looking-at time-stamp)
640 (progn
641 (let* ((day (string-to-number (match-string 1)))
642 (month (cdr (assoc (match-string 2)
643 vc-cvs-local-month-numbers)))
644 (year-tmp (string-to-number (match-string 3)))
645 ;; Years 0..68 are 2000..2068.
646 ;; Years 69..99 are 1969..1999.
647 (year (+ (cond ((> 69 year-tmp) 2000)
648 ((> 100 year-tmp) 1900)
649 (t 0))
650 year-tmp)))
651 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
652 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
653 ;; If we did not look directly at an annotation, there might be
654 ;; some further down. This is the case if we are positioned at
655 ;; the very top of the buffer, for instance.
656 (if (re-search-forward time-stamp nil t)
657 (progn
658 (beginning-of-line nil)
659 (vc-cvs-annotate-time))))))
662 ;;; Snapshot system
665 (defun vc-cvs-create-snapshot (dir name branchp)
666 "Assign to DIR's current version a given NAME.
667 If BRANCHP is non-nil, the name is created as a branch (and the current
668 workspace is immediately moved to that new branch)."
669 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
670 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
672 (defun vc-cvs-retrieve-snapshot (dir name update)
673 "Retrieve a snapshot at and below DIR.
674 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
675 If UPDATE is non-nil, then update (resynch) any affected buffers."
676 (with-current-buffer (get-buffer-create "*vc*")
677 (let ((default-directory dir)
678 (sticky-tag))
679 (erase-buffer)
680 (if (or (not name) (string= name ""))
681 (vc-cvs-command t 0 nil "update")
682 (vc-cvs-command t 0 nil "update" "-r" name)
683 (setq sticky-tag name))
684 (when update
685 (goto-char (point-min))
686 (while (not (eobp))
687 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
688 (let* ((file (expand-file-name (match-string 2) dir))
689 (state (match-string 1))
690 (buffer (find-buffer-visiting file)))
691 (when buffer
692 (cond
693 ((or (string= state "U")
694 (string= state "P"))
695 (vc-file-setprop file 'vc-state 'up-to-date)
696 (vc-file-setprop file 'vc-workfile-version nil)
697 (vc-file-setprop file 'vc-checkout-time
698 (nth 5 (file-attributes file))))
699 ((or (string= state "M")
700 (string= state "C"))
701 (vc-file-setprop file 'vc-state 'edited)
702 (vc-file-setprop file 'vc-workfile-version nil)
703 (vc-file-setprop file 'vc-checkout-time 0)))
704 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
705 (vc-resynch-buffer file t t))))
706 (forward-line 1))))))
710 ;;; Miscellaneous
713 (defun vc-cvs-make-version-backups-p (file)
714 "Return non-nil if version backups should be made for FILE."
715 (vc-cvs-stay-local-p file))
717 (defun vc-cvs-check-headers ()
718 "Check if the current file has any headers in it."
719 (save-excursion
720 (goto-char (point-min))
721 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
722 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
726 ;;; Internal functions
729 (defun vc-cvs-command (buffer okstatus file &rest flags)
730 "A wrapper around `vc-do-command' for use in vc-cvs.el.
731 The difference to vc-do-command is that this function always invokes `cvs',
732 and that it passes `vc-cvs-global-switches' to it before FLAGS."
733 (apply 'vc-do-command buffer okstatus "cvs" file
734 (if (stringp vc-cvs-global-switches)
735 (cons vc-cvs-global-switches flags)
736 (append vc-cvs-global-switches
737 flags))))
739 (defun vc-cvs-stay-local-p (file)
740 "Return non-nil if VC should stay local when handling FILE."
741 (if vc-cvs-stay-local
742 (let* ((dirname (if (file-directory-p file)
743 (directory-file-name file)
744 (file-name-directory file)))
745 (prop
746 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
747 (let ((rootname (expand-file-name "CVS/Root" dirname)))
748 (vc-file-setprop
749 dirname 'vc-cvs-stay-local-p
750 (when (file-readable-p rootname)
751 (with-temp-buffer
752 (vc-insert-file rootname)
753 (goto-char (point-min))
754 (if (looking-at "\\([^:]*\\):")
755 (if (not (stringp vc-cvs-stay-local))
756 'yes
757 (let ((hostname (match-string 1)))
758 (if (string-match vc-cvs-stay-local hostname)
759 'yes
760 'no)))
761 'no))))))))
762 (if (eq prop 'yes) t nil))))
764 (defun vc-cvs-parse-status (&optional full)
765 "Parse output of \"cvs status\" command in the current buffer.
766 Set file properties accordingly. Unless FULL is t, parse only
767 essential information."
768 (let (file status)
769 (goto-char (point-min))
770 (if (re-search-forward "^File: " nil t)
771 (cond
772 ((looking-at "no file") nil)
773 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
774 (setq file (expand-file-name (match-string 1)))
775 (vc-file-setprop file 'vc-backend 'CVS)
776 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
777 (setq status "Unknown")
778 (setq status (match-string 1)))
779 (if (and full
780 (re-search-forward
781 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
782 \[\t ]+\\([0-9.]+\\)"
783 nil t))
784 (vc-file-setprop file 'vc-latest-version (match-string 2)))
785 (vc-file-setprop
786 file 'vc-state
787 (cond
788 ((string-match "Up-to-date" status)
789 (vc-file-setprop file 'vc-checkout-time
790 (nth 5 (file-attributes file)))
791 'up-to-date)
792 ((string-match "Locally Modified" status) 'edited)
793 ((string-match "Needs Merge" status) 'needs-merge)
794 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
795 (t 'edited))))))))
797 (defun vc-cvs-dir-state-heuristic (dir)
798 "Find the CVS state of all files in DIR, using only local information."
799 (with-temp-buffer
800 (vc-insert-file (expand-file-name "CVS/Entries" dir))
801 (goto-char (point-min))
802 (while (not (eobp))
803 ;; CVS-removed files are not taken under VC control.
804 (when (looking-at "/\\([^/]*\\)/[^/-]")
805 (let ((file (expand-file-name (match-string 1) dir)))
806 (unless (vc-file-getprop file 'vc-state)
807 (vc-cvs-parse-entry file t))))
808 (forward-line 1))))
811 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
812 "Return non-nil if TAG is a valid symbolic tag name."
813 ;; According to the CVS manual, a valid symbolic tag must start with
814 ;; an uppercase or lowercase letter and can contain uppercase and
815 ;; lowercase letters, digits, `-', and `_'.
816 (and (string-match "^[a-zA-Z]" tag)
817 (not (string-match "[^a-z0-9A-Z-_]" tag))))
819 (defun vc-cvs-valid-version-number-p (tag)
820 "Return non-nil if TAG is a valid version number."
821 (and (string-match "^[0-9]" tag)
822 (not (string-match "[^0-9.]" tag))))
824 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
825 "Parse and return the sticky tag as a string.
826 `match-data' is protected."
827 (let ((data (match-data))
828 (tag)
829 (type (cond ((string= match-type "D") 'date)
830 ((string= match-type "T")
831 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
832 'symbolic-name
833 'revision-number))
834 (t nil))))
835 (unwind-protect
836 (progn
837 (cond
838 ;; Sticky Date tag. Convert to to a proper date value (`encode-time')
839 ((eq type 'date)
840 (string-match
841 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
842 match-tag)
843 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
844 (month (string-to-number (match-string 2 match-tag)))
845 (day (string-to-number (match-string 3 match-tag)))
846 (hour (string-to-number (match-string 4 match-tag)))
847 (min (string-to-number (match-string 5 match-tag)))
848 (sec (string-to-number (match-string 6 match-tag)))
849 ;; Years 0..68 are 2000..2068.
850 ;; Years 69..99 are 1969..1999.
851 (year (+ (cond ((> 69 year-tmp) 2000)
852 ((> 100 year-tmp) 1900)
853 (t 0))
854 year-tmp)))
855 (setq tag (encode-time sec min hour day month year))))
856 ;; Sticky Tag name or revision number
857 ((eq type 'symbolic-name) (setq tag match-tag))
858 ((eq type 'revision-number) (setq tag match-tag))
859 ;; Default is no sticky tag at all
860 (t nil))
861 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
862 ((eq vc-cvs-sticky-tag-display t)
863 (cond ((eq type 'date) (format-time-string
864 vc-cvs-sticky-date-format-string
865 tag))
866 ((eq type 'symbolic-name) tag)
867 ((eq type 'revision-number) tag)
868 (t nil)))
869 ((functionp vc-cvs-sticky-tag-display)
870 (funcall vc-cvs-sticky-tag-display tag type))
871 (t nil)))
873 (set-match-data data))))
875 (defun vc-cvs-parse-entry (file &optional set-state)
876 "Parse a line from CVS/Entries.
877 Compare modification time to that of the FILE, set file properties
878 accordingly. However, `vc-state' is set only if optional arg SET-STATE
879 is non-nil."
880 (cond
881 ;; entry for a "locally added" file (not yet committed)
882 ((looking-at "/[^/]+/0/")
883 (vc-file-setprop file 'vc-checkout-time 0)
884 (vc-file-setprop file 'vc-workfile-version "0")
885 (if set-state (vc-file-setprop file 'vc-state 'edited)))
886 ;; normal entry
887 ((looking-at
888 (concat "/[^/]+"
889 ;; revision
890 "/\\([^/]*\\)"
891 ;; timestamp
892 "/\\([^/]*\\)"
893 ;; optional conflict field
894 "\\(+[^/]*\\)?/"
895 ;; options
896 "\\([^/]*\\)/"
897 ;; sticky tag
898 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
899 "\\(.*\\)")) ;Sticky tag
900 (vc-file-setprop file 'vc-workfile-version (match-string 1))
901 (vc-file-setprop file 'vc-cvs-sticky-tag
902 (vc-cvs-parse-sticky-tag (match-string 5) (match-string 6)))
903 ;; compare checkout time and modification time
904 (let ((mtime (nth 5 (file-attributes file)))
905 (system-time-locale "C"))
906 (cond ((equal (format-time-string "%c" mtime 'utc) (match-string 2))
907 (vc-file-setprop file 'vc-checkout-time mtime)
908 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
910 (vc-file-setprop file 'vc-checkout-time 0)
911 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
913 (provide 'vc-cvs)
915 ;;; vc-cvs.el ends here