1 ;;; vc-sccs.el --- support for SCCS version-control
3 ;; Copyright (C) 1992-2012 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/>.
26 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
27 ;; to be installed somewhere on Emacs's path for executables.
36 ;;; Customization options
39 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
40 ;; (defcustom vc-sccs-path
42 ;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
43 ;; (if (file-directory-p dir)
46 ;; "List of extra directories to search for SCCS commands."
47 ;; :type '(repeat directory)
55 (defcustom vc-sccs-register-switches nil
56 "Switches for registering a file in SCCS.
57 A string or list of strings passed to the checkin program by
58 \\[vc-register]. If nil, use the value of `vc-register-switches'.
59 If t, use no switches."
60 :type
'(choice (const :tag
"Unspecified" nil
)
62 (string :tag
"Argument String")
63 (repeat :tag
"Argument List" :value
("") string
))
67 (defcustom vc-sccs-diff-switches nil
68 "String or list of strings specifying switches for SCCS diff under VC.
69 If nil, use the value of `vc-diff-switches'. If t, use no switches."
70 :type
'(choice (const :tag
"Unspecified" nil
)
72 (string :tag
"Argument String")
73 (repeat :tag
"Argument List" :value
("") string
))
77 (defcustom vc-sccs-header
'("%W%")
78 "Header keywords to be inserted by `vc-insert-headers'."
79 :type
'(repeat string
)
80 :version
"24.1" ; no longer consult the obsolete vc-header-alist
84 (defcustom vc-sccs-master-templates
85 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
))
86 "Where to look for SCCS master files.
87 For a description of possible values, see `vc-check-master-templates'."
88 :type
'(choice (const :tag
"Use standard SCCS file names"
89 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
))
90 (repeat :tag
"User-specified"
98 ;;; Internal variables
101 (defconst vc-sccs-name-assoc-file
"VC-names")
104 ;;; Properties of the backend
106 (defun vc-sccs-revision-granularity () 'file
)
107 (defun vc-sccs-checkout-model (files) 'locking
)
110 ;;; State-querying functions
113 ;; The autoload cookie below places vc-sccs-registered directly into
114 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
115 ;; every file that is visited. The definition is repeated below
116 ;; so that Help and etags can find it.
118 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
119 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f
))
121 (defun vc-sccs-state (file)
122 "SCCS-specific function to compute the version control state."
123 (if (not (vc-sccs-registered file
))
126 (if (vc-insert-file (vc-sccs-lock-file file
))
127 (let* ((locks (vc-sccs-parse-locks))
128 (working-revision (vc-working-revision file
))
129 (locking-user (cdr (assoc working-revision locks
))))
130 (if (not locking-user
)
131 (if (vc-workfile-unchanged-p file
)
134 (if (string= locking-user
(vc-user-login-name file
))
139 (defun vc-sccs-state-heuristic (file)
140 "SCCS-specific state heuristic."
141 (if (not (vc-mistrust-permissions file
))
142 ;; This implementation assumes that any file which is under version
143 ;; control and has -rw-r--r-- is locked by its owner. This is true
144 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
145 ;; We have to be careful not to exclude files with execute bits on;
146 ;; scripts can be under version control too. Also, we must ignore the
147 ;; group-read and other-read bits, since paranoid users turn them off.
148 (let* ((attributes (file-attributes file
'string
))
149 (owner-name (nth 2 attributes
))
150 (permissions (nth 8 attributes
)))
151 (if (string-match ".r-..-..-." permissions
)
153 (if (string-match ".rw..-..-." permissions
)
154 (if (file-ownership-preserved-p file
)
157 ;; Strange permissions.
158 ;; Fall through to real state computation.
159 (vc-sccs-state file
))))
160 (vc-sccs-state file
)))
162 (defun vc-sccs-dir-status (dir update-function
)
163 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
164 ;; is not TRTD because it returns files from multiple backends.
165 ;; It should also return 'unregistered files.
167 ;; Doing lots of individual VC-state calls is painful, but
168 ;; there is no better option in SCCS-land.
169 (let ((flist (vc-expand-dirs (list dir
)))
172 (let ((state (vc-state file
))
173 (frel (file-relative-name file
)))
174 (when (and (eq (vc-backend file
) 'SCCS
)
175 (not (eq state
'up-to-date
)))
176 (push (list frel state
) result
))))
177 (funcall update-function result
)))
179 (defun vc-sccs-working-revision (file)
180 "SCCS-specific version of `vc-working-revision'."
182 ;; The working revision is always the latest revision number.
183 ;; To find this number, search the entire delta table,
184 ;; rather than just the first entry, because the
185 ;; first entry might be a deleted ("R") revision.
186 (vc-insert-file (vc-name file
) "^\001e\n\001[^s]")
187 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
189 (defun vc-sccs-workfile-unchanged-p (file)
190 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
191 (zerop (apply 'vc-do-command
"*vc*" 1 "vcdiff" (vc-name file
)
193 (concat "-r" (vc-working-revision file
))))))
197 ;;; State-changing functions
200 (defun vc-sccs-do-command (buffer okstatus command file-or-list
&rest flags
)
201 ;; (let ((load-path (append vc-sccs-path load-path)))
202 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
203 (apply 'vc-do-command
(or buffer
"*vc*") okstatus
"sccs" file-or-list command flags
))
205 (defun vc-sccs-create-repo ()
206 "Create a new SCCS repository."
207 ;; SCCS is totally file-oriented, so all we have to do is make the directory
208 (make-directory "SCCS"))
210 (defun vc-sccs-register (files &optional rev comment
)
211 "Register FILES into the SCCS version-control system.
212 REV is the optional revision number for the file. COMMENT can be used
213 to provide an initial description of FILES.
214 Passes either `vc-sccs-register-switches' or `vc-register-switches'
217 Automatically retrieve a read-only version of the files with keywords
218 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
220 (let* ((dirname (or (file-name-directory file
) ""))
221 (basename (file-name-nondirectory file
))
222 (project-file (vc-sccs-search-project-dir dirname basename
)))
225 (format (car vc-sccs-master-templates
) dirname basename
))))
226 (apply 'vc-sccs-do-command nil
0 "admin" vc-name
227 (and rev
(not (string= rev
"")) (concat "-r" rev
))
229 (concat "-i" (file-relative-name file
))
230 (and comment
(concat "-y" comment
))
231 (vc-switches 'SCCS
'register
)))
233 (if vc-keep-workfiles
234 (vc-sccs-do-command nil
0 "get" (vc-name file
))))))
236 (defun vc-sccs-responsible-p (file)
237 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
238 ;; TODO: check for all the patterns in vc-sccs-master-templates
239 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file
)))
240 (stringp (vc-sccs-search-project-dir (or (file-name-directory file
) "")
241 (file-name-nondirectory file
)))))
243 (defun vc-sccs-checkin (files rev comment
)
244 "SCCS-specific version of `vc-backend-checkin'."
245 (dolist (file (vc-expand-dirs files
))
246 (apply 'vc-sccs-do-command nil
0 "delta" (vc-name file
)
247 (if rev
(concat "-r" rev
))
248 (concat "-y" comment
)
249 (vc-switches 'SCCS
'checkin
))
250 (if vc-keep-workfiles
251 (vc-sccs-do-command nil
0 "get" (vc-name file
)))))
253 (defun vc-sccs-find-revision (file rev buffer
)
254 (apply 'vc-sccs-do-command
255 buffer
0 "get" (vc-name file
)
256 "-s" ;; suppress diagnostic output
260 (vc-sccs-lookup-triple file rev
)))
261 (vc-switches 'SCCS
'checkout
)))
263 (defun vc-sccs-checkout (file &optional editable rev
)
264 "Retrieve a copy of a saved revision of SCCS controlled FILE.
265 If FILE is a directory, all version-controlled files beneath are checked out.
266 EDITABLE non-nil means that the file should be writable and
267 locked. REV is the revision to check out."
268 (if (file-directory-p file
)
269 (mapc 'vc-sccs-checkout
(vc-expand-dirs (list file
)))
270 (let ((file-buffer (get-file-buffer file
))
272 (message "Checking out %s..." file
)
274 ;; Change buffers to get local value of vc-checkout-switches.
275 (if file-buffer
(set-buffer file-buffer
))
276 (setq switches
(vc-switches 'SCCS
'checkout
))
277 ;; Save this buffer's default-directory
278 ;; and use save-excursion to make sure it is restored
279 ;; in the same buffer it was saved in.
280 (let ((default-directory default-directory
))
282 ;; Adjust the default-directory so that the check-out creates
283 ;; the file in the right place.
284 (setq default-directory
(file-name-directory file
))
286 (and rev
(or (string= rev
"")
289 (apply 'vc-sccs-do-command nil
0 "get" (vc-name file
)
291 (and rev
(concat "-r" (vc-sccs-lookup-triple file rev
)))
293 (message "Checking out %s...done" file
))))
295 (defun vc-sccs-rollback (files)
296 "Roll back, undoing the most recent checkins of FILES. Directories
297 are expanded to all version-controlled subfiles."
298 (setq files
(vc-expand-dirs files
))
300 (error "SCCS backend doesn't support directory-level rollback"))
302 (let ((discard (vc-working-revision file
)))
303 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
306 (message "Removing revision %s from %s..." discard file
)
307 (vc-sccs-do-command nil
0 "rmdel"
308 (vc-name file
) (concat "-r" discard
))
309 (vc-sccs-do-command nil
0 "get" (vc-name file
) nil
))))
311 (defun vc-sccs-revert (file &optional contents-done
)
312 "Revert FILE to the version it was based on. If FILE is a directory,
313 revert all subfiles."
314 (if (file-directory-p file
)
315 (mapc 'vc-sccs-revert
(vc-expand-dirs (list file
)))
316 (vc-sccs-do-command nil
0 "unget" (vc-name file
))
317 (vc-sccs-do-command nil
0 "get" (vc-name file
))
318 ;; Checking out explicit revisions is not supported under SCCS, yet.
319 ;; We always "revert" to the latest revision; therefore
320 ;; vc-working-revision is cleared here so that it gets recomputed.
321 (vc-file-setprop file
'vc-working-revision nil
)))
323 (defun vc-sccs-steal-lock (file &optional rev
)
324 "Steal the lock on the current workfile for FILE and revision REV."
325 (if (file-directory-p file
)
326 (mapc 'vc-sccs-steal-lock
(vc-expand-dirs (list file
)))
327 (vc-sccs-do-command nil
0 "unget"
328 (vc-name file
) "-n" (if rev
(concat "-r" rev
)))
329 (vc-sccs-do-command nil
0 "get"
330 (vc-name file
) "-g" (if rev
(concat "-r" rev
)))))
332 (defun vc-sccs-modify-change-comment (files rev comment
)
333 "Modify (actually, append to) the change comments for FILES on a specified REV."
334 (dolist (file (vc-expand-dirs files
))
335 (vc-sccs-do-command nil
0 "cdc" (vc-name file
)
336 (concat "-y" comment
) (concat "-r" rev
))))
340 ;;; History functions
343 (defun vc-sccs-print-log (files buffer
&optional shortlog start-revision-ignored limit
)
344 "Get change log associated with FILES."
345 (setq files
(vc-expand-dirs files
))
346 (vc-sccs-do-command buffer
0 "prs" (mapcar 'vc-name files
))
347 (when limit
'limit-unsupported
))
349 (defun vc-sccs-diff (files &optional oldvers newvers buffer
)
350 "Get a difference report using SCCS between two filesets."
351 (setq files
(vc-expand-dirs files
))
352 (setq oldvers
(vc-sccs-lookup-triple (car files
) oldvers
))
353 (setq newvers
(vc-sccs-lookup-triple (car files
) newvers
))
354 (apply 'vc-do-command
(or buffer
"*vc-diff*")
355 1 "vcdiff" (mapcar 'vc-name
(vc-expand-dirs files
))
357 (and oldvers
(concat "-r" oldvers
))
358 (and newvers
(concat "-r" newvers
)))
359 (vc-switches 'SCCS
'diff
))))
363 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
364 ;;; our own set of name-to-revision mappings.
367 (defun vc-sccs-create-tag (dir name branchp
)
369 (error "SCCS backend does not support module branches"))
370 (let ((result (vc-tag-precondition dir
)))
372 (error "File %s is not up-to-date" result
)
376 (vc-sccs-add-triple name f
(vc-working-revision f
)))))))
383 (defun vc-sccs-previous-revision (file rev
)
384 (vc-call-backend 'RCS
'previous-revision file rev
))
386 (defun vc-sccs-next-revision (file rev
)
387 (vc-call-backend 'RCS
'next-revision file rev
))
389 (defun vc-sccs-check-headers ()
390 "Check if the current file has any headers in it."
392 (goto-char (point-min))
393 (re-search-forward "%[A-Z]%" nil t
)))
395 (defun vc-sccs-rename-file (old new
)
396 ;; Move the master file (using vc-rcs-master-templates).
397 (vc-rename-master (vc-name old
) new vc-sccs-master-templates
)
398 ;; Update the tag file.
401 (expand-file-name vc-sccs-name-assoc-file
402 (file-name-directory (vc-name old
))))
403 (goto-char (point-min))
404 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
405 (while (re-search-forward (concat ":" (regexp-quote old
) "$") nil t
)
406 (replace-match (concat ":" new
) nil nil
))
408 (kill-buffer (current-buffer))))
410 (defun vc-sccs-find-file-hook ()
411 ;; If the file is locked by some other user, make
412 ;; the buffer read-only. Like this, even root
413 ;; cannot modify a file that someone else has locked.
414 (and (stringp (vc-state buffer-file-name
'SCCS
))
415 (setq buffer-read-only t
)))
419 ;;; Internal functions
422 ;; This function is wrapped with `progn' so that the autoload cookie
423 ;; copies the whole function itself into loaddefs.el rather than just placing
424 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
425 ;; help us avoid loading vc-sccs.
427 (progn (defun vc-sccs-search-project-dir (dirname basename
)
428 "Return the name of a master file in the SCCS project directory.
429 Does not check whether the file exists but returns nil if it does not
430 find any project directory."
431 (let ((project-dir (getenv "PROJECTDIR")) dirs dir
)
433 (if (file-name-absolute-p project-dir
)
434 (setq dirs
'("SCCS" ""))
435 (setq dirs
'("src/SCCS" "src" "source/SCCS" "source"))
436 (setq project-dir
(expand-file-name (concat "~" project-dir
))))
437 (while (and (not dir
) dirs
)
438 (setq dir
(expand-file-name (car dirs
) project-dir
))
439 (unless (file-directory-p dir
)
441 (setq dirs
(cdr dirs
))))
442 (and dir
(expand-file-name (concat "s." basename
) dir
))))))
444 (defun vc-sccs-lock-file (file)
445 "Generate lock file name corresponding to FILE."
446 (let ((master (vc-name file
)))
449 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master
)
450 (replace-match "p." t t master
2))))
452 (defun vc-sccs-parse-locks ()
453 "Parse SCCS locks in current buffer.
454 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
456 (goto-char (point-min))
457 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
460 (cons (cons (match-string 1) (match-string 2)) master-locks
)))
461 ;; FIXME: is it really necessary to reverse ?
462 (nreverse master-locks
)))
464 (defun vc-sccs-add-triple (name file rev
)
467 (expand-file-name vc-sccs-name-assoc-file
468 (file-name-directory (vc-name file
))))
469 (goto-char (point-max))
470 (insert name
"\t:\t" file
"\t" rev
"\n")
472 (kill-buffer (current-buffer))))
474 (defun vc-sccs-lookup-triple (file name
)
475 "Return the numeric revision corresponding to a named tag of FILE.
476 If NAME is nil or a revision number string it's just passed through."
478 (let ((firstchar (aref name
0)))
479 (and (>= firstchar ?
0) (<= firstchar ?
9))))
483 (expand-file-name vc-sccs-name-assoc-file
484 (file-name-directory (vc-name file
))))
485 (vc-parse-buffer (concat name
"\t:\t" file
"\t\\(.+\\)") 1))))
489 ;;; vc-sccs.el ends here