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