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