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