Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / vc / vc-sccs.el
blobfb7d9596822de42cbc6ef10e6fd94afa08b4ce42
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-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 (defun vc-sccs-state-heuristic (file)
136 "SCCS-specific state heuristic."
137 (if (not (vc-mistrust-permissions file))
138 ;; This implementation assumes that any file which is under version
139 ;; control and has -rw-r--r-- is locked by its owner. This is true
140 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
141 ;; We have to be careful not to exclude files with execute bits on;
142 ;; scripts can be under version control too. Also, we must ignore the
143 ;; group-read and other-read bits, since paranoid users turn them off.
144 (let* ((attributes (file-attributes file 'string))
145 (owner-name (nth 2 attributes))
146 (permissions (nth 8 attributes)))
147 (if (string-match ".r-..-..-." permissions)
148 'up-to-date
149 (if (string-match ".rw..-..-." permissions)
150 (if (file-ownership-preserved-p file)
151 'edited
152 owner-name)
153 ;; Strange permissions.
154 ;; Fall through to real state computation.
155 (vc-sccs-state file))))
156 (vc-sccs-state file)))
158 (autoload 'vc-expand-dirs "vc")
160 (defun vc-sccs-dir-status (dir update-function)
161 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
162 ;; is not TRTD because it returns files from multiple backends.
163 ;; It should also return 'unregistered files.
165 ;; Doing lots of individual VC-state calls is painful, but
166 ;; there is no better option in SCCS-land.
167 (let ((flist (vc-expand-dirs (list dir)))
168 (result nil))
169 (dolist (file flist)
170 (let ((state (vc-state file))
171 (frel (file-relative-name file)))
172 (when (and (eq (vc-backend file) 'SCCS)
173 (not (eq state 'up-to-date)))
174 (push (list frel state) result))))
175 (funcall update-function result)))
177 (defun vc-sccs-working-revision (file)
178 "SCCS-specific version of `vc-working-revision'."
179 (with-temp-buffer
180 ;; The working revision is always the latest revision number.
181 ;; To find this number, search the entire delta table,
182 ;; rather than just the first entry, because the
183 ;; first entry might be a deleted ("R") revision.
184 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
185 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
187 ;; Cf vc-sccs-find-revision.
188 (defun vc-sccs-write-revision (file outfile &optional rev)
189 "Write the SCCS version of input file FILE to output file OUTFILE.
190 Optional string REV is a revision."
191 (with-temp-buffer
192 (apply 'vc-sccs-do-command t 0 "get" (vc-name file)
193 (append '("-s" "-p" "-k") ; -k: no keyword expansion
194 (if rev (list (concat "-r" rev)))))
195 (write-region nil nil outfile nil 'silent)))
197 (defun vc-sccs-workfile-unchanged-p (file)
198 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
199 (let ((tempfile (make-temp-file "vc-sccs")))
200 (unwind-protect
201 (progn
202 (vc-sccs-write-revision file tempfile (vc-working-revision file))
203 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
204 (delete-file tempfile))))
208 ;;; State-changing functions
211 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
212 ;; (let ((load-path (append vc-sccs-path load-path)))
213 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
214 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
216 (defun vc-sccs-create-repo ()
217 "Create a new SCCS repository."
218 ;; SCCS is totally file-oriented, so all we have to do is make the directory
219 (make-directory "SCCS"))
221 (autoload 'vc-switches "vc")
223 (defun vc-sccs-register (files &optional rev comment)
224 "Register FILES into the SCCS version-control system.
225 REV is the optional revision number for the file. COMMENT can be used
226 to provide an initial description of FILES.
227 Passes either `vc-sccs-register-switches' or `vc-register-switches'
228 to the SCCS command.
230 Automatically retrieve a read-only version of the files with keywords
231 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
232 (dolist (file files)
233 (let* ((dirname (or (file-name-directory file) ""))
234 (basename (file-name-nondirectory file))
235 (project-file (vc-sccs-search-project-dir dirname basename)))
236 (let ((vc-name
237 (or project-file
238 (format (car vc-sccs-master-templates) dirname basename))))
239 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
240 (and rev (not (string= rev "")) (concat "-r" rev))
241 "-fb"
242 (concat "-i" (file-relative-name file))
243 (and comment (concat "-y" comment))
244 (vc-switches 'SCCS 'register)))
245 (delete-file file)
246 (if vc-keep-workfiles
247 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
249 (defun vc-sccs-responsible-p (file)
250 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
251 ;; TODO: check for all the patterns in vc-sccs-master-templates
252 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
253 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
254 (file-name-nondirectory file)))))
256 (defun vc-sccs-checkin (files rev comment)
257 "SCCS-specific version of `vc-backend-checkin'."
258 (dolist (file (vc-expand-dirs files))
259 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
260 (if rev (concat "-r" rev))
261 (concat "-y" comment)
262 (vc-switches 'SCCS 'checkin))
263 (if vc-keep-workfiles
264 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
266 (defun vc-sccs-find-revision (file rev buffer)
267 (apply 'vc-sccs-do-command
268 buffer 0 "get" (vc-name file)
269 "-s" ;; suppress diagnostic output
270 "-p"
271 (and rev
272 (concat "-r"
273 (vc-sccs-lookup-triple file rev)))
274 (vc-switches 'SCCS 'checkout)))
276 (defun vc-sccs-checkout (file &optional editable rev)
277 "Retrieve a copy of a saved revision of SCCS controlled FILE.
278 If FILE is a directory, all version-controlled files beneath are checked out.
279 EDITABLE non-nil means that the file should be writable and
280 locked. REV is the revision to check out."
281 (if (file-directory-p file)
282 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
283 (let ((file-buffer (get-file-buffer file))
284 switches)
285 (message "Checking out %s..." file)
286 (save-excursion
287 ;; Change buffers to get local value of vc-checkout-switches.
288 (if file-buffer (set-buffer file-buffer))
289 (setq switches (vc-switches 'SCCS 'checkout))
290 ;; Save this buffer's default-directory
291 ;; and use save-excursion to make sure it is restored
292 ;; in the same buffer it was saved in.
293 (let ((default-directory default-directory))
294 (save-excursion
295 ;; Adjust the default-directory so that the check-out creates
296 ;; the file in the right place.
297 (setq default-directory (file-name-directory file))
299 (and rev (or (string= rev "")
300 (not (stringp rev)))
301 (setq rev nil))
302 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
303 (if editable "-e")
304 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
305 switches))))
306 (message "Checking out %s...done" file))))
308 (defun vc-sccs-rollback (files)
309 "Roll back, undoing the most recent checkins of FILES. Directories
310 are expanded to all version-controlled subfiles."
311 (setq files (vc-expand-dirs files))
312 (if (not files)
313 (error "SCCS backend doesn't support directory-level rollback"))
314 (dolist (file files)
315 (let ((discard (vc-working-revision file)))
316 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
317 discard file)))
318 (error "Aborted"))
319 (message "Removing revision %s from %s..." discard file)
320 (vc-sccs-do-command nil 0 "rmdel"
321 (vc-name file) (concat "-r" discard))
322 (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
324 (defun vc-sccs-revert (file &optional _contents-done)
325 "Revert FILE to the version it was based on. If FILE is a directory,
326 revert all subfiles."
327 (if (file-directory-p file)
328 (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
329 (vc-sccs-do-command nil 0 "unget" (vc-name file))
330 (vc-sccs-do-command nil 0 "get" (vc-name file))
331 ;; Checking out explicit revisions is not supported under SCCS, yet.
332 ;; We always "revert" to the latest revision; therefore
333 ;; vc-working-revision is cleared here so that it gets recomputed.
334 (vc-file-setprop file 'vc-working-revision nil)))
336 (defun vc-sccs-steal-lock (file &optional rev)
337 "Steal the lock on the current workfile for FILE and revision REV."
338 (if (file-directory-p file)
339 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
340 (vc-sccs-do-command nil 0 "unget"
341 (vc-name file) "-n" (if rev (concat "-r" rev)))
342 (vc-sccs-do-command nil 0 "get"
343 (vc-name file) "-g" (if rev (concat "-r" rev)))))
345 (defun vc-sccs-modify-change-comment (files rev comment)
346 "Modify (actually, append to) the change comments for FILES on a specified REV."
347 (dolist (file (vc-expand-dirs files))
348 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
349 (concat "-y" comment) (concat "-r" rev))))
353 ;;; History functions
356 (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
357 "Print commit log associated with FILES into specified BUFFER.
358 Remaining arguments are ignored."
359 (setq files (vc-expand-dirs files))
360 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files))
361 (when limit 'limit-unsupported))
363 (autoload 'vc-setup-buffer "vc-dispatcher")
364 (autoload 'vc-delistify "vc-dispatcher")
366 (defvar w32-quote-process-args)
368 ;; FIXME use sccsdiff if present?
369 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
370 "Get a difference report using SCCS between two filesets."
371 (setq files (vc-expand-dirs files))
372 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
373 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
374 (or buffer (setq buffer "*vc-diff*"))
375 ;; We have to reimplement pieces of vc-do-command, because
376 ;; we want to run multiple external commands, and only do the setup
377 ;; and exit pieces once.
378 (save-current-buffer
379 (unless (or (eq buffer t)
380 (and (stringp buffer) (string= (buffer-name) buffer))
381 (eq buffer (current-buffer)))
382 (vc-setup-buffer buffer))
383 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
384 (if oldvers (list (concat " -r" oldvers)))
385 (if newvers (list (concat " -r" newvers)))))
386 (fake-command
387 (format "diff%s %s"
388 (if fake-flags
389 (concat " " (mapconcat 'identity fake-flags " "))
391 (vc-delistify files)))
392 (status 0)
393 (oldproc (get-buffer-process (current-buffer))))
394 (when vc-command-messages
395 (message "Running %s in foreground..." fake-command))
396 (if oldproc (delete-process oldproc))
397 (dolist (file files)
398 (let ((oldfile (make-temp-file "vc-sccs"))
399 newfile)
400 (unwind-protect
401 (progn
402 (vc-sccs-write-revision file oldfile oldvers)
403 (if newvers
404 (vc-sccs-write-revision file (setq newfile
405 (make-temp-file "vc-sccs"))
406 newvers))
407 (let* ((inhibit-read-only t)
408 (buffer-undo-list t)
409 (process-environment
410 (cons "LC_MESSAGES=C" process-environment))
411 (w32-quote-process-args t)
412 (this-status
413 (apply 'process-file "diff" nil t nil
414 (append (vc-switches 'SCCS 'diff)
415 (list oldfile
416 (or newfile
417 (file-relative-name file)))))))
418 (or (integerp this-status) (setq status 'error))
419 (and (integerp status)
420 (> this-status status)
421 (setq status this-status))))
422 (delete-file oldfile)
423 (if newfile (delete-file newfile)))))
424 (when (or (not (integerp status)) (> status 1))
425 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
426 (pop-to-buffer (current-buffer))
427 (goto-char (point-min))
428 (shrink-window-if-larger-than-buffer))
429 (error "Running %s...FAILED (%s)" fake-command
430 (if (integerp status) (format "status %d" status) status)))
431 (when vc-command-messages
432 (message "Running %s...OK = %d" fake-command status))
433 ;; Should we pretend we ran sccsdiff instead?
434 ;; This might not actually be a valid diff command.
435 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
436 status)))
440 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
441 ;;; our own set of name-to-revision mappings.
444 (autoload 'vc-tag-precondition "vc")
445 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
447 (defun vc-sccs-create-tag (dir name branchp)
448 (when branchp
449 (error "SCCS backend does not support module branches"))
450 (let ((result (vc-tag-precondition dir)))
451 (if (stringp result)
452 (error "File %s is not up-to-date" result)
453 (vc-file-tree-walk
455 (lambda (f)
456 (vc-sccs-add-triple name f (vc-working-revision f)))))))
460 ;;; Miscellaneous
463 (defun vc-sccs-previous-revision (file rev)
464 (vc-call-backend 'RCS 'previous-revision file rev))
466 (defun vc-sccs-next-revision (file rev)
467 (vc-call-backend 'RCS 'next-revision file rev))
469 (defun vc-sccs-check-headers ()
470 "Check if the current file has any headers in it."
471 (save-excursion
472 (goto-char (point-min))
473 (re-search-forward "%[A-Z]%" nil t)))
475 (autoload 'vc-rename-master "vc")
477 (defun vc-sccs-rename-file (old new)
478 ;; Move the master file (using vc-rcs-master-templates).
479 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
480 ;; Update the tag file.
481 (with-current-buffer
482 (find-file-noselect
483 (expand-file-name vc-sccs-name-assoc-file
484 (file-name-directory (vc-name old))))
485 (goto-char (point-min))
486 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
487 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
488 (replace-match (concat ":" new) nil nil))
489 (basic-save-buffer)
490 (kill-buffer (current-buffer))))
492 (defun vc-sccs-find-file-hook ()
493 ;; If the file is locked by some other user, make
494 ;; the buffer read-only. Like this, even root
495 ;; cannot modify a file that someone else has locked.
496 (and (stringp (vc-state buffer-file-name 'SCCS))
497 (setq buffer-read-only t)))
501 ;;; Internal functions
504 ;; This function is wrapped with `progn' so that the autoload cookie
505 ;; copies the whole function itself into loaddefs.el rather than just placing
506 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
507 ;; help us avoid loading vc-sccs.
508 ;;;###autoload
509 (progn (defun vc-sccs-search-project-dir (_dirname basename)
510 "Return the name of a master file in the SCCS project directory.
511 Does not check whether the file exists but returns nil if it does not
512 find any project directory."
513 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
514 (when project-dir
515 (if (file-name-absolute-p project-dir)
516 (setq dirs '("SCCS" ""))
517 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
518 (setq project-dir (expand-file-name (concat "~" project-dir))))
519 (while (and (not dir) dirs)
520 (setq dir (expand-file-name (car dirs) project-dir))
521 (unless (file-directory-p dir)
522 (setq dir nil)
523 (setq dirs (cdr dirs))))
524 (and dir (expand-file-name (concat "s." basename) dir))))))
526 (defun vc-sccs-lock-file (file)
527 "Generate lock file name corresponding to FILE."
528 (let ((master (vc-name file)))
529 (and
530 master
531 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
532 (replace-match "p." t t master 2))))
534 (defun vc-sccs-parse-locks ()
535 "Parse SCCS locks in current buffer.
536 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
537 (let (master-locks)
538 (goto-char (point-min))
539 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
540 nil t)
541 (setq master-locks
542 (cons (cons (match-string 1) (match-string 2)) master-locks)))
543 ;; FIXME: is it really necessary to reverse ?
544 (nreverse master-locks)))
546 (defun vc-sccs-add-triple (name file rev)
547 (with-current-buffer
548 (find-file-noselect
549 (expand-file-name vc-sccs-name-assoc-file
550 (file-name-directory (vc-name file))))
551 (goto-char (point-max))
552 (insert name "\t:\t" file "\t" rev "\n")
553 (basic-save-buffer)
554 (kill-buffer (current-buffer))))
556 (defun vc-sccs-lookup-triple (file name)
557 "Return the numeric revision corresponding to a named tag of FILE.
558 If NAME is nil or a revision number string it's just passed through."
559 (if (or (null name)
560 (let ((firstchar (aref name 0)))
561 (and (>= firstchar ?0) (<= firstchar ?9))))
562 name
563 (with-temp-buffer
564 (vc-insert-file
565 (expand-file-name vc-sccs-name-assoc-file
566 (file-name-directory (vc-name file))))
567 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
569 (provide 'vc-sccs)
571 ;;; vc-sccs.el ends here