1 ;;; vc-sccs.el --- support for SCCS version-control
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
33 ;;; Customization options
36 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
37 ;; (defcustom vc-sccs-path
39 ;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
40 ;; (if (file-directory-p dir)
43 ;; "List of extra directories to search for SCCS commands."
44 ;; :type '(repeat directory)
47 (defcustom vc-sccs-register-switches nil
48 "Switches for registering a file in SCCS.
49 A string or list of strings passed to the checkin program by
50 \\[vc-register]. If nil, use the value of `vc-register-switches'.
51 If t, use no switches."
52 :type
'(choice (const :tag
"Unspecified" nil
)
54 (string :tag
"Argument String")
55 (repeat :tag
"Argument List" :value
("") string
))
59 (defcustom vc-sccs-diff-switches nil
60 "String or list of strings specifying switches for SCCS diff under VC.
61 If nil, use the value of `vc-diff-switches'. If t, use no switches."
62 :type
'(choice (const :tag
"Unspecified" nil
)
64 (string :tag
"Argument String")
65 (repeat :tag
"Argument List" :value
("") string
))
69 (defcustom vc-sccs-header
(or (cdr (assoc 'SCCS vc-header-alist
)) '("%W%"))
70 "Header keywords to be inserted by `vc-insert-headers'."
71 :type
'(repeat string
)
75 (defcustom vc-sccs-master-templates
76 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
)
77 "Where to look for SCCS master files.
78 For a description of possible values, see `vc-check-master-templates'."
79 :type
'(choice (const :tag
"Use standard SCCS file names"
80 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
))
81 (repeat :tag
"User-specified"
89 ;;; Internal variables
92 (defconst vc-sccs-name-assoc-file
"VC-names")
95 ;;; Properties of the backend
97 (defun vc-sccs-revision-granularity () 'file
)
98 (defun vc-sccs-checkout-model (files) 'locking
)
101 ;;; State-querying functions
104 ;; The autoload cookie below places vc-sccs-registered directly into
105 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
106 ;; every file that is visited. The definition is repeated below
107 ;; so that Help and etags can find it.
109 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
110 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f
))
112 (defun vc-sccs-state (file)
113 "SCCS-specific function to compute the version control state."
114 (if (not (vc-sccs-registered file
))
117 (if (vc-insert-file (vc-sccs-lock-file file
))
118 (let* ((locks (vc-sccs-parse-locks))
119 (working-revision (vc-working-revision file
))
120 (locking-user (cdr (assoc working-revision locks
))))
121 (if (not locking-user
)
122 (if (vc-workfile-unchanged-p file
)
125 (if (string= locking-user
(vc-user-login-name file
))
130 (defun vc-sccs-state-heuristic (file)
131 "SCCS-specific state heuristic."
132 (if (not (vc-mistrust-permissions file
))
133 ;; This implementation assumes that any file which is under version
134 ;; control and has -rw-r--r-- is locked by its owner. This is true
135 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
136 ;; We have to be careful not to exclude files with execute bits on;
137 ;; scripts can be under version control too. Also, we must ignore the
138 ;; group-read and other-read bits, since paranoid users turn them off.
139 (let* ((attributes (file-attributes file
'string
))
140 (owner-name (nth 2 attributes
))
141 (permissions (nth 8 attributes
)))
142 (if (string-match ".r-..-..-." permissions
)
144 (if (string-match ".rw..-..-." permissions
)
145 (if (file-ownership-preserved-p file
)
148 ;; Strange permissions.
149 ;; Fall through to real state computation.
150 (vc-sccs-state file
))))
151 (vc-sccs-state file
)))
153 (defun vc-sccs-dir-status (dir update-function
)
154 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
155 ;; is not TRTD because it returns files from multiple backends.
156 ;; It should also return 'unregistered files.
158 ;; Doing lots of individual VC-state calls is painful, but
159 ;; there is no better option in SCCS-land.
160 (let ((flist (vc-expand-dirs (list dir
)))
163 (let ((state (vc-state file
))
164 (frel (file-relative-name file
)))
165 (when (and (eq (vc-backend file
) 'SCCS
)
166 (not (eq state
'up-to-date
)))
167 (push (list frel state
) result
))))
168 (funcall update-function result
)))
170 (defun vc-sccs-working-revision (file)
171 "SCCS-specific version of `vc-working-revision'."
173 ;; The working revision is always the latest revision number.
174 ;; To find this number, search the entire delta table,
175 ;; rather than just the first entry, because the
176 ;; first entry might be a deleted ("R") revision.
177 (vc-insert-file (vc-name file
) "^\001e\n\001[^s]")
178 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
180 (defun vc-sccs-workfile-unchanged-p (file)
181 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
182 (zerop (apply 'vc-do-command
"*vc*" 1 "vcdiff" (vc-name file
)
184 (concat "-r" (vc-working-revision file
))))))
188 ;;; State-changing functions
191 (defun vc-sccs-do-command (buffer okstatus command file-or-list
&rest flags
)
192 ;; (let ((load-path (append vc-sccs-path load-path)))
193 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
194 (apply 'vc-do-command
(or buffer
"*vc*") okstatus
"sccs" file-or-list command flags
))
196 (defun vc-sccs-create-repo ()
197 "Create a new SCCS repository."
198 ;; SCCS is totally file-oriented, so all we have to do is make the directory
199 (make-directory "SCCS"))
201 (defun vc-sccs-register (files &optional rev comment
)
202 "Register FILES into the SCCS version-control system.
203 REV is the optional revision number for the file. COMMENT can be used
204 to provide an initial description of FILES.
205 Passes either `vc-sccs-register-switches' or `vc-register-switches'
208 Automatically retrieve a read-only version of the files with keywords
209 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
211 (let* ((dirname (or (file-name-directory file
) ""))
212 (basename (file-name-nondirectory file
))
213 (project-file (vc-sccs-search-project-dir dirname basename
)))
216 (format (car vc-sccs-master-templates
) dirname basename
))))
217 (apply 'vc-sccs-do-command nil
0 "admin" vc-name
218 (and rev
(not (string= rev
"")) (concat "-r" rev
))
220 (concat "-i" (file-relative-name file
))
221 (and comment
(concat "-y" comment
))
222 (vc-switches 'SCCS
'register
)))
224 (if vc-keep-workfiles
225 (vc-sccs-do-command nil
0 "get" (vc-name file
))))))
227 (defun vc-sccs-responsible-p (file)
228 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
229 ;; TODO: check for all the patterns in vc-sccs-master-templates
230 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file
)))
231 (stringp (vc-sccs-search-project-dir (or (file-name-directory file
) "")
232 (file-name-nondirectory file
)))))
234 (defun vc-sccs-checkin (files rev comment
)
235 "SCCS-specific version of `vc-backend-checkin'."
236 (dolist (file (vc-expand-dirs files
))
237 (apply 'vc-sccs-do-command nil
0 "delta" (vc-name file
)
238 (if rev
(concat "-r" rev
))
239 (concat "-y" comment
)
240 (vc-switches 'SCCS
'checkin
))
241 (if vc-keep-workfiles
242 (vc-sccs-do-command nil
0 "get" (vc-name file
)))))
244 (defun vc-sccs-find-revision (file rev buffer
)
245 (apply 'vc-sccs-do-command
246 buffer
0 "get" (vc-name file
)
247 "-s" ;; suppress diagnostic output
251 (vc-sccs-lookup-triple file rev
)))
252 (vc-switches 'SCCS
'checkout
)))
254 (defun vc-sccs-checkout (file &optional editable rev
)
255 "Retrieve a copy of a saved revision of SCCS controlled FILE.
256 If FILE is a directory, all version-controlled files beneath are checked out.
257 EDITABLE non-nil means that the file should be writable and
258 locked. REV is the revision to check out."
259 (if (file-directory-p file
)
260 (mapc 'vc-sccs-checkout
(vc-expand-dirs (list file
)))
261 (let ((file-buffer (get-file-buffer file
))
263 (message "Checking out %s..." file
)
265 ;; Change buffers to get local value of vc-checkout-switches.
266 (if file-buffer
(set-buffer file-buffer
))
267 (setq switches
(vc-switches 'SCCS
'checkout
))
268 ;; Save this buffer's default-directory
269 ;; and use save-excursion to make sure it is restored
270 ;; in the same buffer it was saved in.
271 (let ((default-directory default-directory
))
273 ;; Adjust the default-directory so that the check-out creates
274 ;; the file in the right place.
275 (setq default-directory
(file-name-directory file
))
277 (and rev
(or (string= rev
"")
280 (apply 'vc-sccs-do-command nil
0 "get" (vc-name file
)
282 (and rev
(concat "-r" (vc-sccs-lookup-triple file rev
)))
284 (message "Checking out %s...done" file
))))
286 (defun vc-sccs-rollback (files)
287 "Roll back, undoing the most recent checkins of FILES. Directories
288 are expanded to all version-controlled subfiles."
289 (setq files
(vc-expand-dirs files
))
291 (error "SCCS backend doesn't support directory-level rollback."))
293 (let ((discard (vc-working-revision file
)))
294 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
297 (message "Removing revision %s from %s..." discard file
)
298 (vc-sccs-do-command nil
0 "rmdel"
299 (vc-name file
) (concat "-r" discard
))
300 (vc-sccs-do-command nil
0 "get" (vc-name file
) nil
))))
302 (defun vc-sccs-revert (file &optional contents-done
)
303 "Revert FILE to the version it was based on. If FILE is a directory,
304 revert all subfiles."
305 (if (file-directory-p file
)
306 (mapc 'vc-sccs-revert
(vc-expand-dirs (list file
)))
307 (vc-sccs-do-command nil
0 "unget" (vc-name file
))
308 (vc-sccs-do-command nil
0 "get" (vc-name file
))
309 ;; Checking out explicit revisions is not supported under SCCS, yet.
310 ;; We always "revert" to the latest revision; therefore
311 ;; vc-working-revision is cleared here so that it gets recomputed.
312 (vc-file-setprop file
'vc-working-revision nil
)))
314 (defun vc-sccs-steal-lock (file &optional rev
)
315 "Steal the lock on the current workfile for FILE and revision REV."
316 (if (file-directory-p file
)
317 (mapc 'vc-sccs-steal-lock
(vc-expand-dirs (list file
)))
318 (vc-sccs-do-command nil
0 "unget"
319 (vc-name file
) "-n" (if rev
(concat "-r" rev
)))
320 (vc-sccs-do-command nil
0 "get"
321 (vc-name file
) "-g" (if rev
(concat "-r" rev
)))))
323 (defun vc-sccs-modify-change-comment (files rev comment
)
324 "Modify (actually, append to) the change comments for FILES on a specified REV."
325 (dolist (file (vc-expand-dirs files
))
326 (vc-sccs-do-command nil
0 "cdc" (vc-name file
)
327 (concat "-y" comment
) (concat "-r" rev
))))
331 ;;; History functions
334 (defun vc-sccs-print-log (files &optional buffer shortlog
)
335 "Get change log associated with FILES."
336 (setq files
(vc-expand-dirs files
))
337 (vc-sccs-do-command buffer
0 "prs" (mapcar 'vc-name files
)))
339 (defun vc-sccs-diff (files &optional oldvers newvers buffer
)
340 "Get a difference report using SCCS between two filesets."
341 (setq files
(vc-expand-dirs files
))
342 (setq oldvers
(vc-sccs-lookup-triple (car files
) oldvers
))
343 (setq newvers
(vc-sccs-lookup-triple (car files
) newvers
))
344 (apply 'vc-do-command
(or buffer
"*vc-diff*")
345 1 "vcdiff" (mapcar 'vc-name
(vc-expand-dirs files
))
347 (and oldvers
(concat "-r" oldvers
))
348 (and newvers
(concat "-r" newvers
)))
349 (vc-switches 'SCCS
'diff
))))
353 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
354 ;;; our own set of name-to-revision mappings.
357 (defun vc-sccs-create-tag (backend dir name branchp
)
359 (error "SCCS backend %s does not support module branches" backend
))
360 (let ((result (vc-tag-precondition dir
)))
362 (error "File %s is not up-to-date" result
)
366 (vc-sccs-add-triple name f
(vc-working-revision f
)))))))
373 (defun vc-sccs-previous-revision (file rev
)
374 (vc-call-backend 'RCS
'previous-revision file rev
))
376 (defun vc-sccs-next-revision (file rev
)
377 (vc-call-backend 'RCS
'next-revision file rev
))
379 (defun vc-sccs-check-headers ()
380 "Check if the current file has any headers in it."
382 (goto-char (point-min))
383 (re-search-forward "%[A-Z]%" nil t
)))
385 (defun vc-sccs-rename-file (old new
)
386 ;; Move the master file (using vc-rcs-master-templates).
387 (vc-rename-master (vc-name old
) new vc-sccs-master-templates
)
388 ;; Update the tag file.
391 (expand-file-name vc-sccs-name-assoc-file
392 (file-name-directory (vc-name old
))))
393 (goto-char (point-min))
394 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
395 (while (re-search-forward (concat ":" (regexp-quote old
) "$") nil t
)
396 (replace-match (concat ":" new
) nil nil
))
398 (kill-buffer (current-buffer))))
400 (defun vc-sccs-find-file-hook ()
401 ;; If the file is locked by some other user, make
402 ;; the buffer read-only. Like this, even root
403 ;; cannot modify a file that someone else has locked.
404 (and (stringp (vc-state buffer-file-name
'SCCS
))
405 (setq buffer-read-only t
)))
409 ;;; Internal functions
412 ;; This function is wrapped with `progn' so that the autoload cookie
413 ;; copies the whole function itself into loaddefs.el rather than just placing
414 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
415 ;; help us avoid loading vc-sccs.
417 (progn (defun vc-sccs-search-project-dir (dirname basename
)
418 "Return the name of a master file in the SCCS project directory.
419 Does not check whether the file exists but returns nil if it does not
420 find any project directory."
421 (let ((project-dir (getenv "PROJECTDIR")) dirs dir
)
423 (if (file-name-absolute-p project-dir
)
424 (setq dirs
'("SCCS" ""))
425 (setq dirs
'("src/SCCS" "src" "source/SCCS" "source"))
426 (setq project-dir
(expand-file-name (concat "~" project-dir
))))
427 (while (and (not dir
) dirs
)
428 (setq dir
(expand-file-name (car dirs
) project-dir
))
429 (unless (file-directory-p dir
)
431 (setq dirs
(cdr dirs
))))
432 (and dir
(expand-file-name (concat "s." basename
) dir
))))))
434 (defun vc-sccs-lock-file (file)
435 "Generate lock file name corresponding to FILE."
436 (let ((master (vc-name file
)))
439 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master
)
440 (replace-match "p." t t master
2))))
442 (defun vc-sccs-parse-locks ()
443 "Parse SCCS locks in current buffer.
444 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
446 (goto-char (point-min))
447 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
450 (cons (cons (match-string 1) (match-string 2)) master-locks
)))
451 ;; FIXME: is it really necessary to reverse ?
452 (nreverse master-locks
)))
454 (defun vc-sccs-add-triple (name file rev
)
457 (expand-file-name vc-sccs-name-assoc-file
458 (file-name-directory (vc-name file
))))
459 (goto-char (point-max))
460 (insert name
"\t:\t" file
"\t" rev
"\n")
462 (kill-buffer (current-buffer))))
464 (defun vc-sccs-lookup-triple (file name
)
465 "Return the numeric revision corresponding to a named tag of FILE.
466 If NAME is nil or a revision number string it's just passed through."
468 (let ((firstchar (aref name
0)))
469 (and (>= firstchar ?
0) (<= firstchar ?
9))))
473 (expand-file-name vc-sccs-name-assoc-file
474 (file-name-directory (vc-name file
))))
475 (vc-parse-buffer (concat name
"\t:\t" file
"\t\\(.+\\)") 1))))
479 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
480 ;;; vc-sccs.el ends here