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