The vc-mistrust-permissions configuration variable is gone.
[emacs.git] / lisp / vc / vc-sccs.el
blobcd4c054b07d9c85b390a5b4bf3e77fc047d51f36
1 ;;; vc-sccs.el --- support for SCCS version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-2014 Free Software Foundation, Inc.
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile
29 (require 'vc))
31 ;;;
32 ;;; Customization options
33 ;;;
35 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
36 ;; ;; Note for GNU CSSC, you can parse sccs -V to get the libexec path.
37 ;; (defcustom vc-sccs-path
38 ;; (prune-directory-list '("/usr/ccs/bin" "/usr/sccs" "/usr/lib/sccs"
39 ;; "/usr/libexec/sccs"))
40 ;; "List of extra directories to search for SCCS commands."
41 ;; :type '(repeat directory)
42 ;; :group 'vc)
44 (defgroup vc-sccs nil
45 "VC SCCS backend."
46 :version "24.1"
47 :group 'vc)
49 (defcustom vc-sccs-register-switches nil
50 "Switches for registering a file in SCCS.
51 A string or list of strings passed to the checkin program by
52 \\[vc-register]. If nil, use the value of `vc-register-switches'.
53 If t, use no switches."
54 :type '(choice (const :tag "Unspecified" nil)
55 (const :tag "None" t)
56 (string :tag "Argument String")
57 (repeat :tag "Argument List" :value ("") string))
58 :version "21.1"
59 :group 'vc-sccs)
61 (defcustom vc-sccs-diff-switches nil
62 "String or list of strings specifying switches for SCCS diff under VC.
63 If nil, use the value of `vc-diff-switches'. If t, use no switches."
64 :type '(choice (const :tag "Unspecified" nil)
65 (const :tag "None" t)
66 (string :tag "Argument String")
67 (repeat :tag "Argument List" :value ("") string))
68 :version "21.1"
69 :group 'vc-sccs)
71 (defcustom vc-sccs-header '("%W%")
72 "Header keywords to be inserted by `vc-insert-headers'."
73 :type '(repeat string)
74 :version "24.1" ; no longer consult the obsolete vc-header-alist
75 :group 'vc-sccs)
77 ;; This needs to be autoloaded because vc-sccs-registered uses it (via
78 ;; vc-default-registered), and vc-hooks needs to be able to check
79 ;; for a registered backend without loading every backend.
80 ;;;###autoload
81 (defcustom vc-sccs-master-templates
82 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
83 "Where to look for SCCS master files.
84 For a description of possible values, see `vc-check-master-templates'."
85 :type '(choice (const :tag "Use standard SCCS file names"
86 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
87 (repeat :tag "User-specified"
88 (choice string
89 function)))
90 :version "21.1"
91 :group 'vc-sccs)
94 ;;;
95 ;;; Internal variables
96 ;;;
98 (defconst vc-sccs-name-assoc-file "VC-names")
101 ;;; Properties of the backend
103 (defun vc-sccs-revision-granularity () 'file)
104 (defun vc-sccs-checkout-model (_files) 'locking)
107 ;;; State-querying functions
110 ;; The autoload cookie below places vc-sccs-registered directly into
111 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
112 ;; every file that is visited.
113 ;;;###autoload
114 (progn
115 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f)))
117 (defun vc-sccs-state (file)
118 "SCCS-specific function to compute the version control state."
119 (if (not (vc-sccs-registered file))
120 'unregistered
121 (with-temp-buffer
122 (if (vc-insert-file (vc-sccs-lock-file file))
123 (let* ((locks (vc-sccs-parse-locks))
124 (working-revision (vc-working-revision file))
125 (locking-user (cdr (assoc working-revision locks))))
126 (if (not locking-user)
127 (if (vc-sccs-workfile-unchanged-p file)
128 'up-to-date
129 'unlocked-changes)
130 (if (string= locking-user (vc-user-login-name file))
131 'edited
132 locking-user)))
133 'up-to-date))))
135 (autoload 'vc-expand-dirs "vc")
137 (defun vc-sccs-dir-status (dir update-function)
138 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
139 ;; is not TRTD because it returns files from multiple backends.
140 ;; It should also return 'unregistered files.
142 ;; Doing lots of individual VC-state calls is painful, but
143 ;; there is no better option in SCCS-land.
144 (let ((flist (vc-expand-dirs (list dir)))
145 (result nil))
146 (dolist (file flist)
147 (let ((state (vc-state file))
148 (frel (file-relative-name file)))
149 (when (and (eq (vc-backend file) 'SCCS)
150 (not (eq state 'up-to-date)))
151 (push (list frel state) result))))
152 (funcall update-function result)))
154 (autoload 'vc-master-name "vc-filewise")
156 (defun vc-sccs-working-revision (file)
157 "SCCS-specific version of `vc-working-revision'."
158 (with-temp-buffer
159 ;; The working revision is always the latest revision number.
160 ;; To find this number, search the entire delta table,
161 ;; rather than just the first entry, because the
162 ;; first entry might be a deleted ("R") revision.
163 (vc-insert-file (vc-master-name file) "^\001e\n\001[^s]")
164 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
166 ;; Cf vc-sccs-find-revision.
167 (defun vc-sccs-write-revision (file outfile &optional rev)
168 "Write the SCCS version of input file FILE to output file OUTFILE.
169 Optional string REV is a revision."
170 (with-temp-buffer
171 (apply 'vc-sccs-do-command t 0 "get" (vc-master-name file)
172 (append '("-s" "-p" "-k") ; -k: no keyword expansion
173 (if rev (list (concat "-r" rev)))))
174 (write-region nil nil outfile nil 'silent)))
176 (defun vc-sccs-workfile-unchanged-p (file)
177 "Has FILE remained unchanged since last checkout?"
178 (let ((tempfile (make-temp-file "vc-sccs")))
179 (unwind-protect
180 (progn
181 (vc-sccs-write-revision file tempfile (vc-working-revision file))
182 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
183 (delete-file tempfile))))
187 ;;; State-changing functions
190 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
191 ;; (let ((load-path (append vc-sccs-path load-path)))
192 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
193 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
195 (defun vc-sccs-create-repo ()
196 "Create a new SCCS repository."
197 ;; SCCS is totally file-oriented, so all we have to do is make the directory
198 (make-directory "SCCS"))
200 (autoload 'vc-switches "vc")
202 (defun vc-sccs-register (files &optional comment)
203 "Register FILES into the SCCS version-control system.
204 COMMENT can be used to provide an initial description of FILES.
205 Passes either `vc-sccs-register-switches' or `vc-register-switches'
206 to the SCCS command.
208 Automatically retrieve a read-only version of the files with keywords
209 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
210 (dolist (file files)
211 (let* ((dirname (or (file-name-directory file) ""))
212 (basename (file-name-nondirectory file))
213 (project-file (vc-sccs-search-project-dir dirname basename)))
214 (let ((vc-master-name
215 (or project-file
216 (format (car vc-sccs-master-templates) dirname basename))))
217 (apply 'vc-sccs-do-command nil 0 "admin" vc-master-name
218 "-fb"
219 (concat "-i" (file-relative-name file))
220 (and comment (concat "-y" comment))
221 (vc-switches 'SCCS 'register)))
222 (delete-file file)
223 (if vc-keep-workfiles
224 (vc-sccs-do-command nil 0 "get" (vc-master-name file))))))
226 (defun vc-sccs-responsible-p (file)
227 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
228 ;; TODO: check for all the patterns in vc-sccs-master-templates
229 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
230 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
231 (file-name-nondirectory file)))))
233 (defun vc-sccs-checkin (files comment)
234 "SCCS-specific version of `vc-backend-checkin'."
235 (dolist (file (vc-expand-dirs files))
236 (apply 'vc-sccs-do-command nil 0 "delta" (vc-master-name file)
237 (concat "-y" comment)
238 (vc-switches 'SCCS 'checkin))
239 (if vc-keep-workfiles
240 (vc-sccs-do-command nil 0 "get" (vc-master-name file)))))
242 (defun vc-sccs-find-revision (file rev buffer)
243 (apply 'vc-sccs-do-command
244 buffer 0 "get" (vc-master-name file)
245 "-s" ;; suppress diagnostic output
246 "-p"
247 (and rev
248 (concat "-r"
249 (vc-sccs-lookup-triple file rev)))
250 (vc-switches 'SCCS 'checkout)))
252 (defun vc-sccs-checkout (file &optional rev)
253 "Retrieve a copy of a saved revision of SCCS controlled FILE.
254 If FILE is a directory, all version-controlled files beneath are checked out.
255 EDITABLE non-nil means that the file should be writable and
256 locked. REV is the revision to check out."
257 (if (file-directory-p file)
258 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
259 (let ((file-buffer (get-file-buffer file))
260 switches)
261 (message "Checking out %s..." file)
262 (save-excursion
263 ;; Change buffers to get local value of vc-checkout-switches.
264 (if file-buffer (set-buffer file-buffer))
265 (setq switches (vc-switches 'SCCS 'checkout))
266 ;; Save this buffer's default-directory
267 ;; and use save-excursion to make sure it is restored
268 ;; in the same buffer it was saved in.
269 (let ((default-directory default-directory))
270 (save-excursion
271 ;; Adjust the default-directory so that the check-out creates
272 ;; the file in the right place.
273 (setq default-directory (file-name-directory file))
275 (and rev (or (string= rev "")
276 (not (stringp rev)))
277 (setq rev nil))
278 (apply 'vc-sccs-do-command nil 0 "get" (vc-master-name file)
279 "-e"
280 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
281 switches))))
282 (message "Checking out %s...done" file))))
284 (defun vc-sccs-rollback (files)
285 "Roll back, undoing the most recent checkins of FILES. Directories
286 are expanded to all version-controlled subfiles."
287 (setq files (vc-expand-dirs files))
288 (if (not files)
289 (error "SCCS backend doesn't support directory-level rollback"))
290 (dolist (file files)
291 (let ((discard (vc-working-revision file)))
292 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
293 discard file)))
294 (error "Aborted"))
295 (message "Removing revision %s from %s..." discard file)
296 (vc-sccs-do-command nil 0 "rmdel"
297 (vc-master-name file) (concat "-r" discard))
298 (vc-sccs-do-command nil 0 "get" (vc-master-name file) nil))))
300 (defun vc-sccs-revert (file &optional _contents-done)
301 "Revert FILE to the version it was based on. If FILE is a directory,
302 revert all subfiles."
303 (if (file-directory-p file)
304 (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
305 (vc-sccs-do-command nil 0 "unget" (vc-master-name file))
306 (vc-sccs-do-command nil 0 "get" (vc-master-name file))
307 ;; Checking out explicit revisions is not supported under SCCS, yet.
308 ;; We always "revert" to the latest revision; therefore
309 ;; vc-working-revision is cleared here so that it gets recomputed.
310 (vc-file-setprop file 'vc-working-revision nil)))
312 (defun vc-sccs-steal-lock (file &optional rev)
313 "Steal the lock on the current workfile for FILE and revision REV."
314 (if (file-directory-p file)
315 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
316 (vc-sccs-do-command nil 0 "unget"
317 (vc-master-name file) "-n" (if rev (concat "-r" rev)))
318 (vc-sccs-do-command nil 0 "get"
319 (vc-master-name file) "-g" (if rev (concat "-r" rev)))))
321 (defun vc-sccs-modify-change-comment (files rev comment)
322 "Modify (actually, append to) the change comments for FILES on a specified REV."
323 (dolist (file (vc-expand-dirs files))
324 (vc-sccs-do-command nil 0 "cdc" (vc-master-name file)
325 (concat "-y" comment) (concat "-r" rev))))
329 ;;; History functions
332 (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
333 "Print commit log associated with FILES into specified BUFFER.
334 Remaining arguments are ignored."
335 (setq files (vc-expand-dirs files))
336 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-master-name files))
337 (when limit 'limit-unsupported))
339 (autoload 'vc-setup-buffer "vc-dispatcher")
340 (autoload 'vc-delistify "vc-dispatcher")
342 (defvar w32-quote-process-args)
344 ;; FIXME use sccsdiff if present?
345 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
346 "Get a difference report using SCCS between two filesets."
347 (setq files (vc-expand-dirs files))
348 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
349 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
350 (or buffer (setq buffer "*vc-diff*"))
351 ;; We have to reimplement pieces of vc-do-command, because
352 ;; we want to run multiple external commands, and only do the setup
353 ;; and exit pieces once.
354 (save-current-buffer
355 (unless (or (eq buffer t)
356 (and (stringp buffer) (string= (buffer-name) buffer))
357 (eq buffer (current-buffer)))
358 (vc-setup-buffer buffer))
359 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
360 (if oldvers (list (concat " -r" oldvers)))
361 (if newvers (list (concat " -r" newvers)))))
362 (fake-command
363 (format "diff%s %s"
364 (if fake-flags
365 (concat " " (mapconcat 'identity fake-flags " "))
367 (vc-delistify files)))
368 (status 0)
369 (oldproc (get-buffer-process (current-buffer))))
370 (when vc-command-messages
371 (message "Running %s in foreground..." fake-command))
372 (if oldproc (delete-process oldproc))
373 (dolist (file files)
374 (let ((oldfile (make-temp-file "vc-sccs"))
375 newfile)
376 (unwind-protect
377 (progn
378 (vc-sccs-write-revision file oldfile oldvers)
379 (if newvers
380 (vc-sccs-write-revision file (setq newfile
381 (make-temp-file "vc-sccs"))
382 newvers))
383 (let* ((inhibit-read-only t)
384 (buffer-undo-list t)
385 (process-environment
386 (cons "LC_MESSAGES=C" process-environment))
387 (w32-quote-process-args t)
388 (this-status
389 (apply 'process-file "diff" nil t nil
390 (append (vc-switches 'SCCS 'diff)
391 (list oldfile
392 (or newfile
393 (file-relative-name file)))))))
394 (or (integerp this-status) (setq status 'error))
395 (and (integerp status)
396 (> this-status status)
397 (setq status this-status))))
398 (delete-file oldfile)
399 (if newfile (delete-file newfile)))))
400 (when (or (not (integerp status)) (> status 1))
401 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
402 (pop-to-buffer (current-buffer))
403 (goto-char (point-min))
404 (shrink-window-if-larger-than-buffer))
405 (error "Running %s...FAILED (%s)" fake-command
406 (if (integerp status) (format "status %d" status) status)))
407 (when vc-command-messages
408 (message "Running %s...OK = %d" fake-command status))
409 ;; Should we pretend we ran sccsdiff instead?
410 ;; This might not actually be a valid diff command.
411 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
412 status)))
416 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
417 ;;; our own set of name-to-revision mappings.
420 (autoload 'vc-tag-precondition "vc")
421 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
423 (defun vc-sccs-create-tag (dir name branchp)
424 (when branchp
425 (error "SCCS backend does not support module branches"))
426 (let ((result (vc-tag-precondition dir)))
427 (if (stringp result)
428 (error "File %s is not up-to-date" result)
429 (vc-file-tree-walk
431 (lambda (f)
432 (vc-sccs-add-triple name f (vc-working-revision f)))))))
436 ;;; Miscellaneous
439 (defun vc-sccs-previous-revision (file rev)
440 (vc-call-backend 'RCS 'previous-revision file rev))
442 (defun vc-sccs-next-revision (file rev)
443 (vc-call-backend 'RCS 'next-revision file rev))
445 (defun vc-sccs-check-headers ()
446 "Check if the current file has any headers in it."
447 (save-excursion
448 (goto-char (point-min))
449 (re-search-forward "%[A-Z]%" nil t)))
451 (autoload 'vc-rename-master "vc-filewise")
453 (defun vc-sccs-rename-file (old new)
454 ;; Move the master file (using vc-rcs-master-templates).
455 (vc-rename-master (vc-master-name old) new vc-sccs-master-templates)
456 ;; Update the tag file.
457 (with-current-buffer
458 (find-file-noselect
459 (expand-file-name vc-sccs-name-assoc-file
460 (file-name-directory (vc-master-name old))))
461 (goto-char (point-min))
462 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
463 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
464 (replace-match (concat ":" new) nil nil))
465 (basic-save-buffer)
466 (kill-buffer (current-buffer))))
468 (defun vc-sccs-find-file-hook ()
469 ;; If the file is locked by some other user, make
470 ;; the buffer read-only. Like this, even root
471 ;; cannot modify a file that someone else has locked.
472 (and (stringp (vc-state buffer-file-name 'SCCS))
473 (setq buffer-read-only t)))
477 ;;; Internal functions
480 ;; This function is wrapped with `progn' so that the autoload cookie
481 ;; copies the whole function itself into loaddefs.el rather than just placing
482 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
483 ;; help us avoid loading vc-sccs.
484 ;;;###autoload
485 (progn (defun vc-sccs-search-project-dir (_dirname basename)
486 "Return the name of a master file in the SCCS project directory.
487 Does not check whether the file exists but returns nil if it does not
488 find any project directory."
489 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
490 (when project-dir
491 (if (file-name-absolute-p project-dir)
492 (setq dirs '("SCCS" ""))
493 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
494 (setq project-dir (expand-file-name (concat "~" project-dir))))
495 (while (and (not dir) dirs)
496 (setq dir (expand-file-name (car dirs) project-dir))
497 (unless (file-directory-p dir)
498 (setq dir nil)
499 (setq dirs (cdr dirs))))
500 (and dir (expand-file-name (concat "s." basename) dir))))))
502 (defun vc-sccs-lock-file (file)
503 "Generate lock file name corresponding to FILE."
504 (let ((master (vc-master-name file)))
505 (and
506 master
507 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
508 (replace-match "p." t t master 2))))
510 (defun vc-sccs-parse-locks ()
511 "Parse SCCS locks in current buffer.
512 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
513 (let (master-locks)
514 (goto-char (point-min))
515 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
516 nil t)
517 (setq master-locks
518 (cons (cons (match-string 1) (match-string 2)) master-locks)))
519 ;; FIXME: is it really necessary to reverse ?
520 (nreverse master-locks)))
522 (defun vc-sccs-add-triple (name file rev)
523 (with-current-buffer
524 (find-file-noselect
525 (expand-file-name vc-sccs-name-assoc-file
526 (file-name-directory (vc-master-name file))))
527 (goto-char (point-max))
528 (insert name "\t:\t" file "\t" rev "\n")
529 (basic-save-buffer)
530 (kill-buffer (current-buffer))))
532 (defun vc-sccs-lookup-triple (file name)
533 "Return the numeric revision corresponding to a named tag of FILE.
534 If NAME is nil or a revision number string it's just passed through."
535 (if (or (null name)
536 (let ((firstchar (aref name 0)))
537 (and (>= firstchar ?0) (<= firstchar ?9))))
538 name
539 (with-temp-buffer
540 (vc-insert-file
541 (expand-file-name vc-sccs-name-assoc-file
542 (file-name-directory (vc-master-name file))))
543 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
545 (provide 'vc-sccs)
547 ;;; vc-sccs.el ends here