Add "Package:" file headers to denote built-in packages.
[emacs.git] / lisp / vc / vc-rcs.el
blob1c3b4c00e3202277075a9ddb9b583a97fff2548d
1 ;;; vc-rcs.el --- support for RCS version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; Package: vc
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 of the License, or
16 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; See vc.el
30 ;; Some features will not work with old RCS versions. Where
31 ;; appropriate, VC finds out which version you have, and allows or
32 ;; disallows those features (stealing locks, for example, works only
33 ;; from 5.6.2 onwards).
34 ;; Even initial checkins will fail if your RCS version is so old that ci
35 ;; doesn't understand -t-; this has been known to happen to people running
36 ;; NExTSTEP 3.0.
38 ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
40 ;;; Code:
42 ;;;
43 ;;; Customization options
44 ;;;
46 (eval-when-compile
47 (require 'cl)
48 (require 'vc))
50 (defcustom vc-rcs-release nil
51 "The release number of your RCS installation, as a string.
52 If nil, VC itself computes this value when it is first needed."
53 :type '(choice (const :tag "Auto" nil)
54 (string :tag "Specified")
55 (const :tag "Unknown" unknown))
56 :group 'vc)
58 (defcustom vc-rcs-register-switches nil
59 "Switches for registering a file in RCS.
60 A string or list of strings passed to the checkin program by
61 \\[vc-register]. If nil, use the value of `vc-register-switches'.
62 If t, use no switches."
63 :type '(choice (const :tag "Unspecified" nil)
64 (const :tag "None" t)
65 (string :tag "Argument String")
66 (repeat :tag "Argument List" :value ("") string))
67 :version "21.1"
68 :group 'vc)
70 (defcustom vc-rcs-diff-switches nil
71 "String or list of strings specifying switches for RCS diff under VC.
72 If nil, use the value of `vc-diff-switches'. If t, use no switches."
73 :type '(choice (const :tag "Unspecified" nil)
74 (const :tag "None" t)
75 (string :tag "Argument String")
76 (repeat :tag "Argument List" :value ("") string))
77 :version "21.1"
78 :group 'vc)
80 (defcustom vc-rcs-header (or (cdr (assoc 'RCS vc-header-alist)) '("\$Id\$"))
81 "Header keywords to be inserted by `vc-insert-headers'."
82 :type '(repeat string)
83 :version "21.1"
84 :group 'vc)
86 (defcustom vc-rcsdiff-knows-brief nil
87 "Indicates whether rcsdiff understands the --brief option.
88 The value is either `yes', `no', or nil. If it is nil, VC tries
89 to use --brief and sets this variable to remember whether it worked."
90 :type '(choice (const :tag "Work out" nil) (const yes) (const no))
91 :group 'vc)
93 ;;;###autoload
94 (defcustom vc-rcs-master-templates
95 (purecopy '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
96 "Where to look for RCS master files.
97 For a description of possible values, see `vc-check-master-templates'."
98 :type '(choice (const :tag "Use standard RCS file names"
99 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
100 (repeat :tag "User-specified"
101 (choice string
102 function)))
103 :version "21.1"
104 :group 'vc)
107 ;;; Properties of the backend
109 (defun vc-rcs-revision-granularity () 'file)
111 (defun vc-rcs-checkout-model (files)
112 "RCS-specific version of `vc-checkout-model'."
113 (let ((file (if (consp files) (car files) files))
114 result)
115 (when vc-consult-headers
116 (vc-file-setprop file 'vc-checkout-model nil)
117 (vc-rcs-consult-headers file)
118 (setq result (vc-file-getprop file 'vc-checkout-model)))
119 (or result
120 (progn (vc-rcs-fetch-master-state file)
121 (vc-file-getprop file 'vc-checkout-model)))))
124 ;;; State-querying functions
127 ;; The autoload cookie below places vc-rcs-registered directly into
128 ;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
129 ;; every file that is visited.
130 ;;;###autoload
131 (progn
132 (defun vc-rcs-registered (f) (vc-default-registered 'RCS f)))
134 (defun vc-rcs-state (file)
135 "Implementation of `vc-state' for RCS."
136 (if (not (vc-rcs-registered file))
137 'unregistered
138 (or (boundp 'vc-rcs-headers-result)
139 (and vc-consult-headers
140 (vc-rcs-consult-headers file)))
141 (let ((state
142 ;; vc-working-revision might not be known; in that case the
143 ;; property is nil. vc-rcs-fetch-master-state knows how to
144 ;; handle that.
145 (vc-rcs-fetch-master-state file
146 (vc-file-getprop file
147 'vc-working-revision))))
148 (if (not (eq state 'up-to-date))
149 state
150 (if (vc-workfile-unchanged-p file)
151 'up-to-date
152 (if (eq (vc-rcs-checkout-model (list file)) 'locking)
153 'unlocked-changes
154 'edited))))))
156 (defun vc-rcs-state-heuristic (file)
157 "State heuristic for RCS."
158 (let (vc-rcs-headers-result)
159 (if (and vc-consult-headers
160 (setq vc-rcs-headers-result
161 (vc-rcs-consult-headers file))
162 (eq vc-rcs-headers-result 'rev-and-lock))
163 (let ((state (vc-file-getprop file 'vc-state)))
164 ;; If the headers say that the file is not locked, the
165 ;; permissions can tell us whether locking is used for
166 ;; the file or not.
167 (if (and (eq state 'up-to-date)
168 (not (vc-mistrust-permissions file))
169 (file-exists-p file))
170 (cond
171 ((string-match ".rw..-..-." (nth 8 (file-attributes file)))
172 (vc-file-setprop file 'vc-checkout-model 'implicit)
173 (setq state
174 (if (vc-rcs-workfile-is-newer file)
175 'edited
176 'up-to-date)))
177 ((string-match ".r-..-..-." (nth 8 (file-attributes file)))
178 (vc-file-setprop file 'vc-checkout-model 'locking))))
179 state)
180 (if (not (vc-mistrust-permissions file))
181 (let* ((attributes (file-attributes file 'string))
182 (owner-name (nth 2 attributes))
183 (permissions (nth 8 attributes)))
184 (cond ((and permissions (string-match ".r-..-..-." permissions))
185 (vc-file-setprop file 'vc-checkout-model 'locking)
186 'up-to-date)
187 ((and permissions (string-match ".rw..-..-." permissions))
188 (if (eq (vc-rcs-checkout-model file) 'locking)
189 (if (file-ownership-preserved-p file)
190 'edited
191 owner-name)
192 (if (vc-rcs-workfile-is-newer file)
193 'edited
194 'up-to-date)))
196 ;; Strange permissions. Fall through to
197 ;; expensive state computation.
198 (vc-rcs-state file))))
199 (vc-rcs-state file)))))
201 (defun vc-rcs-dir-status (dir update-function)
202 ;; FIXME: this function should be rewritten or `vc-expand-dirs'
203 ;; should be changed to take a backend parameter. Using
204 ;; `vc-expand-dirs' is not TRTD because it returns files from
205 ;; multiple backends. It should also return 'unregistered files.
207 ;; Doing individual vc-state calls is painful but there
208 ;; is no better way in RCS-land.
209 (let ((flist (vc-expand-dirs (list dir)))
210 (result nil))
211 (dolist (file flist)
212 (let ((state (vc-state file))
213 (frel (file-relative-name file)))
214 (when (and (eq (vc-backend file) 'RCS)
215 (not (eq state 'up-to-date)))
216 (push (list frel state) result))))
217 (funcall update-function result)))
219 (defun vc-rcs-working-revision (file)
220 "RCS-specific version of `vc-working-revision'."
221 (or (and vc-consult-headers
222 (vc-rcs-consult-headers file)
223 (vc-file-getprop file 'vc-working-revision))
224 (progn
225 (vc-rcs-fetch-master-state file)
226 (vc-file-getprop file 'vc-working-revision))))
228 (defun vc-rcs-latest-on-branch-p (file &optional version)
229 "Return non-nil if workfile version of FILE is the latest on its branch.
230 When VERSION is given, perform check for that version."
231 (unless version (setq version (vc-working-revision file)))
232 (with-temp-buffer
233 (string= version
234 (if (vc-rcs-trunk-p version)
235 (progn
236 ;; Compare VERSION to the head version number.
237 (vc-insert-file (vc-name file) "^[0-9]")
238 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
239 ;; If we are not on the trunk, we need to examine the
240 ;; whole current branch.
241 (vc-insert-file (vc-name file) "^desc")
242 (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
244 (defun vc-rcs-workfile-unchanged-p (file)
245 "RCS-specific implementation of `vc-workfile-unchanged-p'."
246 ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
247 ;; do a double take and remember the fact for the future
248 (let* ((version (concat "-r" (vc-working-revision file)))
249 (status (if (eq vc-rcsdiff-knows-brief 'no)
250 (vc-do-command "*vc*" 1 "rcsdiff" file version)
251 (vc-do-command "*vc*" 2 "rcsdiff" file "--brief" version))))
252 (if (eq status 2)
253 (if (not vc-rcsdiff-knows-brief)
254 (setq vc-rcsdiff-knows-brief 'no
255 status (vc-do-command "*vc*" 1 "rcsdiff" file version))
256 (error "rcsdiff failed"))
257 (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
258 ;; The workfile is unchanged if rcsdiff found no differences.
259 (zerop status)))
263 ;;; State-changing functions
266 (defun vc-rcs-create-repo ()
267 "Create a new RCS repository."
268 ;; RCS is totally file-oriented, so all we have to do is make the directory.
269 (make-directory "RCS"))
271 (defun vc-rcs-register (files &optional rev comment)
272 "Register FILES into the RCS version-control system.
273 REV is the optional revision number for the files. COMMENT can be used
274 to provide an initial description for each FILES.
275 Passes either `vc-rcs-register-switches' or `vc-register-switches'
276 to the RCS command.
278 Automatically retrieve a read-only version of the file with keywords
279 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
280 (let (subdir name)
281 ;; When REV is specified, we need to force using "-t-".
282 (when rev (unless comment (setq comment "")))
283 (dolist (file files)
284 (and (not (file-exists-p
285 (setq subdir (expand-file-name "RCS"
286 (file-name-directory file)))))
287 (not (directory-files (file-name-directory file)
288 nil ".*,v$" t))
289 (yes-or-no-p "Create RCS subdirectory? ")
290 (make-directory subdir))
291 (apply 'vc-do-command "*vc*" 0 "ci" file
292 ;; if available, use the secure registering option
293 (and (vc-rcs-release-p "5.6.4") "-i")
294 (concat (if vc-keep-workfiles "-u" "-r") rev)
295 (and comment (concat "-t-" comment))
296 (vc-switches 'RCS 'register))
297 ;; parse output to find master file name and workfile version
298 (with-current-buffer "*vc*"
299 (goto-char (point-min))
300 (if (not (setq name
301 (if (looking-at (concat "^\\(.*\\) <-- "
302 (file-name-nondirectory file)))
303 (match-string 1))))
304 ;; if we couldn't find the master name,
305 ;; run vc-rcs-registered to get it
306 ;; (will be stored into the vc-name property)
307 (vc-rcs-registered file)
308 (vc-file-setprop file 'vc-name
309 (if (file-name-absolute-p name)
310 name
311 (expand-file-name
312 name
313 (file-name-directory file))))))
314 (vc-file-setprop file 'vc-working-revision
315 (if (re-search-forward
316 "^initial revision: \\([0-9.]+\\).*\n"
317 nil t)
318 (match-string 1))))))
320 (defun vc-rcs-responsible-p (file)
321 "Return non-nil if RCS thinks it would be responsible for registering FILE."
322 ;; TODO: check for all the patterns in vc-rcs-master-templates
323 (file-directory-p (expand-file-name "RCS" (file-name-directory file))))
325 (defun vc-rcs-receive-file (file rev)
326 "Implementation of receive-file for RCS."
327 (let ((checkout-model (vc-rcs-checkout-model (list file))))
328 (vc-rcs-register file rev "")
329 (when (eq checkout-model 'implicit)
330 (vc-rcs-set-non-strict-locking file))
331 (vc-rcs-set-default-branch file (concat rev ".1"))))
333 (defun vc-rcs-unregister (file)
334 "Unregister FILE from RCS.
335 If this leaves the RCS subdirectory empty, ask the user
336 whether to remove it."
337 (let* ((master (vc-name file))
338 (dir (file-name-directory master))
339 (backup-info (find-backup-file-name master)))
340 (if (not backup-info)
341 (delete-file master)
342 (rename-file master (car backup-info) 'ok-if-already-exists)
343 (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
344 (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
345 ;; check whether RCS dir is empty, i.e. it does not
346 ;; contain any files except "." and ".."
347 (not (directory-files dir nil
348 "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
349 (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
350 (delete-directory dir))))
352 (defun vc-rcs-checkin (files rev comment &optional extra-args-ignored)
353 "RCS-specific version of `vc-backend-checkin'."
354 (let ((switches (vc-switches 'RCS 'checkin)))
355 ;; Now operate on the files
356 (dolist (file (vc-expand-dirs files))
357 (let ((old-version (vc-working-revision file)) new-version
358 (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
359 ;; Force branch creation if an appropriate
360 ;; default branch has been set.
361 (and (not rev)
362 default-branch
363 (string-match (concat "^" (regexp-quote old-version) "\\.")
364 default-branch)
365 (setq rev default-branch)
366 (setq switches (cons "-f" switches)))
367 (if (and (not rev) old-version)
368 (setq rev (vc-branch-part old-version)))
369 (apply 'vc-do-command "*vc*" 0 "ci" (vc-name file)
370 ;; if available, use the secure check-in option
371 (and (vc-rcs-release-p "5.6.4") "-j")
372 (concat (if vc-keep-workfiles "-u" "-r") rev)
373 (concat "-m" comment)
374 switches)
375 (vc-file-setprop file 'vc-working-revision nil)
377 ;; determine the new workfile version
378 (set-buffer "*vc*")
379 (goto-char (point-min))
380 (when (or (re-search-forward
381 "new revision: \\([0-9.]+\\);" nil t)
382 (re-search-forward
383 "reverting to previous revision \\([0-9.]+\\)" nil t))
384 (setq new-version (match-string 1))
385 (vc-file-setprop file 'vc-working-revision new-version))
387 ;; if we got to a different branch, adjust the default
388 ;; branch accordingly
389 (cond
390 ((and old-version new-version
391 (not (string= (vc-branch-part old-version)
392 (vc-branch-part new-version))))
393 (vc-rcs-set-default-branch file
394 (if (vc-rcs-trunk-p new-version) nil
395 (vc-branch-part new-version)))
396 ;; If this is an old RCS release, we might have
397 ;; to remove a remaining lock.
398 (if (not (vc-rcs-release-p "5.6.2"))
399 ;; exit status of 1 is also accepted.
400 ;; It means that the lock was removed before.
401 (vc-do-command "*vc*" 1 "rcs" (vc-name file)
402 (concat "-u" old-version)))))))))
404 (defun vc-rcs-find-revision (file rev buffer)
405 (apply 'vc-do-command
406 (or buffer "*vc*") 0 "co" (vc-name file)
407 "-q" ;; suppress diagnostic output
408 (concat "-p" rev)
409 (vc-switches 'RCS 'checkout)))
411 (defun vc-rcs-checkout (file &optional editable rev)
412 "Retrieve a copy of a saved version of FILE. If FILE is a directory,
413 attempt the checkout for all registered files beneath it."
414 (if (file-directory-p file)
415 (mapc 'vc-rcs-checkout (vc-expand-dirs (list file)))
416 (let ((file-buffer (get-file-buffer file))
417 switches)
418 (message "Checking out %s..." file)
419 (save-excursion
420 ;; Change buffers to get local value of vc-checkout-switches.
421 (if file-buffer (set-buffer file-buffer))
422 (setq switches (vc-switches 'RCS 'checkout))
423 ;; Save this buffer's default-directory
424 ;; and use save-excursion to make sure it is restored
425 ;; in the same buffer it was saved in.
426 (let ((default-directory default-directory))
427 (save-excursion
428 ;; Adjust the default-directory so that the check-out creates
429 ;; the file in the right place.
430 (setq default-directory (file-name-directory file))
431 (let (new-version)
432 ;; if we should go to the head of the trunk,
433 ;; clear the default branch first
434 (and rev (string= rev "")
435 (vc-rcs-set-default-branch file nil))
436 ;; now do the checkout
437 (apply 'vc-do-command
438 "*vc*" 0 "co" (vc-name file)
439 ;; If locking is not strict, force to overwrite
440 ;; the writable workfile.
441 (if (eq (vc-rcs-checkout-model (list file)) 'implicit) "-f")
442 (if editable "-l")
443 (if (stringp rev)
444 ;; a literal revision was specified
445 (concat "-r" rev)
446 (let ((workrev (vc-working-revision file)))
447 (if workrev
448 (concat "-r"
449 (if (not rev)
450 ;; no revision specified:
451 ;; use current workfile version
452 workrev
453 ;; REV is t ...
454 (if (not (vc-rcs-trunk-p workrev))
455 ;; ... go to head of current branch
456 (vc-branch-part workrev)
457 ;; ... go to head of trunk
458 (vc-rcs-set-default-branch file
459 nil)
460 ""))))))
461 switches)
462 ;; determine the new workfile version
463 (with-current-buffer "*vc*"
464 (setq new-version
465 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
466 (vc-file-setprop file 'vc-working-revision new-version)
467 ;; if necessary, adjust the default branch
468 (and rev (not (string= rev ""))
469 (vc-rcs-set-default-branch
470 file
471 (if (vc-rcs-latest-on-branch-p file new-version)
472 (if (vc-rcs-trunk-p new-version) nil
473 (vc-branch-part new-version))
474 new-version)))))
475 (message "Checking out %s...done" file))))))
477 (defun vc-rcs-rollback (files)
478 "Roll back, undoing the most recent checkins of FILES. Directories are
479 expanded to all registered subfiles in them."
480 (if (not files)
481 (error "RCS backend doesn't support directory-level rollback"))
482 (dolist (file (vc-expand-dirs files))
483 (let* ((discard (vc-working-revision file))
484 (previous (if (vc-rcs-trunk-p discard) "" (vc-branch-part discard)))
485 (config (current-window-configuration))
486 (done nil))
487 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
488 discard file)))
489 (error "Aborted"))
490 (message "Removing revision %s from %s." discard file)
491 (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-o" discard))
492 ;; Check out the most recent remaining version. If it
493 ;; fails, because the whole branch got deleted, do a
494 ;; double-take and check out the version where the branch
495 ;; started.
496 (while (not done)
497 (condition-case err
498 (progn
499 (vc-do-command "*vc*" 0 "co" (vc-name file) "-f"
500 (concat "-u" previous))
501 (setq done t))
502 (error (set-buffer "*vc*")
503 (goto-char (point-min))
504 (if (search-forward "no side branches present for" nil t)
505 (progn (setq previous (vc-branch-part previous))
506 (vc-rcs-set-default-branch file previous)
507 ;; vc-do-command popped up a window with
508 ;; the error message. Get rid of it, by
509 ;; restoring the old window configuration.
510 (set-window-configuration config))
511 ;; No, it was some other error: re-signal it.
512 (signal (car err) (cdr err)))))))))
514 (defun vc-rcs-revert (file &optional contents-done)
515 "Revert FILE to the version it was based on. If FILE is a directory,
516 revert all registered files beneath it."
517 (if (file-directory-p file)
518 (mapc 'vc-rcs-revert (vc-expand-dirs (list file)))
519 (vc-do-command "*vc*" 0 "co" (vc-name file) "-f"
520 (concat (if (eq (vc-state file) 'edited) "-u" "-r")
521 (vc-working-revision file)))))
523 (defun vc-rcs-merge (file first-version &optional second-version)
524 "Merge changes into current working copy of FILE.
525 The changes are between FIRST-VERSION and SECOND-VERSION."
526 (vc-do-command "*vc*" 1 "rcsmerge" (vc-name file)
527 "-kk" ; ignore keyword conflicts
528 (concat "-r" first-version)
529 (if second-version (concat "-r" second-version))))
531 (defun vc-rcs-steal-lock (file &optional rev)
532 "Steal the lock on the current workfile for FILE and revision REV.
533 If FILE is a directory, steal the lock on all registered files beneath it.
534 Needs RCS 5.6.2 or later for -M."
535 (if (file-directory-p file)
536 (mapc 'vc-rcs-steal-lock (vc-expand-dirs (list file)))
537 (vc-do-command "*vc*" 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
538 ;; Do a real checkout after stealing the lock, so that we see
539 ;; expanded headers.
540 (vc-do-command "*vc*" 0 "co" (vc-name file) "-f" (concat "-l" rev))))
542 (defun vc-rcs-modify-change-comment (files rev comment)
543 "Modify the change comments change on FILES on a specified REV. If FILE is a
544 directory the operation is applied to all registered files beneath it."
545 (dolist (file (vc-expand-dirs files))
546 (vc-do-command "*vc*" 0 "rcs" (vc-name file)
547 (concat "-m" rev ":" comment))))
551 ;;; History functions
554 (defun vc-rcs-print-log-cleanup ()
555 (let ((inhibit-read-only t))
556 (goto-char (point-max))
557 (forward-line -1)
558 (while (looking-at "=*\n")
559 (delete-char (- (match-end 0) (match-beginning 0)))
560 (forward-line -1))
561 (goto-char (point-min))
562 (when (looking-at "[\b\t\n\v\f\r ]+")
563 (delete-char (- (match-end 0) (match-beginning 0))))))
565 (defun vc-rcs-print-log (files buffer &optional shortlog start-revision-ignored limit)
566 "Get change log associated with FILE. If FILE is a
567 directory the operation is applied to all registered files beneath it."
568 (vc-do-command (or buffer "*vc*") 0 "rlog" (mapcar 'vc-name (vc-expand-dirs files)))
569 (with-current-buffer (or buffer "*vc*")
570 (vc-rcs-print-log-cleanup))
571 (when limit 'limit-unsupported))
573 (defun vc-rcs-diff (files &optional oldvers newvers buffer)
574 "Get a difference report using RCS between two sets of files."
575 (apply 'vc-do-command (or buffer "*vc-diff*")
576 1 ;; Always go synchronous, the repo is local
577 "rcsdiff" (vc-expand-dirs files)
578 (append (list "-q"
579 (and oldvers (concat "-r" oldvers))
580 (and newvers (concat "-r" newvers)))
581 (vc-switches 'RCS 'diff))))
583 (defun vc-rcs-comment-history (file)
584 "Return a string with all log entries stored in BACKEND for FILE."
585 (with-current-buffer "*vc*"
586 ;; Has to be written this way, this function is used by the CVS backend too
587 (vc-call-backend (vc-backend file) 'print-log (list file))
588 ;; Remove cruft
589 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
590 "\\(branches: .*;\n\\)?"
591 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
592 (goto-char (point-max)) (forward-line -1)
593 (while (looking-at "=*\n")
594 (delete-char (- (match-end 0) (match-beginning 0)))
595 (forward-line -1))
596 (goto-char (point-min))
597 (if (looking-at "[\b\t\n\v\f\r ]+")
598 (delete-char (- (match-end 0) (match-beginning 0))))
599 (goto-char (point-min))
600 (re-search-forward separator nil t)
601 (delete-region (point-min) (point))
602 (while (re-search-forward separator nil t)
603 (delete-region (match-beginning 0) (match-end 0))))
604 ;; Return the de-crufted comment list
605 (buffer-string)))
607 (defun vc-rcs-annotate-command (file buffer &optional revision)
608 "Annotate FILE, inserting the results in BUFFER.
609 Optional arg REVISION is a revision to annotate from."
610 (vc-setup-buffer buffer)
611 ;; Aside from the "head revision on the trunk", the instructions for
612 ;; each revision on the trunk are an ordered list of kill and insert
613 ;; commands necessary to go from the chronologically-following
614 ;; revision to this one. That is, associated with revision N are
615 ;; edits that applied to revision N+1 would result in revision N.
617 ;; On a branch, however, (some) things are inverted: the commands
618 ;; listed are those necessary to go from the chronologically-preceding
619 ;; revision to this one. That is, associated with revision N are
620 ;; edits that applied to revision N-1 would result in revision N.
622 ;; So, to get per-line history info, we apply reverse-chronological
623 ;; edits, starting with the head revision on the trunk, all the way
624 ;; back through the initial revision (typically "1.1" or similar),
625 ;; then apply forward-chronological edits -- keeping track of which
626 ;; revision is associated with each inserted line -- until we reach
627 ;; the desired revision for display (which may be either on the trunk
628 ;; or on a branch).
629 (let* ((tree (with-temp-buffer
630 (insert-file-contents (vc-rcs-registered file))
631 (vc-rcs-parse)))
632 (revisions (cdr (assq 'revisions tree)))
633 ;; The revision N whose instructions we currently are processing.
634 (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
635 ;; Alist from the parse tree for N.
636 (meta (cdr (assoc cur revisions)))
637 ;; Point and temporary string, respectively.
639 ;; "Next-branch list". Nil means the desired revision to
640 ;; display lives on the trunk. Non-nil means it lives on a
641 ;; branch, in which case the value is a list of revision pairs
642 ;; (PARENT . CHILD), the first PARENT being on the trunk, that
643 ;; links each series of revisions in the path from the initial
644 ;; revision to the desired revision to display.
645 nbls
646 ;; "Path-accumulate-predicate plus revision/date/author".
647 ;; Until set, forward-chronological edits are not accumulated.
648 ;; Once set, its value (updated every revision) is used for
649 ;; the text property `:vc-rcs-r/d/a' for inserts during
650 ;; processing of forward-chronological instructions for N.
651 ;; See internal func `r/d/a'.
652 prda
653 ;; List of forward-chronological instructions, each of the
654 ;; form: (POS . ACTION), where POS is a buffer position. If
655 ;; ACTION is a string, it is inserted, otherwise it is taken as
656 ;; the number of characters to be deleted.
657 path
658 ;; N+1. When `cur' is "", this is the initial revision.
659 pre)
660 (unless revision
661 (setq revision cur))
662 (unless (assoc revision revisions)
663 (error "No such revision: %s" revision))
664 ;; Find which branches (if any) must be included in the edits.
665 (let ((par revision)
666 bpt kids)
667 (while (setq bpt (vc-branch-part par)
668 par (vc-branch-part bpt))
669 (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
670 ;; A branchpoint may have multiple children. Find the right one.
671 (while (not (string= bpt (vc-branch-part (car kids))))
672 (setq kids (cdr kids)))
673 (push (cons par (car kids)) nbls)))
674 ;; Start with the full text.
675 (set-buffer buffer)
676 (insert (cdr (assq 'text meta)))
677 ;; Apply reverse-chronological edits on the trunk, computing and
678 ;; accumulating forward-chronological edits after some point, for
679 ;; later.
680 (flet ((r/d/a () (vector pre
681 (cdr (assq 'date meta))
682 (cdr (assq 'author meta)))))
683 (while (when (setq pre cur cur (cdr (assq 'next meta)))
684 (not (string= "" cur)))
685 (setq
686 ;; Start accumulating the forward-chronological edits when N+1
687 ;; on the trunk is either the desired revision to display, or
688 ;; the appropriate branchpoint for it. Do this before
689 ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
690 prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
691 (r/d/a))
692 meta (cdr (assoc cur revisions)))
693 ;; Edits in the parse tree specify a line number (in the buffer
694 ;; *BEFORE* editing occurs) to start from, but line numbers
695 ;; change as a result of edits. To DTRT, we apply edits in
696 ;; order of descending buffer position so that edits further
697 ;; down in the buffer occur first w/o corrupting specified
698 ;; buffer positions of edits occurring towards the beginning of
699 ;; the buffer. In this way we avoid using markers. A pleasant
700 ;; property of this approach is ability to push instructions
701 ;; onto `path' directly, w/o need to maintain rev boundaries.
702 (dolist (insn (cdr (assq :insn meta)))
703 (goto-char (point-min))
704 (forward-line (1- (pop insn)))
705 (setq p (point))
706 (case (pop insn)
707 (k (setq s (buffer-substring-no-properties
708 p (progn (forward-line (car insn))
709 (point))))
710 (when prda
711 (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
712 (delete-region p (point)))
713 (i (setq s (car insn))
714 (when prda
715 (push `(,p . ,(length s)) path))
716 (insert s)))))
717 ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
718 ;; equivalent to pushing an insert instruction (of the entire buffer
719 ;; contents) onto `path' then erasing the buffer, but less wasteful.
720 (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
721 ;; Now apply the forward-chronological edits for the trunk.
722 (dolist (insn path)
723 (goto-char (pop insn))
724 (if (stringp insn)
725 (insert insn)
726 (delete-char insn)))
727 ;; Now apply the forward-chronological edits (directly from the
728 ;; parse-tree) for the branch(es), if necessary. We re-use vars
729 ;; `pre' and `meta' for the sake of internal func `r/d/a'.
730 (while nbls
731 (setq pre (cdr (pop nbls)))
732 (while (progn
733 (setq meta (cdr (assoc pre revisions))
734 prda nil)
735 (dolist (insn (cdr (assq :insn meta)))
736 (goto-char (point-min))
737 (forward-line (1- (pop insn)))
738 (case (pop insn)
739 (k (delete-region
740 (point) (progn (forward-line (car insn))
741 (point))))
742 (i (insert (propertize
743 (car insn)
744 :vc-rcs-r/d/a
745 (or prda (setq prda (r/d/a))))))))
746 (prog1 (not (string= (if nbls (caar nbls) revision) pre))
747 (setq pre (cdr (assq 'next meta)))))))))
748 ;; Lastly, for each line, insert at bol nicely-formatted history info.
749 ;; We do two passes to collect summary information used to minimize
750 ;; the annotation's usage of screen real-estate: (1) Consider rendered
751 ;; width of revision plus author together as a unit; and (2) Omit
752 ;; author entirely if all authors are the same as the user.
753 (let ((ht (make-hash-table :test 'eq))
754 (me (user-login-name))
755 (maxw 0)
756 (all-me t)
757 rda w a)
758 (goto-char (point-max))
759 (while (not (bobp))
760 (forward-line -1)
761 (setq rda (get-text-property (point) :vc-rcs-r/d/a))
762 (unless (gethash rda ht)
763 (setq a (aref rda 2)
764 all-me (and all-me (string= a me)))
765 (puthash rda (setq w (+ (length (aref rda 0))
766 (length a)))
768 (setq maxw (max w maxw))))
769 (let ((padding (make-string maxw 32)))
770 (flet ((pad (w) (substring-no-properties padding w))
771 (render (rda &rest ls)
772 (propertize
773 (apply 'concat
774 (format-time-string "%Y-%m-%d" (aref rda 1))
776 (aref rda 0)
778 :vc-annotate-prefix t
779 :vc-rcs-r/d/a rda)))
780 (maphash
781 (if all-me
782 (lambda (rda w)
783 (puthash rda (render rda (pad w) ": ") ht))
784 (lambda (rda w)
785 (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
786 ht)))
787 (while (not (eobp))
788 (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
789 (forward-line 1))))
791 (declare-function vc-annotate-convert-time "vc-annotate" (time))
793 (defun vc-rcs-annotate-current-time ()
794 "Return the current time, based at midnight of the current day, and
795 encoded as fractional days."
796 (vc-annotate-convert-time
797 (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
799 (defun vc-rcs-annotate-time ()
800 "Return the time of the next annotation (as fraction of days)
801 systime, or nil if there is none. Also, reposition point."
802 (unless (eobp)
803 (prog1 (vc-annotate-convert-time
804 (aref (get-text-property (point) :vc-rcs-r/d/a) 1))
805 (goto-char (next-single-property-change (point) :vc-annotate-prefix)))))
807 (defun vc-rcs-annotate-extract-revision-at-line ()
808 (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
812 ;;; Tag system
815 (defun vc-rcs-create-tag (backend dir name branchp)
816 (when branchp
817 (error "RCS backend %s does not support module branches" backend))
818 (let ((result (vc-tag-precondition dir)))
819 (if (stringp result)
820 (error "File %s is not up-to-date" result)
821 (vc-file-tree-walk
823 (lambda (f)
824 (vc-do-command "*vc*" 0 "rcs" (vc-name f) (concat "-n" name ":")))))))
828 ;;; Miscellaneous
831 (defun vc-rcs-trunk-p (rev)
832 "Return t if REV is a revision on the trunk."
833 (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
835 (defun vc-rcs-minor-part (rev)
836 "Return the minor revision number of a revision number REV."
837 (string-match "[0-9]+\\'" rev)
838 (substring rev (match-beginning 0) (match-end 0)))
840 (defun vc-rcs-previous-revision (file rev)
841 "Return the revision number immediately preceding REV for FILE,
842 or nil if there is no previous revision. This default
843 implementation works for MAJOR.MINOR-style revision numbers as
844 used by RCS and CVS."
845 (let ((branch (vc-branch-part rev))
846 (minor-num (string-to-number (vc-rcs-minor-part rev))))
847 (when branch
848 (if (> minor-num 1)
849 ;; revision does probably not start a branch or release
850 (concat branch "." (number-to-string (1- minor-num)))
851 (if (vc-rcs-trunk-p rev)
852 ;; we are at the beginning of the trunk --
853 ;; don't know anything to return here
855 ;; we are at the beginning of a branch --
856 ;; return revision of starting point
857 (vc-branch-part branch))))))
859 (defun vc-rcs-next-revision (file rev)
860 "Return the revision number immediately following REV for FILE,
861 or nil if there is no next revision. This default implementation
862 works for MAJOR.MINOR-style revision numbers as used by RCS
863 and CVS."
864 (when (not (string= rev (vc-working-revision file)))
865 (let ((branch (vc-branch-part rev))
866 (minor-num (string-to-number (vc-rcs-minor-part rev))))
867 (concat branch "." (number-to-string (1+ minor-num))))))
869 (defun vc-rcs-update-changelog (files)
870 "Default implementation of update-changelog.
871 Uses `rcs2log' which only works for RCS and CVS."
872 ;; FIXME: We (c|sh)ould add support for cvs2cl
873 (let ((odefault default-directory)
874 (changelog (find-change-log))
875 ;; Presumably not portable to non-Unixy systems, along with rcs2log:
876 (tempfile (make-temp-file
877 (expand-file-name "vc"
878 (or small-temporary-file-directory
879 temporary-file-directory))))
880 (login-name (or user-login-name
881 (format "uid%d" (number-to-string (user-uid)))))
882 (full-name (or add-log-full-name
883 (user-full-name)
884 (user-login-name)
885 (format "uid%d" (number-to-string (user-uid)))))
886 (mailing-address (or add-log-mailing-address
887 user-mail-address)))
888 (find-file-other-window changelog)
889 (barf-if-buffer-read-only)
890 (vc-buffer-sync)
891 (undo-boundary)
892 (goto-char (point-min))
893 (push-mark)
894 (message "Computing change log entries...")
895 (message "Computing change log entries... %s"
896 (unwind-protect
897 (progn
898 (setq default-directory odefault)
899 (if (eq 0 (apply 'call-process
900 (expand-file-name "rcs2log"
901 exec-directory)
902 nil (list t tempfile) nil
903 "-c" changelog
904 "-u" (concat login-name
905 "\t" full-name
906 "\t" mailing-address)
907 (mapcar
908 (lambda (f)
909 (file-relative-name
910 (expand-file-name f odefault)))
911 files)))
912 "done"
913 (pop-to-buffer (get-buffer-create "*vc*"))
914 (erase-buffer)
915 (insert-file-contents tempfile)
916 "failed"))
917 (setq default-directory (file-name-directory changelog))
918 (delete-file tempfile)))))
920 (defun vc-rcs-check-headers ()
921 "Check if the current file has any headers in it."
922 (save-excursion
923 (goto-char (point-min))
924 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
925 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
927 (defun vc-rcs-clear-headers ()
928 "Implementation of vc-clear-headers for RCS."
929 (let ((case-fold-search nil))
930 (goto-char (point-min))
931 (while (re-search-forward
932 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
933 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
934 nil t)
935 (replace-match "$\\1$"))))
937 (defun vc-rcs-rename-file (old new)
938 ;; Just move the master file (using vc-rcs-master-templates).
939 (vc-rename-master (vc-name old) new vc-rcs-master-templates))
941 (defun vc-rcs-find-file-hook ()
942 ;; If the file is locked by some other user, make
943 ;; the buffer read-only. Like this, even root
944 ;; cannot modify a file that someone else has locked.
945 (and (stringp (vc-state buffer-file-name 'RCS))
946 (setq buffer-read-only t)))
950 ;;; Internal functions
953 (defun vc-rcs-workfile-is-newer (file)
954 "Return non-nil if FILE is newer than its RCS master.
955 This likely means that FILE has been changed with respect
956 to its master version."
957 (let ((file-time (nth 5 (file-attributes file)))
958 (master-time (nth 5 (file-attributes (vc-name file)))))
959 (or (> (nth 0 file-time) (nth 0 master-time))
960 (and (= (nth 0 file-time) (nth 0 master-time))
961 (> (nth 1 file-time) (nth 1 master-time))))))
963 (defun vc-rcs-find-most-recent-rev (branch)
964 "Find most recent revision on BRANCH."
965 (goto-char (point-min))
966 (let ((latest-rev -1) value)
967 (while (re-search-forward (concat "^\\(" (regexp-quote branch)
968 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
969 nil t)
970 (let ((rev (string-to-number (match-string 2))))
971 (when (< latest-rev rev)
972 (setq latest-rev rev)
973 (setq value (match-string 1)))))
974 (or value
975 (vc-branch-part branch))))
977 (defun vc-rcs-fetch-master-state (file &optional working-revision)
978 "Compute the master file's idea of the state of FILE.
979 If a WORKING-REVISION is given, compute the state of that version,
980 otherwise determine the workfile version based on the master file.
981 This function sets the properties `vc-working-revision' and
982 `vc-checkout-model' to their correct values, based on the master
983 file."
984 (with-temp-buffer
985 (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
986 (progn (goto-char (point-min))
987 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
988 (error "File %s is not an RCS master file" (vc-name file)))
989 (let ((workfile-is-latest nil)
990 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
991 (vc-file-setprop file 'vc-rcs-default-branch default-branch)
992 (unless working-revision
993 ;; Workfile version not known yet. Determine that first. It
994 ;; is either the head of the trunk, the head of the default
995 ;; branch, or the "default branch" itself, if that is a full
996 ;; revision number.
997 (cond
998 ;; no default branch
999 ((or (not default-branch) (string= "" default-branch))
1000 (setq working-revision
1001 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
1002 (setq workfile-is-latest t))
1003 ;; default branch is actually a revision
1004 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
1005 default-branch)
1006 (setq working-revision default-branch))
1007 ;; else, search for the head of the default branch
1008 (t (vc-insert-file (vc-name file) "^desc")
1009 (setq working-revision
1010 (vc-rcs-find-most-recent-rev default-branch))
1011 (setq workfile-is-latest t)))
1012 (vc-file-setprop file 'vc-working-revision working-revision))
1013 ;; Check strict locking
1014 (goto-char (point-min))
1015 (vc-file-setprop file 'vc-checkout-model
1016 (if (re-search-forward ";[ \t\n]*strict;" nil t)
1017 'locking 'implicit))
1018 ;; Compute state of workfile version
1019 (goto-char (point-min))
1020 (let ((locking-user
1021 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
1022 (regexp-quote working-revision)
1023 "[^0-9.]")
1024 1)))
1025 (cond
1026 ;; not locked
1027 ((not locking-user)
1028 (if (or workfile-is-latest
1029 (vc-rcs-latest-on-branch-p file working-revision))
1030 ;; workfile version is latest on branch
1031 'up-to-date
1032 ;; workfile version is not latest on branch
1033 'needs-update))
1034 ;; locked by the calling user
1035 ((and (stringp locking-user)
1036 (string= locking-user (vc-user-login-name file)))
1037 ;; Don't call `vc-rcs-checkout-model' to avoid inf-looping.
1038 (if (or (eq (vc-file-getprop file 'vc-checkout-model) 'locking)
1039 workfile-is-latest
1040 (vc-rcs-latest-on-branch-p file working-revision))
1041 'edited
1042 ;; Locking is not used for the file, but the owner does
1043 ;; have a lock, and there is a higher version on the current
1044 ;; branch. Not sure if this can occur, and if it is right
1045 ;; to use `needs-merge' in this case.
1046 'needs-merge))
1047 ;; locked by somebody else
1048 ((stringp locking-user)
1049 locking-user)
1051 (error "Error getting state of RCS file")))))))
1053 (defun vc-rcs-consult-headers (file)
1054 "Search for RCS headers in FILE, and set properties accordingly.
1056 Returns: nil if no headers were found
1057 'rev if a workfile revision was found
1058 'rev-and-lock if revision and lock info was found"
1059 (cond
1060 ((not (get-file-buffer file)) nil)
1061 ((let (status version locking-user)
1062 (with-current-buffer (get-file-buffer file)
1063 (save-excursion
1064 (goto-char (point-min))
1065 (cond
1066 ;; search for $Id or $Header
1067 ;; -------------------------
1068 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
1069 ((or (and (search-forward "$Id\ : " nil t)
1070 (looking-at "[^ ]+ \\([0-9.]+\\) "))
1071 (and (progn (goto-char (point-min))
1072 (search-forward "$Header\ : " nil t))
1073 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
1074 (goto-char (match-end 0))
1075 ;; if found, store the revision number ...
1076 (setq version (match-string-no-properties 1))
1077 ;; ... and check for the locking state
1078 (cond
1079 ((looking-at
1080 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
1081 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
1082 "[^ ]+ [^ ]+ ")) ; author & state
1083 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
1084 (cond
1085 ;; unlocked revision
1086 ((looking-at "\\$")
1087 (setq locking-user 'none)
1088 (setq status 'rev-and-lock))
1089 ;; revision is locked by some user
1090 ((looking-at "\\([^ ]+\\) \\$")
1091 (setq locking-user (match-string-no-properties 1))
1092 (setq status 'rev-and-lock))
1093 ;; everything else: false
1094 (nil)))
1095 ;; unexpected information in
1096 ;; keyword string --> quit
1097 (nil)))
1098 ;; search for $Revision
1099 ;; --------------------
1100 ((re-search-forward (concat "\\$"
1101 "Revision: \\([0-9.]+\\) \\$")
1102 nil t)
1103 ;; if found, store the revision number ...
1104 (setq version (match-string-no-properties 1))
1105 ;; and see if there's any lock information
1106 (goto-char (point-min))
1107 (if (re-search-forward (concat "\\$" "Locker:") nil t)
1108 (cond ((looking-at " \\([^ ]+\\) \\$")
1109 (setq locking-user (match-string-no-properties 1))
1110 (setq status 'rev-and-lock))
1111 ((looking-at " *\\$")
1112 (setq locking-user 'none)
1113 (setq status 'rev-and-lock))
1115 (setq locking-user 'none)
1116 (setq status 'rev-and-lock)))
1117 (setq status 'rev)))
1118 ;; else: nothing found
1119 ;; -------------------
1120 (t nil))))
1121 (if status (vc-file-setprop file 'vc-working-revision version))
1122 (and (eq status 'rev-and-lock)
1123 (vc-file-setprop file 'vc-state
1124 (cond
1125 ((eq locking-user 'none) 'up-to-date)
1126 ((string= locking-user (vc-user-login-name file))
1127 'edited)
1128 (t locking-user)))
1129 ;; If the file has headers, we don't want to query the
1130 ;; master file, because that would eliminate all the
1131 ;; performance gain the headers brought us. We therefore
1132 ;; use a heuristic now to find out whether locking is used
1133 ;; for this file. If we trust the file permissions, and the
1134 ;; file is not locked, then if the file is read-only we
1135 ;; assume that locking is used for the file, otherwise
1136 ;; locking is not used.
1137 (not (vc-mistrust-permissions file))
1138 (vc-up-to-date-p file)
1139 (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
1140 (vc-file-setprop file 'vc-checkout-model 'locking)
1141 (vc-file-setprop file 'vc-checkout-model 'implicit)))
1142 status))))
1144 (defun vc-release-greater-or-equal (r1 r2)
1145 "Compare release numbers, represented as strings.
1146 Release components are assumed cardinal numbers, not decimal fractions
1147 \(5.10 is a higher release than 5.9\). Omitted fields are considered
1148 lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
1149 of the string is found, or a non-numeric component shows up \(5.6.7 is
1150 earlier than \"5.6.7 beta\", which is probably not what you want in
1151 some cases\). This code is suitable for existing RCS release numbers.
1152 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
1153 (let (v1 v2 i1 i2)
1154 (catch 'done
1155 (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
1156 (setq i1 (match-end 0))
1157 (setq v1 (string-to-number (match-string 1 r1)))
1158 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1159 (setq i2 (match-end 0))
1160 (setq v2 (string-to-number (match-string 1 r2)))
1161 (if (> v1 v2) (throw 'done t)
1162 (if (< v1 v2) (throw 'done nil)
1163 (throw 'done
1164 (vc-release-greater-or-equal
1165 (substring r1 i1)
1166 (substring r2 i2)))))))
1167 (throw 'done t)))
1168 (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
1169 (throw 'done nil))
1170 (throw 'done t)))))
1172 (defun vc-rcs-release-p (release)
1173 "Return t if we have RELEASE or better."
1174 (let ((installation (vc-rcs-system-release)))
1175 (if (and installation
1176 (not (eq installation 'unknown)))
1177 (vc-release-greater-or-equal installation release))))
1179 (defun vc-rcs-system-release ()
1180 "Return the RCS release installed on this system, as a string.
1181 Return symbol `unknown' if the release cannot be deducted. The user can
1182 override this using variable `vc-rcs-release'.
1184 If the user has not set variable `vc-rcs-release' and it is nil,
1185 variable `vc-rcs-release' is set to the returned value."
1186 (or vc-rcs-release
1187 (setq vc-rcs-release
1188 (or (and (zerop (vc-do-command "*vc*" nil "rcs" nil "-V"))
1189 (with-current-buffer (get-buffer "*vc*")
1190 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
1191 'unknown))))
1193 (defun vc-rcs-set-non-strict-locking (file)
1194 (vc-do-command "*vc*" 0 "rcs" file "-U")
1195 (vc-file-setprop file 'vc-checkout-model 'implicit)
1196 (set-file-modes file (logior (file-modes file) 128)))
1198 (defun vc-rcs-set-default-branch (file branch)
1199 (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-b" branch))
1200 (vc-file-setprop file 'vc-rcs-default-branch branch))
1202 (defun vc-rcs-parse (&optional buffer)
1203 "Parse current buffer, presumed to be in RCS-style masterfile format.
1204 Optional arg BUFFER specifies another buffer to parse. Return an alist
1205 of two elements, w/ keys `headers' and `revisions' and values in turn
1206 sub-alists. For `headers', the values unless otherwise specified are
1207 strings and the keys are:
1209 desc -- description
1210 head -- latest revision
1211 branch -- the branch the \"head revision\" lies on;
1212 absent if the head revision lies on the trunk
1213 access -- ???
1214 symbols -- sub-alist of (SYMBOL . REVISION) elements
1215 locks -- if file is checked out, something like \"ttn:1.7\"
1216 strict -- t if \"strict locking\" is in effect, otherwise nil
1217 comment -- may be absent; typically something like \"# \" or \"; \"
1218 expand -- may be absent; ???
1220 For `revisions', the car is REVISION (string), the cdr a sub-alist,
1221 with string values (unless otherwise specified) and keys:
1223 date -- a time value (like that returned by `encode-time'); as a
1224 special case, a year value less than 100 is augmented by 1900
1225 author -- username
1226 state -- typically \"Exp\" or \"Rel\"
1227 branches -- list of revisions that begin branches from this revision
1228 next -- on the trunk: the chronologically-preceding revision, or \"\";
1229 on a branch: the chronologically-following revision, or \"\"
1230 log -- change log entry
1231 text -- for the head revision on the trunk, the body of the file;
1232 other revisions have `:insn' instead
1233 :insn -- for non-head revisions, a list of parsed instructions
1234 in one of two forms, in both cases START meaning \"first
1235 go to line START\":
1236 - `(START k COUNT)' -- kill COUNT lines
1237 - `(START i TEXT)' -- insert TEXT (a string)
1238 The list is in descending order by START.
1240 The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1241 (setq buffer (get-buffer (or buffer (current-buffer))))
1242 (set-buffer buffer)
1243 ;; An RCS masterfile can be viewed as containing four regular (for the
1244 ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1245 ;; the "description" and (d) the "rev bodies", in that order. In the
1246 ;; returned alist (see docstring), elements from (b) and (d) are
1247 ;; combined pairwise to form the "revisions", while those from (a) and
1248 ;; (c) are simply combined to form the "headers".
1250 ;; Loosely speaking, each section contains a series of alternating
1251 ;; "tags" and "printed representations". In the (b) and (d), many
1252 ;; such series can appear, and a revision number on a line by itself
1253 ;; precedes the series of tags and printed representations associated
1254 ;; with it.
1256 ;; In (a) and (b), the printed representations (with the exception of
1257 ;; the `comment' tag in the headers) terminate with a semicolon, which
1258 ;; is NOT part of the "value" finally associated with the tag. All
1259 ;; other printed representations are in "@@-format"; there is an "@",
1260 ;; the middle part (to be translated into the value), another "@" and
1261 ;; a newline. Each "@@" in the middle part indicates the position of
1262 ;; a single "@" (and consequently the requirement of an additional
1263 ;; initial step when translating to the value).
1265 ;; Parser state includes vars that collect parts of the return value...
1266 (let ((desc nil) (headers nil) (revs nil)
1267 ;; ... as well as vars that support a single-pass, tag-assisted,
1268 ;; minimal-data-copying scan. Basically -- skirting around the
1269 ;; grouping by revision required in (b) and (d) -- we repeatedly
1270 ;; and context-sensitively read a tag (that MUST be present),
1271 ;; determine the bounds of the printed representation, translate
1272 ;; it into a value, and push the tag plus value onto one of the
1273 ;; collection vars. Finally, we return the parse tree
1274 ;; incorporating the values of the collection vars (see "rv").
1276 ;; A symbol or string to keep track of context (for error messages).
1277 context
1278 ;; A symbol, the current tag.
1280 ;; Region (begin and end buffer positions) of the printed
1281 ;; representation for the current tag.
1283 ;; A list of buffer positions where "@@" can be found within the
1284 ;; printed representation region. For each location, we push two
1285 ;; elements onto the list, 1+ and 2+ the location, respectively,
1286 ;; with the 2+ appearing at the head. In this way, the expression
1287 ;; `(,e ,@@-holes ,b)
1288 ;; describes regions that can be concatenated (in reverse order)
1289 ;; to "de-@@-format" the printed representation as the first step
1290 ;; to translating it into some value. See internal func `gather'.
1291 @-holes)
1292 (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1293 (at (tag) (save-excursion (eq tag (read buffer))))
1294 (to-eol () (buffer-substring-no-properties
1295 (point) (progn (forward-line 1)
1296 (1- (point)))))
1297 (to-semi () (setq b (point)
1298 e (progn (search-forward ";")
1299 (1- (point)))))
1300 (to-one@ () (setq @-holes nil
1301 b (progn (search-forward "@") (point))
1302 e (progn (while (and (search-forward "@")
1303 (= ?@ (char-after))
1304 (progn
1305 (push (point) @-holes)
1306 (forward-char 1)
1307 (push (point) @-holes))))
1308 (1- (point)))))
1309 (tok+val (set-b+e name &optional proc)
1310 (unless (eq name (setq tok (read buffer)))
1311 (error "Missing `%s' while parsing %s" name context))
1312 (sw)
1313 (funcall set-b+e)
1314 (cons tok (if proc
1315 (funcall proc)
1316 (buffer-substring-no-properties b e))))
1317 (k-semi (name &optional proc) (tok+val 'to-semi name proc))
1318 (gather () (let ((pairs `(,e ,@@-holes ,b))
1319 acc)
1320 (while pairs
1321 (push (buffer-substring-no-properties
1322 (cadr pairs) (car pairs))
1323 acc)
1324 (setq pairs (cddr pairs)))
1325 (apply 'concat acc)))
1326 (k-one@ (name &optional later) (tok+val 'to-one@ name
1327 (if later
1328 (lambda () t)
1329 'gather))))
1330 (save-excursion
1331 (goto-char (point-min))
1332 ;; headers
1333 (setq context 'headers)
1334 (flet ((hpush (name &optional proc)
1335 (push (k-semi name proc) headers)))
1336 (hpush 'head)
1337 (when (at 'branch)
1338 (hpush 'branch))
1339 (hpush 'access)
1340 (hpush 'symbols
1341 (lambda ()
1342 (mapcar (lambda (together)
1343 (let ((two (split-string together ":")))
1344 (setcar two (intern (car two)))
1345 (setcdr two (cadr two))
1346 two))
1347 (split-string
1348 (buffer-substring-no-properties b e)))))
1349 (hpush 'locks))
1350 (push `(strict . ,(when (at 'strict)
1351 (search-forward ";")
1353 headers)
1354 (when (at 'comment)
1355 (push (k-one@ 'comment) headers)
1356 (search-forward ";"))
1357 (when (at 'expand)
1358 (push (k-one@ 'expand) headers)
1359 (search-forward ";"))
1360 (setq headers (nreverse headers))
1361 ;; rev headers
1362 (sw) (setq context 'rev-headers)
1363 (while (looking-at "[0-9]")
1364 (push `(,(to-eol)
1365 ,(k-semi 'date
1366 (lambda ()
1367 (let ((ls (mapcar 'string-to-number
1368 (split-string
1369 (buffer-substring-no-properties
1370 b e)
1371 "\\."))))
1372 ;; Hack the year -- verified to be the
1373 ;; same algorithm used in RCS 5.7.
1374 (when (< (car ls) 100)
1375 (setcar ls (+ 1900 (car ls))))
1376 (apply 'encode-time (nreverse ls)))))
1377 ,@(mapcar 'k-semi '(author state))
1378 ,(k-semi 'branches
1379 (lambda ()
1380 (split-string
1381 (buffer-substring-no-properties b e))))
1382 ,(k-semi 'next))
1383 revs)
1384 (sw))
1385 (setq revs (nreverse revs))
1386 ;; desc
1387 (sw) (setq context 'desc
1388 desc (k-one@ 'desc))
1389 ;; rev bodies
1390 (let (acc
1391 ;; Element of `revs' that initially holds only header info.
1392 ;; "Pairwise combination" occurs when we add body info.
1394 ;; Components of the editing commands (aside from the actual
1395 ;; text) that comprise the `text' printed representations
1396 ;; (not including the "head" revision).
1397 cmd start act
1398 ;; Ascending (reversed) `@-holes' which the internal func
1399 ;; `incg' pops to effect incremental gathering.
1401 ;; Function to extract text (for the `a' command), either
1402 ;; `incg' or `buffer-substring-no-properties'. (This is
1403 ;; for speed; strictly speaking, it is sufficient to use
1404 ;; only the former since it behaves identically to the
1405 ;; latter in the absense of "@@".)
1406 sub)
1407 (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
1408 (while (and asc (< (car asc) e))
1409 (push (pop asc) @-holes))
1410 ;; Self-deprecate when work is done.
1411 ;; Folding many dimensions into one.
1412 ;; Thanks B.Mandelbrot, for complex sum.
1413 ;; O beauteous math! --the Unvexed Bum
1414 (unless asc
1415 (setq sub 'buffer-substring-no-properties))
1416 (gather))))
1417 (while (and (sw)
1418 (not (eobp))
1419 (setq context (to-eol)
1420 rev (or (assoc context revs)
1421 (error "Rev `%s' has body but no head"
1422 context))))
1423 (push (k-one@ 'log) (cdr rev))
1424 ;; For rev body `text' tags, delay translation slightly...
1425 (push (k-one@ 'text t) (cdr rev))
1426 ;; ... until we decide which tag and value is appropriate to
1427 ;; collect. For the "head" revision, compute the value of the
1428 ;; `text' printed representation by simple `gather'. For all
1429 ;; other revisions, replace the `text' tag+value with `:insn'
1430 ;; plus value, always scanning in-place.
1431 (if (string= context (cdr (assq 'head headers)))
1432 (setcdr (cadr rev) (gather))
1433 (if @-holes
1434 (setq asc (nreverse @-holes)
1435 sub 'incg)
1436 (setq sub 'buffer-substring-no-properties))
1437 (goto-char b)
1438 (setq acc nil)
1439 (while (< (point) e)
1440 (forward-char 1)
1441 (setq cmd (char-before)
1442 start (read (current-buffer))
1443 act (read (current-buffer)))
1444 (forward-char 1)
1445 (push (case cmd
1447 ;; `d' means "delete lines".
1448 ;; For Emacs spirit, we use `k' for "kill".
1449 `(,start k ,act))
1451 ;; `a' means "append after this line" but
1452 ;; internally we normalize it so that START
1453 ;; specifies the actual line for insert, thus
1454 ;; requiring less hair in the realization algs.
1455 ;; For Emacs spirit, we use `i' for "insert".
1456 `(,(1+ start) i
1457 ,(funcall sub (point) (progn (forward-line act)
1458 (point)))))
1459 (t (error "Bad command `%c' in `text' for rev `%s'"
1460 cmd context)))
1461 acc))
1462 (goto-char (1+ e))
1463 (setcar (cdr rev) (cons :insn acc)))))))
1464 ;; rv
1465 `((headers ,desc ,@headers)
1466 (revisions ,@revs)))))
1468 (provide 'vc-rcs)
1470 ;; arch-tag: 759b4916-5b0d-431d-b647-b185b8c652cf
1471 ;;; vc-rcs.el ends here