Put the lower half (the back-end) of NewVC in place. This commit
[emacs.git] / lisp / vc-sccs.el
blob0163e2831289225b5e54a0db1f1f03289da266b1
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 Free Software Foundation, Inc.
6 ;; Author: FSF (see vc.el for full credits)
7 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9 ;; $Id$
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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;;; Code:
32 (eval-when-compile
33 (require 'vc))
35 ;;;
36 ;;; Customization options
37 ;;;
39 (defcustom vc-sccs-register-switches nil
40 "*Extra switches for registering a file in SCCS.
41 A string or list of strings passed to the checkin program by
42 \\[vc-sccs-register]."
43 :type '(choice (const :tag "None" nil)
44 (string :tag "Argument String")
45 (repeat :tag "Argument List"
46 :value ("")
47 string))
48 :version "21.1"
49 :group 'vc)
51 (defcustom vc-sccs-diff-switches nil
52 "*A string or list of strings specifying extra switches for `vcdiff',
53 the diff utility used for SCCS under VC."
54 :type '(choice (const :tag "None" nil)
55 (string :tag "Argument String")
56 (repeat :tag "Argument List"
57 :value ("")
58 string))
59 :version "21.1"
60 :group 'vc)
62 (defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
63 "*Header keywords to be inserted by `vc-insert-headers'."
64 :type '(repeat string)
65 :group 'vc)
67 ;;;###autoload
68 (defcustom vc-sccs-master-templates
69 '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir)
70 "*Where to look for SCCS master files.
71 For a description of possible values, see `vc-check-master-templates'."
72 :type '(choice (const :tag "Use standard SCCS file names"
73 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
74 (repeat :tag "User-specified"
75 (choice string
76 function)))
77 :version "21.1"
78 :group 'vc)
81 ;;;
82 ;;; Internal variables
83 ;;;
85 (defconst vc-sccs-name-assoc-file "VC-names")
88 ;;; Properties of the backend
90 (defun vc-sccs-revision-granularity ()
91 'file)
93 ;;;
94 ;;; State-querying functions
95 ;;;
97 ;;; The autoload cookie below places vc-sccs-registered directly into
98 ;;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
99 ;;; every file that is visited. The definition is repeated below
100 ;;; so that Help and etags can find it.
102 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
103 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
105 (defun vc-sccs-state (file)
106 "SCCS-specific function to compute the version control state."
107 (with-temp-buffer
108 (if (vc-insert-file (vc-sccs-lock-file file))
109 (let* ((locks (vc-sccs-parse-locks))
110 (workfile-version (vc-workfile-version file))
111 (locking-user (cdr (assoc workfile-version locks))))
112 (if (not locking-user)
113 (if (vc-workfile-unchanged-p file)
114 'up-to-date
115 'unlocked-changes)
116 (if (string= locking-user (vc-user-login-name file))
117 'edited
118 locking-user)))
119 'up-to-date)))
121 (defun vc-sccs-state-heuristic (file)
122 "SCCS-specific state heuristic."
123 (if (not (vc-mistrust-permissions file))
124 ;; This implementation assumes that any file which is under version
125 ;; control and has -rw-r--r-- is locked by its owner. This is true
126 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
127 ;; We have to be careful not to exclude files with execute bits on;
128 ;; scripts can be under version control too. Also, we must ignore the
129 ;; group-read and other-read bits, since paranoid users turn them off.
130 (let* ((attributes (file-attributes file 'string))
131 (owner-name (nth 2 attributes))
132 (permissions (nth 8 attributes)))
133 (if (string-match ".r-..-..-." permissions)
134 'up-to-date
135 (if (string-match ".rw..-..-." permissions)
136 (if (file-ownership-preserved-p file)
137 'edited
138 owner-name)
139 ;; Strange permissions.
140 ;; Fall through to real state computation.
141 (vc-sccs-state file))))
142 (vc-sccs-state file)))
144 (defun vc-sccs-workfile-version (file)
145 "SCCS-specific version of `vc-workfile-version'."
146 (with-temp-buffer
147 ;; The workfile version is always the latest version number.
148 ;; To find this number, search the entire delta table,
149 ;; rather than just the first entry, because the
150 ;; first entry might be a deleted ("R") version.
151 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
152 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
154 (defun vc-sccs-checkout-model (file)
155 "SCCS-specific version of `vc-checkout-model'."
156 'locking)
158 (defun vc-sccs-workfile-unchanged-p (file)
159 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
160 (zerop (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
161 (list "--brief" "-q"
162 (concat "-r" (vc-workfile-version file))))))
166 ;;; State-changing functions
169 (defun vc-sccs-create-repo ()
170 "Create a new SCCS repository."
171 ;; SCCS is totally file-oriented, so all we have to do is make the directory
172 (make-directory "SCCS"))
174 (defun vc-sccs-register (files &optional rev comment)
175 "Register FILES into the SCCS version-control system.
176 REV is the optional revision number for the file. COMMENT can be used
177 to provide an initial description of FILES.
179 `vc-register-switches' and `vc-sccs-register-switches' are passed to
180 the SCCS command (in that order).
182 Automatically retrieve a read-only version of the files with keywords
183 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
184 (dolist (file files)
185 (let* ((dirname (or (file-name-directory file) ""))
186 (basename (file-name-nondirectory file))
187 (project-file (vc-sccs-search-project-dir dirname basename)))
188 (let ((vc-name
189 (or project-file
190 (format (car vc-sccs-master-templates) dirname basename))))
191 (apply 'vc-do-command nil 0 "admin" vc-name
192 (and rev (not (string= rev "")) (concat "-r" rev))
193 "-fb"
194 (concat "-i" (file-relative-name file))
195 (and comment (concat "-y" comment))
196 (vc-switches 'SCCS 'register)))
197 (delete-file file)
198 (if vc-keep-workfiles
199 (vc-do-command nil 0 "get" (vc-name file))))))
201 (defun vc-sccs-responsible-p (file)
202 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
203 ;; TODO: check for all the patterns in vc-sccs-master-templates
204 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
205 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
206 (file-name-nondirectory file)))))
208 (defun vc-sccs-checkin (files rev comment)
209 "SCCS-specific version of `vc-backend-checkin'."
210 (dolist (file files)
211 (apply 'vc-do-command nil 0 "delta" (vc-name file)
212 (if rev (concat "-r" rev))
213 (concat "-y" comment)
214 (vc-switches 'SCCS 'checkin))
215 (if vc-keep-workfiles
216 (vc-do-command nil 0 "get" (vc-name file)))))
218 (defun vc-sccs-find-version (file rev buffer)
219 (apply 'vc-do-command
220 buffer 0 "get" (vc-name file)
221 "-s" ;; suppress diagnostic output
222 "-p"
223 (and rev
224 (concat "-r"
225 (vc-sccs-lookup-triple file rev)))
226 (vc-switches 'SCCS 'checkout)))
228 (defun vc-sccs-checkout (file &optional editable rev)
229 "Retrieve a copy of a saved version of SCCS controlled FILE.
230 EDITABLE non-nil means that the file should be writable and
231 locked. REV is the revision to check out."
232 (let ((file-buffer (get-file-buffer file))
233 switches)
234 (message "Checking out %s..." file)
235 (save-excursion
236 ;; Change buffers to get local value of vc-checkout-switches.
237 (if file-buffer (set-buffer file-buffer))
238 (setq switches (vc-switches 'SCCS 'checkout))
239 ;; Save this buffer's default-directory
240 ;; and use save-excursion to make sure it is restored
241 ;; in the same buffer it was saved in.
242 (let ((default-directory default-directory))
243 (save-excursion
244 ;; Adjust the default-directory so that the check-out creates
245 ;; the file in the right place.
246 (setq default-directory (file-name-directory file))
248 (and rev (or (string= rev "")
249 (not (stringp rev)))
250 (setq rev nil))
251 (apply 'vc-do-command nil 0 "get" (vc-name file)
252 (if editable "-e")
253 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
254 switches))))
255 (message "Checking out %s...done" file)))
257 (defun vc-sccs-cancel-version (files)
258 "Roll back, undoing the most recent checkins of FILES."
259 (if (not files)
260 (error "SCCS backend doesn't support directory-level rollback."))
261 (dolist (file files)
262 (let ((discard (vc-workfile-version file)))
263 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
264 discard file)))
265 (error "Aborted"))
266 (message "Removing revision %s from %s..." discard file)
267 (vc-do-command nil 0 "rmdel" (vc-name file) (concat "-r" discard))
268 (vc-do-command nil 0 "get" (vc-name file) nil))))
270 (defun vc-sccs-revert (file &optional contents-done)
271 "Revert FILE to the version it was based on."
272 (vc-do-command nil 0 "unget" (vc-name file))
273 (vc-do-command nil 0 "get" (vc-name file))
274 ;; Checking out explicit versions is not supported under SCCS, yet.
275 ;; We always "revert" to the latest version; therefore
276 ;; vc-workfile-version is cleared here so that it gets recomputed.
277 (vc-file-setprop file 'vc-workfile-version nil))
279 (defun vc-sccs-steal-lock (file &optional rev)
280 "Steal the lock on the current workfile for FILE and revision REV."
281 (vc-do-command nil 0 "unget" (vc-name file) "-n" (if rev (concat "-r" rev)))
282 (vc-do-command nil 0 "get" (vc-name file) "-g" (if rev (concat "-r" rev))))
286 ;;; History functions
289 (defun vc-sccs-print-log (files &optional buffer)
290 "Get change log associated with FILES."
291 (vc-do-command buffer 0 "prs" (mapcar 'vc-name files)))
293 (defun vc-sccs-wash-log ()
294 "Remove all non-comment information from log output."
295 ;; FIXME: not implemented for SCCS
296 nil)
298 (defun vc-sccs-logentry-check ()
299 "Check that the log entry in the current buffer is acceptable for SCCS."
300 (when (>= (buffer-size) 512)
301 (goto-char 512)
302 (error "Log must be less than 512 characters; point is now at pos 512")))
304 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
305 "Get a difference report using SCCS between two filesets."
306 (setq oldvers (vc-sccs-lookup-triple file oldvers))
307 (setq newvers (vc-sccs-lookup-triple file newvers))
308 (apply 'vc-do-command (or buffer "*vc-diff*")
309 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
310 (append (list "-q"
311 (and oldvers (concat "-r" oldvers))
312 (and newvers (concat "-r" newvers)))
313 (vc-switches 'SCCS 'diff))))
317 ;;; Snapshot system
320 (defun vc-sccs-assign-name (file name)
321 "Assign to FILE's latest version a given NAME."
322 (vc-sccs-add-triple name file (vc-workfile-version file)))
326 ;;; Miscellaneous
329 (defun vc-sccs-check-headers ()
330 "Check if the current file has any headers in it."
331 (save-excursion
332 (goto-char (point-min))
333 (re-search-forward "%[A-Z]%" nil t)))
335 (defun vc-sccs-rename-file (old new)
336 ;; Move the master file (using vc-rcs-master-templates).
337 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
338 ;; Update the snapshot file.
339 (with-current-buffer
340 (find-file-noselect
341 (expand-file-name vc-sccs-name-assoc-file
342 (file-name-directory (vc-name old))))
343 (goto-char (point-min))
344 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
345 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
346 (replace-match (concat ":" new) nil nil))
347 (basic-save-buffer)
348 (kill-buffer (current-buffer))))
352 ;;; Internal functions
355 ;; This function is wrapped with `progn' so that the autoload cookie
356 ;; copies the whole function itself into loaddefs.el rather than just placing
357 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
358 ;; help us avoid loading vc-sccs.
359 ;;;###autoload
360 (progn (defun vc-sccs-search-project-dir (dirname basename)
361 "Return the name of a master file in the SCCS project directory.
362 Does not check whether the file exists but returns nil if it does not
363 find any project directory."
364 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
365 (when project-dir
366 (if (file-name-absolute-p project-dir)
367 (setq dirs '("SCCS" ""))
368 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
369 (setq project-dir (expand-file-name (concat "~" project-dir))))
370 (while (and (not dir) dirs)
371 (setq dir (expand-file-name (car dirs) project-dir))
372 (unless (file-directory-p dir)
373 (setq dir nil)
374 (setq dirs (cdr dirs))))
375 (and dir (expand-file-name (concat "s." basename) dir))))))
377 (defun vc-sccs-lock-file (file)
378 "Generate lock file name corresponding to FILE."
379 (let ((master (vc-name file)))
380 (and
381 master
382 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
383 (replace-match "p." t t master 2))))
385 (defun vc-sccs-parse-locks ()
386 "Parse SCCS locks in current buffer.
387 The result is a list of the form ((VERSION . USER) (VERSION . USER) ...)."
388 (let (master-locks)
389 (goto-char (point-min))
390 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
391 nil t)
392 (setq master-locks
393 (cons (cons (match-string 1) (match-string 2)) master-locks)))
394 ;; FIXME: is it really necessary to reverse ?
395 (nreverse master-locks)))
397 (defun vc-sccs-add-triple (name file rev)
398 (with-current-buffer
399 (find-file-noselect
400 (expand-file-name vc-sccs-name-assoc-file
401 (file-name-directory (vc-name file))))
402 (goto-char (point-max))
403 (insert name "\t:\t" file "\t" rev "\n")
404 (basic-save-buffer)
405 (kill-buffer (current-buffer))))
407 (defun vc-sccs-lookup-triple (file name)
408 "Return the numeric version corresponding to a named snapshot of FILE.
409 If NAME is nil or a version number string it's just passed through."
410 (if (or (null name)
411 (let ((firstchar (aref name 0)))
412 (and (>= firstchar ?0) (<= firstchar ?9))))
413 name
414 (with-temp-buffer
415 (vc-insert-file
416 (expand-file-name vc-sccs-name-assoc-file
417 (file-name-directory (vc-name file))))
418 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
420 (provide 'vc-sccs)
422 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
423 ;;; vc-sccs.el ends here