Checked anti.texi, errors.texi, and maps.texi.
[emacs.git] / lisp / vc-sccs.el
blob7628a802677d48490936f45d94b25abae0bb5739
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/>.
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 "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)
53 (const :tag "None" t)
54 (string :tag "Argument String")
55 (repeat :tag "Argument List" :value ("") string))
56 :version "21.1"
57 :group 'vc)
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)
63 (const :tag "None" t)
64 (string :tag "Argument String")
65 (repeat :tag "Argument List" :value ("") string))
66 :version "21.1"
67 :group 'vc)
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)
72 :group 'vc)
74 ;;;###autoload
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"
82 (choice string
83 function)))
84 :version "21.1"
85 :group 'vc)
88 ;;;
89 ;;; Internal variables
90 ;;;
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))
115 'unregistered
116 (with-temp-buffer
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)
123 'up-to-date
124 'unlocked-changes)
125 (if (string= locking-user (vc-user-login-name file))
126 'edited
127 locking-user)))
128 'up-to-date))))
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)
143 'up-to-date
144 (if (string-match ".rw..-..-." permissions)
145 (if (file-ownership-preserved-p file)
146 'edited
147 owner-name)
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)))
161 (result nil))
162 (dolist (file flist)
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'."
172 (with-temp-buffer
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)
183 (list "--brief" "-q"
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'
206 to the SCCS command.
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."
210 (dolist (file files)
211 (let* ((dirname (or (file-name-directory file) ""))
212 (basename (file-name-nondirectory file))
213 (project-file (vc-sccs-search-project-dir dirname basename)))
214 (let ((vc-name
215 (or project-file
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))
219 "-fb"
220 (concat "-i" (file-relative-name file))
221 (and comment (concat "-y" comment))
222 (vc-switches 'SCCS 'register)))
223 (delete-file file)
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
248 "-p"
249 (and rev
250 (concat "-r"
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))
262 switches)
263 (message "Checking out %s..." file)
264 (save-excursion
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))
272 (save-excursion
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 "")
278 (not (stringp rev)))
279 (setq rev nil))
280 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
281 (if editable "-e")
282 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
283 switches))))
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))
290 (if (not files)
291 (error "SCCS backend doesn't support directory-level rollback."))
292 (dolist (file files)
293 (let ((discard (vc-working-revision file)))
294 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
295 discard file)))
296 (error "Aborted"))
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)
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))
346 (append (list "-q"
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)
358 (when branchp
359 (error "SCCS backend %s does not support module branches" backend))
360 (let ((result (vc-tag-precondition dir)))
361 (if (stringp result)
362 (error "File %s is not up-to-date" result)
363 (vc-file-tree-walk
365 (lambda (f)
366 (vc-sccs-add-triple name f (vc-working-revision f)))))))
370 ;;; Miscellaneous
373 (defun vc-sccs-check-headers ()
374 "Check if the current file has any headers in it."
375 (save-excursion
376 (goto-char (point-min))
377 (re-search-forward "%[A-Z]%" nil t)))
379 (defun vc-sccs-rename-file (old new)
380 ;; Move the master file (using vc-rcs-master-templates).
381 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
382 ;; Update the tag file.
383 (with-current-buffer
384 (find-file-noselect
385 (expand-file-name vc-sccs-name-assoc-file
386 (file-name-directory (vc-name old))))
387 (goto-char (point-min))
388 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
389 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
390 (replace-match (concat ":" new) nil nil))
391 (basic-save-buffer)
392 (kill-buffer (current-buffer))))
396 ;;; Internal functions
399 ;; This function is wrapped with `progn' so that the autoload cookie
400 ;; copies the whole function itself into loaddefs.el rather than just placing
401 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
402 ;; help us avoid loading vc-sccs.
403 ;;;###autoload
404 (progn (defun vc-sccs-search-project-dir (dirname basename)
405 "Return the name of a master file in the SCCS project directory.
406 Does not check whether the file exists but returns nil if it does not
407 find any project directory."
408 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
409 (when project-dir
410 (if (file-name-absolute-p project-dir)
411 (setq dirs '("SCCS" ""))
412 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
413 (setq project-dir (expand-file-name (concat "~" project-dir))))
414 (while (and (not dir) dirs)
415 (setq dir (expand-file-name (car dirs) project-dir))
416 (unless (file-directory-p dir)
417 (setq dir nil)
418 (setq dirs (cdr dirs))))
419 (and dir (expand-file-name (concat "s." basename) dir))))))
421 (defun vc-sccs-lock-file (file)
422 "Generate lock file name corresponding to FILE."
423 (let ((master (vc-name file)))
424 (and
425 master
426 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
427 (replace-match "p." t t master 2))))
429 (defun vc-sccs-parse-locks ()
430 "Parse SCCS locks in current buffer.
431 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
432 (let (master-locks)
433 (goto-char (point-min))
434 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
435 nil t)
436 (setq master-locks
437 (cons (cons (match-string 1) (match-string 2)) master-locks)))
438 ;; FIXME: is it really necessary to reverse ?
439 (nreverse master-locks)))
441 (defun vc-sccs-add-triple (name file rev)
442 (with-current-buffer
443 (find-file-noselect
444 (expand-file-name vc-sccs-name-assoc-file
445 (file-name-directory (vc-name file))))
446 (goto-char (point-max))
447 (insert name "\t:\t" file "\t" rev "\n")
448 (basic-save-buffer)
449 (kill-buffer (current-buffer))))
451 (defun vc-sccs-lookup-triple (file name)
452 "Return the numeric revision corresponding to a named tag of FILE.
453 If NAME is nil or a revision number string it's just passed through."
454 (if (or (null name)
455 (let ((firstchar (aref name 0)))
456 (and (>= firstchar ?0) (<= firstchar ?9))))
457 name
458 (with-temp-buffer
459 (vc-insert-file
460 (expand-file-name vc-sccs-name-assoc-file
461 (file-name-directory (vc-name file))))
462 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
464 (provide 'vc-sccs)
466 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
467 ;;; vc-sccs.el ends here