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>
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/>.
32 ;;; Customization options
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)
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
)
56 (string :tag
"Argument String")
57 (repeat :tag
"Argument List" :value
("") string
))
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
)
66 (string :tag
"Argument String")
67 (repeat :tag
"Argument List" :value
("") string
))
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
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.
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"
95 ;;; Internal variables
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.
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
))
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
)
130 (if (string= locking-user
(vc-user-login-name file
))
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
)))
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'."
153 ;; The working revision is always the latest revision number.
154 ;; To find this number, search the entire delta table,
155 ;; rather than just the first entry, because the
156 ;; first entry might be a deleted ("R") revision.
157 (vc-insert-file (vc-master-name file
) "^\001e\n\001[^s]")
158 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
160 ;; Cf vc-sccs-find-revision.
161 (defun vc-sccs-write-revision (file outfile
&optional rev
)
162 "Write the SCCS version of input file FILE to output file OUTFILE.
163 Optional string REV is a revision."
165 (apply 'vc-sccs-do-command t
0 "get" (vc-master-name file
)
166 (append '("-s" "-p" "-k") ; -k: no keyword expansion
167 (if rev
(list (concat "-r" rev
)))))
168 (write-region nil nil outfile nil
'silent
)))
170 (defun vc-sccs-workfile-unchanged-p (file)
171 "Has FILE remained unchanged since last checkout?"
172 (let ((tempfile (make-temp-file "vc-sccs")))
175 (vc-sccs-write-revision file tempfile
(vc-working-revision file
))
176 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile
)))
177 (delete-file tempfile
))))
181 ;;; State-changing functions
184 (defun vc-sccs-do-command (buffer okstatus command file-or-list
&rest flags
)
185 ;; (let ((load-path (append vc-sccs-path load-path)))
186 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
187 (apply 'vc-do-command
(or buffer
"*vc*") okstatus
"sccs" file-or-list command flags
))
189 (defun vc-sccs-create-repo ()
190 "Create a new SCCS repository."
191 ;; SCCS is totally file-oriented, so all we have to do is make the directory
192 (make-directory "SCCS"))
194 (autoload 'vc-switches
"vc")
196 (defun vc-sccs-register (files &optional comment
)
197 "Register FILES into the SCCS version-control system.
198 Automatically retrieve a read-only version of the files with keywords expanded.
199 COMMENT can be used to provide an initial description of FILES.
200 Passes either `vc-sccs-register-switches' or `vc-register-switches'
201 to the SCCS command."
203 (let* ((dirname (or (file-name-directory file
) ""))
204 (basename (file-name-nondirectory file
))
205 (project-file (vc-sccs-search-project-dir dirname basename
)))
206 (let ((vc-master-name
208 (format (car vc-sccs-master-templates
) dirname basename
))))
209 (apply 'vc-sccs-do-command nil
0 "admin" vc-master-name
211 (concat "-i" (file-relative-name file
))
212 (and comment
(concat "-y" comment
))
213 (vc-switches 'SCCS
'register
)))
215 (vc-sccs-do-command nil
0 "get" (vc-master-name file
)))))
217 (defun vc-sccs-responsible-p (file)
218 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
219 ;; TODO: check for all the patterns in vc-sccs-master-templates
220 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file
)))
221 (stringp (vc-sccs-search-project-dir (or (file-name-directory file
) "")
222 (file-name-nondirectory file
)))))
224 (defun vc-sccs-checkin (files comment
)
225 "SCCS-specific version of `vc-backend-checkin'."
226 (dolist (file (vc-expand-dirs files
'SCCS
))
227 (apply 'vc-sccs-do-command nil
0 "delta" (vc-master-name file
)
228 (concat "-y" comment
)
229 (vc-switches 'SCCS
'checkin
))
230 (vc-sccs-do-command nil
0 "get" (vc-master-name file
))))
232 (defun vc-sccs-find-revision (file rev buffer
)
233 (apply 'vc-sccs-do-command
234 buffer
0 "get" (vc-master-name file
)
235 "-s" ;; suppress diagnostic output
239 (vc-sccs-lookup-triple file rev
)))
240 (vc-switches 'SCCS
'checkout
)))
242 (defun vc-sccs-checkout (file &optional rev
)
243 "Retrieve a copy of a saved revision of SCCS controlled FILE.
244 If FILE is a directory, all version-controlled files beneath are checked out.
245 EDITABLE non-nil means that the file should be writable and
246 locked. REV is the revision to check out."
247 (if (file-directory-p file
)
248 (mapc 'vc-sccs-checkout
(vc-expand-dirs (list file
) 'SCCS
))
249 (let ((file-buffer (get-file-buffer file
))
251 (message "Checking out %s..." file
)
253 ;; Change buffers to get local value of vc-checkout-switches.
254 (if file-buffer
(set-buffer file-buffer
))
255 (setq switches
(vc-switches 'SCCS
'checkout
))
256 ;; Save this buffer's default-directory
257 ;; and use save-excursion to make sure it is restored
258 ;; in the same buffer it was saved in.
259 (let ((default-directory default-directory
))
261 ;; Adjust the default-directory so that the check-out creates
262 ;; the file in the right place.
263 (setq default-directory
(file-name-directory file
))
265 (and rev
(or (string= rev
"")
268 (apply 'vc-sccs-do-command nil
0 "get" (vc-master-name file
)
270 (and rev
(concat "-r" (vc-sccs-lookup-triple file rev
)))
272 (message "Checking out %s...done" file
))))
274 (defun vc-sccs-revert (file &optional _contents-done
)
275 "Revert FILE to the version it was based on. If FILE is a directory,
276 revert all subfiles."
277 (if (file-directory-p file
)
278 (mapc 'vc-sccs-revert
(vc-expand-dirs (list file
) 'SCCS
))
279 (vc-sccs-do-command nil
0 "unget" (vc-master-name file
))
280 (vc-sccs-do-command nil
0 "get" (vc-master-name file
))
281 ;; Checking out explicit revisions is not supported under SCCS, yet.
282 ;; We always "revert" to the latest revision; therefore
283 ;; vc-working-revision is cleared here so that it gets recomputed.
284 (vc-file-setprop file
'vc-working-revision nil
)))
286 (defun vc-sccs-steal-lock (file &optional rev
)
287 "Steal the lock on the current workfile for FILE and revision REV."
288 (if (file-directory-p file
)
289 (mapc 'vc-sccs-steal-lock
(vc-expand-dirs (list file
) 'SCCS
))
290 (vc-sccs-do-command nil
0 "unget"
291 (vc-master-name file
) "-n" (if rev
(concat "-r" rev
)))
292 (vc-sccs-do-command nil
0 "get"
293 (vc-master-name file
) "-g" (if rev
(concat "-r" rev
)))))
295 (defun vc-sccs-modify-change-comment (files rev comment
)
296 "Modify (actually, append to) the change comments for FILES on a specified REV."
297 (dolist (file (vc-expand-dirs files
'SCCS
))
298 (vc-sccs-do-command nil
0 "cdc" (vc-master-name file
)
299 (concat "-y" comment
) (concat "-r" rev
))))
303 ;;; History functions
306 (defun vc-sccs-print-log (files buffer
&optional _shortlog _start-revision-ignored limit
)
307 "Print commit log associated with FILES into specified BUFFER.
308 Remaining arguments are ignored."
309 (setq files
(vc-expand-dirs files
'SCCS
))
310 (vc-sccs-do-command buffer
0 "prs" (mapcar 'vc-master-name files
))
311 (when limit
'limit-unsupported
))
313 (autoload 'vc-setup-buffer
"vc-dispatcher")
314 (autoload 'vc-delistify
"vc-dispatcher")
316 (defvar w32-quote-process-args
)
318 ;; FIXME use sccsdiff if present?
319 (defun vc-sccs-diff (files &optional _async oldvers newvers buffer
)
320 "Get a difference report using SCCS between two filesets."
321 (setq files
(vc-expand-dirs files
'SCCS
))
322 (setq oldvers
(vc-sccs-lookup-triple (car files
) oldvers
))
323 (setq newvers
(vc-sccs-lookup-triple (car files
) newvers
))
324 (or buffer
(setq buffer
"*vc-diff*"))
325 ;; We have to reimplement pieces of vc-do-command, because
326 ;; we want to run multiple external commands, and only do the setup
327 ;; and exit pieces once.
329 (unless (or (eq buffer t
)
330 (and (stringp buffer
) (string= (buffer-name) buffer
))
331 (eq buffer
(current-buffer)))
332 (vc-setup-buffer buffer
))
333 (let* ((fake-flags (append (vc-switches 'SCCS
'diff
)
334 (if oldvers
(list (concat " -r" oldvers
)))
335 (if newvers
(list (concat " -r" newvers
)))))
339 (concat " " (mapconcat 'identity fake-flags
" "))
341 (vc-delistify files
)))
343 (oldproc (get-buffer-process (current-buffer))))
344 (when vc-command-messages
345 (message "Running %s in foreground..." fake-command
))
346 (if oldproc
(delete-process oldproc
))
348 (let ((oldfile (make-temp-file "vc-sccs"))
352 (vc-sccs-write-revision file oldfile oldvers
)
354 (vc-sccs-write-revision file
(setq newfile
355 (make-temp-file "vc-sccs"))
357 (let* ((inhibit-read-only t
)
360 (cons "LC_MESSAGES=C" process-environment
))
361 (w32-quote-process-args t
)
363 (apply 'process-file
"diff" nil t nil
364 (append (vc-switches 'SCCS
'diff
)
367 (file-relative-name file
)))))))
368 (or (integerp this-status
) (setq status
'error
))
369 (and (integerp status
)
370 (> this-status status
)
371 (setq status this-status
))))
372 (delete-file oldfile
)
373 (if newfile
(delete-file newfile
)))))
374 (when (or (not (integerp status
)) (> status
1))
375 (unless (eq ?\s
(aref (buffer-name (current-buffer)) 0))
376 (pop-to-buffer (current-buffer))
377 (goto-char (point-min))
378 (shrink-window-if-larger-than-buffer))
379 (error "Running %s...FAILED (%s)" fake-command
380 (if (integerp status
) (format "status %d" status
) status
)))
381 (when vc-command-messages
382 (message "Running %s...OK = %d" fake-command status
))
383 ;; Should we pretend we ran sccsdiff instead?
384 ;; This might not actually be a valid diff command.
385 (run-hook-with-args 'vc-post-command-functions
"diff" files fake-flags
)
390 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
391 ;;; our own set of name-to-revision mappings.
394 (autoload 'vc-tag-precondition
"vc")
395 (declare-function vc-file-tree-walk
"vc" (dirname func
&rest args
))
397 (defun vc-sccs-create-tag (dir name branchp
)
399 (error "SCCS backend does not support module branches"))
400 (let ((result (vc-tag-precondition dir
)))
402 (error "File %s is not up-to-date" result
)
406 (vc-sccs-add-triple name f
(vc-working-revision f
)))))))
413 (defun vc-sccs-previous-revision (file rev
)
414 (vc-call-backend 'RCS
'previous-revision file rev
))
416 (defun vc-sccs-next-revision (file rev
)
417 (vc-call-backend 'RCS
'next-revision file rev
))
419 (defun vc-sccs-check-headers ()
420 "Check if the current file has any headers in it."
422 (goto-char (point-min))
423 (re-search-forward "%[A-Z]%" nil t
)))
425 (autoload 'vc-rename-master
"vc-filewise")
427 (defun vc-sccs-rename-file (old new
)
428 ;; Move the master file (using vc-rcs-master-templates).
429 (vc-rename-master (vc-master-name old
) new vc-sccs-master-templates
)
430 ;; Update the tag file.
433 (expand-file-name vc-sccs-name-assoc-file
434 (file-name-directory (vc-master-name old
))))
435 (goto-char (point-min))
436 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
437 (while (re-search-forward (concat ":" (regexp-quote old
) "$") nil t
)
438 (replace-match (concat ":" new
) nil nil
))
440 (kill-buffer (current-buffer))))
442 (defun vc-sccs-find-file-hook ()
443 ;; If the file is locked by some other user, make
444 ;; the buffer read-only. Like this, even root
445 ;; cannot modify a file that someone else has locked.
446 (and (stringp (vc-state buffer-file-name
'SCCS
))
447 (setq buffer-read-only t
)))
451 ;;; Internal functions
454 ;; This function is wrapped with `progn' so that the autoload cookie
455 ;; copies the whole function itself into loaddefs.el rather than just placing
456 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
457 ;; help us avoid loading vc-sccs.
459 (progn (defun vc-sccs-search-project-dir (_dirname basename
)
460 "Return the name of a master file in the SCCS project directory.
461 Does not check whether the file exists but returns nil if it does not
462 find any project directory."
463 (let ((project-dir (getenv "PROJECTDIR")) dirs dir
)
465 (if (file-name-absolute-p project-dir
)
466 (setq dirs
'("SCCS" ""))
467 (setq dirs
'("src/SCCS" "src" "source/SCCS" "source"))
468 (setq project-dir
(expand-file-name (concat "~" project-dir
))))
469 (while (and (not dir
) dirs
)
470 (setq dir
(expand-file-name (car dirs
) project-dir
))
471 (unless (file-directory-p dir
)
473 (setq dirs
(cdr dirs
))))
474 (and dir
(expand-file-name (concat "s." basename
) dir
))))))
476 (defun vc-sccs-lock-file (file)
477 "Generate lock file name corresponding to FILE."
478 (let ((master (vc-master-name file
)))
481 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master
)
482 (replace-match "p." t t master
2))))
484 (defun vc-sccs-parse-locks ()
485 "Parse SCCS locks in current buffer.
486 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
488 (goto-char (point-min))
489 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
492 (cons (cons (match-string 1) (match-string 2)) master-locks
)))
493 ;; FIXME: is it really necessary to reverse ?
494 (nreverse master-locks
)))
496 (defun vc-sccs-add-triple (name file rev
)
499 (expand-file-name vc-sccs-name-assoc-file
500 (file-name-directory (vc-master-name file
))))
501 (goto-char (point-max))
502 (insert name
"\t:\t" file
"\t" rev
"\n")
504 (kill-buffer (current-buffer))))
506 (defun vc-sccs-lookup-triple (file name
)
507 "Return the numeric revision corresponding to a named tag of FILE.
508 If NAME is nil or a revision number string it's just passed through."
510 (let ((firstchar (aref name
0)))
511 (and (>= firstchar ?
0) (<= firstchar ?
9))))
515 (expand-file-name vc-sccs-name-assoc-file
516 (file-name-directory (vc-master-name file
))))
517 (vc-parse-buffer (concat name
"\t:\t" file
"\t\\(.+\\)") 1))))
521 ;;; vc-sccs.el ends here