(ldap-search-internal): Skip error message from ldapsearch. Allow listing
[bpt/emacs.git] / lisp / vc-cvs.el
CommitLineData
c1b25099
GM
1;;; vc-cvs.el --- non-resident support for CVS version-control
2
54be33bc 3;; Copyright (C) 1995,98,99,2000,2001 Free Software Foundation, Inc.
c1b25099
GM
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
f0529b5b 8;; $Id: vc-cvs.el,v 1.28 2001/11/30 13:47:39 spiegel Exp $
c1b25099
GM
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
ed5d446e 31(eval-when-compile
54be33bc 32 (require 'vc))
89e65817 33
9b0630e5 34;;;
8f98485f
AS
35;;; Customization options
36;;;
37
c1b25099
GM
38(defcustom vc-cvs-register-switches nil
39 "*Extra switches for registering a file into CVS.
40A string or list of strings passed to the checkin program by
41\\[vc-register]."
42 :type '(choice (const :tag "None" nil)
43 (string :tag "Argument String")
44 (repeat :tag "Argument List"
45 :value ("")
46 string))
0d685c4f 47 :version "21.1"
c1b25099
GM
48 :group 'vc)
49
633147f5
AS
50(defcustom vc-cvs-diff-switches nil
51 "*A string or list of strings specifying extra switches for cvs diff under VC."
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
c1b25099
GM
60(defcustom vc-cvs-header (or (cdr (assoc 'CVS vc-header-alist)) '("\$Id\$"))
61 "*Header keywords to be inserted by `vc-insert-headers'."
0d685c4f 62 :version "21.1"
ee3275df 63 :type '(repeat string)
c1b25099
GM
64 :group 'vc)
65
66(defcustom vc-cvs-use-edit t
67 "*Non-nil means to use `cvs edit' to \"check out\" a file.
68This is only meaningful if you don't use the implicit checkout model
69\(i.e. if you have $CVSREAD set)."
70 :type 'boolean
0d685c4f 71 :version "21.1"
c1b25099
GM
72 :group 'vc)
73
74(defcustom vc-cvs-stay-local t
0d685c4f 75 "*Non-nil means use local operations when possible for remote repositories.
c6a84d17
SM
76This avoids slow queries over the network and instead uses heuristics
77and past information to determine the current status of a file.
78The value can also be a regular expression to match against the host name
79of a repository; then VC only stays local for hosts that match it."
c1b25099
GM
80 :type '(choice (const :tag "Always stay local" t)
81 (string :tag "Host regexp")
82 (const :tag "Don't stay local" nil))
0d685c4f 83 :version "21.1"
c1b25099
GM
84 :group 'vc)
85
9b0630e5 86
8f98485f
AS
87;;;
88;;; Internal variables
89;;;
90
91(defvar vc-cvs-local-month-numbers
92 '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4)
93 ("May" . 5) ("Jun" . 6) ("Jul" . 7) ("Aug" . 8)
94 ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12))
95 "Local association list of month numbers.")
96
9b0630e5 97
8f98485f 98;;;
9b0630e5 99;;; State-querying functions
8f98485f
AS
100;;;
101
c1b25099
GM
102;;;###autoload (defun vc-cvs-registered (f)
103;;;###autoload (when (file-readable-p (expand-file-name
104;;;###autoload "CVS/Entries" (file-name-directory f)))
105;;;###autoload (require 'vc-cvs)
106;;;###autoload (vc-cvs-registered f)))
107
108(defun vc-cvs-registered (file)
109 "Check if FILE is CVS registered."
110 (let ((dirname (or (file-name-directory file) ""))
111 (basename (file-name-nondirectory file))
112 ;; make sure that the file name is searched case-sensitively
113 (case-fold-search nil))
114 (if (file-readable-p (expand-file-name "CVS/Entries" dirname))
115 (with-temp-buffer
116 (vc-insert-file (expand-file-name "CVS/Entries" dirname))
117 (goto-char (point-min))
0d685c4f
DL
118 (cond
119 ((re-search-forward
c1b25099
GM
120 (concat "^/" (regexp-quote basename) "/") nil t)
121 (beginning-of-line)
122 (vc-cvs-parse-entry file)
123 t)
124 (t nil)))
125 nil)))
126
c1b25099
GM
127(defun vc-cvs-state (file)
128 "CVS-specific version of `vc-state'."
129 (if (vc-cvs-stay-local-p file)
130 (let ((state (vc-file-getprop file 'vc-state)))
131 ;; If we should stay local, use the heuristic but only if
132 ;; we don't have a more precise state already available.
133 (if (memq state '(up-to-date edited))
134 (vc-cvs-state-heuristic file)
135 state))
136 (with-temp-buffer
137 (cd (file-name-directory file))
138 (vc-do-command t 0 "cvs" file "status")
139 (vc-cvs-parse-status t))))
140
141(defun vc-cvs-state-heuristic (file)
142 "CVS-specific state heuristic."
143 ;; If the file has not changed since checkout, consider it `up-to-date'.
144 ;; Otherwise consider it `edited'.
145 (let ((checkout-time (vc-file-getprop file 'vc-checkout-time))
146 (lastmod (nth 5 (file-attributes file))))
147 (if (equal checkout-time lastmod)
148 'up-to-date
149 'edited)))
150
8f98485f
AS
151(defun vc-cvs-dir-state (dir)
152 "Find the CVS state of all files in DIR."
153 (if (vc-cvs-stay-local-p dir)
154 (vc-cvs-dir-state-heuristic dir)
155 (let ((default-directory dir))
156 ;; Don't specify DIR in this command, the default-directory is
157 ;; enough. Otherwise it might fail with remote repositories.
158 (with-temp-buffer
159 (vc-do-command t 0 "cvs" nil "status" "-l")
160 (goto-char (point-min))
161 (while (re-search-forward "^=+\n\\([^=\n].*\n\\|\n\\)+" nil t)
162 (narrow-to-region (match-beginning 0) (match-end 0))
163 (vc-cvs-parse-status)
164 (goto-char (point-max))
165 (widen))))))
166
167(defun vc-cvs-workfile-version (file)
168 "CVS-specific version of `vc-workfile-version'."
169 ;; There is no need to consult RCS headers under CVS, because we
170 ;; get the workfile version for free when we recognize that a file
171 ;; is registered in CVS.
172 (vc-cvs-registered file)
173 (vc-file-getprop file 'vc-workfile-version))
174
8f98485f
AS
175(defun vc-cvs-checkout-model (file)
176 "CVS-specific version of `vc-checkout-model'."
177 (if (or (getenv "CVSREAD")
178 ;; If the file is not writable (despite CVSREAD being
179 ;; undefined), this is probably because the file is being
180 ;; "watched" by other developers.
181 ;; (If vc-mistrust-permissions was t, we actually shouldn't
182 ;; trust this, but there is no other way to learn this from CVS
183 ;; at the moment (version 1.9).)
184 (string-match "r-..-..-." (nth 8 (file-attributes file))))
185 'announce
186 'implicit))
187
099bd78a
SM
188(defun vc-cvs-mode-line-string (file)
189 "Return string for placement into the modeline for FILE.
190Compared to the default implementation, this function handles the
9b0630e5 191special case of a CVS file that is added but not yet committed."
099bd78a
SM
192 (let ((state (vc-state file))
193 (rev (vc-workfile-version file)))
194 (cond ((string= rev "0")
9b0630e5 195 ;; A file that is added but not yet committed.
099bd78a
SM
196 "CVS @@")
197 ((or (eq state 'up-to-date)
198 (eq state 'needs-patch))
199 (concat "CVS-" rev))
200 ((stringp state)
201 (concat "CVS:" state ":" rev))
202 (t
203 ;; Not just for the 'edited state, but also a fallback
204 ;; for all other states. Think about different symbols
205 ;; for 'needs-patch and 'needs-merge.
206 (concat "CVS:" rev)))))
207
8f98485f
AS
208(defun vc-cvs-dired-state-info (file)
209 "CVS-specific version of `vc-dired-state-info'."
210 (let* ((cvs-state (vc-state file))
c6a84d17
SM
211 (state (cond ((eq cvs-state 'edited) "modified")
212 ((eq cvs-state 'needs-patch) "patch")
213 ((eq cvs-state 'needs-merge) "merge")
8f98485f 214 ;; FIXME: those two states cannot occur right now
c6a84d17
SM
215 ((eq cvs-state 'unlocked-changes) "conflict")
216 ((eq cvs-state 'locally-added) "added")
8f98485f
AS
217 )))
218 (if state (concat "(" state ")"))))
c1b25099 219
9b0630e5 220
8f98485f
AS
221;;;
222;;; State-changing functions
223;;;
c1b25099 224
8f98485f
AS
225(defun vc-cvs-register (file &optional rev comment)
226 "Register FILE into the CVS version-control system.
227COMMENT can be used to provide an initial description of FILE.
c1b25099 228
8f98485f
AS
229`vc-register-switches' and `vc-cvs-register-switches' are passed to
230the CVS command (in that order)."
231 (let ((switches (list
232 (if (stringp vc-register-switches)
233 (list vc-register-switches)
234 vc-register-switches)
235 (if (stringp vc-cvs-register-switches)
236 (list vc-cvs-register-switches)
237 vc-cvs-register-switches))))
9b0630e5 238
8f98485f
AS
239 (apply 'vc-do-command nil 0 "cvs" file
240 "add"
241 (and comment (string-match "[^\t\n ]" comment)
242 (concat "-m" comment))
243 switches)))
c1b25099 244
8f98485f
AS
245(defun vc-cvs-responsible-p (file)
246 "Return non-nil if CVS thinks it is responsible for FILE."
247 (file-directory-p (expand-file-name "CVS"
248 (if (file-directory-p file)
249 file
250 (file-name-directory file)))))
c1b25099 251
8f98485f
AS
252(defun vc-cvs-could-register (file)
253 "Return non-nil if FILE could be registered in CVS.
254This is only possible if CVS is responsible for FILE's directory."
255 (vc-cvs-responsible-p file))
c1b25099
GM
256
257(defun vc-cvs-checkin (file rev comment)
258 "CVS-specific version of `vc-backend-checkin'."
259 (let ((switches (if (stringp vc-checkin-switches)
260 (list vc-checkin-switches)
261 vc-checkin-switches))
262 status)
263 ;; explicit check-in to the trunk requires a double check-in (first
264 ;; unexplicit) (CVS-1.3)
265 (if (and rev (vc-trunk-p rev))
266 (apply 'vc-do-command nil 1 "cvs" file
267 "ci" "-m" "intermediate"
268 switches))
269 (setq status (apply 'vc-do-command nil 1 "cvs" file
270 "ci" (if rev (concat "-r" rev))
271 (concat "-m" comment)
272 switches))
c1b25099
GM
273 (set-buffer "*vc*")
274 (goto-char (point-min))
bee48f25
AS
275 (when (not (zerop status))
276 ;; Check checkin problem.
277 (cond
278 ((re-search-forward "Up-to-date check failed" nil t)
279 (vc-file-setprop file 'vc-state 'needs-merge)
280 (error (substitute-command-keys
281 (concat "Up-to-date check failed: "
282 "type \\[vc-next-action] to merge in changes"))))
283 (t
284 (pop-to-buffer (current-buffer))
285 (goto-char (point-min))
286 (shrink-window-if-larger-than-buffer)
287 (error "Check-in failed"))))
c1b25099
GM
288 ;; Update file properties
289 (vc-file-setprop
290 file 'vc-workfile-version
291 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
292 ;; Forget the checkout model of the file, because we might have
293 ;; guessed wrong when we found the file. After commit, we can
294 ;; tell it from the permissions of the file (see
295 ;; vc-cvs-checkout-model).
296 (vc-file-setprop file 'vc-checkout-model nil)
c1b25099 297 ;; if this was an explicit check-in, remove the sticky tag
6e024fc8 298 (if rev (vc-do-command nil 0 "cvs" file "update" "-A"))))
c1b25099 299
34abd98e 300(defun vc-cvs-checkout (file &optional editable rev workfile)
c1b25099 301 "Retrieve a revision of FILE into a WORKFILE.
34abd98e 302EDITABLE non-nil means that the file should be writable.
c1b25099
GM
303REV is the revision to check out into WORKFILE."
304 (let ((filename (or workfile file))
305 (file-buffer (get-file-buffer file))
306 switches)
307 (message "Checking out %s..." filename)
308 (save-excursion
309 ;; Change buffers to get local value of vc-checkout-switches.
310 (if file-buffer (set-buffer file-buffer))
311 (setq switches (if (stringp vc-checkout-switches)
312 (list vc-checkout-switches)
313 vc-checkout-switches))
314 ;; Save this buffer's default-directory
315 ;; and use save-excursion to make sure it is restored
316 ;; in the same buffer it was saved in.
317 (let ((default-directory default-directory))
318 (save-excursion
319 ;; Adjust the default-directory so that the check-out creates
320 ;; the file in the right place.
321 (setq default-directory (file-name-directory filename))
322 (if workfile
9b0630e5 323 (let ((failed t)
81d995bb 324 (backup-name (if (string= file workfile)
a8b6979f
AS
325 (car (find-backup-file-name filename)))))
326 (when backup-name
9b0630e5 327 (copy-file filename backup-name
a8b6979f
AS
328 'ok-if-already-exists 'keep-date)
329 (unless (file-writable-p filename)
330 (set-file-modes filename
331 (logior (file-modes filename) 128))))
c1b25099
GM
332 (unwind-protect
333 (progn
334 (let ((coding-system-for-read 'no-conversion)
335 (coding-system-for-write 'no-conversion))
336 (with-temp-file filename
337 (apply 'vc-do-command
338 (current-buffer) 0 "cvs" file
339 "-Q" ; suppress diagnostic output
c6a84d17 340 "update"
c1b25099
GM
341 (and rev (not (string= rev ""))
342 (concat "-r" rev))
343 "-p"
344 switches)))
345 (setq failed nil))
9b0630e5 346 (if failed
81d995bb 347 (if backup-name
9b0630e5 348 (rename-file backup-name filename
81d995bb
AS
349 'ok-if-already-exists)
350 (if (file-exists-p filename)
351 (delete-file filename)))
a8b6979f
AS
352 (and backup-name
353 (not vc-make-backup-files)
354 (delete-file backup-name)))))
c1b25099
GM
355 (if (and (file-exists-p file) (not rev))
356 ;; If no revision was specified, just make the file writable
357 ;; if necessary (using `cvs-edit' if requested).
c6a84d17 358 (and editable (not (eq (vc-cvs-checkout-model file) 'implicit))
c1b25099
GM
359 (if vc-cvs-use-edit
360 (vc-do-command nil 0 "cvs" file "edit")
361 (set-file-modes file (logior (file-modes file) 128))
362 (if file-buffer (toggle-read-only -1))))
363 ;; Check out a particular version (or recreate the file).
364 (vc-file-setprop file 'vc-workfile-version nil)
365 (apply 'vc-do-command nil 0 "cvs" file
c6a84d17
SM
366 (and editable
367 (or (not (file-exists-p file))
368 (not (eq (vc-cvs-checkout-model file)
369 'implicit)))
370 "-w")
371 "update"
372 ;; default for verbose checkout: clear the sticky tag so
373 ;; that the actual update will get the head of the trunk
0d685c4f
DL
374 (if (or (not rev) (string= rev ""))
375 "-A"
c1b25099 376 (concat "-r" rev))
099bd78a 377 switches))))
c1b25099
GM
378 (vc-mode-line file)
379 (message "Checking out %s...done" filename)))))
380
99739bbf 381(defun vc-cvs-revert (file &optional contents-done)
8f98485f 382 "Revert FILE to the version it was based on."
99739bbf
AS
383 (unless contents-done
384 ;; Check out via standard output (caused by the final argument
385 ;; FILE below), so that no sticky tag is set.
386 (vc-cvs-checkout file nil (vc-workfile-version file) file))
387 (unless (eq (vc-checkout-model file) 'implicit)
388 (if vc-cvs-use-edit
389 (vc-do-command nil 0 "cvs" file "unedit")
390 ;; Make the file read-only by switching off all w-bits
391 (set-file-modes file (logand (file-modes file) 3950)))))
8f98485f
AS
392
393(defun vc-cvs-merge (file first-version &optional second-version)
394 "Merge changes into current working copy of FILE.
395The changes are between FIRST-VERSION and SECOND-VERSION."
396 (vc-do-command nil 0 "cvs" file
397 "update" "-kk"
398 (concat "-j" first-version)
399 (concat "-j" second-version))
400 (vc-file-setprop file 'vc-state 'edited)
401 (save-excursion
402 (set-buffer (get-buffer "*vc*"))
403 (goto-char (point-min))
404 (if (re-search-forward "conflicts during merge" nil t)
405 1 ; signal error
406 0))) ; signal success
407
408(defun vc-cvs-merge-news (file)
409 "Merge in any new changes made to FILE."
410 (message "Merging changes into %s..." file)
411 (save-excursion
412 ;; (vc-file-setprop file 'vc-workfile-version nil)
413 (vc-file-setprop file 'vc-checkout-time 0)
414 (vc-do-command nil 0 "cvs" file "update")
415 ;; Analyze the merge result reported by CVS, and set
416 ;; file properties accordingly.
417 (set-buffer (get-buffer "*vc*"))
418 (goto-char (point-min))
419 ;; get new workfile version
420 (if (re-search-forward (concat "^Merging differences between "
421 "[01234567890.]* and "
422 "\\([01234567890.]*\\) into")
423 nil t)
424 (vc-file-setprop file 'vc-workfile-version (match-string 1))
425 (vc-file-setprop file 'vc-workfile-version nil))
426 ;; get file status
427 (prog1
428 (if (eq (buffer-size) 0)
429 0 ;; there were no news; indicate success
430 (if (re-search-forward
431 (concat "^\\([CMUP] \\)?"
432 (regexp-quote (file-name-nondirectory file))
433 "\\( already contains the differences between \\)?")
434 nil t)
435 (cond
436 ;; Merge successful, we are in sync with repository now
437 ((or (match-string 2)
438 (string= (match-string 1) "U ")
439 (string= (match-string 1) "P "))
440 (vc-file-setprop file 'vc-state 'up-to-date)
441 (vc-file-setprop file 'vc-checkout-time
442 (nth 5 (file-attributes file)))
443 0);; indicate success to the caller
444 ;; Merge successful, but our own changes are still in the file
445 ((string= (match-string 1) "M ")
446 (vc-file-setprop file 'vc-state 'edited)
447 0);; indicate success to the caller
448 ;; Conflicts detected!
449 (t
450 (vc-file-setprop file 'vc-state 'edited)
451 1);; signal the error to the caller
452 )
453 (pop-to-buffer "*vc*")
454 (error "Couldn't analyze cvs update result")))
455 (message "Merging changes into %s...done" file))))
456
9b0630e5 457
8f98485f
AS
458;;;
459;;; History functions
460;;;
461
462(defun vc-cvs-print-log (file)
463 "Get change log associated with FILE."
8f9ab403
EZ
464 (vc-do-command
465 nil
466 (if (and (vc-cvs-stay-local-p file) (fboundp 'start-process)) 'async 0)
467 "cvs" file "log"))
8f98485f
AS
468
469(defun vc-cvs-show-log-entry (version)
470 (when (re-search-forward
471 ;; also match some context, for safety
472 (concat "----\nrevision " version
473 "\\(\tlocked by:.*\n\\|\n\\)date: ") nil t)
474 ;; set the display window so that
475 ;; the whole log entry is displayed
476 (let (start end lines)
477 (beginning-of-line) (forward-line -1) (setq start (point))
478 (if (not (re-search-forward "^----*\nrevision" nil t))
479 (setq end (point-max))
480 (beginning-of-line) (forward-line -1) (setq end (point)))
481 (setq lines (count-lines start end))
482 (cond
483 ;; if the global information and this log entry fit
484 ;; into the window, display from the beginning
485 ((< (count-lines (point-min) end) (window-height))
486 (goto-char (point-min))
487 (recenter 0)
488 (goto-char start))
489 ;; if the whole entry fits into the window,
490 ;; display it centered
491 ((< (1+ lines) (window-height))
492 (goto-char start)
493 (recenter (1- (- (/ (window-height) 2) (/ lines 2)))))
494 ;; otherwise (the entry is too large for the window),
495 ;; display from the start
496 (t
497 (goto-char start)
498 (recenter 0))))))
499
500(defun vc-cvs-diff (file &optional oldvers newvers)
501 "Get a difference report using CVS between two versions of FILE."
633147f5 502 (let (options status (diff-switches-list (vc-diff-switches-list cvs)))
8f98485f
AS
503 (if (string= (vc-workfile-version file) "0")
504 ;; This file is added but not yet committed; there is no master file.
505 (if (or oldvers newvers)
506 (error "No revisions of %s exist" file)
507 ;; we regard this as "changed".
508 ;; diff it against /dev/null.
6e024fc8 509 (apply 'vc-do-command "*vc-diff*"
8f98485f
AS
510 1 "diff" file
511 (append diff-switches-list '("/dev/null"))))
512 (setq status
6e024fc8 513 (apply 'vc-do-command "*vc-diff*"
8f9ab403
EZ
514 (if (and (vc-cvs-stay-local-p file)
515 (fboundp 'start-process))
516 'async
517 1)
8f98485f
AS
518 "cvs" file "diff"
519 (and oldvers (concat "-r" oldvers))
520 (and newvers (concat "-r" newvers))
521 diff-switches-list))
9b0630e5
SS
522 (if (vc-cvs-stay-local-p file)
523 1 ;; async diff, pessimistic assumption
8f98485f
AS
524 status))))
525
5e0fdc5a
AS
526(defun vc-cvs-diff-tree (dir &optional rev1 rev2)
527 "Diff all files at and below DIR."
528 (with-current-buffer "*vc-diff*"
529 (setq default-directory dir)
530 (if (vc-cvs-stay-local-p dir)
531 ;; local diff: do it filewise, and only for files that are modified
532 (vc-file-tree-walk
533 dir
534 (lambda (f)
535 (vc-exec-after
536 `(let ((coding-system-for-read (vc-coding-system-for-diff ',f)))
537 ;; possible optimization: fetch the state of all files
538 ;; in the tree via vc-cvs-dir-state-heuristic
539 (unless (vc-up-to-date-p ',f)
540 (message "Looking at %s" ',f)
541 (vc-diff-internal ',f ',rel1 ',rel2))))))
542 ;; cvs diff: use a single call for the entire tree
543 (let ((coding-system-for-read
544 (or coding-system-for-read 'undecided)))
545 (apply 'vc-do-command "*vc-diff*" 1 "cvs" nil "diff"
546 (and rel1 (concat "-r" rel1))
547 (and rel2 (concat "-r" rel2))
548 (vc-diff-switches-list cvs))))))
549
8f98485f
AS
550(defun vc-cvs-annotate-command (file buffer &optional version)
551 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
552Optional arg VERSION is a version to annotate from."
553 (vc-do-command buffer 0 "cvs" file "annotate" (if version
554 (concat "-r" version))))
c1b25099 555
8ba2df32
AS
556(defun vc-cvs-annotate-current-time ()
557 "Return the current time, based at midnight of the current day, and
558encoded as fractional days."
559 (vc-annotate-convert-time
560 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
561
562(defun vc-cvs-annotate-time ()
563 "Return the time of the next annotation (as fraction of days)
f0529b5b 564systime, or nil if there is none."
8ba2df32
AS
565 (let ((time-stamp
566 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): "))
567 (if (looking-at time-stamp)
c1b25099
GM
568 (progn
569 (let* ((day (string-to-number (match-string 1)))
8ba2df32
AS
570 (month (cdr (assoc (match-string 2)
571 vc-cvs-local-month-numbers)))
c1b25099
GM
572 (year-tmp (string-to-number (match-string 3)))
573 ;; Years 0..68 are 2000..2068.
574 ;; Years 69..99 are 1969..1999.
575 (year (+ (cond ((> 69 year-tmp) 2000)
576 ((> 100 year-tmp) 1900)
577 (t 0))
578 year-tmp)))
579 (goto-char (match-end 0)) ; Position at end makes for nicer overlay result
8ba2df32 580 (vc-annotate-convert-time (encode-time 0 0 0 day month year))))
c1b25099
GM
581 ;; If we did not look directly at an annotation, there might be
582 ;; some further down. This is the case if we are positioned at
583 ;; the very top of the buffer, for instance.
8ba2df32 584 (if (re-search-forward time-stamp nil t)
c1b25099
GM
585 (progn
586 (beginning-of-line nil)
8ba2df32 587 (vc-cvs-annotate-time))))))
9b0630e5 588
8f98485f
AS
589;;;
590;;; Snapshot system
591;;;
592
593(defun vc-cvs-create-snapshot (dir name branchp)
594 "Assign to DIR's current version a given NAME.
595If BRANCHP is non-nil, the name is created as a branch (and the current
596workspace is immediately moved to that new branch)."
597 (vc-do-command nil 0 "cvs" dir "tag" "-c" (if branchp "-b") name)
598 (when branchp (vc-do-command nil 0 "cvs" dir "update" "-r" name)))
599
600(defun vc-cvs-retrieve-snapshot (dir name update)
601 "Retrieve a snapshot at and below DIR.
602NAME is the name of the snapshot; if it is empty, do a `cvs update'.
603If UPDATE is non-nil, then update (resynch) any affected buffers."
604 (with-current-buffer (get-buffer-create "*vc*")
605 (let ((default-directory dir))
606 (erase-buffer)
607 (if (or (not name) (string= name ""))
608 (vc-do-command t 0 "cvs" nil "update")
609 (vc-do-command t 0 "cvs" nil "update" "-r" name))
610 (when update
611 (goto-char (point-min))
612 (while (not (eobp))
613 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
614 (let* ((file (expand-file-name (match-string 2) dir))
615 (state (match-string 1))
616 (buffer (find-buffer-visiting file)))
617 (when buffer
618 (cond
619 ((or (string= state "U")
620 (string= state "P"))
621 (vc-file-setprop file 'vc-state 'up-to-date)
622 (vc-file-setprop file 'vc-workfile-version nil)
623 (vc-file-setprop file 'vc-checkout-time
624 (nth 5 (file-attributes file))))
625 ((or (string= state "M")
626 (string= state "C"))
627 (vc-file-setprop file 'vc-state 'edited)
628 (vc-file-setprop file 'vc-workfile-version nil)
629 (vc-file-setprop file 'vc-checkout-time 0)))
630 (vc-resynch-buffer file t t))))
631 (forward-line 1))))))
632
9b0630e5 633
8f98485f
AS
634;;;
635;;; Miscellaneous
636;;;
637
638(defun vc-cvs-make-version-backups-p (file)
639 "Return non-nil if version backups should be made for FILE."
640 (vc-cvs-stay-local-p file))
641
642(defun vc-cvs-check-headers ()
643 "Check if the current file has any headers in it."
644 (save-excursion
645 (goto-char (point-min))
646 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
647\\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
648
9b0630e5 649
8f98485f
AS
650;;;
651;;; Internal functions
652;;;
653
654(defun vc-cvs-stay-local-p (file)
655 "Return non-nil if VC should stay local when handling FILE."
656 (if vc-cvs-stay-local
657 (let* ((dirname (if (file-directory-p file)
658 (directory-file-name file)
659 (file-name-directory file)))
660 (prop
661 (or (vc-file-getprop dirname 'vc-cvs-stay-local-p)
662 (let ((rootname (expand-file-name "CVS/Root" dirname)))
663 (vc-file-setprop
664 dirname 'vc-cvs-stay-local-p
665 (when (file-readable-p rootname)
666 (with-temp-buffer
667 (vc-insert-file rootname)
668 (goto-char (point-min))
669 (if (looking-at "\\([^:]*\\):")
670 (if (not (stringp vc-cvs-stay-local))
671 'yes
672 (let ((hostname (match-string 1)))
673 (if (string-match vc-cvs-stay-local hostname)
674 'yes
675 'no)))
676 'no))))))))
677 (if (eq prop 'yes) t nil))))
678
679(defun vc-cvs-parse-status (&optional full)
680 "Parse output of \"cvs status\" command in the current buffer.
681Set file properties accordingly. Unless FULL is t, parse only
682essential information."
683 (let (file status)
684 (goto-char (point-min))
685 (if (re-search-forward "^File: " nil t)
686 (cond
687 ((looking-at "no file") nil)
688 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
689 (setq file (expand-file-name (match-string 1)))
690 (vc-file-setprop file 'vc-backend 'CVS)
691 (if (not (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t))
692 (setq status "Unknown")
693 (setq status (match-string 1)))
694 (if (and full
695 (re-search-forward
44f663a4 696 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
8f98485f
AS
697\[\t ]+\\([0-9.]+\\)"
698 nil t))
699 (vc-file-setprop file 'vc-latest-version (match-string 2)))
44f663a4
AS
700 (vc-file-setprop
701 file 'vc-state
702 (cond
703 ((string-match "Up-to-date" status)
704 (vc-file-setprop file 'vc-checkout-time
705 (nth 5 (file-attributes file)))
706 'up-to-date)
707 ((string-match "Locally Modified" status) 'edited)
708 ((string-match "Needs Merge" status) 'needs-merge)
709 ((string-match "Needs \\(Checkout\\|Patch\\)" status) 'needs-patch)
710 (t 'edited))))))))
8f98485f
AS
711
712(defun vc-cvs-dir-state-heuristic (dir)
713 "Find the CVS state of all files in DIR, using only local information."
714 (with-temp-buffer
715 (vc-insert-file (expand-file-name "CVS/Entries" dir))
716 (goto-char (point-min))
717 (while (not (eobp))
718 (when (looking-at "/\\([^/]*\\)/")
719 (let ((file (expand-file-name (match-string 1) dir)))
720 (unless (vc-file-getprop file 'vc-state)
721 (vc-cvs-parse-entry file t))))
722 (forward-line 1))))
723
724(defun vc-cvs-parse-entry (file &optional set-state)
725 "Parse a line from CVS/Entries.
726Compare modification time to that of the FILE, set file properties
727accordingly. However, `vc-state' is set only if optional arg SET-STATE
728is non-nil."
729 (cond
730 ;; entry for a "locally added" file (not yet committed)
731 ((looking-at "/[^/]+/0/")
732 (vc-file-setprop file 'vc-checkout-time 0)
733 (vc-file-setprop file 'vc-workfile-version "0")
734 (if set-state (vc-file-setprop file 'vc-state 'edited)))
735 ;; normal entry
736 ((looking-at
737 (concat "/[^/]+"
738 ;; revision
739 "/\\([^/]*\\)"
740 ;; timestamp
4a9b4f30 741 "/\\([^/]*\\)"))
8f98485f
AS
742 (vc-file-setprop file 'vc-workfile-version (match-string 1))
743 ;; compare checkout time and modification time
4a9b4f30
SM
744 (let ((mtime (nth 5 (file-attributes file)))
745 (system-time-locale "C"))
746 (cond ((equal (format-time-string "%c" mtime 'utc) (match-string 2))
8f98485f
AS
747 (vc-file-setprop file 'vc-checkout-time mtime)
748 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
749 (t
750 (vc-file-setprop file 'vc-checkout-time 0)
4a9b4f30
SM
751 (if set-state (vc-file-setprop file 'vc-state 'edited))))))))
752
c1b25099
GM
753(provide 'vc-cvs)
754
755;;; vc-cvs.el ends here