(vc-rcs-header): Fix :type.
[bpt/emacs.git] / lisp / vc-sccs.el
CommitLineData
7a004b71
GM
1;;; vc-sccs.el --- support for SCCS version-control
2
3;; Copyright (C) 1992,93,94,95,96,97,98,99,2000 Free Software Foundation, Inc.
4
5;; Author: FSF (see vc.el for full credits)
6;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7
1862f9ef 8;; $Id: vc-sccs.el,v 1.5 2000/11/16 18:14:41 spiegel Exp $
7a004b71
GM
9
10;; This file is part of GNU Emacs.
11
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 2, or (at your option)
15;; any later version.
16
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.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
26
27;;; Commentary:
28
29;;; Code:
30
8f98485f
AS
31;;;
32;;; Customization options
33;;;
34
7a004b71 35(defcustom vc-sccs-register-switches nil
a7cafade
DL
36 "*Extra switches for registering a file in SCCS.
37A string or list of strings passed to the checkin program by
7a004b71
GM
38\\[vc-sccs-register]."
39 :type '(choice (const :tag "None" nil)
40 (string :tag "Argument String")
41 (repeat :tag "Argument List"
42 :value ("")
43 string))
a7cafade 44 :version "21.1"
7a004b71
GM
45 :group 'vc)
46
47(defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
48 "*Header keywords to be inserted by `vc-insert-headers'."
49 :type 'string
50 :group 'vc)
51
52;;;###autoload
53(defcustom vc-sccs-master-templates
54 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir)
55 "*Where to look for SCCS master files.
56For a description of possible values, see `vc-check-master-templates'."
57 :type '(choice (const :tag "Use standard SCCS file names"
58 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
59 (repeat :tag "User-specified"
60 (choice string
61 function)))
a7cafade 62 :version "21.1"
7a004b71
GM
63 :group 'vc)
64
8f98485f
AS
65\f
66;;;
67;;; Internal variables
68;;;
69
099bd78a
SM
70(defconst vc-sccs-name-assoc-file "VC-names")
71
8f98485f
AS
72\f
73;;;
74;;; State-querying functions
75;;;
76
7a004b71
GM
77;;;###autoload
78(progn (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f)))
79
80(defun vc-sccs-state (file)
81 "SCCS-specific function to compute the version control state."
82 (with-temp-buffer
83 (if (vc-insert-file (vc-sccs-lock-file file))
84 (let* ((locks (vc-sccs-parse-locks))
85 (workfile-version (vc-workfile-version file))
86 (locking-user (cdr (assoc workfile-version locks))))
87 (if (not locking-user)
88 (if (vc-workfile-unchanged-p file)
89 'up-to-date
90 'unlocked-changes)
91 (if (string= locking-user (vc-user-login-name))
92 'edited
93 locking-user)))
94 'up-to-date)))
95
96(defun vc-sccs-state-heuristic (file)
97 "SCCS-specific state heuristic."
98 (if (not (vc-mistrust-permissions file))
99 ;; This implementation assumes that any file which is under version
100 ;; control and has -rw-r--r-- is locked by its owner. This is true
101 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
102 ;; We have to be careful not to exclude files with execute bits on;
103 ;; scripts can be under version control too. Also, we must ignore the
104 ;; group-read and other-read bits, since paranoid users turn them off.
105 (let* ((attributes (file-attributes file))
106 (owner-uid (nth 2 attributes))
107 (permissions (nth 8 attributes)))
108 (if (string-match ".r-..-..-." permissions)
109 'up-to-date
110 (if (string-match ".rw..-..-." permissions)
111 (if (file-ownership-preserved-p file)
112 'edited
113 (vc-user-login-name owner-uid))
a7cafade 114 ;; Strange permissions.
7a004b71
GM
115 ;; Fall through to real state computation.
116 (vc-sccs-state file)))
117 (vc-sccs-state file))))
118
119(defun vc-sccs-workfile-version (file)
120 "SCCS-specific version of `vc-workfile-version'."
121 (with-temp-buffer
122 (vc-insert-file (vc-name file) "^\001e")
123 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
124
a7cafade 125(defun vc-sccs-checkout-model (file)
7a004b71
GM
126 "SCCS-specific version of `vc-checkout-model'."
127 'locking)
128
129(defun vc-sccs-workfile-unchanged-p (file)
130 "SCCS-specific implementation of vc-workfile-unchanged-p."
131 (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
a7cafade 132 (list "--brief" "-q"
7a004b71
GM
133 (concat "-r" (vc-workfile-version file)))))
134
7a004b71 135\f
8f98485f
AS
136;;;
137;;; State-changing functions
138;;;
7a004b71
GM
139
140(defun vc-sccs-register (file &optional rev comment)
141 "Register FILE into the SCCS version-control system.
142REV is the optional revision number for the file. COMMENT can be used
143to provide an initial description of FILE.
144
145`vc-register-switches' and `vc-sccs-register-switches' are passed to
146the SCCS command (in that order).
147
148Automatically retrieve a read-only version of the file with keywords
149expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
7a004b71
GM
150 (let* ((switches (list
151 (if (stringp vc-register-switches)
152 (list vc-register-switches)
153 vc-register-switches)
154 (if (stringp vc-sccs-register-switches)
155 (list vc-sccs-register-switches)
156 vc-sccs-register-switches)))
157 (dirname (or (file-name-directory file) ""))
158 (basename (file-name-nondirectory file))
159 (project-file (vc-sccs-search-project-dir dirname basename)))
160 (let ((vc-name
161 (or project-file
162 (format (car vc-sccs-master-templates) dirname basename)))|)
a7cafade 163 (apply 'vc-do-command nil 0 "admin" nil
7a004b71
GM
164 (and rev (concat "-r" rev))
165 "-fb"
166 (concat "-i" file)
167 (and comment (concat "-y" comment))
168 vc-name
169 switches))
170 (delete-file file)
171 (if vc-keep-workfiles
172 (vc-do-command nil 0 "get" (vc-name file)))))
173
8f98485f
AS
174(defun vc-sccs-responsible-p (file)
175 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
176 ;; TODO: check for all the patterns in vc-sccs-master-templates
177 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
178 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
179 (file-name-nondirectory file)))))
180
181(defun vc-sccs-checkin (file rev comment)
182 "SCCS-specific version of `vc-backend-checkin'."
183 (let ((switches (if (stringp vc-checkin-switches)
184 (list vc-checkin-switches)
185 vc-checkin-switches)))
186 (apply 'vc-do-command nil 0 "delta" (vc-name file)
187 (if rev (concat "-r" rev))
188 (concat "-y" comment)
189 switches)
190 (if vc-keep-workfiles
191 (vc-do-command nil 0 "get" (vc-name file)))))
192
1862f9ef 193(defun vc-sccs-checkout (file &optional editable rev workfile)
a7cafade 194 "Retrieve a copy of a saved version of SCCS controlled FILE into a WORKFILE.
1862f9ef
AS
195EDITABLE non-nil means that the file should be writable and
196locked. REV is the revision to check out into WORKFILE."
7a004b71
GM
197 (let ((filename (or workfile file))
198 (file-buffer (get-file-buffer file))
199 switches)
200 (message "Checking out %s..." filename)
201 (save-excursion
202 ;; Change buffers to get local value of vc-checkout-switches.
203 (if file-buffer (set-buffer file-buffer))
204 (setq switches (if (stringp vc-checkout-switches)
205 (list vc-checkout-switches)
206 vc-checkout-switches))
207 ;; Save this buffer's default-directory
208 ;; and use save-excursion to make sure it is restored
209 ;; in the same buffer it was saved in.
210 (let ((default-directory default-directory))
211 (save-excursion
212 ;; Adjust the default-directory so that the check-out creates
213 ;; the file in the right place.
214 (setq default-directory (file-name-directory filename))
215
216 (and rev (string= rev "") (setq rev nil))
217 (if workfile
218 ;; Some SCCS implementations allow checking out directly to a
219 ;; file using the -G option, but then some don't so use the
220 ;; least common denominator approach and use the -p option
221 ;; ala RCS.
222 (let ((vc-modes (logior (file-modes (vc-name file))
1862f9ef 223 (if editable 128 0)))
7a004b71
GM
224 (failed t))
225 (unwind-protect
226 (progn
227 (let ((coding-system-for-read 'no-conversion)
228 (coding-system-for-write 'no-conversion))
229 (with-temp-file filename
230 (apply 'vc-do-command
231 (current-buffer) 0 "get" (vc-name file)
232 "-s" ;; suppress diagnostic output
1862f9ef 233 (if editable "-e")
a7cafade 234 "-p"
7a004b71 235 (and rev
a7cafade 236 (concat "-r"
7a004b71
GM
237 (vc-sccs-lookup-triple file rev)))
238 switches)))
239 (set-file-modes filename
240 (logior (file-modes (vc-name file))
1862f9ef 241 (if editable 128 0)))
7a004b71
GM
242 (setq failed nil))
243 (and failed (file-exists-p filename)
244 (delete-file filename))))
245 (apply 'vc-do-command nil 0 "get" (vc-name file)
1862f9ef 246 (if editable "-e")
7a004b71 247 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
099bd78a
SM
248 switches)))))
249 (message "Checking out %s...done" filename)))
7a004b71 250
8f98485f
AS
251(defun vc-sccs-revert (file)
252 "Revert FILE to the version it was based on."
253 (vc-do-command nil 0 "unget" (vc-name file))
254 (vc-do-command nil 0 "get" (vc-name file))
255 ;; Checking out explicit versions is not supported under SCCS, yet.
256 ;; We always "revert" to the latest version; therefore
257 ;; vc-workfile-version is cleared here so that it gets recomputed.
258 (vc-file-setprop file 'vc-workfile-version nil))
259
1862f9ef 260(defun vc-sccs-cancel-version (file editable)
8f98485f 261 "Undo the most recent checkin of FILE.
1862f9ef 262EDITABLE non-nil means previous version should be locked."
8f98485f
AS
263 (vc-do-command nil 0 "rmdel"
264 (vc-name file)
265 (concat "-r" (vc-workfile-version file)))
266 (vc-do-command nil 0 "get"
267 (vc-name file)
1862f9ef 268 (if editable "-e")))
8f98485f
AS
269
270(defun vc-sccs-steal-lock (file &optional rev)
271 "Steal the lock on the current workfile for FILE and revision REV."
272 (vc-do-command nil 0 "unget" (vc-name file) "-n" (if rev (concat "-r" rev)))
273 (vc-do-command nil 0 "get" (vc-name file) "-g" (if rev (concat "-r" rev))))
274
275\f
276;;;
277;;; History functions
278;;;
279
280(defun vc-sccs-print-log (file)
281 "Get change log associated with FILE."
282 (vc-do-command t 0 "prs" (vc-name file)))
283
284(defun vc-sccs-logentry-check ()
285 "Check that the log entry in the current buffer is acceptable for SCCS."
286 (when (>= (buffer-size) 512)
287 (goto-char 512)
288 (error "Log must be less than 512 characters; point is now at pos 512")))
289
290(defun vc-sccs-diff (file &optional oldvers newvers)
291 "Get a difference report using SCCS between two versions of FILE."
292 (setq oldvers (vc-sccs-lookup-triple file oldvers))
293 (setq newvers (vc-sccs-lookup-triple file newvers))
294 (let* ((diff-switches-list (if (listp diff-switches)
295 diff-switches
296 (list diff-switches)))
297 (options (append (list "-q"
298 (and oldvers (concat "-r" oldvers))
299 (and newvers (concat "-r" newvers)))
300 diff-switches-list)))
301 (apply 'vc-do-command t 1 "vcdiff" (vc-name file) options)))
302
303\f
304;;;
305;;; Snapshot system
306;;;
307
308(defun vc-sccs-assign-name (file name)
309 "Assign to FILE's latest version a given NAME."
310 (vc-sccs-add-triple name file (vc-workfile-version file)))
311
312\f
313;;;
314;;; Miscellaneous
315;;;
316
317(defun vc-sccs-check-headers ()
318 "Check if the current file has any headers in it."
319 (save-excursion
320 (goto-char (point-min))
321 (re-search-forward "%[A-Z]%" nil t)))
322
323(defun vc-sccs-rename-file (old new)
324 ;; Move the master file (using vc-rcs-master-templates).
325 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
326 ;; Update the snapshot file.
327 (with-current-buffer
328 (find-file-noselect
329 (expand-file-name vc-sccs-name-assoc-file
330 (file-name-directory (vc-name old))))
331 (goto-char (point-min))
332 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
333 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
334 (replace-match (concat ":" new) nil nil))
335 (basic-save-buffer)
336 (kill-buffer (current-buffer))))
337
338\f
339;;;
340;;; Internal functions
341;;;
342
343;; This function is wrapped with `progn' so that the autoload cookie
344;; copies the whole function itself into loaddefs.el rather than just placing
345;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
346;; help us avoid loading vc-sccs.
347;;;###autoload
348(progn (defun vc-sccs-search-project-dir (dirname basename)
349 "Return the name of a master file in the SCCS project directory.
350Does not check whether the file exists but returns nil if it does not
351find any project directory."
352 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
353 (when project-dir
354 (if (file-name-absolute-p project-dir)
355 (setq dirs '("SCCS" ""))
356 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
357 (setq project-dir (expand-file-name (concat "~" project-dir))))
358 (while (and (not dir) dirs)
359 (setq dir (expand-file-name (car dirs) project-dir))
360 (unless (file-directory-p dir)
361 (setq dir nil)
362 (setq dirs (cdr dirs))))
363 (and dir (expand-file-name (concat "s." basename) dir))))))
364
365(defun vc-sccs-lock-file (file)
366 "Generate lock file name corresponding to FILE."
367 (let ((master (vc-name file)))
368 (and
369 master
370 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
371 (replace-match "p." t t master 2))))
372
373(defun vc-sccs-parse-locks ()
374 "Parse SCCS locks in current buffer.
375The result is a list of the form ((VERSION . USER) (VERSION . USER) ...)."
376 (let (master-locks)
377 (goto-char (point-min))
378 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
379 nil t)
380 (setq master-locks
381 (cons (cons (match-string 1) (match-string 2)) master-locks)))
382 ;; FIXME: is it really necessary to reverse ?
383 (nreverse master-locks)))
384
385(defun vc-sccs-add-triple (name file rev)
386 (with-current-buffer
387 (find-file-noselect
388 (expand-file-name vc-sccs-name-assoc-file
389 (file-name-directory (vc-name file))))
390 (goto-char (point-max))
391 (insert name "\t:\t" file "\t" rev "\n")
392 (basic-save-buffer)
393 (kill-buffer (current-buffer))))
394
395(defun vc-sccs-lookup-triple (file name)
396 "Return the numeric version corresponding to a named snapshot of FILE.
397If NAME is nil or a version number string it's just passed through."
398 (if (or (null name)
399 (let ((firstchar (aref name 0)))
400 (and (>= firstchar ?0) (<= firstchar ?9))))
401 name
402 (with-temp-buffer
403 (vc-insert-file
404 (expand-file-name vc-sccs-name-assoc-file
405 (file-name-directory (vc-name file))))
406 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
7a004b71
GM
407
408(provide 'vc-sccs)
409
410;;; vc-sccs.el ends here
8f98485f
AS
411
412
413