Merge from origin/emacs-24
[emacs.git] / lisp / vc / vc-sccs.el
blob8d8d9e844edd3214affccfb46fcef3cd8b8b7944
1 ;;; vc-sccs.el --- support for SCCS version-control -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992-2015 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-files (dir files update-function)
138 (if (not files) (setq files (vc-expand-dirs (list dir) 'RCS)))
139 (let ((result nil))
140 (dolist (file files)
141 (let ((state (vc-state file))
142 (frel (file-relative-name file)))
143 (when (and (eq (vc-backend file) 'SCCS)
144 (not (eq state 'up-to-date)))
145 (push (list frel state) result))))
146 (funcall update-function result)))
148 (autoload 'vc-master-name "vc-filewise")
150 (defun vc-sccs-working-revision (file)
151 "SCCS-specific version of `vc-working-revision'."
152 (when (and (file-regular-p file) (vc-master-name file))
153 (with-temp-buffer
154 ;; The working revision is always the latest revision number.
155 ;; To find this number, search the entire delta table,
156 ;; rather than just the first entry, because the
157 ;; first entry might be a deleted ("R") revision.
158 (vc-insert-file (vc-master-name file) "^\001e\n\001[^s]")
159 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1))))
161 ;; Cf vc-sccs-find-revision.
162 (defun vc-sccs-write-revision (file outfile &optional rev)
163 "Write the SCCS version of input file FILE to output file OUTFILE.
164 Optional string REV is a revision."
165 (with-temp-buffer
166 (apply 'vc-sccs-do-command t 0 "get" (vc-master-name file)
167 (append '("-s" "-p" "-k") ; -k: no keyword expansion
168 (if rev (list (concat "-r" rev)))))
169 (write-region nil nil outfile nil 'silent)))
171 (defun vc-sccs-workfile-unchanged-p (file)
172 "Has FILE remained unchanged since last checkout?"
173 (let ((tempfile (make-temp-file "vc-sccs")))
174 (unwind-protect
175 (progn
176 (vc-sccs-write-revision file tempfile (vc-working-revision file))
177 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
178 (delete-file tempfile))))
182 ;;; State-changing functions
185 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
186 ;; (let ((load-path (append vc-sccs-path load-path)))
187 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
188 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
190 (defun vc-sccs-create-repo ()
191 "Create a new SCCS repository."
192 ;; SCCS is totally file-oriented, so all we have to do is make the directory
193 (make-directory "SCCS"))
195 (autoload 'vc-switches "vc")
197 (defun vc-sccs-register (files &optional comment)
198 "Register FILES into the SCCS version-control system.
199 Automatically retrieve a read-only version of the files with keywords expanded.
200 COMMENT can be used to provide an initial description of FILES.
201 Passes either `vc-sccs-register-switches' or `vc-register-switches'
202 to the SCCS command."
203 (dolist (file files)
204 (let* ((dirname (or (file-name-directory file) ""))
205 (basename (file-name-nondirectory file))
206 (project-file (vc-sccs-search-project-dir dirname basename)))
207 (let ((vc-master-name
208 (or project-file
209 (format (car vc-sccs-master-templates) dirname basename))))
210 (apply 'vc-sccs-do-command nil 0 "admin" vc-master-name
211 "-fb"
212 (concat "-i" (file-relative-name file))
213 (and comment (concat "-y" comment))
214 (vc-switches 'SCCS 'register)))
215 (delete-file file)
216 (vc-sccs-do-command nil 0 "get" (vc-master-name file)))))
218 (defun vc-sccs-responsible-p (file)
219 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
220 ;; TODO: check for all the patterns in vc-sccs-master-templates
221 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
222 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
223 (file-name-nondirectory file)))))
225 (defun vc-sccs-checkin (files comment)
226 "SCCS-specific version of `vc-backend-checkin'."
227 (dolist (file (vc-expand-dirs files 'SCCS))
228 (apply 'vc-sccs-do-command nil 0 "delta" (vc-master-name file)
229 (concat "-y" comment)
230 (vc-switches 'SCCS 'checkin))
231 (vc-sccs-do-command nil 0 "get" (vc-master-name file))))
233 (defun vc-sccs-find-revision (file rev buffer)
234 (apply 'vc-sccs-do-command
235 buffer 0 "get" (vc-master-name file)
236 "-s" ;; suppress diagnostic output
237 "-p"
238 (and rev
239 (concat "-r"
240 (vc-sccs-lookup-triple file rev)))
241 (vc-switches 'SCCS 'checkout)))
243 (defun vc-sccs-checkout (file &optional rev)
244 "Retrieve a copy of a saved revision of SCCS controlled FILE.
245 If FILE is a directory, all version-controlled files beneath are checked out.
246 EDITABLE non-nil means that the file should be writable and
247 locked. REV is the revision to check out."
248 (if (file-directory-p file)
249 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file) 'SCCS))
250 (let ((file-buffer (get-file-buffer file))
251 switches)
252 (message "Checking out %s..." file)
253 (save-excursion
254 ;; Change buffers to get local value of vc-checkout-switches.
255 (if file-buffer (set-buffer file-buffer))
256 (setq switches (vc-switches 'SCCS 'checkout))
257 ;; Save this buffer's default-directory
258 ;; and use save-excursion to make sure it is restored
259 ;; in the same buffer it was saved in.
260 (let ((default-directory default-directory))
261 (save-excursion
262 ;; Adjust the default-directory so that the check-out creates
263 ;; the file in the right place.
264 (setq default-directory (file-name-directory file))
266 (and rev (or (string= rev "")
267 (not (stringp rev)))
268 (setq rev nil))
269 (apply 'vc-sccs-do-command nil 0 "get" (vc-master-name file)
270 "-e"
271 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
272 switches))))
273 (message "Checking out %s...done" file))))
275 (defun vc-sccs-revert (file &optional _contents-done)
276 "Revert FILE to the version it was based on. If FILE is a directory,
277 revert all subfiles."
278 (if (file-directory-p file)
279 (mapc 'vc-sccs-revert (vc-expand-dirs (list file) 'SCCS))
280 (vc-sccs-do-command nil 0 "unget" (vc-master-name file))
281 (vc-sccs-do-command nil 0 "get" (vc-master-name file))
282 ;; Checking out explicit revisions is not supported under SCCS, yet.
283 ;; We always "revert" to the latest revision; therefore
284 ;; vc-working-revision is cleared here so that it gets recomputed.
285 (vc-file-setprop file 'vc-working-revision nil)))
287 (defun vc-sccs-steal-lock (file &optional rev)
288 "Steal the lock on the current workfile for FILE and revision REV."
289 (if (file-directory-p file)
290 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file) 'SCCS))
291 (vc-sccs-do-command nil 0 "unget"
292 (vc-master-name file) "-n" (if rev (concat "-r" rev)))
293 (vc-sccs-do-command nil 0 "get"
294 (vc-master-name file) "-g" (if rev (concat "-r" rev)))))
296 (defun vc-sccs-modify-change-comment (files rev comment)
297 "Modify (actually, append to) the change comments for FILES on a specified REV."
298 (dolist (file (vc-expand-dirs files 'SCCS))
299 (vc-sccs-do-command nil 0 "cdc" (vc-master-name file)
300 (concat "-y" comment) (concat "-r" rev))))
304 ;;; History functions
307 (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
308 "Print commit log associated with FILES into specified BUFFER.
309 Remaining arguments are ignored."
310 (setq files (vc-expand-dirs files 'SCCS))
311 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-master-name files))
312 (when limit 'limit-unsupported))
314 (autoload 'vc-setup-buffer "vc-dispatcher")
315 (autoload 'vc-delistify "vc-dispatcher")
317 (defvar w32-quote-process-args)
319 ;; FIXME use sccsdiff if present?
320 (defun vc-sccs-diff (files &optional oldvers newvers buffer _async)
321 "Get a difference report using SCCS between two filesets."
322 (setq files (vc-expand-dirs files 'SCCS))
323 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
324 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
325 (or buffer (setq buffer "*vc-diff*"))
326 ;; We have to reimplement pieces of vc-do-command, because
327 ;; we want to run multiple external commands, and only do the setup
328 ;; and exit pieces once.
329 (save-current-buffer
330 (unless (or (eq buffer t)
331 (and (stringp buffer) (string= (buffer-name) buffer))
332 (eq buffer (current-buffer)))
333 (vc-setup-buffer buffer))
334 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
335 (if oldvers (list (concat " -r" oldvers)))
336 (if newvers (list (concat " -r" newvers)))))
337 (fake-command
338 (format "diff%s %s"
339 (if fake-flags
340 (concat " " (mapconcat 'identity fake-flags " "))
342 (vc-delistify files)))
343 (status 0)
344 (oldproc (get-buffer-process (current-buffer))))
345 (when vc-command-messages
346 (message "Running %s in foreground..." fake-command))
347 (if oldproc (delete-process oldproc))
348 (dolist (file files)
349 (let ((oldfile (make-temp-file "vc-sccs"))
350 newfile)
351 (unwind-protect
352 (progn
353 (vc-sccs-write-revision file oldfile oldvers)
354 (if newvers
355 (vc-sccs-write-revision file (setq newfile
356 (make-temp-file "vc-sccs"))
357 newvers))
358 (let* ((inhibit-read-only t)
359 (buffer-undo-list t)
360 (process-environment
361 (cons "LC_MESSAGES=C" process-environment))
362 (w32-quote-process-args t)
363 (this-status
364 (apply 'process-file "diff" nil t nil
365 (append (vc-switches 'SCCS 'diff)
366 (list oldfile
367 (or newfile
368 (file-relative-name file)))))))
369 (or (integerp this-status) (setq status 'error))
370 (and (integerp status)
371 (> this-status status)
372 (setq status this-status))))
373 (delete-file oldfile)
374 (if newfile (delete-file newfile)))))
375 (when (or (not (integerp status)) (> status 1))
376 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
377 (pop-to-buffer (current-buffer))
378 (goto-char (point-min))
379 (shrink-window-if-larger-than-buffer))
380 (error "Running %s...FAILED (%s)" fake-command
381 (if (integerp status) (format "status %d" status) status)))
382 (when vc-command-messages
383 (message "Running %s...OK = %d" fake-command status))
384 ;; Should we pretend we ran sccsdiff instead?
385 ;; This might not actually be a valid diff command.
386 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
387 status)))
391 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
392 ;;; our own set of name-to-revision mappings.
395 (autoload 'vc-tag-precondition "vc")
396 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
398 (defun vc-sccs-create-tag (dir name branchp)
399 (when branchp
400 (error "SCCS backend does not support module branches"))
401 (let ((result (vc-tag-precondition dir)))
402 (if (stringp result)
403 (error "File %s is not up-to-date" result)
404 (vc-file-tree-walk
406 (lambda (f)
407 (vc-sccs-add-triple name f (vc-working-revision f)))))))
411 ;;; Miscellaneous
414 (defun vc-sccs-previous-revision (file rev)
415 (vc-call-backend 'RCS 'previous-revision file rev))
417 (defun vc-sccs-next-revision (file rev)
418 (vc-call-backend 'RCS 'next-revision file rev))
420 (defun vc-sccs-check-headers ()
421 "Check if the current file has any headers in it."
422 (save-excursion
423 (goto-char (point-min))
424 (re-search-forward "%[A-Z]%" nil t)))
426 (autoload 'vc-rename-master "vc-filewise")
428 (defun vc-sccs-rename-file (old new)
429 ;; Move the master file (using vc-rcs-master-templates).
430 (vc-rename-master (vc-master-name old) new vc-sccs-master-templates)
431 ;; Update the tag file.
432 (with-current-buffer
433 (find-file-noselect
434 (expand-file-name vc-sccs-name-assoc-file
435 (file-name-directory (vc-master-name old))))
436 (goto-char (point-min))
437 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
438 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
439 (replace-match (concat ":" new) nil nil))
440 (basic-save-buffer)
441 (kill-buffer (current-buffer))))
443 (defun vc-sccs-find-file-hook ()
444 ;; If the file is locked by some other user, make
445 ;; the buffer read-only. Like this, even root
446 ;; cannot modify a file that someone else has locked.
447 (and (stringp (vc-state buffer-file-name 'SCCS))
448 (setq buffer-read-only t)))
452 ;;; Internal functions
455 ;; This function is wrapped with `progn' so that the autoload cookie
456 ;; copies the whole function itself into loaddefs.el rather than just placing
457 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
458 ;; help us avoid loading vc-sccs.
459 ;;;###autoload
460 (progn (defun vc-sccs-search-project-dir (_dirname basename)
461 "Return the name of a master file in the SCCS project directory.
462 Does not check whether the file exists but returns nil if it does not
463 find any project directory."
464 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
465 (when project-dir
466 (if (file-name-absolute-p project-dir)
467 (setq dirs '("SCCS" ""))
468 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
469 (setq project-dir (expand-file-name (concat "~" project-dir))))
470 (while (and (not dir) dirs)
471 (setq dir (expand-file-name (car dirs) project-dir))
472 (unless (file-directory-p dir)
473 (setq dir nil)
474 (setq dirs (cdr dirs))))
475 (and dir (expand-file-name (concat "s." basename) dir))))))
477 (defun vc-sccs-lock-file (file)
478 "Generate lock file name corresponding to FILE."
479 (let ((master (vc-master-name file)))
480 (and
481 master
482 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
483 (replace-match "p." t t master 2))))
485 (defun vc-sccs-parse-locks ()
486 "Parse SCCS locks in current buffer.
487 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
488 (let (master-locks)
489 (goto-char (point-min))
490 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
491 nil t)
492 (setq master-locks
493 (cons (cons (match-string 1) (match-string 2)) master-locks)))
494 ;; FIXME: is it really necessary to reverse ?
495 (nreverse master-locks)))
497 (defun vc-sccs-add-triple (name file rev)
498 (with-current-buffer
499 (find-file-noselect
500 (expand-file-name vc-sccs-name-assoc-file
501 (file-name-directory (vc-master-name file))))
502 (goto-char (point-max))
503 (insert name "\t:\t" file "\t" rev "\n")
504 (basic-save-buffer)
505 (kill-buffer (current-buffer))))
507 (defun vc-sccs-lookup-triple (file name)
508 "Return the numeric revision corresponding to a named tag of FILE.
509 If NAME is nil or a revision number string it's just passed through."
510 (if (or (null name)
511 (let ((firstchar (aref name 0)))
512 (and (>= firstchar ?0) (<= firstchar ?9))))
513 name
514 (with-temp-buffer
515 (vc-insert-file
516 (expand-file-name vc-sccs-name-assoc-file
517 (file-name-directory (vc-master-name file))))
518 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
520 (provide 'vc-sccs)
522 ;;; vc-sccs.el ends here