* xdisp.c (try_scrolling): Check INT_MAX instead of
[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 (or (string= basename "")
200 (with-temp-buffer
201 (vc-cvs-get-entries dirname)
202 (goto-char (point-min))
203 (cond ((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 ((null checkout-time) 'unregistered)
235 (t 'edited))))
236
237 (defun vc-cvs-working-revision (file)
238 "CVS-specific version of `vc-working-revision'."
239 ;; There is no need to consult RCS headers under CVS, because we
240 ;; get the workfile version for free when we recognize that a file
241 ;; is registered in CVS.
242 (vc-cvs-registered file)
243 (vc-file-getprop file 'vc-working-revision))
244
245 (defun vc-cvs-mode-line-string (file)
246 "Return string for placement into the modeline for FILE.
247 Compared to the default implementation, this function does two things:
248 Handle the special case of a CVS file that is added but not yet
249 committed and support display of sticky tags."
250 (let* ((sticky-tag (vc-file-getprop file 'vc-cvs-sticky-tag))
251 help-echo
252 (string
253 (let ((def-ml (vc-default-mode-line-string 'CVS file)))
254 (setq help-echo
255 (get-text-property 0 'help-echo def-ml))
256 def-ml)))
257 (propertize
258 (if (zerop (length sticky-tag))
259 string
260 (setq help-echo (format "%s on the '%s' branch"
261 help-echo sticky-tag))
262 (concat string "[" sticky-tag "]"))
263 'help-echo help-echo)))
264
265
266 ;;;
267 ;;; State-changing functions
268 ;;;
269
270 (defun vc-cvs-register (files &optional rev comment)
271 "Register FILES into the CVS version-control system.
272 COMMENT can be used to provide an initial description of FILES.
273
274 `vc-register-switches' and `vc-cvs-register-switches' are passed to
275 the CVS command (in that order)."
276 ;; Register the directories if needed.
277 (let (dirs)
278 (dolist (file files)
279 (and (not (vc-cvs-responsible-p file))
280 (vc-cvs-could-register file)
281 (push (directory-file-name (file-name-directory file)) dirs)))
282 (if dirs (vc-cvs-register dirs)))
283 (apply 'vc-cvs-command nil 0 files
284 "add"
285 (and comment (string-match "[^\t\n ]" comment)
286 (concat "-m" comment))
287 (vc-switches 'CVS 'register)))
288
289 (defun vc-cvs-responsible-p (file)
290 "Return non-nil if CVS thinks it is responsible for FILE."
291 (file-directory-p (expand-file-name "CVS"
292 (if (file-directory-p file)
293 file
294 (file-name-directory file)))))
295
296 (defun vc-cvs-could-register (file)
297 "Return non-nil if FILE could be registered in CVS.
298 This is only possible if CVS is managing FILE's directory or one of
299 its parents."
300 (let ((dir file))
301 (while (and (stringp dir)
302 (not (equal dir (setq dir (file-name-directory dir))))
303 dir)
304 (setq dir (if (file-directory-p
305 (expand-file-name "CVS/Entries" dir))
306 t (directory-file-name dir))))
307 (eq dir t)))
308
309 (defun vc-cvs-checkin (files rev comment)
310 "CVS-specific version of `vc-backend-checkin'."
311 (unless (or (not rev) (vc-cvs-valid-revision-number-p rev))
312 (if (not (vc-cvs-valid-symbolic-tag-name-p rev))
313 (error "%s is not a valid symbolic tag name" rev)
314 ;; If the input revison is a valid symbolic tag name, we create it
315 ;; as a branch, commit and switch to it.
316 (apply 'vc-cvs-command nil 0 files "tag" "-b" (list rev))
317 (apply 'vc-cvs-command nil 0 files "update" "-r" (list rev))
318 (mapc (lambda (file) (vc-file-setprop file 'vc-cvs-sticky-tag rev))
319 files)))
320 (let ((status (apply 'vc-cvs-command nil 1 files
321 "ci" (if rev (concat "-r" rev))
322 (concat "-m" comment)
323 (vc-switches 'CVS 'checkin))))
324 (set-buffer "*vc*")
325 (goto-char (point-min))
326 (when (not (zerop status))
327 ;; Check checkin problem.
328 (cond
329 ((re-search-forward "Up-to-date check failed" nil t)
330 (mapc (lambda (file) (vc-file-setprop file 'vc-state 'needs-merge))
331 files)
332 (error "%s" (substitute-command-keys
333 (concat "Up-to-date check failed: "
334 "type \\[vc-next-action] to merge in changes"))))
335 (t
336 (pop-to-buffer (current-buffer))
337 (goto-char (point-min))
338 (shrink-window-if-larger-than-buffer)
339 (error "Check-in failed"))))
340 ;; Single-file commit? Then update the revision by parsing the buffer.
341 ;; Otherwise we can't necessarily tell what goes with what; clear
342 ;; its properties so they have to be refetched.
343 (if (= (length files) 1)
344 (vc-file-setprop
345 (car files) 'vc-working-revision
346 (vc-parse-buffer "^\\(new\\|initial\\) revision: \\([0-9.]+\\)" 2))
347 (mapc 'vc-file-clearprops files))
348 ;; Anyway, forget the checkout model of the file, because we might have
349 ;; guessed wrong when we found the file. After commit, we can
350 ;; tell it from the permissions of the file (see
351 ;; vc-cvs-checkout-model).
352 (mapc (lambda (file) (vc-file-setprop file 'vc-checkout-model nil))
353 files)
354
355 ;; if this was an explicit check-in (does not include creation of
356 ;; a branch), remove the sticky tag.
357 (if (and rev (not (vc-cvs-valid-symbolic-tag-name-p rev)))
358 (vc-cvs-command nil 0 files "update" "-A"))))
359
360 (defun vc-cvs-find-revision (file rev buffer)
361 (apply 'vc-cvs-command
362 buffer 0 file
363 "-Q" ; suppress diagnostic output
364 "update"
365 (and rev (not (string= rev ""))
366 (concat "-r" rev))
367 "-p"
368 (vc-switches 'CVS 'checkout)))
369
370 (defun vc-cvs-checkout (file &optional editable rev)
371 "Checkout a revision of FILE into the working area.
372 EDITABLE non-nil means that the file should be writable.
373 REV is the revision to check out."
374 (message "Checking out %s..." file)
375 ;; Change buffers to get local value of vc-checkout-switches.
376 (with-current-buffer (or (get-file-buffer file) (current-buffer))
377 (if (and (file-exists-p file) (not rev))
378 ;; If no revision was specified, just make the file writable
379 ;; if necessary (using `cvs-edit' if requested).
380 (and editable (not (eq (vc-cvs-checkout-model (list file)) 'implicit))
381 (if vc-cvs-use-edit
382 (vc-cvs-command nil 0 file "edit")
383 (set-file-modes file (logior (file-modes file) 128))
384 (if (equal file buffer-file-name) (toggle-read-only -1))))
385 ;; Check out a particular revision (or recreate the file).
386 (vc-file-setprop file 'vc-working-revision nil)
387 (apply 'vc-cvs-command nil 0 file
388 (and editable "-w")
389 "update"
390 (when rev
391 (unless (eq rev t)
392 ;; default for verbose checkout: clear the
393 ;; sticky tag so that the actual update will
394 ;; get the head of the trunk
395 (if (string= rev "")
396 "-A"
397 (concat "-r" rev))))
398 (vc-switches 'CVS 'checkout)))
399 (vc-mode-line file))
400 (message "Checking out %s...done" file))
401
402 (defun vc-cvs-delete-file (file)
403 (vc-cvs-command nil 0 file "remove" "-f"))
404
405 (defun vc-cvs-revert (file &optional contents-done)
406 "Revert FILE to the working revision on which it was based."
407 (vc-default-revert 'CVS file contents-done)
408 (unless (eq (vc-cvs-checkout-model (list file)) 'implicit)
409 (if vc-cvs-use-edit
410 (vc-cvs-command nil 0 file "unedit")
411 ;; Make the file read-only by switching off all w-bits
412 (set-file-modes file (logand (file-modes file) 3950)))))
413
414 (defun vc-cvs-merge (file first-revision &optional second-revision)
415 "Merge changes into current working copy of FILE.
416 The changes are between FIRST-REVISION and SECOND-REVISION."
417 (vc-cvs-command nil 0 file
418 "update" "-kk"
419 (concat "-j" first-revision)
420 (concat "-j" second-revision))
421 (vc-file-setprop file 'vc-state 'edited)
422 (with-current-buffer (get-buffer "*vc*")
423 (goto-char (point-min))
424 (if (re-search-forward "conflicts during merge" nil t)
425 (progn
426 (vc-file-setprop file 'vc-state 'conflict)
427 ;; signal error
428 1)
429 (vc-file-setprop file 'vc-state 'edited)
430 ;; signal success
431 0)))
432
433 (defun vc-cvs-merge-news (file)
434 "Merge in any new changes made to FILE."
435 (message "Merging changes into %s..." file)
436 ;; (vc-file-setprop file 'vc-working-revision nil)
437 (vc-file-setprop file 'vc-checkout-time 0)
438 (vc-cvs-command nil nil file "update")
439 ;; Analyze the merge result reported by CVS, and set
440 ;; file properties accordingly.
441 (with-current-buffer (get-buffer "*vc*")
442 (goto-char (point-min))
443 ;; get new working revision
444 (if (re-search-forward
445 "^Merging differences between [0-9.]* and \\([0-9.]*\\) into" nil t)
446 (vc-file-setprop file 'vc-working-revision (match-string 1))
447 (vc-file-setprop file 'vc-working-revision nil))
448 ;; get file status
449 (prog1
450 (if (eq (buffer-size) 0)
451 0 ;; there were no news; indicate success
452 (if (re-search-forward
453 (concat "^\\([CMUP] \\)?"
454 (regexp-quote (file-name-nondirectory file))
455 "\\( already contains the differences between \\)?")
456 nil t)
457 (cond
458 ;; Merge successful, we are in sync with repository now
459 ((or (match-string 2)
460 (string= (match-string 1) "U ")
461 (string= (match-string 1) "P "))
462 (vc-file-setprop file 'vc-state 'up-to-date)
463 (vc-file-setprop file 'vc-checkout-time
464 (nth 5 (file-attributes file)))
465 0);; indicate success to the caller
466 ;; Merge successful, but our own changes are still in the file
467 ((string= (match-string 1) "M ")
468 (vc-file-setprop file 'vc-state 'edited)
469 0);; indicate success to the caller
470 ;; Conflicts detected!
471 (t
472 (vc-file-setprop file 'vc-state 'conflict)
473 1);; signal the error to the caller
474 )
475 (pop-to-buffer "*vc*")
476 (error "Couldn't analyze cvs update result")))
477 (message "Merging changes into %s...done" file))))
478
479 (defun vc-cvs-modify-change-comment (files rev comment)
480 "Modify the change comments for FILES on a specified REV.
481 Will fail unless you have administrative privileges on the repo."
482 (vc-cvs-command nil 0 files "admin" (concat "-m" rev ":" comment)))
483
484 ;;;
485 ;;; History functions
486 ;;;
487
488 (defun vc-cvs-print-log (files &optional buffer)
489 "Get change logs associated with FILES."
490 ;; It's just the catenation of the individual logs.
491 (vc-cvs-command
492 buffer
493 (if (vc-stay-local-p files) 'async 0)
494 files "log"))
495
496 (defun vc-cvs-comment-history (file)
497 "Get comment history of a file."
498 (vc-call-backend 'RCS 'comment-history file))
499
500 (defun vc-cvs-diff (files &optional oldvers newvers buffer)
501 "Get a difference report using CVS between two revisions of FILE."
502 (let* ((async (and (not vc-disable-async-diff)
503 (vc-stay-local-p files)))
504 (invoke-cvs-diff-list nil)
505 status)
506 ;; Look through the file list and see if any files have backups
507 ;; that can be used to do a plain "diff" instead of "cvs diff".
508 (dolist (file files)
509 (let ((ov oldvers)
510 (nv newvers))
511 (when (or (not ov) (string-equal ov ""))
512 (setq ov (vc-working-revision file)))
513 (when (string-equal nv "")
514 (setq nv nil))
515 (let ((file-oldvers (vc-version-backup-file file ov))
516 (file-newvers (if (not nv)
517 file
518 (vc-version-backup-file file nv)))
519 (coding-system-for-read (vc-coding-system-for-diff file)))
520 (if (and file-oldvers file-newvers)
521 (progn
522 (apply 'vc-do-command (or buffer "*vc-diff*") 1 "diff" nil
523 (append (if (listp diff-switches)
524 diff-switches
525 (list diff-switches))
526 (if (listp vc-diff-switches)
527 vc-diff-switches
528 (list vc-diff-switches))
529 (list (file-relative-name file-oldvers)
530 (file-relative-name file-newvers))))
531 (setq status 0))
532 (push file invoke-cvs-diff-list)))))
533 (when invoke-cvs-diff-list
534 (setq status (apply 'vc-cvs-command (or buffer "*vc-diff*")
535 (if async 'async 1)
536 invoke-cvs-diff-list "diff"
537 (and oldvers (concat "-r" oldvers))
538 (and newvers (concat "-r" newvers))
539 (vc-switches 'CVS 'diff))))
540 (if async 1 status))) ; async diff, pessimistic assumption
541
542 (defconst vc-cvs-annotate-first-line-re "^[0-9]")
543
544 (defun vc-cvs-annotate-process-filter (process string)
545 (setq string (concat (process-get process 'output) string))
546 (if (not (string-match vc-cvs-annotate-first-line-re string))
547 ;; Still waiting for the first real line.
548 (process-put process 'output string)
549 (let ((vc-filter (process-get process 'vc-filter)))
550 (set-process-filter process vc-filter)
551 (funcall vc-filter process (substring string (match-beginning 0))))))
552
553 (defun vc-cvs-annotate-command (file buffer &optional revision)
554 "Execute \"cvs annotate\" on FILE, inserting the contents in BUFFER.
555 Optional arg REVISION is a revision to annotate from."
556 (vc-cvs-command buffer
557 (if (vc-stay-local-p file)
558 'async 0)
559 file "annotate"
560 (if revision (concat "-r" revision)))
561 ;; Strip the leading few lines.
562 (let ((proc (get-buffer-process buffer)))
563 (if proc
564 ;; If running asynchronously, use a process filter.
565 (progn
566 (process-put proc 'vc-filter (process-filter proc))
567 (set-process-filter proc 'vc-cvs-annotate-process-filter))
568 (with-current-buffer buffer
569 (goto-char (point-min))
570 (re-search-forward vc-cvs-annotate-first-line-re)
571 (delete-region (point-min) (1- (point)))))))
572
573 (declare-function vc-annotate-convert-time "vc-annotate" (time))
574
575 (defun vc-cvs-annotate-current-time ()
576 "Return the current time, based at midnight of the current day, and
577 encoded as fractional days."
578 (vc-annotate-convert-time
579 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
580
581 (defun vc-cvs-annotate-time ()
582 "Return the time of the next annotation (as fraction of days)
583 systime, or nil if there is none."
584 (let* ((bol (point))
585 (cache (get-text-property bol 'vc-cvs-annotate-time))
586 (inhibit-read-only t)
587 (inhibit-modification-hooks t))
588 (cond
589 (cache)
590 ((looking-at
591 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
592 (let ((day (string-to-number (match-string 1)))
593 (month (cdr (assq (intern (match-string 2))
594 '((Jan . 1) (Feb . 2) (Mar . 3)
595 (Apr . 4) (May . 5) (Jun . 6)
596 (Jul . 7) (Aug . 8) (Sep . 9)
597 (Oct . 10) (Nov . 11) (Dec . 12)))))
598 (year (let ((tmp (string-to-number (match-string 3))))
599 ;; Years 0..68 are 2000..2068.
600 ;; Years 69..99 are 1969..1999.
601 (+ (cond ((> 69 tmp) 2000)
602 ((> 100 tmp) 1900)
603 (t 0))
604 tmp))))
605 (put-text-property
606 bol (1+ bol) 'vc-cvs-annotate-time
607 (setq cache (cons
608 ;; Position at end makes for nicer overlay result.
609 ;; Don't put actual buffer pos here, but only relative
610 ;; distance, so we don't ever move backward in the
611 ;; goto-char below, even if the text is moved.
612 (- (match-end 0) (match-beginning 0))
613 (vc-annotate-convert-time
614 (encode-time 0 0 0 day month year))))))))
615 (when cache
616 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
617 (cdr cache)))) ; days (float)
618
619 (defun vc-cvs-annotate-extract-revision-at-line ()
620 (save-excursion
621 (beginning-of-line)
622 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
623 (line-end-position) t)
624 (match-string-no-properties 1)
625 nil)))
626
627 ;;;
628 ;;; Tag system
629 ;;;
630
631 (defun vc-cvs-create-tag (dir name branchp)
632 "Assign to DIR's current revision a given NAME.
633 If BRANCHP is non-nil, the name is created as a branch (and the current
634 workspace is immediately moved to that new branch)."
635 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
636 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
637
638 (defun vc-cvs-retrieve-tag (dir name update)
639 "Retrieve a tag at and below DIR.
640 NAME is the name of the tag; if it is empty, do a `cvs update'.
641 If UPDATE is non-nil, then update (resynch) any affected buffers."
642 (with-current-buffer (get-buffer-create "*vc*")
643 (let ((default-directory dir)
644 (sticky-tag))
645 (erase-buffer)
646 (if (or (not name) (string= name ""))
647 (vc-cvs-command t 0 nil "update")
648 (vc-cvs-command t 0 nil "update" "-r" name)
649 (setq sticky-tag name))
650 (when update
651 (goto-char (point-min))
652 (while (not (eobp))
653 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
654 (let* ((file (expand-file-name (match-string 2) dir))
655 (state (match-string 1))
656 (buffer (find-buffer-visiting file)))
657 (when buffer
658 (cond
659 ((or (string= state "U")
660 (string= state "P"))
661 (vc-file-setprop file 'vc-state 'up-to-date)
662 (vc-file-setprop file 'vc-working-revision nil)
663 (vc-file-setprop file 'vc-checkout-time
664 (nth 5 (file-attributes file))))
665 ((or (string= state "M")
666 (string= state "C"))
667 (vc-file-setprop file 'vc-state 'edited)
668 (vc-file-setprop file 'vc-working-revision nil)
669 (vc-file-setprop file 'vc-checkout-time 0)))
670 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
671 (vc-resynch-buffer file t t))))
672 (forward-line 1))))))
673
674
675 ;;;
676 ;;; Miscellaneous
677 ;;;
678
679 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
680 "Return non-nil if version backups should be made for FILE.")
681
682 (defun vc-cvs-check-headers ()
683 "Check if the current file has any headers in it."
684 (save-excursion
685 (goto-char (point-min))
686 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
687 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
688
689
690 ;;;
691 ;;; Internal functions
692 ;;;
693
694 (defun vc-cvs-root (dir)
695 (vc-find-root dir "CVS" t))
696
697 (defun vc-cvs-command (buffer okstatus files &rest flags)
698 "A wrapper around `vc-do-command' for use in vc-cvs.el.
699 The difference to vc-do-command is that this function always invokes `cvs',
700 and that it passes `vc-cvs-global-switches' to it before FLAGS."
701 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
702 (if (stringp vc-cvs-global-switches)
703 (cons vc-cvs-global-switches flags)
704 (append vc-cvs-global-switches
705 flags))))
706
707 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
708
709 (defun vc-cvs-repository-hostname (dirname)
710 "Hostname of the CVS server associated to workarea DIRNAME."
711 (let ((rootname (expand-file-name "CVS/Root" dirname)))
712 (when (file-readable-p rootname)
713 (with-temp-buffer
714 (let ((coding-system-for-read
715 (or file-name-coding-system
716 default-file-name-coding-system)))
717 (vc-insert-file rootname))
718 (goto-char (point-min))
719 (nth 2 (vc-cvs-parse-root
720 (buffer-substring (point)
721 (line-end-position))))))))
722
723 (defun vc-cvs-parse-root (root)
724 "Split CVS ROOT specification string into a list of fields.
725 A CVS root specification of the form
726 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
727 is converted to a normalized record with the following structure:
728 \(METHOD USER HOSTNAME CVS-ROOT).
729 The default METHOD for a CVS root of the form
730 /path/to/repository
731 is `local'.
732 The default METHOD for a CVS root of the form
733 [USER@]HOSTNAME:/path/to/repository
734 is `ext'.
735 For an empty string, nil is returned (invalid CVS root)."
736 ;; Split CVS root into colon separated fields (0-4).
737 ;; The `x:' makes sure, that leading colons are not lost;
738 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
739 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
740 (len (length root-list))
741 ;; All syntactic varieties will get a proper METHOD.
742 (root-list
743 (cond
744 ((= len 0)
745 ;; Invalid CVS root
746 nil)
747 ((= len 1)
748 ;; Simple PATH => method `local'
749 (cons "local"
750 (cons nil root-list)))
751 ((= len 2)
752 ;; [USER@]HOST:PATH => method `ext'
753 (and (not (equal (car root-list) ""))
754 (cons "ext" root-list)))
755 ((= len 3)
756 ;; :METHOD:PATH
757 (cons (cadr root-list)
758 (cons nil (cddr root-list))))
759 (t
760 ;; :METHOD:[USER@]HOST:PATH
761 (cdr root-list)))))
762 (if root-list
763 (let ((method (car root-list))
764 (uhost (or (cadr root-list) ""))
765 (root (nth 2 root-list))
766 user host)
767 ;; Split USER@HOST
768 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
769 (setq user (match-string 1 uhost)
770 host (match-string 2 uhost))
771 (setq host uhost))
772 ;; Remove empty HOST
773 (and (equal host "")
774 (setq host))
775 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
776 (and host
777 (equal method "local")
778 (setq root (concat host ":" root) host))
779 ;; Normalize CVS root record
780 (list method user host root)))))
781
782 ;; XXX: This does not work correctly for subdirectories. "cvs status"
783 ;; information is context sensitive, it contains lines like:
784 ;; cvs status: Examining DIRNAME
785 ;; and the file entries after that don't show the full path.
786 ;; Because of this VC directory listings only show changed files
787 ;; at the top level for CVS.
788 (defun vc-cvs-parse-status (&optional full)
789 "Parse output of \"cvs status\" command in the current buffer.
790 Set file properties accordingly. Unless FULL is t, parse only
791 essential information. Note that this can never set the 'ignored
792 state."
793 (let (file status missing)
794 (goto-char (point-min))
795 (while (looking-at "? \\(.*\\)")
796 (setq file (expand-file-name (match-string 1)))
797 (vc-file-setprop file 'vc-state 'unregistered)
798 (forward-line 1))
799 (when (re-search-forward "^File: " nil t)
800 (when (setq missing (looking-at "no file "))
801 (goto-char (match-end 0)))
802 (cond
803 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
804 (setq file (expand-file-name (match-string 1)))
805 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
806 (match-string 1) "Unknown"))
807 (when (and full
808 (re-search-forward
809 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
810 \[\t ]+\\([0-9.]+\\)"
811 nil t))
812 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
813 (vc-file-setprop
814 file 'vc-state
815 (cond
816 ((string-match "Up-to-date" status)
817 (vc-file-setprop file 'vc-checkout-time
818 (nth 5 (file-attributes file)))
819 'up-to-date)
820 ((string-match "Locally Modified" status) 'edited)
821 ((string-match "Needs Merge" status) 'needs-merge)
822 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
823 (if missing 'missing 'needs-update))
824 ((string-match "Locally Added" status) 'added)
825 ((string-match "Locally Removed" status) 'removed)
826 ((string-match "File had conflicts " status) 'conflict)
827 ((string-match "Unknown" status) 'unregistered)
828 (t 'edited))))))))
829
830 (defun vc-cvs-after-dir-status (update-function)
831 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
832 ;; This needs a lot of testing.
833 (let ((status nil)
834 (status-str nil)
835 (file nil)
836 (result nil)
837 (missing nil)
838 (subdir default-directory))
839 (goto-char (point-min))
840 (while
841 ;; Look for either a file entry, an unregistered file, or a
842 ;; directory change.
843 (re-search-forward
844 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: Examining .*\n\\)"
845 nil t)
846 ;; FIXME: get rid of narrowing here.
847 (narrow-to-region (match-beginning 0) (match-end 0))
848 (goto-char (point-min))
849 ;; The subdir
850 (when (looking-at "cvs status: Examining \\(.+\\)")
851 (setq subdir (expand-file-name (match-string 1))))
852 ;; Unregistered files
853 (while (looking-at "? \\(.*\\)")
854 (setq file (file-relative-name
855 (expand-file-name (match-string 1) subdir)))
856 (push (list file 'unregistered) result)
857 (forward-line 1))
858 ;; A file entry.
859 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
860 (setq missing (match-string 1))
861 (setq file (file-relative-name
862 (expand-file-name (match-string 2) subdir)))
863 (setq status-str (match-string 3))
864 (setq status
865 (cond
866 ((string-match "Up-to-date" status-str) 'up-to-date)
867 ((string-match "Locally Modified" status-str) 'edited)
868 ((string-match "Needs Merge" status-str) 'needs-merge)
869 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
870 (if missing 'missing 'needs-update))
871 ((string-match "Locally Added" status-str) 'added)
872 ((string-match "Locally Removed" status-str) 'removed)
873 ((string-match "File had conflicts " status-str) 'conflict)
874 ((string-match "Unknown" status-str) 'unregistered)
875 (t 'edited)))
876 (unless (eq status 'up-to-date)
877 (push (list file status) result)))
878 (goto-char (point-max))
879 (widen))
880 (funcall update-function result))
881 ;; Alternative implementation: use the "update" command instead of
882 ;; the "status" command.
883 ;; (let ((result nil)
884 ;; (translation '((?? . unregistered)
885 ;; (?A . added)
886 ;; (?C . conflict)
887 ;; (?M . edited)
888 ;; (?P . needs-merge)
889 ;; (?R . removed)
890 ;; (?U . needs-update))))
891 ;; (goto-char (point-min))
892 ;; (while (not (eobp))
893 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
894 ;; (push (list (match-string 1)
895 ;; (cdr (assoc (char-after) translation)))
896 ;; result)
897 ;; (cond
898 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
899 ;; ;; Format is:
900 ;; ;; cvs update: warning: FILENAME was lost
901 ;; ;; U FILENAME
902 ;; (push (list (match-string 1) 'missing) result)
903 ;; ;; Skip the "U" line
904 ;; (forward-line 1))
905 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
906 ;; (push (list (match-string 1) 'unregistered) result))))
907 ;; (forward-line 1))
908 ;; (funcall update-function result)))
909 )
910
911 (defun vc-cvs-dir-status (dir update-function)
912 "Create a list of conses (file . state) for DIR."
913 (vc-cvs-command (current-buffer) 'async dir "-f" "status")
914 ;; Alternative implementation: use the "update" command instead of
915 ;; the "status" command.
916 ;; (vc-cvs-command (current-buffer) 'async
917 ;; (file-relative-name dir)
918 ;; "-f" "-n" "update" "-d" "-P")
919 (vc-exec-after
920 `(vc-cvs-after-dir-status (quote ,update-function))))
921
922 (defun vc-cvs-file-to-string (file)
923 "Read the content of FILE and return it as a string."
924 (condition-case nil
925 (with-temp-buffer
926 (insert-file-contents file)
927 (goto-char (point-min))
928 (buffer-substring (point) (point-max)))
929 (file-error nil)))
930
931 (defun vc-cvs-status-extra-headers (dir)
932 "Extract and represent per-directory properties of a CVS working copy."
933 (let ((repo
934 (condition-case nil
935 (with-temp-buffer
936 (insert-file-contents "CVS/Root")
937 (goto-char (point-min))
938 (and (looking-at ":ext:") (delete-char 5))
939 (buffer-substring (point) (1- (point-max))))
940 (file-error nil)))
941 (module
942 (condition-case nil
943 (with-temp-buffer
944 (insert-file-contents "CVS/Repository")
945 (goto-char (point-min))
946 (re-search-forward "[^/\n]*" nil t)
947 (concat (match-string 0) "\n"))
948 (file-error nil))))
949 (concat
950 (cond (module
951 (concat (propertize "Module : " 'face 'font-lock-type-face)
952 (propertize module 'face 'font-lock-variable-name-face)))
953 (t ""))
954 (cond (repo
955 (concat (propertize "Repository : " 'face 'font-lock-type-face)
956 (propertize repo 'face 'font-lock-variable-name-face)))
957 (t ""))
958 ;; In CVS, branch is a per-file property, not a per-directory property.
959 ;; We can't really do this here without making dangerous assumptions.
960 ;;(propertize "Branch: " 'face 'font-lock-type-face)
961 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
962 ;; 'face 'font-lock-warning-face)
963 )))
964
965 (defun vc-cvs-get-entries (dir)
966 "Insert the CVS/Entries file from below DIR into the current buffer.
967 This function ensures that the correct coding system is used for that,
968 which may not be the one that is used for the files' contents.
969 CVS/Entries should only be accessed through this function."
970 (let ((coding-system-for-read (or file-name-coding-system
971 default-file-name-coding-system)))
972 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
973
974 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
975 "Return non-nil if TAG is a valid symbolic tag name."
976 ;; According to the CVS manual, a valid symbolic tag must start with
977 ;; an uppercase or lowercase letter and can contain uppercase and
978 ;; lowercase letters, digits, `-', and `_'.
979 (and (string-match "^[a-zA-Z]" tag)
980 (not (string-match "[^a-z0-9A-Z-_]" tag))))
981
982 (defun vc-cvs-valid-revision-number-p (tag)
983 "Return non-nil if TAG is a valid revision number."
984 (and (string-match "^[0-9]" tag)
985 (not (string-match "[^0-9.]" tag))))
986
987 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
988 "Parse and return the sticky tag as a string.
989 `match-data' is protected."
990 (let ((data (match-data))
991 (tag)
992 (type (cond ((string= match-type "D") 'date)
993 ((string= match-type "T")
994 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
995 'symbolic-name
996 'revision-number))
997 (t nil))))
998 (unwind-protect
999 (progn
1000 (cond
1001 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1002 ((eq type 'date)
1003 (string-match
1004 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1005 match-tag)
1006 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1007 (month (string-to-number (match-string 2 match-tag)))
1008 (day (string-to-number (match-string 3 match-tag)))
1009 (hour (string-to-number (match-string 4 match-tag)))
1010 (min (string-to-number (match-string 5 match-tag)))
1011 (sec (string-to-number (match-string 6 match-tag)))
1012 ;; Years 0..68 are 2000..2068.
1013 ;; Years 69..99 are 1969..1999.
1014 (year (+ (cond ((> 69 year-tmp) 2000)
1015 ((> 100 year-tmp) 1900)
1016 (t 0))
1017 year-tmp)))
1018 (setq tag (encode-time sec min hour day month year))))
1019 ;; Sticky Tag name or revision number
1020 ((eq type 'symbolic-name) (setq tag match-tag))
1021 ((eq type 'revision-number) (setq tag match-tag))
1022 ;; Default is no sticky tag at all
1023 (t nil))
1024 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1025 ((eq vc-cvs-sticky-tag-display t)
1026 (cond ((eq type 'date) (format-time-string
1027 vc-cvs-sticky-date-format-string
1028 tag))
1029 ((eq type 'symbolic-name) tag)
1030 ((eq type 'revision-number) tag)
1031 (t nil)))
1032 ((functionp vc-cvs-sticky-tag-display)
1033 (funcall vc-cvs-sticky-tag-display tag type))
1034 (t nil)))
1035
1036 (set-match-data data))))
1037
1038 (defun vc-cvs-parse-entry (file &optional set-state)
1039 "Parse a line from CVS/Entries.
1040 Compare modification time to that of the FILE, set file properties
1041 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1042 is non-nil."
1043 (cond
1044 ;; entry for a "locally added" file (not yet committed)
1045 ((looking-at "/[^/]+/0/")
1046 (vc-file-setprop file 'vc-checkout-time 0)
1047 (vc-file-setprop file 'vc-working-revision "0")
1048 (if set-state (vc-file-setprop file 'vc-state 'added)))
1049 ;; normal entry
1050 ((looking-at
1051 (concat "/[^/]+"
1052 ;; revision
1053 "/\\([^/]*\\)"
1054 ;; timestamp and optional conflict field
1055 "/\\([^/]*\\)/"
1056 ;; options
1057 "\\([^/]*\\)/"
1058 ;; sticky tag
1059 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1060 "\\(.*\\)")) ;Sticky tag
1061 (vc-file-setprop file 'vc-working-revision (match-string 1))
1062 (vc-file-setprop file 'vc-cvs-sticky-tag
1063 (vc-cvs-parse-sticky-tag (match-string 4)
1064 (match-string 5)))
1065 ;; Compare checkout time and modification time.
1066 ;; This is intentionally different from the algorithm that CVS uses
1067 ;; (which is based on textual comparison), because there can be problems
1068 ;; generating a time string that looks exactly like the one from CVS.
1069 (let ((mtime (nth 5 (file-attributes file))))
1070 (require 'parse-time)
1071 (let ((parsed-time
1072 (parse-time-string (concat (match-string 2) " +0000"))))
1073 (cond ((and (not (string-match "\\+" (match-string 2)))
1074 (car parsed-time)
1075 (equal mtime (apply 'encode-time parsed-time)))
1076 (vc-file-setprop file 'vc-checkout-time mtime)
1077 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1078 (t
1079 (vc-file-setprop file 'vc-checkout-time 0)
1080 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
1081
1082 ;; Completion of revision names.
1083 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1084 ;; `cvs log' so I can list all the revision numbers rather than only
1085 ;; tag names.
1086
1087 (defun vc-cvs-revision-table (file)
1088 (let ((default-directory (file-name-directory file))
1089 (res nil))
1090 (with-temp-buffer
1091 (vc-cvs-command t nil file "log")
1092 (goto-char (point-min))
1093 (when (re-search-forward "^symbolic names:\n" nil t)
1094 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1095 (push (cons (match-string 1) (match-string 2)) res)
1096 (forward-line 1)))
1097 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1098 (push (match-string 1) res))
1099 res)))
1100
1101 (defun vc-cvs-revision-completion-table (files)
1102 (lexical-let ((files files)
1103 table)
1104 (setq table (lazy-completion-table
1105 table (lambda () (vc-cvs-revision-table (car files)))))
1106 table))
1107
1108
1109 (provide 'vc-cvs)
1110
1111 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
1112 ;;; vc-cvs.el ends here