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 Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
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 2, or (at your option)
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.
35 ;;; Customization options
42 (defcustom vc-rcs-release nil
43 "*The release number of your RCS installation, as a string.
44 If nil, VC itself computes this value when it is first needed."
45 :type
'(choice (const :tag
"Auto" nil
)
46 (string :tag
"Specified")
47 (const :tag
"Unknown" unknown
))
50 (defcustom vc-rcs-register-switches nil
51 "*Extra switches for registering a file in RCS.
52 A string or list of strings. These are passed to the checkin program
53 by \\[vc-rcs-register]."
54 :type
'(choice (const :tag
"None" nil
)
55 (string :tag
"Argument String")
56 (repeat :tag
"Argument List"
62 (defcustom vc-rcs-diff-switches nil
63 "*A string or list of strings specifying extra switches for rcsdiff under VC."
64 :type
'(choice (const :tag
"None" nil
)
65 (string :tag
"Argument String")
66 (repeat :tag
"Argument List"
72 (defcustom vc-rcs-header
(or (cdr (assoc 'RCS vc-header-alist
)) '("\$Id\$"))
73 "*Header keywords to be inserted by `vc-insert-headers'."
74 :type
'(repeat string
)
78 (defcustom vc-rcsdiff-knows-brief nil
79 "*Indicates whether rcsdiff understands the --brief option.
80 The value is either `yes', `no', or nil. If it is nil, VC tries
81 to use --brief and sets this variable to remember whether it worked."
82 :type
'(choice (const :tag
"Work out" nil
) (const yes
) (const no
))
86 (defcustom vc-rcs-master-templates
87 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s")
88 "*Where to look for RCS master files.
89 For a description of possible values, see `vc-check-master-templates'."
90 :type
'(choice (const :tag
"Use standard RCS file names"
91 '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
92 (repeat :tag
"User-specified"
99 ;;; Properties of the backend
101 (defun vc-rcs-revision-granularity ()
105 ;;; State-querying functions
108 ;;; The autoload cookie below places vc-rcs-registered directly into
109 ;;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
110 ;;; every file that is visited. The definition is repeated below
111 ;;; so that Help and etags can find it.
113 ;;;###autoload (defun vc-rcs-registered (f) (vc-default-registered 'RCS f))
114 (defun vc-rcs-registered (f) (vc-default-registered 'RCS f
))
116 (defun vc-rcs-state (file)
117 "Implementation of `vc-state' for RCS."
118 (or (boundp 'vc-rcs-headers-result
)
119 (and vc-consult-headers
120 (vc-rcs-consult-headers file
)))
122 ;; vc-workfile-version might not be known; in that case the
123 ;; property is nil. vc-rcs-fetch-master-state knows how to
125 (vc-rcs-fetch-master-state file
126 (vc-file-getprop file
127 'vc-workfile-version
))))
128 (if (not (eq state
'up-to-date
))
130 (if (vc-workfile-unchanged-p file
)
132 (if (eq (vc-checkout-model file
) 'locking
)
136 (defun vc-rcs-state-heuristic (file)
137 "State heuristic for RCS."
138 (let (vc-rcs-headers-result)
139 (if (and vc-consult-headers
140 (setq vc-rcs-headers-result
141 (vc-rcs-consult-headers file
))
142 (eq vc-rcs-headers-result
'rev-and-lock
))
143 (let ((state (vc-file-getprop file
'vc-state
)))
144 ;; If the headers say that the file is not locked, the
145 ;; permissions can tell us whether locking is used for
147 (if (and (eq state
'up-to-date
)
148 (not (vc-mistrust-permissions file
)))
150 ((string-match ".rw..-..-." (nth 8 (file-attributes file
)))
151 (vc-file-setprop file
'vc-checkout-model
'implicit
)
153 (if (vc-rcs-workfile-is-newer file
)
156 ((string-match ".r-..-..-." (nth 8 (file-attributes file
)))
157 (vc-file-setprop file
'vc-checkout-model
'locking
))))
159 (if (not (vc-mistrust-permissions file
))
160 (let* ((attributes (file-attributes file
'string
))
161 (owner-name (nth 2 attributes
))
162 (permissions (nth 8 attributes
)))
163 (cond ((string-match ".r-..-..-." permissions
)
164 (vc-file-setprop file
'vc-checkout-model
'locking
)
166 ((string-match ".rw..-..-." permissions
)
167 (if (eq (vc-checkout-model file
) 'locking
)
168 (if (file-ownership-preserved-p file
)
171 (if (vc-rcs-workfile-is-newer file
)
175 ;; Strange permissions. Fall through to
176 ;; expensive state computation.
177 (vc-rcs-state file
))))
178 (vc-rcs-state file
)))))
180 (defun vc-rcs-workfile-version (file)
181 "RCS-specific version of `vc-workfile-version'."
182 (or (and vc-consult-headers
183 (vc-rcs-consult-headers file
)
184 (vc-file-getprop file
'vc-workfile-version
))
186 (vc-rcs-fetch-master-state file
)
187 (vc-file-getprop file
'vc-workfile-version
))))
189 (defun vc-rcs-latest-on-branch-p (file &optional version
)
190 "Return non-nil if workfile version of FILE is the latest on its branch.
191 When VERSION is given, perform check for that version."
192 (unless version
(setq version
(vc-workfile-version file
)))
195 (if (vc-trunk-p version
)
197 ;; Compare VERSION to the head version number.
198 (vc-insert-file (vc-name file
) "^[0-9]")
199 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
200 ;; If we are not on the trunk, we need to examine the
201 ;; whole current branch.
202 (vc-insert-file (vc-name file
) "^desc")
203 (vc-rcs-find-most-recent-rev (vc-branch-part version
))))))
205 (defun vc-rcs-checkout-model (file)
206 "RCS-specific version of `vc-checkout-model'."
208 (when vc-consult-headers
209 (vc-file-setprop file
'vc-checkout-model nil
)
210 (vc-rcs-consult-headers file
)
211 (setq result
(vc-file-getprop file
'vc-checkout-model
)))
213 (progn (vc-rcs-fetch-master-state file
)
214 (vc-file-getprop file
'vc-checkout-model
)))))
216 (defun vc-rcs-workfile-unchanged-p (file)
217 "RCS-specific implementation of `vc-workfile-unchanged-p'."
218 ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
219 ;; do a double take and remember the fact for the future
220 (let* ((version (concat "-r" (vc-workfile-version file
)))
221 (status (if (eq vc-rcsdiff-knows-brief
'no
)
222 (vc-do-command nil
1 "rcsdiff" file version
)
223 (vc-do-command nil
2 "rcsdiff" file
"--brief" version
))))
225 (if (not vc-rcsdiff-knows-brief
)
226 (setq vc-rcsdiff-knows-brief
'no
227 status
(vc-do-command nil
1 "rcsdiff" file version
))
228 (error "rcsdiff failed"))
229 (if (not vc-rcsdiff-knows-brief
) (setq vc-rcsdiff-knows-brief
'yes
)))
230 ;; The workfile is unchanged if rcsdiff found no differences.
235 ;;; State-changing functions
238 (defun vc-rcs-create-repo ()
239 "Create a new RCS repository."
240 ;; RCS is totally file-oriented, so all we have to do is make the directory
241 (make-directory "RCS"))
243 (defun vc-rcs-register (files &optional rev comment
)
244 "Register FILES into the RCS version-control system.
245 REV is the optional revision number for the files. COMMENT can be used
246 to provide an initial description for each FILES.
248 `vc-register-switches' and `vc-rcs-register-switches' are passed to
249 the RCS command (in that order).
251 Automatically retrieve a read-only version of the file with keywords
252 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
253 (let ((subdir (expand-file-name "RCS" (file-name-directory file
))))
255 (and (not (file-exists-p subdir
))
256 (not (directory-files (file-name-directory file
)
258 (yes-or-no-p "Create RCS subdirectory? ")
259 (make-directory subdir
))
260 (apply 'vc-do-command nil
0 "ci" file
261 ;; if available, use the secure registering option
262 (and (vc-rcs-release-p "5.6.4") "-i")
263 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
264 (and comment
(concat "-t-" comment
))
265 (vc-switches 'RCS
'register
))
266 ;; parse output to find master file name and workfile version
267 (with-current-buffer "*vc*"
268 (goto-char (point-min))
269 (let ((name (if (looking-at (concat "^\\(.*\\) <-- "
270 (file-name-nondirectory file
)))
273 ;; if we couldn't find the master name,
274 ;; run vc-rcs-registered to get it
275 ;; (will be stored into the vc-name property)
276 (vc-rcs-registered file
)
277 (vc-file-setprop file
'vc-name
278 (if (file-name-absolute-p name
)
282 (file-name-directory file
))))))
283 (vc-file-setprop file
'vc-workfile-version
284 (if (re-search-forward
285 "^initial revision: \\([0-9.]+\\).*\n"
287 (match-string 1)))))))
289 (defun vc-rcs-responsible-p (file)
290 "Return non-nil if RCS thinks it would be responsible for registering FILE."
291 ;; TODO: check for all the patterns in vc-rcs-master-templates
292 (file-directory-p (expand-file-name "RCS" (file-name-directory file
))))
294 (defun vc-rcs-receive-file (file rev
)
295 "Implementation of receive-file for RCS."
296 (let ((checkout-model (vc-checkout-model file
)))
297 (vc-rcs-register file rev
"")
298 (when (eq checkout-model
'implicit
)
299 (vc-rcs-set-non-strict-locking file
))
300 (vc-rcs-set-default-branch file
(concat rev
".1"))))
302 (defun vc-rcs-unregister (file)
303 "Unregister FILE from RCS.
304 If this leaves the RCS subdirectory empty, ask the user
305 whether to remove it."
306 (let* ((master (vc-name file
))
307 (dir (file-name-directory master
))
308 (backup-info (find-backup-file-name master
)))
309 (if (not backup-info
)
311 (rename-file master
(car backup-info
) 'ok-if-already-exists
)
312 (dolist (f (cdr backup-info
)) (ignore-errors (delete-file f
))))
313 (and (string= (file-name-nondirectory (directory-file-name dir
)) "RCS")
314 ;; check whether RCS dir is empty, i.e. it does not
315 ;; contain any files except "." and ".."
316 (not (directory-files dir nil
317 "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
318 (yes-or-no-p (format "Directory %s is empty; remove it? " dir
))
319 (delete-directory dir
))))
321 (defun vc-rcs-checkin (files rev comment
)
322 "RCS-specific version of `vc-backend-checkin'."
323 (let ((switches (vc-switches 'RCS
'checkin
)))
324 ;; Now operate on the files
326 (let ((old-version (vc-workfile-version file
)) new-version
327 (default-branch (vc-file-getprop file
'vc-rcs-default-branch
)))
328 ;; Force branch creation if an appropriate
329 ;; default branch has been set.
332 (string-match (concat "^" (regexp-quote old-version
) "\\.")
334 (setq rev default-branch
)
335 (setq switches
(cons "-f" switches
)))
336 (if (and (not rev
) old-version
)
337 (setq rev
(vc-branch-part old-version
)))
338 (apply 'vc-do-command nil
0 "ci" (vc-name file
)
339 ;; if available, use the secure check-in option
340 (and (vc-rcs-release-p "5.6.4") "-j")
341 (concat (if vc-keep-workfiles
"-u" "-r") rev
)
342 (concat "-m" comment
)
344 (vc-file-setprop file
'vc-workfile-version nil
)
346 ;; determine the new workfile version
348 (goto-char (point-min))
349 (when (or (re-search-forward
350 "new revision: \\([0-9.]+\\);" nil t
)
352 "reverting to previous revision \\([0-9.]+\\)" nil t
))
353 (setq new-version
(match-string 1))
354 (vc-file-setprop file
'vc-workfile-version new-version
))
356 ;; if we got to a different branch, adjust the default
357 ;; branch accordingly
359 ((and old-version new-version
360 (not (string= (vc-branch-part old-version
)
361 (vc-branch-part new-version
))))
362 (vc-rcs-set-default-branch file
363 (if (vc-trunk-p new-version
) nil
364 (vc-branch-part new-version
)))
365 ;; If this is an old RCS release, we might have
366 ;; to remove a remaining lock.
367 (if (not (vc-rcs-release-p "5.6.2"))
368 ;; exit status of 1 is also accepted.
369 ;; It means that the lock was removed before.
370 (vc-do-command nil
1 "rcs" (vc-name file
)
371 (concat "-u" old-version
)))))))))
373 (defun vc-rcs-find-version (file rev buffer
)
374 (apply 'vc-do-command
375 buffer
0 "co" (vc-name file
)
376 "-q" ;; suppress diagnostic output
378 (vc-switches 'RCS
'checkout
)))
380 (defun vc-rcs-checkout (file &optional editable rev
)
381 "Retrieve a copy of a saved version of FILE."
382 (let ((file-buffer (get-file-buffer file
))
384 (message "Checking out %s..." file
)
386 ;; Change buffers to get local value of vc-checkout-switches.
387 (if file-buffer
(set-buffer file-buffer
))
388 (setq switches
(vc-switches 'RCS
'checkout
))
389 ;; Save this buffer's default-directory
390 ;; and use save-excursion to make sure it is restored
391 ;; in the same buffer it was saved in.
392 (let ((default-directory default-directory
))
394 ;; Adjust the default-directory so that the check-out creates
395 ;; the file in the right place.
396 (setq default-directory
(file-name-directory file
))
398 ;; if we should go to the head of the trunk,
399 ;; clear the default branch first
400 (and rev
(string= rev
"")
401 (vc-rcs-set-default-branch file nil
))
402 ;; now do the checkout
403 (apply 'vc-do-command
404 nil
0 "co" (vc-name file
)
405 ;; If locking is not strict, force to overwrite
406 ;; the writable workfile.
407 (if (eq (vc-checkout-model file
) 'implicit
) "-f")
410 ;; a literal revision was specified
412 (let ((workrev (vc-workfile-version file
)))
416 ;; no revision specified:
417 ;; use current workfile version
420 (if (not (vc-trunk-p workrev
))
421 ;; ... go to head of current branch
422 (vc-branch-part workrev
)
423 ;; ... go to head of trunk
424 (vc-rcs-set-default-branch file
428 ;; determine the new workfile version
429 (with-current-buffer "*vc*"
431 (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
432 (vc-file-setprop file
'vc-workfile-version new-version
)
433 ;; if necessary, adjust the default branch
434 (and rev
(not (string= rev
""))
435 (vc-rcs-set-default-branch
437 (if (vc-rcs-latest-on-branch-p file new-version
)
438 (if (vc-trunk-p new-version
) nil
439 (vc-branch-part new-version
))
441 (message "Checking out %s...done" file
)))))
443 (defun vc-rcs-rollback (files)
444 "Roll back, undoing the most recent checkins of FILES."
446 (error "RCS backend doesn't support directory-level rollback."))
448 (let* ((discard (vc-workfile-version file
))
449 (previous (if (vc-trunk-p discard
) "" (vc-branch-part discard
)))
450 (config (current-window-configuration))
452 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
455 (message "Removing revision %s from %s." discard file
)
456 (vc-do-command nil
0 "rcs" (vc-name file
) (concat "-o" discard
))
457 ;; Check out the most recent remaining version. If it
458 ;; fails, because the whole branch got deleted, do a
459 ;; double-take and check out the version where the branch
464 (vc-do-command nil
0 "co" (vc-name file
) "-f"
465 (concat "-u" previous
))
467 (error (set-buffer "*vc*")
468 (goto-char (point-min))
469 (if (search-forward "no side branches present for" nil t
)
470 (progn (setq previous
(vc-branch-part previous
))
471 (vc-rcs-set-default-branch file previous
)
472 ;; vc-do-command popped up a window with
473 ;; the error message. Get rid of it, by
474 ;; restoring the old window configuration.
475 (set-window-configuration config
))
476 ;; No, it was some other error: re-signal it.
477 (signal (car err
) (cdr err
)))))))))
479 (defun vc-rcs-revert (file &optional contents-done
)
480 "Revert FILE to the version it was based on."
481 (vc-do-command nil
0 "co" (vc-name file
) "-f"
482 (concat (if (eq (vc-state file
) 'edited
) "-u" "-r")
483 (vc-workfile-version file
))))
485 (defun vc-rcs-merge (file first-version
&optional second-version
)
486 "Merge changes into current working copy of FILE.
487 The changes are between FIRST-VERSION and SECOND-VERSION."
488 (vc-do-command nil
1 "rcsmerge" (vc-name file
)
489 "-kk" ; ignore keyword conflicts
490 (concat "-r" first-version
)
491 (if second-version
(concat "-r" second-version
))))
493 (defun vc-rcs-steal-lock (file &optional rev
)
494 "Steal the lock on the current workfile for FILE and revision REV.
495 Needs RCS 5.6.2 or later for -M."
496 (vc-do-command nil
0 "rcs" (vc-name file
) "-M" (concat "-u" rev
))
497 ;; Do a real checkout after stealing the lock, so that we see
499 (vc-do-command nil
0 "co" (vc-name file
) "-f" (concat "-l" rev
)))
504 ;;; History functions
507 (defun vc-rcs-print-log (files &optional buffer
)
508 "Get change log associated with FILE."
509 (vc-do-command buffer
0 "rlog" (mapcar 'vc-name files
)))
511 (defun vc-rcs-diff (files &optional oldvers newvers buffer
)
512 "Get a difference report using RCS between two sets of files."
513 (apply 'vc-do-command
(or buffer
"*vc-diff*")
514 1 ;; Always go synchronous, the repo is local
515 "rcsdiff" (vc-expand-dirs files
)
517 (and oldvers
(concat "-r" oldvers
))
518 (and newvers
(concat "-r" newvers
)))
519 (vc-switches 'RCS
'diff
))))
521 (defun vc-rcs-wash-log ()
522 "Remove all non-comment information from log output."
523 (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
524 "\\(branches: .*;\n\\)?"
525 "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
526 (goto-char (point-max)) (forward-line -
1)
527 (while (looking-at "=*\n")
528 (delete-char (- (match-end 0) (match-beginning 0)))
530 (goto-char (point-min))
531 (if (looking-at "[\b\t\n\v\f\r ]+")
532 (delete-char (- (match-end 0) (match-beginning 0))))
533 (goto-char (point-min))
534 (re-search-forward separator nil t
)
535 (delete-region (point-min) (point))
536 (while (re-search-forward separator nil t
)
537 (delete-region (match-beginning 0) (match-end 0)))))
539 (defun vc-rcs-annotate-command (file buffer
&optional revision
)
540 "Annotate FILE, inserting the results in BUFFER.
541 Optional arg REVISION is a revision to annotate from."
542 (vc-setup-buffer buffer
)
543 ;; Aside from the "head revision on the trunk", the instructions for
544 ;; each revision on the trunk are an ordered list of kill and insert
545 ;; commands necessary to go from the chronologically-following
546 ;; revision to this one. That is, associated with revision N are
547 ;; edits that applied to revision N+1 would result in revision N.
549 ;; On a branch, however, (some) things are inverted: the commands
550 ;; listed are those necessary to go from the chronologically-preceding
551 ;; revision to this one. That is, associated with revision N are
552 ;; edits that applied to revision N-1 would result in revision N.
554 ;; So, to get per-line history info, we apply reverse-chronological
555 ;; edits, starting with the head revision on the trunk, all the way
556 ;; back through the initial revision (typically "1.1" or similar),
557 ;; then apply forward-chronological edits -- keeping track of which
558 ;; revision is associated with each inserted line -- until we reach
559 ;; the desired revision for display (which may be either on the trunk
561 (let* ((tree (with-temp-buffer
562 (insert-file-contents (vc-rcs-registered file
))
564 (revisions (cdr (assq 'revisions tree
)))
565 ;; The revision N whose instructions we currently are processing.
566 (cur (cdr (assq 'head
(cdr (assq 'headers tree
)))))
567 ;; Alist from the parse tree for N.
568 (meta (cdr (assoc cur revisions
)))
569 ;; Point and temporary string, respectively.
571 ;; "Next-branch list". Nil means the desired revision to
572 ;; display lives on the trunk. Non-nil means it lives on a
573 ;; branch, in which case the value is a list of revision pairs
574 ;; (PARENT . CHILD), the first PARENT being on the trunk, that
575 ;; links each series of revisions in the path from the initial
576 ;; revision to the desired revision to display.
578 ;; "Path-accumulate-predicate plus revision/date/author".
579 ;; Until set, forward-chronological edits are not accumulated.
580 ;; Once set, its value (updated every revision) is used for
581 ;; the text property `:vc-rcs-r/d/a' for inserts during
582 ;; processing of forward-chronological instructions for N.
583 ;; See internal func `r/d/a'.
585 ;; List of forward-chronological instructions, each of the
586 ;; form: (POS . ACTION), where POS is a buffer position. If
587 ;; ACTION is a string, it is inserted, otherwise it is taken as
588 ;; the number of characters to be deleted.
590 ;; N+1. When `cur' is "", this is the initial revision.
594 (unless (assoc revision revisions
)
595 (error "No such revision: %s" revision
))
596 ;; Find which branches (if any) must be included in the edits.
599 (while (setq bpt
(vc-branch-part par
)
600 par
(vc-branch-part bpt
))
601 (setq kids
(cdr (assq 'branches
(cdr (assoc par revisions
)))))
602 ;; A branchpoint may have multiple children. Find the right one.
603 (while (not (string= bpt
(vc-branch-part (car kids
))))
604 (setq kids
(cdr kids
)))
605 (push (cons par
(car kids
)) nbls
)))
606 ;; Start with the full text.
608 (insert (cdr (assq 'text meta
)))
609 ;; Apply reverse-chronological edits on the trunk, computing and
610 ;; accumulating forward-chronological edits after some point, for
612 (flet ((r/d
/a
() (vector pre
613 (cdr (assq 'date meta
))
614 (cdr (assq 'author meta
)))))
615 (while (when (setq pre cur cur
(cdr (assq 'next meta
)))
616 (not (string= "" cur
)))
618 ;; Start accumulating the forward-chronological edits when N+1
619 ;; on the trunk is either the desired revision to display, or
620 ;; the appropriate branchpoint for it. Do this before
621 ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
622 prda
(when (or prda
(string= (if nbls
(caar nbls
) revision
) pre
))
624 meta
(cdr (assoc cur revisions
)))
625 ;; Edits in the parse tree specify a line number (in the buffer
626 ;; *BEFORE* editing occurs) to start from, but line numbers
627 ;; change as a result of edits. To DTRT, we apply edits in
628 ;; order of descending buffer position so that edits further
629 ;; down in the buffer occur first w/o corrupting specified
630 ;; buffer positions of edits occurring towards the beginning of
631 ;; the buffer. In this way we avoid using markers. A pleasant
632 ;; property of this approach is ability to push instructions
633 ;; onto `path' directly, w/o need to maintain rev boundaries.
634 (dolist (insn (cdr (assq :insn meta
)))
635 (goto-line (pop insn
))
638 (k (setq s
(buffer-substring-no-properties
639 p
(progn (forward-line (car insn
))
642 (push `(,p .
,(propertize s
:vc-rcs-r
/d
/a prda
)) path
))
643 (delete-region p
(point)))
644 (i (setq s
(car insn
))
646 (push `(,p .
,(length s
)) path
))
648 ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
649 ;; equivalent to pushing an insert instruction (of the entire buffer
650 ;; contents) onto `path' then erasing the buffer, but less wasteful.
651 (put-text-property (point-min) (point-max) :vc-rcs-r
/d
/a
(r/d
/a
))
652 ;; Now apply the forward-chronological edits for the trunk.
654 (goto-char (pop insn
))
658 ;; Now apply the forward-chronological edits (directly from the
659 ;; parse-tree) for the branch(es), if necessary. We re-use vars
660 ;; `pre' and `meta' for the sake of internal func `r/d/a'.
662 (setq pre
(cdr (pop nbls
)))
664 (setq meta
(cdr (assoc pre revisions
))
666 (dolist (insn (cdr (assq :insn meta
)))
667 (goto-line (pop insn
))
670 (point) (progn (forward-line (car insn
))
672 (i (insert (propertize
675 (or prda
(setq prda
(r/d
/a
))))))))
676 (prog1 (not (string= (if nbls
(caar nbls
) revision
) pre
))
677 (setq pre
(cdr (assq 'next meta
)))))))))
678 ;; Lastly, for each line, insert at bol nicely-formatted history info.
679 ;; We do two passes to collect summary information used to minimize
680 ;; the annotation's usage of screen real-estate: (1) Consider rendered
681 ;; width of revision plus author together as a unit; and (2) Omit
682 ;; author entirely if all authors are the same as the user.
683 (let ((ht (make-hash-table :test
'eq
))
684 (me (user-login-name))
688 (goto-char (point-max))
691 (setq rda
(get-text-property (point) :vc-rcs-r
/d
/a
))
692 (unless (gethash rda ht
)
694 all-me
(and all-me
(string= a me
)))
695 (puthash rda
(setq w
(+ (length (aref rda
0))
698 (setq maxw
(max w maxw
))))
699 (let ((padding (make-string maxw
32)))
700 (flet ((pad (w) (substring-no-properties padding w
))
701 (render (rda &rest ls
)
704 (format-time-string "%Y-%m-%d" (aref rda
1))
712 (puthash rda
(render rda
(pad w
) ": ") ht
))
714 (puthash rda
(render rda
" " (pad w
) " " (aref rda
2) ": ") ht
)))
717 (insert (gethash (get-text-property (point) :vc-rcs-r
/d
/a
) ht
))
720 (defun vc-rcs-annotate-current-time ()
721 "Return the current time, based at midnight of the current day, and
722 encoded as fractional days."
723 (vc-annotate-convert-time
724 (apply 'encode-time
0 0 0 (nthcdr 3 (decode-time (current-time))))))
726 (defun vc-rcs-annotate-time ()
727 "Return the time of the next annotation (as fraction of days)
728 systime, or nil if there is none. Also, reposition point."
730 (prog1 (vc-annotate-convert-time
731 (aref (get-text-property (point) :vc-rcs-r
/d
/a
) 1))
732 (goto-char (next-single-property-change (point) :vc-annotate-prefix
)))))
734 (defun vc-rcs-annotate-extract-revision-at-line ()
735 (aref (get-text-property (point) :vc-rcs-r
/d
/a
) 0))
742 (defun vc-rcs-assign-name (file name
)
743 "Assign to FILE's latest version a given NAME."
744 (vc-do-command nil
0 "rcs" (vc-name file
) (concat "-n" name
":")))
751 (defun vc-rcs-check-headers ()
752 "Check if the current file has any headers in it."
754 (goto-char (point-min))
755 (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
756 \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t
)))
758 (defun vc-rcs-clear-headers ()
759 "Implementation of vc-clear-headers for RCS."
760 (let ((case-fold-search nil
))
761 (goto-char (point-min))
762 (while (re-search-forward
763 (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
764 "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
766 (replace-match "$\\1$"))))
768 (defun vc-rcs-rename-file (old new
)
769 ;; Just move the master file (using vc-rcs-master-templates).
770 (vc-rename-master (vc-name old
) new vc-rcs-master-templates
))
774 ;;; Internal functions
777 (defun vc-rcs-workfile-is-newer (file)
778 "Return non-nil if FILE is newer than its RCS master.
779 This likely means that FILE has been changed with respect
780 to its master version."
781 (let ((file-time (nth 5 (file-attributes file
)))
782 (master-time (nth 5 (file-attributes (vc-name file
)))))
783 (or (> (nth 0 file-time
) (nth 0 master-time
))
784 (and (= (nth 0 file-time
) (nth 0 master-time
))
785 (> (nth 1 file-time
) (nth 1 master-time
))))))
787 (defun vc-rcs-find-most-recent-rev (branch)
788 "Find most recent revision on BRANCH."
789 (goto-char (point-min))
790 (let ((latest-rev -
1) value
)
791 (while (re-search-forward (concat "^\\(" (regexp-quote branch
)
792 "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
794 (let ((rev (string-to-number (match-string 2))))
795 (when (< latest-rev rev
)
796 (setq latest-rev rev
)
797 (setq value
(match-string 1)))))
799 (vc-branch-part branch
))))
801 (defun vc-rcs-fetch-master-state (file &optional workfile-version
)
802 "Compute the master file's idea of the state of FILE.
803 If a WORKFILE-VERSION is given, compute the state of that version,
804 otherwise determine the workfile version based on the master file.
805 This function sets the properties `vc-workfile-version' and
806 `vc-checkout-model' to their correct values, based on the master
809 (if (or (not (vc-insert-file (vc-name file
) "^[0-9]"))
810 (progn (goto-char (point-min))
811 (not (looking-at "^head[ \t\n]+[^;]+;$"))))
812 (error "File %s is not an RCS master file" (vc-name file
)))
813 (let ((workfile-is-latest nil
)
814 (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
815 (vc-file-setprop file
'vc-rcs-default-branch default-branch
)
816 (unless workfile-version
817 ;; Workfile version not known yet. Determine that first. It
818 ;; is either the head of the trunk, the head of the default
819 ;; branch, or the "default branch" itself, if that is a full
823 ((or (not default-branch
) (string= "" default-branch
))
824 (setq workfile-version
825 (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
826 (setq workfile-is-latest t
))
827 ;; default branch is actually a revision
828 ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
830 (setq workfile-version default-branch
))
831 ;; else, search for the head of the default branch
832 (t (vc-insert-file (vc-name file
) "^desc")
833 (setq workfile-version
834 (vc-rcs-find-most-recent-rev default-branch
))
835 (setq workfile-is-latest t
)))
836 (vc-file-setprop file
'vc-workfile-version workfile-version
))
837 ;; Check strict locking
838 (goto-char (point-min))
839 (vc-file-setprop file
'vc-checkout-model
840 (if (re-search-forward ";[ \t\n]*strict;" nil t
)
842 ;; Compute state of workfile version
843 (goto-char (point-min))
845 (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
846 (regexp-quote workfile-version
)
852 (if (or workfile-is-latest
853 (vc-rcs-latest-on-branch-p file workfile-version
))
854 ;; workfile version is latest on branch
856 ;; workfile version is not latest on branch
858 ;; locked by the calling user
859 ((and (stringp locking-user
)
860 (string= locking-user
(vc-user-login-name file
)))
861 (if (or (eq (vc-checkout-model file
) 'locking
)
863 (vc-rcs-latest-on-branch-p file workfile-version
))
865 ;; Locking is not used for the file, but the owner does
866 ;; have a lock, and there is a higher version on the current
867 ;; branch. Not sure if this can occur, and if it is right
868 ;; to use `needs-merge' in this case.
870 ;; locked by somebody else
871 ((stringp locking-user
)
874 (error "Error getting state of RCS file")))))))
876 (defun vc-rcs-consult-headers (file)
877 "Search for RCS headers in FILE, and set properties accordingly.
879 Returns: nil if no headers were found
880 'rev if a workfile revision was found
881 'rev-and-lock if revision and lock info was found"
883 ((not (get-file-buffer file
)) nil
)
884 ((let (status version locking-user
)
886 (set-buffer (get-file-buffer file
))
887 (goto-char (point-min))
889 ;; search for $Id or $Header
890 ;; -------------------------
891 ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
892 ((or (and (search-forward "$Id\ : " nil t
)
893 (looking-at "[^ ]+ \\([0-9.]+\\) "))
894 (and (progn (goto-char (point-min))
895 (search-forward "$Header\ : " nil t
))
896 (looking-at "[^ ]+ \\([0-9.]+\\) ")))
897 (goto-char (match-end 0))
898 ;; if found, store the revision number ...
899 (setq version
(match-string-no-properties 1))
900 ;; ... and check for the locking state
903 (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
904 "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
905 "[^ ]+ [^ ]+ ")) ; author & state
906 (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
910 (setq locking-user
'none
)
911 (setq status
'rev-and-lock
))
912 ;; revision is locked by some user
913 ((looking-at "\\([^ ]+\\) \\$")
914 (setq locking-user
(match-string-no-properties 1))
915 (setq status
'rev-and-lock
))
916 ;; everything else: false
918 ;; unexpected information in
919 ;; keyword string --> quit
921 ;; search for $Revision
922 ;; --------------------
923 ((re-search-forward (concat "\\$"
924 "Revision: \\([0-9.]+\\) \\$")
926 ;; if found, store the revision number ...
927 (setq version
(match-string-no-properties 1))
928 ;; and see if there's any lock information
929 (goto-char (point-min))
930 (if (re-search-forward (concat "\\$" "Locker:") nil t
)
931 (cond ((looking-at " \\([^ ]+\\) \\$")
932 (setq locking-user
(match-string-no-properties 1))
933 (setq status
'rev-and-lock
))
934 ((looking-at " *\\$")
935 (setq locking-user
'none
)
936 (setq status
'rev-and-lock
))
938 (setq locking-user
'none
)
939 (setq status
'rev-and-lock
)))
941 ;; else: nothing found
942 ;; -------------------
944 (if status
(vc-file-setprop file
'vc-workfile-version version
))
945 (and (eq status
'rev-and-lock
)
946 (vc-file-setprop file
'vc-state
948 ((eq locking-user
'none
) 'up-to-date
)
949 ((string= locking-user
(vc-user-login-name file
))
952 ;; If the file has headers, we don't want to query the
953 ;; master file, because that would eliminate all the
954 ;; performance gain the headers brought us. We therefore
955 ;; use a heuristic now to find out whether locking is used
956 ;; for this file. If we trust the file permissions, and the
957 ;; file is not locked, then if the file is read-only we
958 ;; assume that locking is used for the file, otherwise
959 ;; locking is not used.
960 (not (vc-mistrust-permissions file
))
961 (vc-up-to-date-p file
)
962 (if (string-match ".r-..-..-." (nth 8 (file-attributes file
)))
963 (vc-file-setprop file
'vc-checkout-model
'locking
)
964 (vc-file-setprop file
'vc-checkout-model
'implicit
)))
967 (defun vc-release-greater-or-equal (r1 r2
)
968 "Compare release numbers, represented as strings.
969 Release components are assumed cardinal numbers, not decimal fractions
970 \(5.10 is a higher release than 5.9\). Omitted fields are considered
971 lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
972 of the string is found, or a non-numeric component shows up \(5.6.7 is
973 earlier than \"5.6.7 beta\", which is probably not what you want in
974 some cases\). This code is suitable for existing RCS release numbers.
975 CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
978 (or (and (string-match "^\\.?\\([0-9]+\\)" r1
)
979 (setq i1
(match-end 0))
980 (setq v1
(string-to-number (match-string 1 r1
)))
981 (or (and (string-match "^\\.?\\([0-9]+\\)" r2
)
982 (setq i2
(match-end 0))
983 (setq v2
(string-to-number (match-string 1 r2
)))
984 (if (> v1 v2
) (throw 'done t
)
985 (if (< v1 v2
) (throw 'done nil
)
987 (vc-release-greater-or-equal
989 (substring r2 i2
)))))))
991 (or (and (string-match "^\\.?\\([0-9]+\\)" r2
)
995 (defun vc-rcs-release-p (release)
996 "Return t if we have RELEASE or better."
997 (let ((installation (vc-rcs-system-release)))
998 (if (and installation
999 (not (eq installation
'unknown
)))
1000 (vc-release-greater-or-equal installation release
))))
1002 (defun vc-rcs-system-release ()
1003 "Return the RCS release installed on this system, as a string.
1004 Return symbol UNKNOWN if the release cannot be deducted. The user can
1005 override this using variable `vc-rcs-release'.
1007 If the user has not set variable `vc-rcs-release' and it is nil,
1008 variable `vc-rcs-release' is set to the returned value."
1010 (setq vc-rcs-release
1011 (or (and (zerop (vc-do-command nil nil
"rcs" nil
"-V"))
1012 (with-current-buffer (get-buffer "*vc*")
1013 (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
1016 (defun vc-rcs-set-non-strict-locking (file)
1017 (vc-do-command nil
0 "rcs" file
"-U")
1018 (vc-file-setprop file
'vc-checkout-model
'implicit
)
1019 (set-file-modes file
(logior (file-modes file
) 128)))
1021 (defun vc-rcs-set-default-branch (file branch
)
1022 (vc-do-command nil
0 "rcs" (vc-name file
) (concat "-b" branch
))
1023 (vc-file-setprop file
'vc-rcs-default-branch branch
))
1025 (defun vc-rcs-parse (&optional buffer
)
1026 "Parse current buffer, presumed to be in RCS-style masterfile format.
1027 Optional arg BUFFER specifies another buffer to parse. Return an alist
1028 of two elements, w/ keys `headers' and `revisions' and values in turn
1029 sub-alists. For `headers', the values unless otherwise specified are
1030 strings and the keys are:
1033 head -- latest revision
1034 branch -- the branch the \"head revision\" lies on;
1035 absent if the head revision lies on the trunk
1037 symbols -- sub-alist of (SYMBOL . REVISION) elements
1038 locks -- if file is checked out, something like \"ttn:1.7\"
1039 strict -- t if \"strict locking\" is in effect, otherwise nil
1040 comment -- may be absent; typically something like \"# \" or \"; \"
1041 expand -- may be absent; ???
1043 For `revisions', the car is REVISION (string), the cdr a sub-alist,
1044 with string values (unless otherwise specified) and keys:
1046 date -- a time value (like that returned by `encode-time'); as a
1047 special case, a year value less than 100 is augmented by 1900
1049 state -- typically \"Exp\" or \"Rel\"
1050 branches -- list of revisions that begin branches from this revision
1051 next -- on the trunk: the chronologically-preceding revision, or \"\";
1052 on a branch: the chronologically-following revision, or \"\"
1053 log -- change log entry
1054 text -- for the head revision on the trunk, the body of the file;
1055 other revisions have `:insn' instead
1056 :insn -- for non-head revisions, a list of parsed instructions
1057 in one of two forms, in both cases START meaning \"first
1059 - `(START k COUNT)' -- kill COUNT lines
1060 - `(START i TEXT)' -- insert TEXT (a string)
1061 The list is in descending order by START.
1063 The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
1064 (setq buffer
(get-buffer (or buffer
(current-buffer))))
1066 ;; An RCS masterfile can be viewed as containing four regular (for the
1067 ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
1068 ;; the "description" and (d) the "rev bodies", in that order. In the
1069 ;; returned alist (see docstring), elements from (b) and (d) are
1070 ;; combined pairwise to form the "revisions", while those from (a) and
1071 ;; (c) are simply combined to form the "headers".
1073 ;; Loosely speaking, each section contains a series of alternating
1074 ;; "tags" and "printed representations". In the (b) and (d), many
1075 ;; such series can appear, and a revision number on a line by itself
1076 ;; precedes the series of tags and printed representations associated
1079 ;; In (a) and (b), the printed representations (with the exception of
1080 ;; the `comment' tag in the headers) terminate with a semicolon, which
1081 ;; is NOT part of the "value" finally associated with the tag. All
1082 ;; other printed representations are in "@@-format"; there is an "@",
1083 ;; the middle part (to be translated into the value), another "@" and
1084 ;; a newline. Each "@@" in the middle part indicates the position of
1085 ;; a single "@" (and consequently the requirement of an additional
1086 ;; initial step when translating to the value).
1088 ;; Parser state includes vars that collect parts of the return value...
1089 (let ((desc nil
) (headers nil
) (revs nil
)
1090 ;; ... as well as vars that support a single-pass, tag-assisted,
1091 ;; minimal-data-copying scan. Basically -- skirting around the
1092 ;; grouping by revision required in (b) and (d) -- we repeatedly
1093 ;; and context-sensitively read a tag (that MUST be present),
1094 ;; determine the bounds of the printed representation, translate
1095 ;; it into a value, and push the tag plus value onto one of the
1096 ;; collection vars. Finally, we return the parse tree
1097 ;; incorporating the values of the collection vars (see "rv").
1099 ;; A symbol or string to keep track of context (for error messages).
1101 ;; A symbol, the current tag.
1103 ;; Region (begin and end buffer positions) of the printed
1104 ;; representation for the current tag.
1106 ;; A list of buffer positions where "@@" can be found within the
1107 ;; printed representation region. For each location, we push two
1108 ;; elements onto the list, 1+ and 2+ the location, respectively,
1109 ;; with the 2+ appearing at the head. In this way, the expression
1110 ;; `(,e ,@@-holes ,b)
1111 ;; describes regions that can be concatenated (in reverse order)
1112 ;; to "de-@@-format" the printed representation as the first step
1113 ;; to translating it into some value. See internal func `gather'.
1115 (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
1116 (at (tag) (save-excursion (eq tag
(read buffer
))))
1117 (to-eol () (buffer-substring-no-properties
1118 (point) (progn (forward-line 1)
1120 (to-semi () (setq b
(point)
1121 e
(progn (search-forward ";")
1123 (to-one@ () (setq @-holes nil
1124 b
(progn (search-forward "@") (point))
1125 e
(progn (while (and (search-forward "@")
1128 (push (point) @-holes
)
1130 (push (point) @-holes
))))
1132 (tok+val
(set-b+e name
&optional proc
)
1133 (unless (eq name
(setq tok
(read buffer
)))
1134 (error "Missing `%s' while parsing %s" name context
))
1139 (buffer-substring-no-properties b e
))))
1140 (k-semi (name &optional proc
) (tok+val
'to-semi name proc
))
1141 (gather () (let ((pairs `(,e
,@@-holes
,b
))
1144 (push (buffer-substring-no-properties
1145 (cadr pairs
) (car pairs
))
1147 (setq pairs
(cddr pairs
)))
1148 (apply 'concat acc
)))
1149 (k-one@ (name &optional later
) (tok+val
'to-one
@ name
1154 (goto-char (point-min))
1156 (setq context
'headers
)
1157 (flet ((hpush (name &optional proc
)
1158 (push (k-semi name proc
) headers
)))
1165 (mapcar (lambda (together)
1166 (let ((two (split-string together
":")))
1167 (setcar two
(intern (car two
)))
1168 (setcdr two
(cadr two
))
1171 (buffer-substring-no-properties b e
)))))
1173 (push `(strict .
,(when (at 'strict
)
1174 (search-forward ";")
1178 (push (k-one@ 'comment
) headers
)
1179 (search-forward ";"))
1181 (push (k-one@ 'expand
) headers
)
1182 (search-forward ";"))
1183 (setq headers
(nreverse headers
))
1185 (sw) (setq context
'rev-headers
)
1186 (while (looking-at "[0-9]")
1190 (let ((ls (mapcar 'string-to-number
1192 (buffer-substring-no-properties
1195 ;; Hack the year -- verified to be the
1196 ;; same algorithm used in RCS 5.7.
1197 (when (< (car ls
) 100)
1198 (setcar ls
(+ 1900 (car ls
))))
1199 (apply 'encode-time
(nreverse ls
)))))
1200 ,@(mapcar 'k-semi
'(author state
))
1204 (buffer-substring-no-properties b e
))))
1208 (setq revs
(nreverse revs
))
1210 (sw) (setq context
'desc
1211 desc
(k-one@ 'desc
))
1214 ;; Element of `revs' that initially holds only header info.
1215 ;; "Pairwise combination" occurs when we add body info.
1217 ;; Components of the editing commands (aside from the actual
1218 ;; text) that comprise the `text' printed representations
1219 ;; (not including the "head" revision).
1221 ;; Ascending (reversed) `@-holes' which the internal func
1222 ;; `incg' pops to effect incremental gathering.
1224 ;; Function to extract text (for the `a' command), either
1225 ;; `incg' or `buffer-substring-no-properties'. (This is
1226 ;; for speed; strictly speaking, it is sufficient to use
1227 ;; only the former since it behaves identically to the
1228 ;; latter in the absense of "@@".)
1230 (flet ((incg (beg end
) (let ((b beg
) (e end
) @-holes
)
1231 (while (and asc
(< (car asc
) e
))
1232 (push (pop asc
) @-holes
))
1233 ;; Self-deprecate when work is done.
1234 ;; Folding many dimensions into one.
1235 ;; Thanks B.Mandelbrot, for complex sum.
1236 ;; O beauteous math! --the Unvexed Bum
1238 (setq sub
'buffer-substring-no-properties
))
1242 (setq context
(to-eol)
1243 rev
(or (assoc context revs
)
1244 (error "Rev `%s' has body but no head"
1246 (push (k-one@ 'log
) (cdr rev
))
1247 ;; For rev body `text' tags, delay translation slightly...
1248 (push (k-one@ 'text t
) (cdr rev
))
1249 ;; ... until we decide which tag and value is appropriate to
1250 ;; collect. For the "head" revision, compute the value of the
1251 ;; `text' printed representation by simple `gather'. For all
1252 ;; other revisions, replace the `text' tag+value with `:insn'
1253 ;; plus value, always scanning in-place.
1254 (if (string= context
(cdr (assq 'head headers
)))
1255 (setcdr (cadr rev
) (gather))
1257 (setq asc
(nreverse @-holes
)
1259 (setq sub
'buffer-substring-no-properties
))
1262 (while (< (point) e
)
1264 (setq cmd
(char-before)
1265 start
(read (current-buffer))
1266 act
(read (current-buffer)))
1270 ;; `d' means "delete lines".
1271 ;; For Emacs spirit, we use `k' for "kill".
1274 ;; `a' means "append after this line" but
1275 ;; internally we normalize it so that START
1276 ;; specifies the actual line for insert, thus
1277 ;; requiring less hair in the realization algs.
1278 ;; For Emacs spirit, we use `i' for "insert".
1280 ,(funcall sub
(point) (progn (forward-line act
)
1282 (t (error "Bad command `%c' in `text' for rev `%s'"
1286 (setcar (cdr rev
) (cons :insn acc
)))))))
1288 `((headers ,desc
,@headers
)
1289 (revisions ,@revs
)))))
1293 ;; arch-tag: 759b4916-5b0d-431d-b647-b185b8c652cf
1294 ;;; vc-rcs.el ends here