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