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