(init_jpeg_functions, jpeg_resync_to_restart_wrapper):
[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.49 2002/10/17 15:43:48 lektu 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 (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))))))
223
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))
231
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))
244
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))
264 (t
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)))))
269
270 (defun vc-cvs-dired-state-info (file)
271 "CVS-specific version of `vc-dired-state-info'."
272 (let* ((cvs-state (vc-state file))
273 (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")
279 )))
280 (if state (concat "(" state ")"))))
281
282
283 ;;;
284 ;;; State-changing functions
285 ;;;
286
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.
290
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))))
300
301 (apply 'vc-cvs-command nil 0 file
302 "add"
303 (and comment (string-match "[^\t\n ]" comment)
304 (concat "-m" comment))
305 switches)))
306
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)))))
313
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))
318
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"))))
351 (t
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)
365
366 ;; if this was an explicit check-in (does not include creation of
367 ;; a branch), remove the sticky tag.
368 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
369 (vc-cvs-command nil 0 file "update" "-A"))))
370
371 (defun vc-cvs-find-version (file rev buffer)
372 (apply 'vc-cvs-command
373 buffer 0 file
374 "-Q" ; suppress diagnostic output
375 "update"
376 (and rev (not (string= rev ""))
377 (concat "-r" rev))
378 "-p"
379 (if (stringp vc-checkout-switches)
380 (list vc-checkout-switches)
381 vc-checkout-switches)))
382
383 (defun vc-cvs-checkout (file &optional editable rev workfile)
384 "Retrieve a revision of FILE into a WORKFILE.
385 EDITABLE non-nil means that the file should be writable.
386 REV is the revision to check out into WORKFILE."
387 (let ((filename (or workfile file))
388 (file-buffer (get-file-buffer file))
389 switches)
390 (message "Checking out %s..." filename)
391 (save-excursion
392 ;; Change buffers to get local value of vc-checkout-switches.
393 (if file-buffer (set-buffer file-buffer))
394 (setq switches (if (stringp vc-checkout-switches)
395 (list vc-checkout-switches)
396 vc-checkout-switches))
397 ;; Save this buffer's default-directory
398 ;; and use save-excursion to make sure it is restored
399 ;; in the same buffer it was saved in.
400 (let ((default-directory default-directory))
401 (save-excursion
402 ;; Adjust the default-directory so that the check-out creates
403 ;; the file in the right place.
404 (setq default-directory (file-name-directory filename))
405 (if workfile
406 (let ((failed t)
407 (backup-name (if (string= file workfile)
408 (car (find-backup-file-name filename)))))
409 (when backup-name
410 (copy-file filename backup-name
411 'ok-if-already-exists 'keep-date)
412 (unless (file-writable-p filename)
413 (set-file-modes filename
414 (logior (file-modes filename) 128))))
415 (unwind-protect
416 (progn
417 (let ((coding-system-for-read 'no-conversion)
418 (coding-system-for-write 'no-conversion))
419 (with-temp-file filename
420 (apply 'vc-cvs-command
421 (current-buffer) 0 file
422 "-Q" ; suppress diagnostic output
423 "update"
424 (and (stringp rev)
425 (not (string= rev ""))
426 (concat "-r" rev))
427 "-p"
428 switches)))
429 (setq failed nil))
430 (if failed
431 (if backup-name
432 (rename-file backup-name filename
433 'ok-if-already-exists)
434 (if (file-exists-p filename)
435 (delete-file filename)))
436 (and backup-name
437 (not vc-make-backup-files)
438 (delete-file backup-name)))))
439 (if (and (file-exists-p file) (not rev))
440 ;; If no revision was specified, just make the file writable
441 ;; if necessary (using `cvs-edit' if requested).
442 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
443 (if vc-cvs-use-edit
444 (vc-cvs-command nil 0 file "edit")
445 (set-file-modes file (logior (file-modes file) 128))
446 (if file-buffer (toggle-read-only -1))))
447 ;; Check out a particular version (or recreate the file).
448 (vc-file-setprop file 'vc-workfile-version nil)
449 (apply 'vc-cvs-command nil 0 file
450 (and editable
451 (or (not (file-exists-p file))
452 (not (eq (vc-cvs-checkout-model file)
453 'implicit)))
454 "-w")
455 "update"
456 ;; default for verbose checkout: clear the sticky tag so
457 ;; that the actual update will get the head of the trunk
458 (if (or (not rev) (eq rev t) (string= rev ""))
459 "-A"
460 (concat "-r" rev))
461 switches))))
462 (vc-mode-line file)
463 (message "Checking out %s...done" filename)))))
464
465 (defun vc-cvs-revert (file &optional contents-done)
466 "Revert FILE to the version it was based on."
467 (unless contents-done
468 ;; Check out via standard output (caused by the final argument
469 ;; FILE below), so that no sticky tag is set.
470 (vc-cvs-checkout file nil (vc-workfile-version file) file))
471 (unless (eq (vc-checkout-model file) 'implicit)
472 (if vc-cvs-use-edit
473 (vc-cvs-command nil 0 file "unedit")
474 ;; Make the file read-only by switching off all w-bits
475 (set-file-modes file (logand (file-modes file) 3950)))))
476
477 (defun vc-cvs-merge (file first-version &optional second-version)
478 "Merge changes into current working copy of FILE.
479 The changes are between FIRST-VERSION and SECOND-VERSION."
480 (vc-cvs-command nil 0 file
481 "update" "-kk"
482 (concat "-j" first-version)
483 (concat "-j" second-version))
484 (vc-file-setprop file 'vc-state 'edited)
485 (save-excursion
486 (set-buffer (get-buffer "*vc*"))
487 (goto-char (point-min))
488 (if (re-search-forward "conflicts during merge" nil t)
489 1 ; signal error
490 0))) ; signal success
491
492 (defun vc-cvs-merge-news (file)
493 "Merge in any new changes made to FILE."
494 (message "Merging changes into %s..." file)
495 (save-excursion
496 ;; (vc-file-setprop file 'vc-workfile-version nil)
497 (vc-file-setprop file 'vc-checkout-time 0)
498 (vc-cvs-command nil 0 file "update")
499 ;; Analyze the merge result reported by CVS, and set
500 ;; file properties accordingly.
501 (set-buffer (get-buffer "*vc*"))
502 (goto-char (point-min))
503 ;; get new workfile version
504 (if (re-search-forward (concat "^Merging differences between "
505 "[01234567890.]* and "
506 "\\([01234567890.]*\\) into")
507 nil t)
508 (vc-file-setprop file 'vc-workfile-version (match-string 1))
509 (vc-file-setprop file 'vc-workfile-version nil))
510 ;; get file status
511 (prog1
512 (if (eq (buffer-size) 0)
513 0 ;; there were no news; indicate success
514 (if (re-search-forward
515 (concat "^\\([CMUP] \\)?"
516 (regexp-quote (file-name-nondirectory file))
517 "\\( already contains the differences between \\)?")
518 nil t)
519 (cond
520 ;; Merge successful, we are in sync with repository now
521 ((or (match-string 2)
522 (string= (match-string 1) "U ")
523 (string= (match-string 1) "P "))
524 (vc-file-setprop file 'vc-state 'up-to-date)
525 (vc-file-setprop file 'vc-checkout-time
526 (nth 5 (file-attributes file)))
527 0);; indicate success to the caller
528 ;; Merge successful, but our own changes are still in the file
529 ((string= (match-string 1) "M ")
530 (vc-file-setprop file 'vc-state 'edited)
531 0);; indicate success to the caller
532 ;; Conflicts detected!
533 (t
534 (vc-file-setprop file 'vc-state 'edited)
535 1);; signal the error to the caller
536 )
537 (pop-to-buffer "*vc*")
538 (error "Couldn't analyze cvs update result")))
539 (message "Merging changes into %s...done" file))))
540
541
542 ;;;
543 ;;; History functions
544 ;;;
545
546 (defun vc-cvs-print-log (file)
547 "Get change log associated with FILE."
548 (vc-cvs-command
549 nil
550 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
551 file "log"))
552
553 (defun vc-cvs-diff (file &optional oldvers newvers)
554 "Get a difference report using CVS between two versions of FILE."
555 (let (options status (diff-switches-list (vc-diff-switches-list 'CVS)))
556 (if (string= (vc-workfile-version file) "0")
557 ;; This file is added but not yet committed; there is no master file.
558 (if (or oldvers newvers)
559 (error "No revisions of %s exist" file)
560 ;; We regard this as "changed".
561 ;; Diff it against /dev/null.
562 ;; Note: this is NOT a "cvs diff".
563 (apply 'vc-do-command "*vc-diff*"
564 1 "diff" file
565 (append diff-switches-list '("/dev/null"))))
566 (setq status
567 (apply 'vc-cvs-command "*vc-diff*"
568 (if (and (vc-cvs-stay-local-p file)
569 (fboundp 'start-process))
570 'async
571 1)
572 file "diff"
573 (and oldvers (concat "-r" oldvers))
574 (and newvers (concat "-r" newvers))
575 diff-switches-list))
576 (if (vc-cvs-stay-local-p file)
577 1 ;; async diff, pessimistic assumption
578 status))))
579
580 (defun vc-cvs-diff-tree (dir &optional rev1 rev2)
581 "Diff all files at and below DIR."
582 (with-current-buffer "*vc-diff*"
583 (setq default-directory dir)
584 (if (vc-cvs-stay-local-p dir)
585 ;; local diff: do it filewise, and only for files that are modified
586 (vc-file-tree-walk
587 dir
588 (lambda (f)
589 (vc-exec-after
590 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
591 ;; possible optimization: fetch the state of all files
592 ;; in the tree via vc-cvs-dir-state-heuristic
593 (unless (vc-up-to-date-p ',f)
594 (message "Looking at %s" ',f)
595 (vc-diff-internal ',f ',rev1 ',rev2))))))
596 ;; cvs diff: use a single call for the entire tree
597 (let ((coding-system-for-read
598 (or coding-system-for-read 'undecided)))
599 (apply 'vc-cvs-command "*vc-diff*" 1 nil "diff"
600 (and rev1 (concat "-r" rev1))
601 (and rev2 (concat "-r" rev2))
602 (vc-diff-switches-list 'CVS))))))
603
604 (defun vc-cvs-annotate-command (file buffer &optional version)
605 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
606 Optional arg VERSION is a version to annotate from."
607 (vc-cvs-command buffer 0 file "annotate" (if version
608 (concat "-r" version))))
609
610 (defun vc-cvs-annotate-current-time ()
611 "Return the current time, based at midnight of the current day, and
612 encoded as fractional days."
613 (vc-annotate-convert-time
614 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
615
616 (defun vc-cvs-annotate-time ()
617 "Return the time of the next annotation (as fraction of days)
618 systime, or nil if there is none."
619 (let ((time-stamp
620 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
621 (if (looking-at time-stamp)
622 (progn
623 (let* ((day (string-to-number (match-string 1)))
624 (month (cdr (assoc (match-string 2)
625 vc-cvs-local-month-numbers)))
626 (year-tmp (string-to-number (match-string 3)))
627 ;; Years 0..68 are 2000..2068.
628 ;; Years 69..99 are 1969..1999.
629 (year (+ (cond ((> 69 year-tmp) 2000)
630 ((> 100 year-tmp) 1900)
631 (t 0))
632 year-tmp)))
633 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
634 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
635 ;; If we did not look directly at an annotation, there might be
636 ;; some further down. This is the case if we are positioned at
637 ;; the very top of the buffer, for instance.
638 (if (re-search-forward time-stamp nil t)
639 (progn
640 (beginning-of-line nil)
641 (vc-cvs-annotate-time))))))
642
643 ;;;
644 ;;; Snapshot system
645 ;;;
646
647 (defun vc-cvs-create-snapshot (dir name branchp)
648 "Assign to DIR's current version a given NAME.
649 If BRANCHP is non-nil, the name is created as a branch (and the current
650 workspace is immediately moved to that new branch)."
651 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
652 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
653
654 (defun vc-cvs-retrieve-snapshot (dir name update)
655 "Retrieve a snapshot at and below DIR.
656 NAME is the name of the snapshot; if it is empty, do a `cvs update'.
657 If UPDATE is non-nil, then update (resynch) any affected buffers."
658 (with-current-buffer (get-buffer-create "*vc*")
659 (let ((default-directory dir)
660 (sticky-tag))
661 (erase-buffer)
662 (if (or (not name) (string= name ""))
663 (vc-cvs-command t 0 nil "update")
664 (vc-cvs-command t 0 nil "update" "-r" name)
665 (setq sticky-tag name))
666 (when update
667 (goto-char (point-min))
668 (while (not (eobp))
669 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
670 (let* ((file (expand-file-name (match-string 2) dir))
671 (state (match-string 1))
672 (buffer (find-buffer-visiting file)))
673 (when buffer
674 (cond
675 ((or (string= state "U")
676 (string= state "P"))
677 (vc-file-setprop file 'vc-state 'up-to-date)
678 (vc-file-setprop file 'vc-workfile-version nil)
679 (vc-file-setprop file 'vc-checkout-time
680 (nth 5 (file-attributes file))))
681 ((or (string= state "M")
682 (string= state "C"))
683 (vc-file-setprop file 'vc-state 'edited)
684 (vc-file-setprop file 'vc-workfile-version nil)
685 (vc-file-setprop file 'vc-checkout-time 0)))
686 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
687 (vc-resynch-buffer file t t))))
688 (forward-line 1))))))
689
690
691 ;;;
692 ;;; Miscellaneous
693 ;;;
694
695 (defun vc-cvs-make-version-backups-p (file)
696 "Return non-nil if version backups should be made for FILE."
697 (vc-cvs-stay-local-p 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 (defun vc-cvs-stay-local-p (file)
722 "Return non-nil if VC should stay local when handling FILE."
723 (if vc-cvs-stay-local
724 (let* ((dirname (if (file-directory-p file)
725 (directory-file-name file)
726 (file-name-directory file)))
727 (prop
728 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
729 (let ((rootname (expand-file-name "CVS/Root" dirname)))
730 (vc-file-setprop
731 dirname 'vc-cvs-stay-local-p
732 (when (file-readable-p rootname)
733 (with-temp-buffer
734 (vc-insert-file rootname)
735 (goto-char (point-min))
736 (if (looking-at "\\([^:]*\\):")
737 (if (not (stringp vc-cvs-stay-local))
738 'yes
739 (let ((hostname (match-string 1)))
740 (if (string-match vc-cvs-stay-local hostname)
741 'yes
742 'no)))
743 'no))))))))
744 (if (eq prop 'yes) t nil))))
745
746 (defun vc-cvs-parse-status (&optional full)
747 "Parse output of \"cvs status\" command in the current buffer.
748 Set file properties accordingly. Unless FULL is t, parse only
749 essential information."
750 (let (file status)
751 (goto-char (point-min))
752 (if (re-search-forward "^File: " nil t)
753 (cond
754 ((looking-at "no file") nil)
755 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
756 (setq file (expand-file-name (match-string 1)))
757 (vc-file-setprop file 'vc-backend 'CVS)
758 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
759 (setq status "Unknown")
760 (setq status (match-string 1)))
761 (if (and full
762 (re-search-forward
763 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
764 \[\t ]+\\([0-9.]+\\)"
765 nil t))
766 (vc-file-setprop file 'vc-latest-version (match-string 2)))
767 (vc-file-setprop
768 file 'vc-state
769 (cond
770 ((string-match "Up-to-date" status)
771 (vc-file-setprop file 'vc-checkout-time
772 (nth 5 (file-attributes file)))
773 'up-to-date)
774 ((string-match "Locally Modified" status) 'edited)
775 ((string-match "Needs Merge" status) 'needs-merge)
776 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
777 (t 'edited))))))))
778
779 (defun vc-cvs-dir-state-heuristic (dir)
780 "Find the CVS state of all files in DIR, using only local information."
781 (with-temp-buffer
782 (vc-insert-file (expand-file-name "CVS/Entries" dir))
783 (goto-char (point-min))
784 (while (not (eobp))
785 ;; CVS-removed files are not taken under VC control.
786 (when (looking-at "/\\([^/]*\\)/[^/-]")
787 (let ((file (expand-file-name (match-string 1) dir)))
788 (unless (vc-file-getprop file 'vc-state)
789 (vc-cvs-parse-entry file t))))
790 (forward-line 1))))
791
792
793 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
794 "Return non-nil if TAG is a valid symbolic tag name."
795 ;; According to the CVS manual, a valid symbolic tag must start with
796 ;; an uppercase or lowercase letter and can contain uppercase and
797 ;; lowercase letters, digits, `-', and `_'.
798 (and (string-match "^[a-zA-Z]" tag)
799 (not (string-match "[^a-z0-9A-Z-_]" tag))))
800
801 (defun vc-cvs-valid-version-number-p (tag)
802 "Return non-nil if TAG is a valid version number."
803 (and (string-match "^[0-9]" tag)
804 (not (string-match "[^0-9.]" tag))))
805
806 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
807 "Parse and return the sticky tag as a string.
808 `match-data' is protected."
809 (let ((data (match-data))
810 (tag)
811 (type (cond ((string= match-type "D") 'date)
812 ((string= match-type "T")
813 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
814 'symbolic-name
815 'revision-number))
816 (t nil))))
817 (unwind-protect
818 (progn
819 (cond
820 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
821 ((eq type 'date)
822 (string-match
823 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
824 match-tag)
825 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
826 (month (string-to-number (match-string 2 match-tag)))
827 (day (string-to-number (match-string 3 match-tag)))
828 (hour (string-to-number (match-string 4 match-tag)))
829 (min (string-to-number (match-string 5 match-tag)))
830 (sec (string-to-number (match-string 6 match-tag)))
831 ;; Years 0..68 are 2000..2068.
832 ;; Years 69..99 are 1969..1999.
833 (year (+ (cond ((> 69 year-tmp) 2000)
834 ((> 100 year-tmp) 1900)
835 (t 0))
836 year-tmp)))
837 (setq tag (encode-time sec min hour day month year))))
838 ;; Sticky Tag name or revision number
839 ((eq type 'symbolic-name) (setq tag match-tag))
840 ((eq type 'revision-number) (setq tag match-tag))
841 ;; Default is no sticky tag at all
842 (t nil))
843 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
844 ((eq vc-cvs-sticky-tag-display t)
845 (cond ((eq type 'date) (format-time-string
846 vc-cvs-sticky-date-format-string
847 tag))
848 ((eq type 'symbolic-name) tag)
849 ((eq type 'revision-number) tag)
850 (t nil)))
851 ((functionp vc-cvs-sticky-tag-display)
852 (funcall vc-cvs-sticky-tag-display tag type))
853 (t nil)))
854
855 (set-match-data data))))
856
857 (defun vc-cvs-parse-entry (file &optional set-state)
858 "Parse a line from CVS/Entries.
859 Compare modification time to that of the FILE, set file properties
860 accordingly. However, `vc-state' is set only if optional arg SET-STATE
861 is non-nil."
862 (cond
863 ;; entry for a "locally added" file (not yet committed)
864 ((looking-at "/[^/]+/0/")
865 (vc-file-setprop file 'vc-checkout-time 0)
866 (vc-file-setprop file 'vc-workfile-version "0")
867 (if set-state (vc-file-setprop file 'vc-state 'edited)))
868 ;; normal entry
869 ((looking-at
870 (concat "/[^/]+"
871 ;; revision
872 "/\\([^/]*\\)"
873 ;; timestamp
874 "/\\([^/]*\\)"
875 ;; optional conflict field
876 "\\(+[^/]*\\)?/"
877 ;; options
878 "\\([^/]*\\)/"
879 ;; sticky tag
880 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
881 "\\(.*\\)")) ;Sticky tag
882 (vc-file-setprop file 'vc-workfile-version (match-string 1))
883 (vc-file-setprop file 'vc-cvs-sticky-tag
884 (vc-cvs-parse-sticky-tag (match-string 5) (match-string 6)))
885 ;; compare checkout time and modification time
886 (let ((mtime (nth 5 (file-attributes file))))
887 (require 'parse-time)
888 (let ((parsed-time
889 (parse-time-string (concat (match-string 2) " +0000"))))
890 (cond ((and (car parsed-time)
891 (equal mtime (apply 'encode-time parsed-time)))
892 (vc-file-setprop file 'vc-checkout-time mtime)
893 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
894 (t
895 (vc-file-setprop file 'vc-checkout-time 0)
896 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
897
898 (provide 'vc-cvs)
899
900 ;;; vc-cvs.el ends here