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