(format-alist, format-write-file): Doc fix.
[emacs.git] / lisp / vc-sccs.el
blobb8957b94c386ba9719f68293d8f9c1f5aa646312
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
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/>.
25 ;;; Commentary:
27 ;;; Code:
29 (eval-when-compile
30 (require 'vc))
32 ;;;
33 ;;; Customization options
34 ;;;
36 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
37 ;; (defcustom vc-sccs-path
38 ;; (let ((path ()))
39 ;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
40 ;; (if (file-directory-p dir)
41 ;; (push dir path)))
42 ;; path)
43 ;; "List of extra directories to search for SCCS commands."
44 ;; :type '(repeat directory)
45 ;; :group 'vc)
47 (defcustom vc-sccs-register-switches nil
48 "*Extra switches for registering a file in SCCS.
49 A string or list of strings passed to the checkin program by
50 \\[vc-sccs-register]."
51 :type '(choice (const :tag "None" nil)
52 (string :tag "Argument String")
53 (repeat :tag "Argument List"
54 :value ("")
55 string))
56 :version "21.1"
57 :group 'vc)
59 (defcustom vc-sccs-diff-switches nil
60 "*A string or list of strings specifying extra switches for `vcdiff',
61 the diff utility used for SCCS under VC."
62 :type '(choice (const :tag "None" nil)
63 (string :tag "Argument String")
64 (repeat :tag "Argument List"
65 :value ("")
66 string))
67 :version "21.1"
68 :group 'vc)
70 (defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
71 "*Header keywords to be inserted by `vc-insert-headers'."
72 :type '(repeat string)
73 :group 'vc)
75 ;;;###autoload
76 (defcustom vc-sccs-master-templates
77 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir)
78 "*Where to look for SCCS master files.
79 For a description of possible values, see `vc-check-master-templates'."
80 :type '(choice (const :tag "Use standard SCCS file names"
81 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
82 (repeat :tag "User-specified"
83 (choice string
84 function)))
85 :version "21.1"
86 :group 'vc)
89 ;;;
90 ;;; Internal variables
91 ;;;
93 (defconst vc-sccs-name-assoc-file "VC-names")
96 ;;; Properties of the backend
98 (defun vc-sccs-revision-granularity () 'file)
99 (defun vc-sccs-checkout-model (files) 'locking)
102 ;;; State-querying functions
105 ;; The autoload cookie below places vc-sccs-registered directly into
106 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
107 ;; every file that is visited. The definition is repeated below
108 ;; so that Help and etags can find it.
110 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
111 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
113 (defun vc-sccs-state (file)
114 "SCCS-specific function to compute the version control state."
115 (if (not (vc-sccs-registered file))
116 'unregistered
117 (with-temp-buffer
118 (if (vc-insert-file (vc-sccs-lock-file file))
119 (let* ((locks (vc-sccs-parse-locks))
120 (working-revision (vc-working-revision file))
121 (locking-user (cdr (assoc working-revision locks))))
122 (if (not locking-user)
123 (if (vc-workfile-unchanged-p file)
124 'up-to-date
125 'unlocked-changes)
126 (if (string= locking-user (vc-user-login-name file))
127 'edited
128 locking-user)))
129 'up-to-date))))
131 (defun vc-sccs-state-heuristic (file)
132 "SCCS-specific state heuristic."
133 (if (not (vc-mistrust-permissions file))
134 ;; This implementation assumes that any file which is under version
135 ;; control and has -rw-r--r-- is locked by its owner. This is true
136 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
137 ;; We have to be careful not to exclude files with execute bits on;
138 ;; scripts can be under version control too. Also, we must ignore the
139 ;; group-read and other-read bits, since paranoid users turn them off.
140 (let* ((attributes (file-attributes file 'string))
141 (owner-name (nth 2 attributes))
142 (permissions (nth 8 attributes)))
143 (if (string-match ".r-..-..-." permissions)
144 'up-to-date
145 (if (string-match ".rw..-..-." permissions)
146 (if (file-ownership-preserved-p file)
147 'edited
148 owner-name)
149 ;; Strange permissions.
150 ;; Fall through to real state computation.
151 (vc-sccs-state file))))
152 (vc-sccs-state file)))
154 (defun vc-sccs-dir-status (dir update-function)
155 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
156 ;; is not TRTD because it returns files from multiple backends.
157 ;; It should also return 'unregistered files.
159 ;; Doing lots of individual VC-state calls is painful, but
160 ;; there is no better option in SCCS-land.
161 (let ((flist (vc-expand-dirs (list dir)))
162 (result nil))
163 (dolist (file flist)
164 (let ((state (vc-state file))
165 (frel (file-relative-name file)))
166 (when (and (eq (vc-backend file) 'SCCS)
167 (not (eq state 'up-to-date)))
168 (push (list frel state) result))))
169 (funcall update-function result)))
171 (defun vc-sccs-working-revision (file)
172 "SCCS-specific version of `vc-working-revision'."
173 (with-temp-buffer
174 ;; The working revision is always the latest revision number.
175 ;; To find this number, search the entire delta table,
176 ;; rather than just the first entry, because the
177 ;; first entry might be a deleted ("R") revision.
178 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
179 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
181 (defun vc-sccs-workfile-unchanged-p (file)
182 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
183 (zerop (apply 'vc-do-command "*vc*" 1 "vcdiff" (vc-name file)
184 (list "--brief" "-q"
185 (concat "-r" (vc-working-revision file))))))
189 ;;; State-changing functions
192 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
193 ;; (let ((load-path (append vc-sccs-path load-path)))
194 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
195 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
197 (defun vc-sccs-create-repo ()
198 "Create a new SCCS repository."
199 ;; SCCS is totally file-oriented, so all we have to do is make the directory
200 (make-directory "SCCS"))
202 (defun vc-sccs-register (files &optional rev comment)
203 "Register FILES into the SCCS version-control system.
204 REV is the optional revision number for the file. COMMENT can be used
205 to provide an initial description of FILES.
207 `vc-register-switches' and `vc-sccs-register-switches' are passed to
208 the SCCS command (in that order).
210 Automatically retrieve a read-only version of the files with keywords
211 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
212 (dolist (file files)
213 (let* ((dirname (or (file-name-directory file) ""))
214 (basename (file-name-nondirectory file))
215 (project-file (vc-sccs-search-project-dir dirname basename)))
216 (let ((vc-name
217 (or project-file
218 (format (car vc-sccs-master-templates) dirname basename))))
219 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
220 (and rev (not (string= rev "")) (concat "-r" rev))
221 "-fb"
222 (concat "-i" (file-relative-name file))
223 (and comment (concat "-y" comment))
224 (vc-switches 'SCCS 'register)))
225 (delete-file file)
226 (if vc-keep-workfiles
227 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
229 (defun vc-sccs-responsible-p (file)
230 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
231 ;; TODO: check for all the patterns in vc-sccs-master-templates
232 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
233 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
234 (file-name-nondirectory file)))))
236 (defun vc-sccs-checkin (files rev comment)
237 "SCCS-specific version of `vc-backend-checkin'."
238 (dolist (file (vc-expand-dirs files))
239 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
240 (if rev (concat "-r" rev))
241 (concat "-y" comment)
242 (vc-switches 'SCCS 'checkin))
243 (if vc-keep-workfiles
244 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
246 (defun vc-sccs-find-revision (file rev buffer)
247 (apply 'vc-sccs-do-command
248 buffer 0 "get" (vc-name file)
249 "-s" ;; suppress diagnostic output
250 "-p"
251 (and rev
252 (concat "-r"
253 (vc-sccs-lookup-triple file rev)))
254 (vc-switches 'SCCS 'checkout)))
256 (defun vc-sccs-checkout (file &optional editable rev)
257 "Retrieve a copy of a saved revision of SCCS controlled FILE.
258 If FILE is a directory, all version-controlled files beneath are checked out.
259 EDITABLE non-nil means that the file should be writable and
260 locked. REV is the revision to check out."
261 (if (file-directory-p file)
262 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
263 (let ((file-buffer (get-file-buffer file))
264 switches)
265 (message "Checking out %s..." file)
266 (save-excursion
267 ;; Change buffers to get local value of vc-checkout-switches.
268 (if file-buffer (set-buffer file-buffer))
269 (setq switches (vc-switches 'SCCS 'checkout))
270 ;; Save this buffer's default-directory
271 ;; and use save-excursion to make sure it is restored
272 ;; in the same buffer it was saved in.
273 (let ((default-directory default-directory))
274 (save-excursion
275 ;; Adjust the default-directory so that the check-out creates
276 ;; the file in the right place.
277 (setq default-directory (file-name-directory file))
279 (and rev (or (string= rev "")
280 (not (stringp rev)))
281 (setq rev nil))
282 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
283 (if editable "-e")
284 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
285 switches))))
286 (message "Checking out %s...done" file))))
288 (defun vc-sccs-rollback (files)
289 "Roll back, undoing the most recent checkins of FILES. Directories
290 are expanded to all version-controlled subfiles."
291 (setq files (vc-expand-dirs files))
292 (if (not files)
293 (error "SCCS backend doesn't support directory-level rollback."))
294 (dolist (file files)
295 (let ((discard (vc-working-revision file)))
296 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
297 discard file)))
298 (error "Aborted"))
299 (message "Removing revision %s from %s..." discard file)
300 (vc-sccs-do-command nil 0 "rmdel"
301 (vc-name file) (concat "-r" discard))
302 (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
304 (defun vc-sccs-revert (file &optional contents-done)
305 "Revert FILE to the version it was based on. If FILE is a directory,
306 revert all subfiles."
307 (if (file-directory-p file)
308 (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
309 (vc-sccs-do-command nil 0 "unget" (vc-name file))
310 (vc-sccs-do-command nil 0 "get" (vc-name file))
311 ;; Checking out explicit revisions is not supported under SCCS, yet.
312 ;; We always "revert" to the latest revision; therefore
313 ;; vc-working-revision is cleared here so that it gets recomputed.
314 (vc-file-setprop file 'vc-working-revision nil)))
316 (defun vc-sccs-steal-lock (file &optional rev)
317 "Steal the lock on the current workfile for FILE and revision REV."
318 (if (file-directory-p file)
319 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
320 (vc-sccs-do-command nil 0 "unget"
321 (vc-name file) "-n" (if rev (concat "-r" rev)))
322 (vc-sccs-do-command nil 0 "get"
323 (vc-name file) "-g" (if rev (concat "-r" rev)))))
325 (defun vc-sccs-modify-change-comment (files rev comment)
326 "Modify (actually, append to) the change comments for FILES on a specified REV."
327 (dolist (file (vc-expand-dirs files))
328 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
329 (concat "-y" comment) (concat "-r" rev))))
333 ;;; History functions
336 (defun vc-sccs-print-log (files &optional buffer)
337 "Get change log associated with FILES."
338 (setq files (vc-expand-dirs files))
339 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files)))
341 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
342 "Get a difference report using SCCS between two filesets."
343 (setq files (vc-expand-dirs files))
344 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
345 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
346 (apply 'vc-do-command (or buffer "*vc-diff*")
347 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
348 (append (list "-q"
349 (and oldvers (concat "-r" oldvers))
350 (and newvers (concat "-r" newvers)))
351 (vc-switches 'SCCS 'diff))))
355 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
356 ;;; our own set of name-to-revision mappings.
359 (defun vc-sccs-create-tag (backend dir name branchp)
360 (when branchp
361 (error "SCCS backend %s does not support module branches" backend))
362 (let ((result (vc-tag-precondition dir)))
363 (if (stringp result)
364 (error "File %s is not up-to-date" result)
365 (vc-file-tree-walk
367 (lambda (f)
368 (vc-sccs-add-triple name f (vc-working-revision f)))))))
372 ;;; Miscellaneous
375 (defun vc-sccs-check-headers ()
376 "Check if the current file has any headers in it."
377 (save-excursion
378 (goto-char (point-min))
379 (re-search-forward "%[A-Z]%" nil t)))
381 (defun vc-sccs-rename-file (old new)
382 ;; Move the master file (using vc-rcs-master-templates).
383 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
384 ;; Update the tag file.
385 (with-current-buffer
386 (find-file-noselect
387 (expand-file-name vc-sccs-name-assoc-file
388 (file-name-directory (vc-name old))))
389 (goto-char (point-min))
390 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
391 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
392 (replace-match (concat ":" new) nil nil))
393 (basic-save-buffer)
394 (kill-buffer (current-buffer))))
398 ;;; Internal functions
401 ;; This function is wrapped with `progn' so that the autoload cookie
402 ;; copies the whole function itself into loaddefs.el rather than just placing
403 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
404 ;; help us avoid loading vc-sccs.
405 ;;;###autoload
406 (progn (defun vc-sccs-search-project-dir (dirname basename)
407 "Return the name of a master file in the SCCS project directory.
408 Does not check whether the file exists but returns nil if it does not
409 find any project directory."
410 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
411 (when project-dir
412 (if (file-name-absolute-p project-dir)
413 (setq dirs '("SCCS" ""))
414 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
415 (setq project-dir (expand-file-name (concat "~" project-dir))))
416 (while (and (not dir) dirs)
417 (setq dir (expand-file-name (car dirs) project-dir))
418 (unless (file-directory-p dir)
419 (setq dir nil)
420 (setq dirs (cdr dirs))))
421 (and dir (expand-file-name (concat "s." basename) dir))))))
423 (defun vc-sccs-lock-file (file)
424 "Generate lock file name corresponding to FILE."
425 (let ((master (vc-name file)))
426 (and
427 master
428 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
429 (replace-match "p." t t master 2))))
431 (defun vc-sccs-parse-locks ()
432 "Parse SCCS locks in current buffer.
433 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
434 (let (master-locks)
435 (goto-char (point-min))
436 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
437 nil t)
438 (setq master-locks
439 (cons (cons (match-string 1) (match-string 2)) master-locks)))
440 ;; FIXME: is it really necessary to reverse ?
441 (nreverse master-locks)))
443 (defun vc-sccs-add-triple (name file rev)
444 (with-current-buffer
445 (find-file-noselect
446 (expand-file-name vc-sccs-name-assoc-file
447 (file-name-directory (vc-name file))))
448 (goto-char (point-max))
449 (insert name "\t:\t" file "\t" rev "\n")
450 (basic-save-buffer)
451 (kill-buffer (current-buffer))))
453 (defun vc-sccs-lookup-triple (file name)
454 "Return the numeric revision corresponding to a named tag of FILE.
455 If NAME is nil or a revision number string it's just passed through."
456 (if (or (null name)
457 (let ((firstchar (aref name 0)))
458 (and (>= firstchar ?0) (<= firstchar ?9))))
459 name
460 (with-temp-buffer
461 (vc-insert-file
462 (expand-file-name vc-sccs-name-assoc-file
463 (file-name-directory (vc-name file))))
464 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
466 (provide 'vc-sccs)
468 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
469 ;;; vc-sccs.el ends here