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