(emerge-show-file-name): Read event irrespective of whether minibuffer
[bpt/emacs.git] / lisp / vc-cvs.el
1 ;;; vc-cvs.el --- non-resident support for CVS version-control
2
3 ;; Copyright (C) 1995,98,99,2000,2001,2002 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
8 ;; $Id: vc-cvs.el,v 1.50 2002/12/26 14:05:48 spiegel Exp $
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32 (require 'vc))
33
34 ;;;
35 ;;; Customization options
36 ;;;
37
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.4"
46 :group 'vc)
47
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)
59
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)
69
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)
75
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)
83
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)
95
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.4"
102 :group 'vc)
103
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.
112
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.
115
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\")))
121
122 Here's an example that will abbreviate to the first character only,
123 any text before the first occurrence 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.
126
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.
137
138 See also variable `vc-cvs-sticky-date-format-string'."
139 :type '(choice boolean function)
140 :version "21.4"
141 :group 'vc)
142
143 ;;;
144 ;;; Internal variables
145 ;;;
146
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.")
152
153
154 ;;;
155 ;;; State-querying functions
156 ;;;
157
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 (load "vc-cvs")
162 ;;;###autoload (vc-cvs-registered f)))
163
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)
180 t)
181 (t nil)))
182 nil)))
183
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))))
197
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)))
207
208 (defun vc-cvs-dir-state (dir)
209 "Find the CVS state of all files in DIR."
210 ;; if DIR is not under CVS control, don't do anything
211 (if (file-readable-p (expand-file-name "CVS/Entries" dir))
212 (if (vc-cvs-stay-local-p dir)
213 (vc-cvs-dir-state-heuristic dir)
214 (let ((default-directory dir))
215 ;; Don't specify DIR in this command, the default-directory is
216 ;; enough. Otherwise it might fail with remote repositories.
217 (with-temp-buffer
218 (vc-do-command t 0 "cvs" nil "status" "-l")
219 (goto-char (point-min))
220 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
221 (narrow-to-region (match-beginning 0) (match-end 0))
222 (vc-cvs-parse-status)
223 (goto-char (point-max))
224 (widen)))))))
225
226 (defun vc-cvs-workfile-version (file)
227 "CVS-specific version of `vc-workfile-version'."
228 ;; There is no need to consult RCS headers under CVS, because we
229 ;; get the workfile version for free when we recognize that a file
230 ;; is registered in CVS.
231 (vc-cvs-registered file)
232 (vc-file-getprop file 'vc-workfile-version))
233
234 (defun vc-cvs-checkout-model (file)
235 "CVS-specific version of `vc-checkout-model'."
236 (if (or (getenv "CVSREAD")
237 ;; If the file is not writable (despite CVSREAD being
238 ;; undefined), this is probably because the file is being
239 ;; "watched" by other developers.
240 ;; (If vc-mistrust-permissions was t, we actually shouldn't
241 ;; trust this, but there is no other way to learn this from CVS
242 ;; at the moment (version 1.9).)
243 (string-match "r-..-..-." (nth 8 (file-attributes file))))
244 'announce
245 'implicit))
246
247 (defun vc-cvs-mode-line-string (file)
248 "Return string for placement into the modeline for FILE.
249 Compared to the default implementation, this function does two things:
250 Handle the special case of a CVS file that is added but not yet
251 committed and support display of sticky tags."
252 (let* ((state (vc-state file))
253 (rev (vc-workfile-version file))
254 (sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
255 (sticky-tag-printable (and sticky-tag
256 (not (string= sticky-tag ""))
257 (concat "[" sticky-tag "]"))))
258 (cond ((string= rev "0")
259 ;; A file that is added but not yet committed.
260 "CVS @@")
261 ((or (eq state 'up-to-date)
262 (eq state 'needs-patch))
263 (concat "CVS-" rev sticky-tag-printable))
264 ((stringp state)
265 (concat "CVS:" state ":" rev sticky-tag-printable))
266 (t
267 ;; Not just for the 'edited state, but also a fallback
268 ;; for all other states. Think about different symbols
269 ;; for 'needs-patch and 'needs-merge.
270 (concat "CVS:" rev sticky-tag-printable)))))
271
272 (defun vc-cvs-dired-state-info (file)
273 "CVS-specific version of `vc-dired-state-info'."
274 (let* ((cvs-state (vc-state file))
275 (state (cond ((eq cvs-state 'edited) "modified")
276 ((eq cvs-state 'needs-patch) "patch")
277 ((eq cvs-state 'needs-merge) "merge")
278 ;; FIXME: those two states cannot occur right now
279 ((eq cvs-state 'unlocked-changes) "conflict")
280 ((eq cvs-state 'locally-added) "added")
281 )))
282 (if state (concat "(" state ")"))))
283
284
285 ;;;
286 ;;; State-changing functions
287 ;;;
288
289 (defun vc-cvs-register (file &optional rev comment)
290 "Register FILE into the CVS version-control system.
291 COMMENT can be used to provide an initial description of FILE.
292
293 `vc-register-switches' and `vc-cvs-register-switches' are passed to
294 the CVS command (in that order)."
295 (let ((switches (append
296 (if (stringp vc-register-switches)
297 (list vc-register-switches)
298 vc-register-switches)
299 (if (stringp vc-cvs-register-switches)
300 (list vc-cvs-register-switches)
301 vc-cvs-register-switches))))
302
303 (apply 'vc-cvs-command nil 0 file
304 "add"
305 (and comment (string-match "[^\t\n ]" comment)
306 (concat "-m" comment))
307 switches)))
308
309 (defun vc-cvs-responsible-p (file)
310 "Return non-nil if CVS thinks it is responsible for FILE."
311 (file-directory-p (expand-file-name "CVS"
312 (if (file-directory-p file)
313 file
314 (file-name-directory file)))))
315
316 (defun vc-cvs-could-register (file)
317 "Return non-nil if FILE could be registered in CVS.
318 This is only possible if CVS is responsible for FILE's directory."
319 (vc-cvs-responsible-p file))
320
321 (defun vc-cvs-checkin (file rev comment)
322 "CVS-specific version of `vc-backend-checkin'."
323 (let ((switches (if (stringp vc-checkin-switches)
324 (list vc-checkin-switches)
325 vc-checkin-switches))
326 status)
327 (if (or (not rev) (vc-cvs-valid-version-number-p rev))
328 (setq status (apply 'vc-cvs-command nil 1 file
329 "ci" (if rev (concat "-r" rev))
330 (concat "-m" comment)
331 switches))
332 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
333 (error "%s is not a valid symbolic tag name" rev)
334 ;; If the input revison is a valid symbolic tag name, we create it
335 ;; as a branch, commit and switch to it.
336 (apply 'vc-cvs-command nil 0 file "tag" "-b" (list rev))
337 (apply 'vc-cvs-command nil 0 file "update" "-r" (list rev))
338 (setq status (apply 'vc-cvs-command nil 1 file
339 "ci"
340 (concat "-m" comment)
341 switches))
342 (vc-file-setprop file 'vc-cvs-sticky-tag rev)))
343 (set-buffer "*vc*")
344 (goto-char (point-min))
345 (when (not (zerop status))
346 ;; Check checkin problem.
347 (cond
348 ((re-search-forward "Up-to-date check failed" nil t)
349 (vc-file-setprop file 'vc-state 'needs-merge)
350 (error (substitute-command-keys
351 (concat "Up-to-date check failed: "
352 "type \\[vc-next-action] to merge in changes"))))
353 (t
354 (pop-to-buffer (current-buffer))
355 (goto-char (point-min))
356 (shrink-window-if-larger-than-buffer)
357 (error "Check-in failed"))))
358 ;; Update file properties
359 (vc-file-setprop
360 file 'vc-workfile-version
361 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
362 ;; Forget the checkout model of the file, because we might have
363 ;; guessed wrong when we found the file. After commit, we can
364 ;; tell it from the permissions of the file (see
365 ;; vc-cvs-checkout-model).
366 (vc-file-setprop file 'vc-checkout-model nil)
367
368 ;; if this was an explicit check-in (does not include creation of
369 ;; a branch), remove the sticky tag.
370 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
371 (vc-cvs-command nil 0 file "update" "-A"))))
372
373 (defun vc-cvs-find-version (file rev buffer)
374 (apply 'vc-cvs-command
375 buffer 0 file
376 "-Q" ; suppress diagnostic output
377 "update"
378 (and rev (not (string= rev ""))
379 (concat "-r" rev))
380 "-p"
381 (if (stringp vc-checkout-switches)
382 (list vc-checkout-switches)
383 vc-checkout-switches)))
384
385 (defun vc-cvs-checkout (file &optional editable rev workfile)
386 "Retrieve a revision of FILE into a WORKFILE.
387 EDITABLE non-nil means that the file should be writable.
388 REV is the revision to check out into WORKFILE."
389 (let ((filename (or workfile file))
390 (file-buffer (get-file-buffer file))
391 switches)
392 (message "Checking out %s..." filename)
393 (save-excursion
394 ;; Change buffers to get local value of vc-checkout-switches.
395 (if file-buffer (set-buffer file-buffer))
396 (setq switches (if (stringp vc-checkout-switches)
397 (list vc-checkout-switches)
398 vc-checkout-switches))
399 ;; Save this buffer's default-directory
400 ;; and use save-excursion to make sure it is restored
401 ;; in the same buffer it was saved in.
402 (let ((default-directory default-directory))
403 (save-excursion
404 ;; Adjust the default-directory so that the check-out creates
405 ;; the file in the right place.
406 (setq default-directory (file-name-directory filename))
407 (if workfile
408 (let ((failed t)
409 (backup-name (if (string= file workfile)
410 (car (find-backup-file-name filename)))))
411 (when backup-name
412 (copy-file filename backup-name
413 'ok-if-already-exists 'keep-date)
414 (unless (file-writable-p filename)
415 (set-file-modes filename
416 (logior (file-modes filename) 128))))
417 (unwind-protect
418 (progn
419 (let ((coding-system-for-read 'no-conversion)
420 (coding-system-for-write 'no-conversion))
421 (with-temp-file filename
422 (apply 'vc-cvs-command
423 (current-buffer) 0 file
424 "-Q" ; suppress diagnostic output
425 "update"
426 (and (stringp rev)
427 (not (string= rev ""))
428 (concat "-r" rev))
429 "-p"
430 switches)))
431 (setq failed nil))
432 (if failed
433 (if backup-name
434 (rename-file backup-name filename
435 'ok-if-already-exists)
436 (if (file-exists-p filename)
437 (delete-file filename)))
438 (and backup-name
439 (not vc-make-backup-files)
440 (delete-file backup-name)))))
441 (if (and (file-exists-p file) (not rev))
442 ;; If no revision was specified, just make the file writable
443 ;; if necessary (using `cvs-edit' if requested).
444 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
445 (if vc-cvs-use-edit
446 (vc-cvs-command nil 0 file "edit")
447 (set-file-modes file (logior (file-modes file) 128))
448 (if file-buffer (toggle-read-only -1))))
449 ;; Check out a particular version (or recreate the file).
450 (vc-file-setprop file 'vc-workfile-version nil)
451 (apply 'vc-cvs-command nil 0 file
452 (and editable
453 (or (not (file-exists-p file))
454 (not (eq (vc-cvs-checkout-model file)
455 'implicit)))
456 "-w")
457 "update"
458 ;; default for verbose checkout: clear the sticky tag so
459 ;; that the actual update will get the head of the trunk
460 (if (or (not rev) (eq rev t) (string= rev ""))
461 "-A"
462 (concat "-r" rev))
463 switches))))
464 (vc-mode-line file)
465 (message "Checking out %s...done" filename)))))
466
467 (defun vc-cvs-revert (file &optional contents-done)
468 "Revert FILE to the version it was based on."
469 (unless contents-done
470 ;; Check out via standard output (caused by the final argument
471 ;; FILE below), so that no sticky tag is set.
472 (vc-cvs-checkout file nil (vc-workfile-version file) file))
473 (unless (eq (vc-checkout-model file) 'implicit)
474 (if vc-cvs-use-edit
475 (vc-cvs-command nil 0 file "unedit")
476 ;; Make the file read-only by switching off all w-bits
477 (set-file-modes file (logand (file-modes file) 3950)))))
478
479 (defun vc-cvs-merge (file first-version &optional second-version)
480 "Merge changes into current working copy of FILE.
481 The changes are between FIRST-VERSION and SECOND-VERSION."
482 (vc-cvs-command nil 0 file
483 "update" "-kk"
484 (concat "-j" first-version)
485 (concat "-j" second-version))
486 (vc-file-setprop file 'vc-state 'edited)
487 (save-excursion
488 (set-buffer (get-buffer "*vc*"))
489 (goto-char (point-min))
490 (if (re-search-forward "conflicts during merge" nil t)
491 1 ; signal error
492 0))) ; signal success
493
494 (defun vc-cvs-merge-news (file)
495 "Merge in any new changes made to FILE."
496 (message "Merging changes into %s..." file)
497 (save-excursion
498 ;; (vc-file-setprop file 'vc-workfile-version nil)
499 (vc-file-setprop file 'vc-checkout-time 0)
500 (vc-cvs-command nil 0 file "update")
501 ;; Analyze the merge result reported by CVS, and set
502 ;; file properties accordingly.
503 (set-buffer (get-buffer "*vc*"))
504 (goto-char (point-min))
505 ;; get new workfile version
506 (if (re-search-forward (concat "^Merging differences between "
507 "[01234567890.]* and "
508 "\\([01234567890.]*\\) into")
509 nil t)
510 (vc-file-setprop file 'vc-workfile-version (match-string 1))
511 (vc-file-setprop file 'vc-workfile-version nil))
512 ;; get file status
513 (prog1
514 (if (eq (buffer-size) 0)
515 0 ;; there were no news; indicate success
516 (if (re-search-forward
517 (concat "^\\([CMUP] \\)?"
518 (regexp-quote (file-name-nondirectory file))
519 "\\( already contains the differences between \\)?")
520 nil t)
521 (cond
522 ;; Merge successful, we are in sync with repository now
523 ((or (match-string 2)
524 (string= (match-string 1) "U ")
525 (string= (match-string 1) "P "))
526 (vc-file-setprop file 'vc-state 'up-to-date)
527 (vc-file-setprop file 'vc-checkout-time
528 (nth 5 (file-attributes file)))
529 0);; indicate success to the caller
530 ;; Merge successful, but our own changes are still in the file
531 ((string= (match-string 1) "M ")
532 (vc-file-setprop file 'vc-state 'edited)
533 0);; indicate success to the caller
534 ;; Conflicts detected!
535 (t
536 (vc-file-setprop file 'vc-state 'edited)
537 1);; signal the error to the caller
538 )
539 (pop-to-buffer "*vc*")
540 (error "Couldn't analyze cvs update result")))
541 (message "Merging changes into %s...done" file))))
542
543
544 ;;;
545 ;;; History functions
546 ;;;
547
548 (defun vc-cvs-print-log (file)
549 "Get change log associated with FILE."
550 (vc-cvs-command
551 nil
552 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
553 file "log"))
554
555 (defun vc-cvs-diff (file &optional oldvers newvers)
556 "Get a difference report using CVS between two versions of FILE."
557 (let (options status (diff-switches-list (vc-diff-switches-list 'CVS)))
558 (if (string= (vc-workfile-version file) "0")
559 ;; This file is added but not yet committed; there is no master file.
560 (if (or oldvers newvers)
561 (error "No revisions of %s exist" file)
562 ;; We regard this as "changed".
563 ;; Diff it against /dev/null.
564 ;; Note: this is NOT a "cvs diff".
565 (apply 'vc-do-command "*vc-diff*"
566 1 "diff" file
567 (append diff-switches-list '("/dev/null"))))
568 (setq status
569 (apply 'vc-cvs-command "*vc-diff*"
570 (if (and (vc-cvs-stay-local-p file)
571 (fboundp 'start-process))
572 'async
573 1)
574 file "diff"
575 (and oldvers (concat "-r" oldvers))
576 (and newvers (concat "-r" newvers))
577 diff-switches-list))
578 (if (vc-cvs-stay-local-p file)
579 1 ;; async diff, pessimistic assumption
580 status))))
581
582 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
583 "Diff all files at and below DIR."
584 (with-current-buffer "*vc-diff*"
585 (setq default-directory dir)
586 (if (vc-cvs-stay-local-p dir)
587 ;; local diff: do it filewise, and only for files that are modified
588 (vc-file-tree-walk
589 dir
590 (lambda (f)
591 (vc-exec-after
592 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
593 ;; possible optimization: fetch the state of all files
594 ;; in the tree via vc-cvs-dir-state-heuristic
595 (unless (vc-up-to-date-p ',f)
596 (message "Looking at %s" ',f)
597 (vc-diff-internal ',f ',rev1 ',rev2))))))
598 ;; cvs diff: use a single call for the entire tree
599 (let ((coding-system-for-read
600 (or coding-system-for-read 'undecided)))
601 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
602 (and rev1 (concat "-r" rev1))
603 (and rev2 (concat "-r" rev2))
604 (vc-diff-switches-list 'CVS))))))
605
606 (defun vc-cvs-annotate-command (file buffer &optional version)
607 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
608 Optional arg VERSION is a version to annotate from."
609 (vc-cvs-command buffer 0 file "annotate" (if version
610 (concat "-r" version))))
611
612 (defun vc-cvs-annotate-current-time ()
613 "Return the current time, based at midnight of the current day, and
614 encoded as fractional days."
615 (vc-annotate-convert-time
616 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
617
618 (defun vc-cvs-annotate-time ()
619 "Return the time of the next annotation (as fraction of days)
620 systime, or nil if there is none."
621 (let ((time-stamp
622 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
623 (if (looking-at time-stamp)
624 (progn
625 (let* ((day (string-to-number (match-string 1)))
626 (month (cdr (assoc (match-string 2)
627 vc-cvs-local-month-numbers)))
628 (year-tmp (string-to-number (match-string 3)))
629 ;; Years 0..68 are 2000..2068.
630 ;; Years 69..99 are 1969..1999.
631 (year (+ (cond ((> 69 year-tmp) 2000)
632 ((> 100 year-tmp) 1900)
633 (t 0))
634 year-tmp)))
635 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
636 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
637 ;; If we did not look directly at an annotation, there might be
638 ;; some further down. This is the case if we are positioned at
639 ;; the very top of the buffer, for instance.
640 (if (re-search-forward time-stamp nil t)
641 (progn
642 (beginning-of-line nil)
643 (vc-cvs-annotate-time))))))
644
645 ;;;
646 ;;; Snapshot system
647 ;;;
648
649 (defun vc-cvs-create-snapshot (dir name branchp)
650 "Assign to DIR's current version a given NAME.
651 If BRANCHP is non-nil, the name is created as a branch (and the current
652 workspace is immediately moved to that new branch)."
653 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
654 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
655
656 (defun vc-cvs-retrieve-snapshot (dir name update)
657 "Retrieve a snapshot at and below DIR.
658 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
659 If UPDATE is non-nil, then update (resynch) any affected buffers."
660 (with-current-buffer (get-buffer-create "*vc*")
661 (let ((default-directory dir)
662 (sticky-tag))
663 (erase-buffer)
664 (if (or (not name) (string= name ""))
665 (vc-cvs-command t 0 nil "update")
666 (vc-cvs-command t 0 nil "update" "-r" name)
667 (setq sticky-tag name))
668 (when update
669 (goto-char (point-min))
670 (while (not (eobp))
671 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
672 (let* ((file (expand-file-name (match-string 2) dir))
673 (state (match-string 1))
674 (buffer (find-buffer-visiting file)))
675 (when buffer
676 (cond
677 ((or (string= state "U")
678 (string= state "P"))
679 (vc-file-setprop file 'vc-state 'up-to-date)
680 (vc-file-setprop file 'vc-workfile-version nil)
681 (vc-file-setprop file 'vc-checkout-time
682 (nth 5 (file-attributes file))))
683 ((or (string= state "M")
684 (string= state "C"))
685 (vc-file-setprop file 'vc-state 'edited)
686 (vc-file-setprop file 'vc-workfile-version nil)
687 (vc-file-setprop file 'vc-checkout-time 0)))
688 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
689 (vc-resynch-buffer file t t))))
690 (forward-line 1))))))
691
692
693 ;;;
694 ;;; Miscellaneous
695 ;;;
696
697 (defun vc-cvs-make-version-backups-p (file)
698 "Return non-nil if version backups should be made for FILE."
699 (vc-cvs-stay-local-p file))
700
701 (defun vc-cvs-check-headers ()
702 "Check if the current file has any headers in it."
703 (save-excursion
704 (goto-char (point-min))
705 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
706 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
707
708
709 ;;;
710 ;;; Internal functions
711 ;;;
712
713 (defun vc-cvs-command (buffer okstatus file &rest flags)
714 "A wrapper around `vc-do-command' for use in vc-cvs.el.
715 The difference to vc-do-command is that this function always invokes `cvs',
716 and that it passes `vc-cvs-global-switches' to it before FLAGS."
717 (apply 'vc-do-command buffer okstatus "cvs" file
718 (if (stringp vc-cvs-global-switches)
719 (cons vc-cvs-global-switches flags)
720 (append vc-cvs-global-switches
721 flags))))
722
723 (defun vc-cvs-stay-local-p (file)
724 "Return non-nil if VC should stay local when handling FILE."
725 (if vc-cvs-stay-local
726 (let* ((dirname (if (file-directory-p file)
727 (directory-file-name file)
728 (file-name-directory file)))
729 (prop
730 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
731 (let ((rootname (expand-file-name "CVS/Root" dirname)))
732 (vc-file-setprop
733 dirname 'vc-cvs-stay-local-p
734 (when (file-readable-p rootname)
735 (with-temp-buffer
736 (vc-insert-file rootname)
737 (goto-char (point-min))
738 (if (looking-at "\\([^:]*\\):")
739 (if (not (stringp vc-cvs-stay-local))
740 'yes
741 (let ((hostname (match-string 1)))
742 (if (string-match vc-cvs-stay-local hostname)
743 'yes
744 'no)))
745 'no))))))))
746 (if (eq prop 'yes) t nil))))
747
748 (defun vc-cvs-parse-status (&optional full)
749 "Parse output of \"cvs status\" command in the current buffer.
750 Set file properties accordingly. Unless FULL is t, parse only
751 essential information."
752 (let (file status)
753 (goto-char (point-min))
754 (if (re-search-forward "^File: " nil t)
755 (cond
756 ((looking-at "no file") nil)
757 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
758 (setq file (expand-file-name (match-string 1)))
759 (vc-file-setprop file 'vc-backend 'CVS)
760 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
761 (setq status "Unknown")
762 (setq status (match-string 1)))
763 (if (and full
764 (re-search-forward
765 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
766 \[\t ]+\\([0-9.]+\\)"
767 nil t))
768 (vc-file-setprop file 'vc-latest-version (match-string 2)))
769 (vc-file-setprop
770 file 'vc-state
771 (cond
772 ((string-match "Up-to-date" status)
773 (vc-file-setprop file 'vc-checkout-time
774 (nth 5 (file-attributes file)))
775 'up-to-date)
776 ((string-match "Locally Modified" status) 'edited)
777 ((string-match "Needs Merge" status) 'needs-merge)
778 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
779 (t 'edited))))))))
780
781 (defun vc-cvs-dir-state-heuristic (dir)
782 "Find the CVS state of all files in DIR, using only local information."
783 (with-temp-buffer
784 (vc-insert-file (expand-file-name "CVS/Entries" dir))
785 (goto-char (point-min))
786 (while (not (eobp))
787 ;; CVS-removed files are not taken under VC control.
788 (when (looking-at "/\\([^/]*\\)/[^/-]")
789 (let ((file (expand-file-name (match-string 1) dir)))
790 (unless (vc-file-getprop file 'vc-state)
791 (vc-cvs-parse-entry file t))))
792 (forward-line 1))))
793
794
795 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
796 "Return non-nil if TAG is a valid symbolic tag name."
797 ;; According to the CVS manual, a valid symbolic tag must start with
798 ;; an uppercase or lowercase letter and can contain uppercase and
799 ;; lowercase letters, digits, `-', and `_'.
800 (and (string-match "^[a-zA-Z]" tag)
801 (not (string-match "[^a-z0-9A-Z-_]" tag))))
802
803 (defun vc-cvs-valid-version-number-p (tag)
804 "Return non-nil if TAG is a valid version number."
805 (and (string-match "^[0-9]" tag)
806 (not (string-match "[^0-9.]" tag))))
807
808 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
809 "Parse and return the sticky tag as a string.
810 `match-data' is protected."
811 (let ((data (match-data))
812 (tag)
813 (type (cond ((string= match-type "D") 'date)
814 ((string= match-type "T")
815 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
816 'symbolic-name
817 'revision-number))
818 (t nil))))
819 (unwind-protect
820 (progn
821 (cond
822 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
823 ((eq type 'date)
824 (string-match
825 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
826 match-tag)
827 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
828 (month (string-to-number (match-string 2 match-tag)))
829 (day (string-to-number (match-string 3 match-tag)))
830 (hour (string-to-number (match-string 4 match-tag)))
831 (min (string-to-number (match-string 5 match-tag)))
832 (sec (string-to-number (match-string 6 match-tag)))
833 ;; Years 0..68 are 2000..2068.
834 ;; Years 69..99 are 1969..1999.
835 (year (+ (cond ((> 69 year-tmp) 2000)
836 ((> 100 year-tmp) 1900)
837 (t 0))
838 year-tmp)))
839 (setq tag (encode-time sec min hour day month year))))
840 ;; Sticky Tag name or revision number
841 ((eq type 'symbolic-name) (setq tag match-tag))
842 ((eq type 'revision-number) (setq tag match-tag))
843 ;; Default is no sticky tag at all
844 (t nil))
845 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
846 ((eq vc-cvs-sticky-tag-display t)
847 (cond ((eq type 'date) (format-time-string
848 vc-cvs-sticky-date-format-string
849 tag))
850 ((eq type 'symbolic-name) tag)
851 ((eq type 'revision-number) tag)
852 (t nil)))
853 ((functionp vc-cvs-sticky-tag-display)
854 (funcall vc-cvs-sticky-tag-display tag type))
855 (t nil)))
856
857 (set-match-data data))))
858
859 (defun vc-cvs-parse-entry (file &optional set-state)
860 "Parse a line from CVS/Entries.
861 Compare modification time to that of the FILE, set file properties
862 accordingly. However, `vc-state' is set only if optional arg SET-STATE
863 is non-nil."
864 (cond
865 ;; entry for a "locally added" file (not yet committed)
866 ((looking-at "/[^/]+/0/")
867 (vc-file-setprop file 'vc-checkout-time 0)
868 (vc-file-setprop file 'vc-workfile-version "0")
869 (if set-state (vc-file-setprop file 'vc-state 'edited)))
870 ;; normal entry
871 ((looking-at
872 (concat "/[^/]+"
873 ;; revision
874 "/\\([^/]*\\)"
875 ;; timestamp
876 "/\\([^/]*\\)"
877 ;; optional conflict field
878 "\\(+[^/]*\\)?/"
879 ;; options
880 "\\([^/]*\\)/"
881 ;; sticky tag
882 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
883 "\\(.*\\)")) ;Sticky tag
884 (vc-file-setprop file 'vc-workfile-version (match-string 1))
885 (vc-file-setprop file 'vc-cvs-sticky-tag
886 (vc-cvs-parse-sticky-tag (match-string 5) (match-string 6)))
887 ;; compare checkout time and modification time
888 (let ((mtime (nth 5 (file-attributes file))))
889 (require 'parse-time)
890 (let ((parsed-time
891 (parse-time-string (concat (match-string 2) " +0000"))))
892 (cond ((and (car parsed-time)
893 (equal mtime (apply 'encode-time parsed-time)))
894 (vc-file-setprop file 'vc-checkout-time mtime)
895 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
896 (t
897 (vc-file-setprop file 'vc-checkout-time 0)
898 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
899
900 (provide 'vc-cvs)
901
902 ;;; vc-cvs.el ends here