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