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, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: FSF (see vc.el for full credits)
8 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
29 ;; to be installed somewhere on Emacs's path for executables.
38 ;;; Customization options
41 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
42 ;; (defcustom vc-sccs-path
44 ;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
45 ;; (if (file-directory-p dir)
48 ;; "List of extra directories to search for SCCS commands."
49 ;; :type '(repeat directory)
52 (defcustom vc-sccs-register-switches nil
53 "Switches for registering a file in SCCS.
54 A string or list of strings passed to the checkin program by
55 \\[vc-register]. If nil, use the value of `vc-register-switches'.
56 If t, use no switches."
57 :type
'(choice (const :tag
"Unspecified" nil
)
59 (string :tag
"Argument String")
60 (repeat :tag
"Argument List" :value
("") string
))
64 (defcustom vc-sccs-diff-switches nil
65 "String or list of strings specifying switches for SCCS diff under VC.
66 If nil, use the value of `vc-diff-switches'. If t, use no switches."
67 :type
'(choice (const :tag
"Unspecified" nil
)
69 (string :tag
"Argument String")
70 (repeat :tag
"Argument List" :value
("") string
))
74 (defcustom vc-sccs-header
(or (cdr (assoc 'SCCS vc-header-alist
)) '("%W%"))
75 "Header keywords to be inserted by `vc-insert-headers'."
76 :type
'(repeat string
)
80 (defcustom vc-sccs-master-templates
81 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
))
82 "Where to look for SCCS master files.
83 For a description of possible values, see `vc-check-master-templates'."
84 :type
'(choice (const :tag
"Use standard SCCS file names"
85 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir
))
86 (repeat :tag
"User-specified"
94 ;;; Internal variables
97 (defconst vc-sccs-name-assoc-file
"VC-names")
100 ;;; Properties of the backend
102 (defun vc-sccs-revision-granularity () 'file
)
103 (defun vc-sccs-checkout-model (files) 'locking
)
106 ;;; State-querying functions
109 ;; The autoload cookie below places vc-sccs-registered directly into
110 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
111 ;; every file that is visited. The definition is repeated below
112 ;; so that Help and etags can find it.
114 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
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-workfile-unchanged-p file
)
130 (if (string= locking-user
(vc-user-login-name file
))
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
)
149 (if (string-match ".rw..-..-." permissions
)
150 (if (file-ownership-preserved-p file
)
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
)))
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'."
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 (defun vc-sccs-workfile-unchanged-p (file)
186 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
187 (zerop (apply 'vc-do-command
"*vc*" 1 "vcdiff" (vc-name file
)
189 (concat "-r" (vc-working-revision file
))))))
193 ;;; State-changing functions
196 (defun vc-sccs-do-command (buffer okstatus command file-or-list
&rest flags
)
197 ;; (let ((load-path (append vc-sccs-path load-path)))
198 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
199 (apply 'vc-do-command
(or buffer
"*vc*") okstatus
"sccs" file-or-list command flags
))
201 (defun vc-sccs-create-repo ()
202 "Create a new SCCS repository."
203 ;; SCCS is totally file-oriented, so all we have to do is make the directory
204 (make-directory "SCCS"))
206 (defun vc-sccs-register (files &optional rev comment
)
207 "Register FILES into the SCCS version-control system.
208 REV is the optional revision number for the file. COMMENT can be used
209 to provide an initial description of FILES.
210 Passes either `vc-sccs-register-switches' or `vc-register-switches'
213 Automatically retrieve a read-only version of the files with keywords
214 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
216 (let* ((dirname (or (file-name-directory file
) ""))
217 (basename (file-name-nondirectory file
))
218 (project-file (vc-sccs-search-project-dir dirname basename
)))
221 (format (car vc-sccs-master-templates
) dirname basename
))))
222 (apply 'vc-sccs-do-command nil
0 "admin" vc-name
223 (and rev
(not (string= rev
"")) (concat "-r" rev
))
225 (concat "-i" (file-relative-name file
))
226 (and comment
(concat "-y" comment
))
227 (vc-switches 'SCCS
'register
)))
229 (if vc-keep-workfiles
230 (vc-sccs-do-command nil
0 "get" (vc-name file
))))))
232 (defun vc-sccs-responsible-p (file)
233 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
234 ;; TODO: check for all the patterns in vc-sccs-master-templates
235 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file
)))
236 (stringp (vc-sccs-search-project-dir (or (file-name-directory file
) "")
237 (file-name-nondirectory file
)))))
239 (defun vc-sccs-checkin (files rev comment
&optional extra-args-ignored
)
240 "SCCS-specific version of `vc-backend-checkin'."
241 (dolist (file (vc-expand-dirs files
))
242 (apply 'vc-sccs-do-command nil
0 "delta" (vc-name file
)
243 (if rev
(concat "-r" rev
))
244 (concat "-y" comment
)
245 (vc-switches 'SCCS
'checkin
))
246 (if vc-keep-workfiles
247 (vc-sccs-do-command nil
0 "get" (vc-name file
)))))
249 (defun vc-sccs-find-revision (file rev buffer
)
250 (apply 'vc-sccs-do-command
251 buffer
0 "get" (vc-name file
)
252 "-s" ;; suppress diagnostic output
256 (vc-sccs-lookup-triple file rev
)))
257 (vc-switches 'SCCS
'checkout
)))
259 (defun vc-sccs-checkout (file &optional editable rev
)
260 "Retrieve a copy of a saved revision of SCCS controlled FILE.
261 If FILE is a directory, all version-controlled files beneath are checked out.
262 EDITABLE non-nil means that the file should be writable and
263 locked. REV is the revision to check out."
264 (if (file-directory-p file
)
265 (mapc 'vc-sccs-checkout
(vc-expand-dirs (list file
)))
266 (let ((file-buffer (get-file-buffer file
))
268 (message "Checking out %s..." file
)
270 ;; Change buffers to get local value of vc-checkout-switches.
271 (if file-buffer
(set-buffer file-buffer
))
272 (setq switches
(vc-switches 'SCCS
'checkout
))
273 ;; Save this buffer's default-directory
274 ;; and use save-excursion to make sure it is restored
275 ;; in the same buffer it was saved in.
276 (let ((default-directory default-directory
))
278 ;; Adjust the default-directory so that the check-out creates
279 ;; the file in the right place.
280 (setq default-directory
(file-name-directory file
))
282 (and rev
(or (string= rev
"")
285 (apply 'vc-sccs-do-command nil
0 "get" (vc-name file
)
287 (and rev
(concat "-r" (vc-sccs-lookup-triple file rev
)))
289 (message "Checking out %s...done" file
))))
291 (defun vc-sccs-rollback (files)
292 "Roll back, undoing the most recent checkins of FILES. Directories
293 are expanded to all version-controlled subfiles."
294 (setq files
(vc-expand-dirs files
))
296 (error "SCCS backend doesn't support directory-level rollback"))
298 (let ((discard (vc-working-revision file
)))
299 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
302 (message "Removing revision %s from %s..." discard file
)
303 (vc-sccs-do-command nil
0 "rmdel"
304 (vc-name file
) (concat "-r" discard
))
305 (vc-sccs-do-command nil
0 "get" (vc-name file
) nil
))))
307 (defun vc-sccs-revert (file &optional contents-done
)
308 "Revert FILE to the version it was based on. If FILE is a directory,
309 revert all subfiles."
310 (if (file-directory-p file
)
311 (mapc 'vc-sccs-revert
(vc-expand-dirs (list file
)))
312 (vc-sccs-do-command nil
0 "unget" (vc-name file
))
313 (vc-sccs-do-command nil
0 "get" (vc-name file
))
314 ;; Checking out explicit revisions is not supported under SCCS, yet.
315 ;; We always "revert" to the latest revision; therefore
316 ;; vc-working-revision is cleared here so that it gets recomputed.
317 (vc-file-setprop file
'vc-working-revision nil
)))
319 (defun vc-sccs-steal-lock (file &optional rev
)
320 "Steal the lock on the current workfile for FILE and revision REV."
321 (if (file-directory-p file
)
322 (mapc 'vc-sccs-steal-lock
(vc-expand-dirs (list file
)))
323 (vc-sccs-do-command nil
0 "unget"
324 (vc-name file
) "-n" (if rev
(concat "-r" rev
)))
325 (vc-sccs-do-command nil
0 "get"
326 (vc-name file
) "-g" (if rev
(concat "-r" rev
)))))
328 (defun vc-sccs-modify-change-comment (files rev comment
)
329 "Modify (actually, append to) the change comments for FILES on a specified REV."
330 (dolist (file (vc-expand-dirs files
))
331 (vc-sccs-do-command nil
0 "cdc" (vc-name file
)
332 (concat "-y" comment
) (concat "-r" rev
))))
336 ;;; History functions
339 (defun vc-sccs-print-log (files buffer
&optional shortlog start-revision-ignored limit
)
340 "Get change log associated with FILES."
341 (setq files
(vc-expand-dirs files
))
342 (vc-sccs-do-command buffer
0 "prs" (mapcar 'vc-name files
))
343 (when limit
'limit-unsupported
))
345 (defun vc-sccs-diff (files &optional oldvers newvers buffer
)
346 "Get a difference report using SCCS between two filesets."
347 (setq files
(vc-expand-dirs files
))
348 (setq oldvers
(vc-sccs-lookup-triple (car files
) oldvers
))
349 (setq newvers
(vc-sccs-lookup-triple (car files
) newvers
))
350 (apply 'vc-do-command
(or buffer
"*vc-diff*")
351 1 "vcdiff" (mapcar 'vc-name
(vc-expand-dirs files
))
353 (and oldvers
(concat "-r" oldvers
))
354 (and newvers
(concat "-r" newvers
)))
355 (vc-switches 'SCCS
'diff
))))
359 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
360 ;;; our own set of name-to-revision mappings.
363 (defun vc-sccs-create-tag (backend dir name branchp
)
365 (error "SCCS backend %s does not support module branches" backend
))
366 (let ((result (vc-tag-precondition dir
)))
368 (error "File %s is not up-to-date" result
)
372 (vc-sccs-add-triple name f
(vc-working-revision f
)))))))
379 (defun vc-sccs-previous-revision (file rev
)
380 (vc-call-backend 'RCS
'previous-revision file rev
))
382 (defun vc-sccs-next-revision (file rev
)
383 (vc-call-backend 'RCS
'next-revision file rev
))
385 (defun vc-sccs-check-headers ()
386 "Check if the current file has any headers in it."
388 (goto-char (point-min))
389 (re-search-forward "%[A-Z]%" nil t
)))
391 (defun vc-sccs-rename-file (old new
)
392 ;; Move the master file (using vc-rcs-master-templates).
393 (vc-rename-master (vc-name old
) new vc-sccs-master-templates
)
394 ;; Update the tag file.
397 (expand-file-name vc-sccs-name-assoc-file
398 (file-name-directory (vc-name old
))))
399 (goto-char (point-min))
400 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
401 (while (re-search-forward (concat ":" (regexp-quote old
) "$") nil t
)
402 (replace-match (concat ":" new
) nil nil
))
404 (kill-buffer (current-buffer))))
406 (defun vc-sccs-find-file-hook ()
407 ;; If the file is locked by some other user, make
408 ;; the buffer read-only. Like this, even root
409 ;; cannot modify a file that someone else has locked.
410 (and (stringp (vc-state buffer-file-name
'SCCS
))
411 (setq buffer-read-only t
)))
415 ;;; Internal functions
418 ;; This function is wrapped with `progn' so that the autoload cookie
419 ;; copies the whole function itself into loaddefs.el rather than just placing
420 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
421 ;; help us avoid loading vc-sccs.
423 (progn (defun vc-sccs-search-project-dir (dirname basename
)
424 "Return the name of a master file in the SCCS project directory.
425 Does not check whether the file exists but returns nil if it does not
426 find any project directory."
427 (let ((project-dir (getenv "PROJECTDIR")) dirs dir
)
429 (if (file-name-absolute-p project-dir
)
430 (setq dirs
'("SCCS" ""))
431 (setq dirs
'("src/SCCS" "src" "source/SCCS" "source"))
432 (setq project-dir
(expand-file-name (concat "~" project-dir
))))
433 (while (and (not dir
) dirs
)
434 (setq dir
(expand-file-name (car dirs
) project-dir
))
435 (unless (file-directory-p dir
)
437 (setq dirs
(cdr dirs
))))
438 (and dir
(expand-file-name (concat "s." basename
) dir
))))))
440 (defun vc-sccs-lock-file (file)
441 "Generate lock file name corresponding to FILE."
442 (let ((master (vc-name file
)))
445 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master
)
446 (replace-match "p." t t master
2))))
448 (defun vc-sccs-parse-locks ()
449 "Parse SCCS locks in current buffer.
450 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
452 (goto-char (point-min))
453 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
456 (cons (cons (match-string 1) (match-string 2)) master-locks
)))
457 ;; FIXME: is it really necessary to reverse ?
458 (nreverse master-locks
)))
460 (defun vc-sccs-add-triple (name file rev
)
463 (expand-file-name vc-sccs-name-assoc-file
464 (file-name-directory (vc-name file
))))
465 (goto-char (point-max))
466 (insert name
"\t:\t" file
"\t" rev
"\n")
468 (kill-buffer (current-buffer))))
470 (defun vc-sccs-lookup-triple (file name
)
471 "Return the numeric revision corresponding to a named tag of FILE.
472 If NAME is nil or a revision number string it's just passed through."
474 (let ((firstchar (aref name
0)))
475 (and (>= firstchar ?
0) (<= firstchar ?
9))))
479 (expand-file-name vc-sccs-name-assoc-file
480 (file-name-directory (vc-name file
))))
481 (vc-parse-buffer (concat name
"\t:\t" file
"\t\\(.+\\)") 1))))
485 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
486 ;;; vc-sccs.el ends here