(flymake-process-filter): Make sure the source buffer isn't dead.
[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 ((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 (defun vc-cvs-annotate-current-time ()
574 "Return the current time, based at midnight of the current day, and
575 encoded as fractional days."
576 (vc-annotate-convert-time
577 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
578
579 (defun vc-cvs-annotate-time ()
580 "Return the time of the next annotation (as fraction of days)
581 systime, or nil if there is none."
582 (let* ((bol (point))
583 (cache (get-text-property bol 'vc-cvs-annotate-time))
584 (inhibit-read-only t)
585 (inhibit-modification-hooks t))
586 (cond
587 (cache)
588 ((looking-at
589 "^\\S-+\\s-+\\S-+\\s-+\\([0-9]+\\)-\\(\\sw+\\)-\\([0-9]+\\)): ")
590 (let ((day (string-to-number (match-string 1)))
591 (month (cdr (assq (intern (match-string 2))
592 '((Jan . 1) (Feb . 2) (Mar . 3)
593 (Apr . 4) (May . 5) (Jun . 6)
594 (Jul . 7) (Aug . 8) (Sep . 9)
595 (Oct . 10) (Nov . 11) (Dec . 12)))))
596 (year (let ((tmp (string-to-number (match-string 3))))
597 ;; Years 0..68 are 2000..2068.
598 ;; Years 69..99 are 1969..1999.
599 (+ (cond ((> 69 tmp) 2000)
600 ((> 100 tmp) 1900)
601 (t 0))
602 tmp))))
603 (put-text-property
604 bol (1+ bol) 'vc-cvs-annotate-time
605 (setq cache (cons
606 ;; Position at end makes for nicer overlay result.
607 ;; Don't put actual buffer pos here, but only relative
608 ;; distance, so we don't ever move backward in the
609 ;; goto-char below, even if the text is moved.
610 (- (match-end 0) (match-beginning 0))
611 (vc-annotate-convert-time
612 (encode-time 0 0 0 day month year))))))))
613 (when cache
614 (goto-char (+ bol (car cache))) ; Fontify from here to eol.
615 (cdr cache)))) ; days (float)
616
617 (defun vc-cvs-annotate-extract-revision-at-line ()
618 (save-excursion
619 (beginning-of-line)
620 (if (re-search-forward "^\\([0-9]+\\.[0-9]+\\(\\.[0-9]+\\)*\\) +("
621 (line-end-position) t)
622 (match-string-no-properties 1)
623 nil)))
624
625 ;;;
626 ;;; Tag system
627 ;;;
628
629 (defun vc-cvs-create-tag (dir name branchp)
630 "Assign to DIR's current revision a given NAME.
631 If BRANCHP is non-nil, the name is created as a branch (and the current
632 workspace is immediately moved to that new branch)."
633 (vc-cvs-command nil 0 dir "tag" "-c" (if branchp "-b") name)
634 (when branchp (vc-cvs-command nil 0 dir "update" "-r" name)))
635
636 (defun vc-cvs-retrieve-tag (dir name update)
637 "Retrieve a tag at and below DIR.
638 NAME is the name of the tag; if it is empty, do a `cvs update'.
639 If UPDATE is non-nil, then update (resynch) any affected buffers."
640 (with-current-buffer (get-buffer-create "*vc*")
641 (let ((default-directory dir)
642 (sticky-tag))
643 (erase-buffer)
644 (if (or (not name) (string= name ""))
645 (vc-cvs-command t 0 nil "update")
646 (vc-cvs-command t 0 nil "update" "-r" name)
647 (setq sticky-tag name))
648 (when update
649 (goto-char (point-min))
650 (while (not (eobp))
651 (if (looking-at "\\([CMUP]\\) \\(.*\\)")
652 (let* ((file (expand-file-name (match-string 2) dir))
653 (state (match-string 1))
654 (buffer (find-buffer-visiting file)))
655 (when buffer
656 (cond
657 ((or (string= state "U")
658 (string= state "P"))
659 (vc-file-setprop file 'vc-state 'up-to-date)
660 (vc-file-setprop file 'vc-working-revision nil)
661 (vc-file-setprop file 'vc-checkout-time
662 (nth 5 (file-attributes file))))
663 ((or (string= state "M")
664 (string= state "C"))
665 (vc-file-setprop file 'vc-state 'edited)
666 (vc-file-setprop file 'vc-working-revision nil)
667 (vc-file-setprop file 'vc-checkout-time 0)))
668 (vc-file-setprop file 'vc-cvs-sticky-tag sticky-tag)
669 (vc-resynch-buffer file t t))))
670 (forward-line 1))))))
671
672
673 ;;;
674 ;;; Miscellaneous
675 ;;;
676
677 (defalias 'vc-cvs-make-version-backups-p 'vc-stay-local-p
678 "Return non-nil if version backups should be made for FILE.")
679
680 (defun vc-cvs-check-headers ()
681 "Check if the current file has any headers in it."
682 (save-excursion
683 (goto-char (point-min))
684 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
685 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
686
687
688 ;;;
689 ;;; Internal functions
690 ;;;
691
692 (defun vc-cvs-root (dir)
693 (vc-find-root dir "CVS" t))
694
695 (defun vc-cvs-command (buffer okstatus files &rest flags)
696 "A wrapper around `vc-do-command' for use in vc-cvs.el.
697 The difference to vc-do-command is that this function always invokes `cvs',
698 and that it passes `vc-cvs-global-switches' to it before FLAGS."
699 (apply 'vc-do-command (or buffer "*vc*") okstatus "cvs" files
700 (if (stringp vc-cvs-global-switches)
701 (cons vc-cvs-global-switches flags)
702 (append vc-cvs-global-switches
703 flags))))
704
705 (defalias 'vc-cvs-stay-local-p 'vc-stay-local-p) ;Back-compatibility.
706
707 (defun vc-cvs-repository-hostname (dirname)
708 "Hostname of the CVS server associated to workarea DIRNAME."
709 (let ((rootname (expand-file-name "CVS/Root" dirname)))
710 (when (file-readable-p rootname)
711 (with-temp-buffer
712 (let ((coding-system-for-read
713 (or file-name-coding-system
714 default-file-name-coding-system)))
715 (vc-insert-file rootname))
716 (goto-char (point-min))
717 (nth 2 (vc-cvs-parse-root
718 (buffer-substring (point)
719 (line-end-position))))))))
720
721 (defun vc-cvs-parse-root (root)
722 "Split CVS ROOT specification string into a list of fields.
723 A CVS root specification of the form
724 [:METHOD:][[USER@]HOSTNAME:]/path/to/repository
725 is converted to a normalized record with the following structure:
726 \(METHOD USER HOSTNAME CVS-ROOT).
727 The default METHOD for a CVS root of the form
728 /path/to/repository
729 is `local'.
730 The default METHOD for a CVS root of the form
731 [USER@]HOSTNAME:/path/to/repository
732 is `ext'.
733 For an empty string, nil is returned (invalid CVS root)."
734 ;; Split CVS root into colon separated fields (0-4).
735 ;; The `x:' makes sure, that leading colons are not lost;
736 ;; `HOST:/PATH' is then different from `:METHOD:/PATH'.
737 (let* ((root-list (cdr (split-string (concat "x:" root) ":")))
738 (len (length root-list))
739 ;; All syntactic varieties will get a proper METHOD.
740 (root-list
741 (cond
742 ((= len 0)
743 ;; Invalid CVS root
744 nil)
745 ((= len 1)
746 ;; Simple PATH => method `local'
747 (cons "local"
748 (cons nil root-list)))
749 ((= len 2)
750 ;; [USER@]HOST:PATH => method `ext'
751 (and (not (equal (car root-list) ""))
752 (cons "ext" root-list)))
753 ((= len 3)
754 ;; :METHOD:PATH
755 (cons (cadr root-list)
756 (cons nil (cddr root-list))))
757 (t
758 ;; :METHOD:[USER@]HOST:PATH
759 (cdr root-list)))))
760 (if root-list
761 (let ((method (car root-list))
762 (uhost (or (cadr root-list) ""))
763 (root (nth 2 root-list))
764 user host)
765 ;; Split USER@HOST
766 (if (string-match "\\(.*\\)@\\(.*\\)" uhost)
767 (setq user (match-string 1 uhost)
768 host (match-string 2 uhost))
769 (setq host uhost))
770 ;; Remove empty HOST
771 (and (equal host "")
772 (setq host))
773 ;; Fix windows style CVS root `:local:C:\\project\\cvs\\some\\dir'
774 (and host
775 (equal method "local")
776 (setq root (concat host ":" root) host))
777 ;; Normalize CVS root record
778 (list method user host root)))))
779
780 ;; XXX: This does not work correctly for subdirectories. "cvs status"
781 ;; information is context sensitive, it contains lines like:
782 ;; cvs status: Examining DIRNAME
783 ;; and the file entries after that don't show the full path.
784 ;; Because of this VC directory listings only show changed files
785 ;; at the top level for CVS.
786 (defun vc-cvs-parse-status (&optional full)
787 "Parse output of \"cvs status\" command in the current buffer.
788 Set file properties accordingly. Unless FULL is t, parse only
789 essential information. Note that this can never set the 'ignored
790 state."
791 (let (file status missing)
792 (goto-char (point-min))
793 (while (looking-at "? \\(.*\\)")
794 (setq file (expand-file-name (match-string 1)))
795 (vc-file-setprop file 'vc-state 'unregistered)
796 (forward-line 1))
797 (when (re-search-forward "^File: " nil t)
798 (when (setq missing (looking-at "no file "))
799 (goto-char (match-end 0)))
800 (cond
801 ((re-search-forward "\\=\\([^ \t]+\\)" nil t)
802 (setq file (expand-file-name (match-string 1)))
803 (vc-file-setprop file 'vc-backend 'CVS)
804 (setq status(if (re-search-forward "\\=[ \t]+Status: \\(.*\\)" nil t)
805 (match-string 1) "Unknown"))
806 (when (and full
807 (re-search-forward
808 "\\(RCS Version\\|RCS Revision\\|Repository revision\\):\
809 \[\t ]+\\([0-9.]+\\)"
810 nil t))
811 (vc-file-setprop file 'vc-latest-revision (match-string 2)))
812 (vc-file-setprop
813 file 'vc-state
814 (cond
815 ((string-match "Up-to-date" status)
816 (vc-file-setprop file 'vc-checkout-time
817 (nth 5 (file-attributes file)))
818 'up-to-date)
819 ((string-match "Locally Modified" status) 'edited)
820 ((string-match "Needs Merge" status) 'needs-merge)
821 ((string-match "Needs \\(Checkout\\|Patch\\)" status)
822 (if missing 'missing 'needs-update))
823 ((string-match "Locally Added" status) 'added)
824 ((string-match "Locally Removed" status) 'removed)
825 ((string-match "File had conflicts " status) 'conflict)
826 ((string-match "Unknown" status) 'unregistered)
827 (t 'edited))))))))
828
829 (defun vc-cvs-after-dir-status (update-function)
830 ;; Heavily inspired by vc-cvs-parse-status. AKA a quick hack.
831 ;; This needs a lot of testing.
832 (let ((status nil)
833 (status-str nil)
834 (file nil)
835 (result nil)
836 (missing nil)
837 (subdir default-directory))
838 (goto-char (point-min))
839 (while
840 ;; Look for either a file entry, an unregistered file, or a
841 ;; directory change.
842 (re-search-forward
843 "\\(^=+\n\\([^=c?\n].*\n\\|\n\\)+\\)\\|\\(\\(^?? .*\n\\)+\\)\\|\\(^cvs status: Examining .*\n\\)"
844 nil t)
845 ;; FIXME: get rid of narrowing here.
846 (narrow-to-region (match-beginning 0) (match-end 0))
847 (goto-char (point-min))
848 ;; The subdir
849 (when (looking-at "cvs status: Examining \\(.+\\)")
850 (setq subdir (expand-file-name (match-string 1))))
851 ;; Unregistered files
852 (while (looking-at "? \\(.*\\)")
853 (setq file (file-relative-name
854 (expand-file-name (match-string 1) subdir)))
855 (push (list file 'unregistered) result)
856 (forward-line 1))
857 ;; A file entry.
858 (when (re-search-forward "^File: \\(no file \\)?\\(.*[^ \t]\\)[ \t]+Status: \\(.*\\)" nil t)
859 (setq missing (match-string 1))
860 (setq file (file-relative-name
861 (expand-file-name (match-string 2) subdir)))
862 (setq status-str (match-string 3))
863 (setq status
864 (cond
865 ((string-match "Up-to-date" status-str) 'up-to-date)
866 ((string-match "Locally Modified" status-str) 'edited)
867 ((string-match "Needs Merge" status-str) 'needs-merge)
868 ((string-match "Needs \\(Checkout\\|Patch\\)" status-str)
869 (if missing 'missing 'needs-update))
870 ((string-match "Locally Added" status-str) 'added)
871 ((string-match "Locally Removed" status-str) 'removed)
872 ((string-match "File had conflicts " status-str) 'conflict)
873 ((string-match "Unknown" status-str) 'unregistered)
874 (t 'edited)))
875 (unless (eq status 'up-to-date)
876 (push (list file status) result)))
877 (goto-char (point-max))
878 (widen))
879 (funcall update-function result))
880 ;; Alternative implementation: use the "update" command instead of
881 ;; the "status" command.
882 ;; (let ((result nil)
883 ;; (translation '((?? . unregistered)
884 ;; (?A . added)
885 ;; (?C . conflict)
886 ;; (?M . edited)
887 ;; (?P . needs-merge)
888 ;; (?R . removed)
889 ;; (?U . needs-update))))
890 ;; (goto-char (point-min))
891 ;; (while (not (eobp))
892 ;; (if (looking-at "^[ACMPRU?] \\(.*\\)$")
893 ;; (push (list (match-string 1)
894 ;; (cdr (assoc (char-after) translation)))
895 ;; result)
896 ;; (cond
897 ;; ((looking-at "cvs update: warning: \\(.*\\) was lost")
898 ;; ;; Format is:
899 ;; ;; cvs update: warning: FILENAME was lost
900 ;; ;; U FILENAME
901 ;; (push (list (match-string 1) 'missing) result)
902 ;; ;; Skip the "U" line
903 ;; (forward-line 1))
904 ;; ((looking-at "cvs update: New directory `\\(.*\\)' -- ignored")
905 ;; (push (list (match-string 1) 'unregistered) result))))
906 ;; (forward-line 1))
907 ;; (funcall update-function result)))
908 )
909
910 (defun vc-cvs-dir-status (dir update-function)
911 "Create a list of conses (file . state) for DIR."
912 (vc-cvs-command (current-buffer) 'async dir "status")
913 ;; Alternative implementation: use the "update" command instead of
914 ;; the "status" command.
915 ;; (vc-cvs-command (current-buffer) 'async
916 ;; (file-relative-name dir)
917 ;; "-f" "-n" "update" "-d" "-P")
918 (vc-exec-after
919 `(vc-cvs-after-dir-status (quote ,update-function))))
920
921 (defun vc-cvs-file-to-string (file)
922 "Read the content of FILE and return it as a string."
923 (condition-case nil
924 (with-temp-buffer
925 (insert-file-contents file)
926 (goto-char (point-min))
927 (buffer-substring (point) (point-max)))
928 (file-error nil)))
929
930 (defun vc-cvs-status-extra-headers (dir)
931 "Extract and represent per-directory properties of a CVS working copy."
932 (let ((repo
933 (condition-case nil
934 (with-temp-buffer
935 (insert-file-contents "CVS/Root")
936 (goto-char (point-min))
937 (and (looking-at ":ext:") (delete-char 5))
938 (buffer-substring (point) (point-max)))
939 (file-error nil)))
940 (module
941 (condition-case nil
942 (with-temp-buffer
943 (insert-file-contents "CVS/Repository")
944 (goto-char (point-min))
945 (re-search-forward "[^/]*" nil t)
946 (concat (match-string 0) "\n"))
947 (file-error nil))))
948 (concat
949 (cond (module
950 (concat
951 (propertize "Module : " 'face 'font-lock-type-face)
952 (propertize module 'face 'font-lock-variable-name-face)))
953 (t ""))
954 (cond (repo
955 (concat
956 (propertize "Repository : " 'face 'font-lock-type-face)
957 (propertize repo 'face 'font-lock-variable-name-face)))
958 (t ""))
959 ;; In CVS, branch is a per-file property, not a per-directory property. We
960 ;; can't really do this here without making dangerous assumptions.
961 ;;(propertize "Branch: " 'face 'font-lock-type-face)
962 ;;(propertize "ADD CODE TO PRINT THE BRANCH NAME\n"
963 ;; 'face 'font-lock-warning-face)
964 )))
965
966 (defun vc-cvs-get-entries (dir)
967 "Insert the CVS/Entries file from below DIR into the current buffer.
968 This function ensures that the correct coding system is used for that,
969 which may not be the one that is used for the files' contents.
970 CVS/Entries should only be accessed through this function."
971 (let ((coding-system-for-read (or file-name-coding-system
972 default-file-name-coding-system)))
973 (vc-insert-file (expand-file-name "CVS/Entries" dir))))
974
975 (defun vc-cvs-valid-symbolic-tag-name-p (tag)
976 "Return non-nil if TAG is a valid symbolic tag name."
977 ;; According to the CVS manual, a valid symbolic tag must start with
978 ;; an uppercase or lowercase letter and can contain uppercase and
979 ;; lowercase letters, digits, `-', and `_'.
980 (and (string-match "^[a-zA-Z]" tag)
981 (not (string-match "[^a-z0-9A-Z-_]" tag))))
982
983 (defun vc-cvs-valid-revision-number-p (tag)
984 "Return non-nil if TAG is a valid revision number."
985 (and (string-match "^[0-9]" tag)
986 (not (string-match "[^0-9.]" tag))))
987
988 (defun vc-cvs-parse-sticky-tag (match-type match-tag)
989 "Parse and return the sticky tag as a string.
990 `match-data' is protected."
991 (let ((data (match-data))
992 (tag)
993 (type (cond ((string= match-type "D") 'date)
994 ((string= match-type "T")
995 (if (vc-cvs-valid-symbolic-tag-name-p match-tag)
996 'symbolic-name
997 'revision-number))
998 (t nil))))
999 (unwind-protect
1000 (progn
1001 (cond
1002 ;; Sticky Date tag. Convert to a proper date value (`encode-time')
1003 ((eq type 'date)
1004 (string-match
1005 "\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)\\.\\([0-9]+\\)"
1006 match-tag)
1007 (let* ((year-tmp (string-to-number (match-string 1 match-tag)))
1008 (month (string-to-number (match-string 2 match-tag)))
1009 (day (string-to-number (match-string 3 match-tag)))
1010 (hour (string-to-number (match-string 4 match-tag)))
1011 (min (string-to-number (match-string 5 match-tag)))
1012 (sec (string-to-number (match-string 6 match-tag)))
1013 ;; Years 0..68 are 2000..2068.
1014 ;; Years 69..99 are 1969..1999.
1015 (year (+ (cond ((> 69 year-tmp) 2000)
1016 ((> 100 year-tmp) 1900)
1017 (t 0))
1018 year-tmp)))
1019 (setq tag (encode-time sec min hour day month year))))
1020 ;; Sticky Tag name or revision number
1021 ((eq type 'symbolic-name) (setq tag match-tag))
1022 ((eq type 'revision-number) (setq tag match-tag))
1023 ;; Default is no sticky tag at all
1024 (t nil))
1025 (cond ((eq vc-cvs-sticky-tag-display nil) nil)
1026 ((eq vc-cvs-sticky-tag-display t)
1027 (cond ((eq type 'date) (format-time-string
1028 vc-cvs-sticky-date-format-string
1029 tag))
1030 ((eq type 'symbolic-name) tag)
1031 ((eq type 'revision-number) tag)
1032 (t nil)))
1033 ((functionp vc-cvs-sticky-tag-display)
1034 (funcall vc-cvs-sticky-tag-display tag type))
1035 (t nil)))
1036
1037 (set-match-data data))))
1038
1039 (defun vc-cvs-parse-entry (file &optional set-state)
1040 "Parse a line from CVS/Entries.
1041 Compare modification time to that of the FILE, set file properties
1042 accordingly. However, `vc-state' is set only if optional arg SET-STATE
1043 is non-nil."
1044 (cond
1045 ;; entry for a "locally added" file (not yet committed)
1046 ((looking-at "/[^/]+/0/")
1047 (vc-file-setprop file 'vc-backend 'CVS)
1048 (vc-file-setprop file 'vc-checkout-time 0)
1049 (vc-file-setprop file 'vc-working-revision "0")
1050 (if set-state (vc-file-setprop file 'vc-state 'added)))
1051 ;; normal entry
1052 ((looking-at
1053 (concat "/[^/]+"
1054 ;; revision
1055 "/\\([^/]*\\)"
1056 ;; timestamp and optional conflict field
1057 "/\\([^/]*\\)/"
1058 ;; options
1059 "\\([^/]*\\)/"
1060 ;; sticky tag
1061 "\\(.\\|\\)" ;Sticky tag type (date or tag name, could be empty)
1062 "\\(.*\\)")) ;Sticky tag
1063 (vc-file-setprop file 'vc-backend 'CVS)
1064 (vc-file-setprop file 'vc-working-revision (match-string 1))
1065 (vc-file-setprop file 'vc-cvs-sticky-tag
1066 (vc-cvs-parse-sticky-tag (match-string 4)
1067 (match-string 5)))
1068 ;; Compare checkout time and modification time.
1069 ;; This is intentionally different from the algorithm that CVS uses
1070 ;; (which is based on textual comparison), because there can be problems
1071 ;; generating a time string that looks exactly like the one from CVS.
1072 (let ((mtime (nth 5 (file-attributes file))))
1073 (require 'parse-time)
1074 (let ((parsed-time
1075 (parse-time-string (concat (match-string 2) " +0000"))))
1076 (cond ((and (not (string-match "\\+" (match-string 2)))
1077 (car parsed-time)
1078 (equal mtime (apply 'encode-time parsed-time)))
1079 (vc-file-setprop file 'vc-checkout-time mtime)
1080 (if set-state (vc-file-setprop file 'vc-state 'up-to-date)))
1081 (t
1082 (vc-file-setprop file 'vc-checkout-time 0)
1083 (if set-state (vc-file-setprop file 'vc-state 'edited)))))))))
1084
1085 ;; Completion of revision names.
1086 ;; Just so I don't feel like I'm duplicating code from pcl-cvs, I'll use
1087 ;; `cvs log' so I can list all the revision numbers rather than only
1088 ;; tag names.
1089
1090 (defun vc-cvs-revision-table (file)
1091 (let ((default-directory (file-name-directory file))
1092 (res nil))
1093 (with-temp-buffer
1094 (vc-cvs-command t nil file "log")
1095 (goto-char (point-min))
1096 (when (re-search-forward "^symbolic names:\n" nil t)
1097 (while (looking-at "^ \\(.*\\): \\(.*\\)")
1098 (push (cons (match-string 1) (match-string 2)) res)
1099 (forward-line 1)))
1100 (while (re-search-forward "^revision \\([0-9.]+\\)" nil t)
1101 (push (match-string 1) res))
1102 res)))
1103
1104 (defun vc-cvs-revision-completion-table (files)
1105 (lexical-let ((files files)
1106 table)
1107 (setq table (lazy-completion-table
1108 table (lambda () (vc-cvs-revision-table (car files)))))
1109 table))
1110
1111
1112 (provide 'vc-cvs)
1113
1114 ;; arch-tag: 60e1402a-aa53-4607-927a-cf74f144b432
1115 ;;; vc-cvs.el ends here