]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-sccs.el
merge trunk
[gnu-emacs] / lisp / vc / vc-sccs.el
1 ;;; vc-sccs.el --- support for SCCS version-control
2
3 ;; Copyright (C) 1992-2012 Free Software Foundation, Inc.
4
5 ;; Author: FSF (see vc.el for full credits)
6 ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
7 ;; Package: vc
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile
29 (require 'vc))
30
31 ;;;
32 ;;; Customization options
33 ;;;
34
35 ;; ;; Maybe a better solution is to not use "get" but "sccs get".
36 ;; ;; Note for GNU CSSC, you can parse sccs -V to get the libexec path.
37 ;; (defcustom vc-sccs-path
38 ;; (prune-directory-list '("/usr/ccs/bin" "/usr/sccs" "/usr/lib/sccs"
39 ;; "/usr/libexec/sccs"))
40 ;; "List of extra directories to search for SCCS commands."
41 ;; :type '(repeat directory)
42 ;; :group 'vc)
43
44 (defgroup vc-sccs nil
45 "VC SCCS backend."
46 :version "24.1"
47 :group 'vc)
48
49 (defcustom vc-sccs-register-switches nil
50 "Switches for registering a file in SCCS.
51 A string or list of strings passed to the checkin program by
52 \\[vc-register]. If nil, use the value of `vc-register-switches'.
53 If t, use no switches."
54 :type '(choice (const :tag "Unspecified" nil)
55 (const :tag "None" t)
56 (string :tag "Argument String")
57 (repeat :tag "Argument List" :value ("") string))
58 :version "21.1"
59 :group 'vc-sccs)
60
61 (defcustom vc-sccs-diff-switches nil
62 "String or list of strings specifying switches for SCCS diff under VC.
63 If nil, use the value of `vc-diff-switches'. If t, use no switches."
64 :type '(choice (const :tag "Unspecified" nil)
65 (const :tag "None" t)
66 (string :tag "Argument String")
67 (repeat :tag "Argument List" :value ("") string))
68 :version "21.1"
69 :group 'vc-sccs)
70
71 (defcustom vc-sccs-header '("%W%")
72 "Header keywords to be inserted by `vc-insert-headers'."
73 :type '(repeat string)
74 :version "24.1" ; no longer consult the obsolete vc-header-alist
75 :group 'vc-sccs)
76
77 ;;;###autoload
78 (defcustom vc-sccs-master-templates
79 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
80 "Where to look for SCCS master files.
81 For a description of possible values, see `vc-check-master-templates'."
82 :type '(choice (const :tag "Use standard SCCS file names"
83 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
84 (repeat :tag "User-specified"
85 (choice string
86 function)))
87 :version "21.1"
88 :group 'vc-sccs)
89
90 \f
91 ;;;
92 ;;; Internal variables
93 ;;;
94
95 (defconst vc-sccs-name-assoc-file "VC-names")
96
97 \f
98 ;;; Properties of the backend
99
100 (defun vc-sccs-revision-granularity () 'file)
101 (defun vc-sccs-checkout-model (files) 'locking)
102
103 ;;;
104 ;;; State-querying functions
105 ;;;
106
107 ;; The autoload cookie below places vc-sccs-registered directly into
108 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
109 ;; every file that is visited. The definition is repeated below
110 ;; so that Help and etags can find it.
111
112 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
113 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
114
115 (defun vc-sccs-state (file)
116 "SCCS-specific function to compute the version control state."
117 (if (not (vc-sccs-registered file))
118 'unregistered
119 (with-temp-buffer
120 (if (vc-insert-file (vc-sccs-lock-file file))
121 (let* ((locks (vc-sccs-parse-locks))
122 (working-revision (vc-working-revision file))
123 (locking-user (cdr (assoc working-revision locks))))
124 (if (not locking-user)
125 (if (vc-workfile-unchanged-p file)
126 'up-to-date
127 'unlocked-changes)
128 (if (string= locking-user (vc-user-login-name file))
129 'edited
130 locking-user)))
131 'up-to-date))))
132
133 (defun vc-sccs-state-heuristic (file)
134 "SCCS-specific state heuristic."
135 (if (not (vc-mistrust-permissions file))
136 ;; This implementation assumes that any file which is under version
137 ;; control and has -rw-r--r-- is locked by its owner. This is true
138 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
139 ;; We have to be careful not to exclude files with execute bits on;
140 ;; scripts can be under version control too. Also, we must ignore the
141 ;; group-read and other-read bits, since paranoid users turn them off.
142 (let* ((attributes (file-attributes file 'string))
143 (owner-name (nth 2 attributes))
144 (permissions (nth 8 attributes)))
145 (if (string-match ".r-..-..-." permissions)
146 'up-to-date
147 (if (string-match ".rw..-..-." permissions)
148 (if (file-ownership-preserved-p file)
149 'edited
150 owner-name)
151 ;; Strange permissions.
152 ;; Fall through to real state computation.
153 (vc-sccs-state file))))
154 (vc-sccs-state file)))
155
156 (defun vc-sccs-dir-status (dir update-function)
157 ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
158 ;; is not TRTD because it returns files from multiple backends.
159 ;; It should also return 'unregistered files.
160
161 ;; Doing lots of individual VC-state calls is painful, but
162 ;; there is no better option in SCCS-land.
163 (let ((flist (vc-expand-dirs (list dir)))
164 (result nil))
165 (dolist (file flist)
166 (let ((state (vc-state file))
167 (frel (file-relative-name file)))
168 (when (and (eq (vc-backend file) 'SCCS)
169 (not (eq state 'up-to-date)))
170 (push (list frel state) result))))
171 (funcall update-function result)))
172
173 (defun vc-sccs-working-revision (file)
174 "SCCS-specific version of `vc-working-revision'."
175 (with-temp-buffer
176 ;; The working revision is always the latest revision number.
177 ;; To find this number, search the entire delta table,
178 ;; rather than just the first entry, because the
179 ;; first entry might be a deleted ("R") revision.
180 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
181 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
182
183 ;; Cf vc-sccs-find-revision.
184 (defun vc-sccs-write-revision (file outfile &optional rev)
185 "Write the SCCS version of input file FILE to output file OUTFILE.
186 Optional string REV is a revision."
187 (with-temp-buffer
188 (apply 'vc-sccs-do-command t 0 "get" (vc-name file)
189 (append '("-s" "-p" "-k") ; -k: no keyword expansion
190 (if rev (list (concat "-r" rev)))))
191 (write-region nil nil outfile nil 'silent)))
192
193 (defun vc-sccs-workfile-unchanged-p (file)
194 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
195 (let ((tempfile (make-temp-file "vc-sccs")))
196 (unwind-protect
197 (progn
198 (vc-sccs-write-revision file tempfile (vc-working-revision file))
199 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
200 (delete-file tempfile))))
201
202 \f
203 ;;;
204 ;;; State-changing functions
205 ;;;
206
207 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
208 ;; (let ((load-path (append vc-sccs-path load-path)))
209 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
210 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
211
212 (defun vc-sccs-create-repo ()
213 "Create a new SCCS repository."
214 ;; SCCS is totally file-oriented, so all we have to do is make the directory
215 (make-directory "SCCS"))
216
217 (defun vc-sccs-register (files &optional rev comment)
218 "Register FILES into the SCCS version-control system.
219 REV is the optional revision number for the file. COMMENT can be used
220 to provide an initial description of FILES.
221 Passes either `vc-sccs-register-switches' or `vc-register-switches'
222 to the SCCS command.
223
224 Automatically retrieve a read-only version of the files with keywords
225 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
226 (dolist (file files)
227 (let* ((dirname (or (file-name-directory file) ""))
228 (basename (file-name-nondirectory file))
229 (project-file (vc-sccs-search-project-dir dirname basename)))
230 (let ((vc-name
231 (or project-file
232 (format (car vc-sccs-master-templates) dirname basename))))
233 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
234 (and rev (not (string= rev "")) (concat "-r" rev))
235 "-fb"
236 (concat "-i" (file-relative-name file))
237 (and comment (concat "-y" comment))
238 (vc-switches 'SCCS 'register)))
239 (delete-file file)
240 (if vc-keep-workfiles
241 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
242
243 (defun vc-sccs-responsible-p (file)
244 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
245 ;; TODO: check for all the patterns in vc-sccs-master-templates
246 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
247 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
248 (file-name-nondirectory file)))))
249
250 (defun vc-sccs-checkin (files rev comment)
251 "SCCS-specific version of `vc-backend-checkin'."
252 (dolist (file (vc-expand-dirs files))
253 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
254 (if rev (concat "-r" rev))
255 (concat "-y" comment)
256 (vc-switches 'SCCS 'checkin))
257 (if vc-keep-workfiles
258 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
259
260 (defun vc-sccs-find-revision (file rev buffer)
261 (apply 'vc-sccs-do-command
262 buffer 0 "get" (vc-name file)
263 "-s" ;; suppress diagnostic output
264 "-p"
265 (and rev
266 (concat "-r"
267 (vc-sccs-lookup-triple file rev)))
268 (vc-switches 'SCCS 'checkout)))
269
270 (defun vc-sccs-checkout (file &optional editable rev)
271 "Retrieve a copy of a saved revision of SCCS controlled FILE.
272 If FILE is a directory, all version-controlled files beneath are checked out.
273 EDITABLE non-nil means that the file should be writable and
274 locked. REV is the revision to check out."
275 (if (file-directory-p file)
276 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
277 (let ((file-buffer (get-file-buffer file))
278 switches)
279 (message "Checking out %s..." file)
280 (save-excursion
281 ;; Change buffers to get local value of vc-checkout-switches.
282 (if file-buffer (set-buffer file-buffer))
283 (setq switches (vc-switches 'SCCS 'checkout))
284 ;; Save this buffer's default-directory
285 ;; and use save-excursion to make sure it is restored
286 ;; in the same buffer it was saved in.
287 (let ((default-directory default-directory))
288 (save-excursion
289 ;; Adjust the default-directory so that the check-out creates
290 ;; the file in the right place.
291 (setq default-directory (file-name-directory file))
292
293 (and rev (or (string= rev "")
294 (not (stringp rev)))
295 (setq rev nil))
296 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
297 (if editable "-e")
298 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
299 switches))))
300 (message "Checking out %s...done" file))))
301
302 (defun vc-sccs-rollback (files)
303 "Roll back, undoing the most recent checkins of FILES. Directories
304 are expanded to all version-controlled subfiles."
305 (setq files (vc-expand-dirs files))
306 (if (not files)
307 (error "SCCS backend doesn't support directory-level rollback"))
308 (dolist (file files)
309 (let ((discard (vc-working-revision file)))
310 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
311 discard file)))
312 (error "Aborted"))
313 (message "Removing revision %s from %s..." discard file)
314 (vc-sccs-do-command nil 0 "rmdel"
315 (vc-name file) (concat "-r" discard))
316 (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
317
318 (defun vc-sccs-revert (file &optional contents-done)
319 "Revert FILE to the version it was based on. If FILE is a directory,
320 revert all subfiles."
321 (if (file-directory-p file)
322 (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
323 (vc-sccs-do-command nil 0 "unget" (vc-name file))
324 (vc-sccs-do-command nil 0 "get" (vc-name file))
325 ;; Checking out explicit revisions is not supported under SCCS, yet.
326 ;; We always "revert" to the latest revision; therefore
327 ;; vc-working-revision is cleared here so that it gets recomputed.
328 (vc-file-setprop file 'vc-working-revision nil)))
329
330 (defun vc-sccs-steal-lock (file &optional rev)
331 "Steal the lock on the current workfile for FILE and revision REV."
332 (if (file-directory-p file)
333 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
334 (vc-sccs-do-command nil 0 "unget"
335 (vc-name file) "-n" (if rev (concat "-r" rev)))
336 (vc-sccs-do-command nil 0 "get"
337 (vc-name file) "-g" (if rev (concat "-r" rev)))))
338
339 (defun vc-sccs-modify-change-comment (files rev comment)
340 "Modify (actually, append to) the change comments for FILES on a specified REV."
341 (dolist (file (vc-expand-dirs files))
342 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
343 (concat "-y" comment) (concat "-r" rev))))
344
345 \f
346 ;;;
347 ;;; History functions
348 ;;;
349
350 (defun vc-sccs-print-log (files buffer &optional shortlog start-revision-ignored limit)
351 "Get change log associated with FILES."
352 (setq files (vc-expand-dirs files))
353 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files))
354 (when limit 'limit-unsupported))
355
356 ;; FIXME use sccsdiff if present?
357 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
358 "Get a difference report using SCCS between two filesets."
359 (setq files (vc-expand-dirs files))
360 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
361 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
362 (or buffer (setq buffer "*vc-diff*"))
363 ;; We have to reimplement pieces of vc-do-command, because
364 ;; we want to run multiple external commands, and only do the setup
365 ;; and exit pieces once.
366 (save-current-buffer
367 (unless (or (eq buffer t)
368 (and (stringp buffer) (string= (buffer-name) buffer))
369 (eq buffer (current-buffer)))
370 (vc-setup-buffer buffer))
371 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
372 (if oldvers (list (concat " -r" oldvers)))
373 (if newvers (list (concat " -r" newvers)))))
374 (fake-command
375 (format "diff%s %s"
376 (if fake-flags
377 (concat " " (mapconcat 'identity fake-flags " "))
378 "")
379 (vc-delistify files)))
380 (status 0)
381 (oldproc (get-buffer-process (current-buffer))))
382 (when vc-command-messages
383 (message "Running %s in foreground..." fake-command))
384 (if oldproc (delete-process oldproc))
385 (dolist (file files)
386 (let ((oldfile (make-temp-file "vc-sccs"))
387 newfile)
388 (unwind-protect
389 (progn
390 (vc-sccs-write-revision file oldfile oldvers)
391 (if newvers
392 (vc-sccs-write-revision file (setq newfile
393 (make-temp-file "vc-sccs"))
394 newvers))
395 (let* ((inhibit-read-only t)
396 (buffer-undo-list t)
397 (process-environment
398 (cons "LC_MESSAGES=C" process-environment))
399 (w32-quote-process-args t)
400 (this-status
401 (apply 'process-file "diff" nil t nil
402 (append (vc-switches 'SCCS 'diff)
403 (list oldfile
404 (or newfile
405 (file-relative-name file)))))))
406 (or (integerp this-status) (setq status 'error))
407 (and (integerp status)
408 (> this-status status)
409 (setq status this-status))))
410 (delete-file oldfile)
411 (if newfile (delete-file newfile)))))
412 (when (or (not (integerp status)) (> status 1))
413 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
414 (pop-to-buffer (current-buffer))
415 (goto-char (point-min))
416 (shrink-window-if-larger-than-buffer))
417 (error "Running %s...FAILED (%s)" fake-command
418 (if (integerp status) (format "status %d" status) status)))
419 (when vc-command-messages
420 (message "Running %s...OK = %d" fake-command status))
421 ;; Should we pretend we ran sccsdiff instead?
422 ;; This might not actually be a valid diff command.
423 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
424 status)))
425
426 \f
427 ;;;
428 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
429 ;;; our own set of name-to-revision mappings.
430 ;;;
431
432 (defun vc-sccs-create-tag (dir name branchp)
433 (when branchp
434 (error "SCCS backend does not support module branches"))
435 (let ((result (vc-tag-precondition dir)))
436 (if (stringp result)
437 (error "File %s is not up-to-date" result)
438 (vc-file-tree-walk
439 dir
440 (lambda (f)
441 (vc-sccs-add-triple name f (vc-working-revision f)))))))
442
443 \f
444 ;;;
445 ;;; Miscellaneous
446 ;;;
447
448 (defun vc-sccs-previous-revision (file rev)
449 (vc-call-backend 'RCS 'previous-revision file rev))
450
451 (defun vc-sccs-next-revision (file rev)
452 (vc-call-backend 'RCS 'next-revision file rev))
453
454 (defun vc-sccs-check-headers ()
455 "Check if the current file has any headers in it."
456 (save-excursion
457 (goto-char (point-min))
458 (re-search-forward "%[A-Z]%" nil t)))
459
460 (defun vc-sccs-rename-file (old new)
461 ;; Move the master file (using vc-rcs-master-templates).
462 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
463 ;; Update the tag file.
464 (with-current-buffer
465 (find-file-noselect
466 (expand-file-name vc-sccs-name-assoc-file
467 (file-name-directory (vc-name old))))
468 (goto-char (point-min))
469 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
470 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
471 (replace-match (concat ":" new) nil nil))
472 (basic-save-buffer)
473 (kill-buffer (current-buffer))))
474
475 (defun vc-sccs-find-file-hook ()
476 ;; If the file is locked by some other user, make
477 ;; the buffer read-only. Like this, even root
478 ;; cannot modify a file that someone else has locked.
479 (and (stringp (vc-state buffer-file-name 'SCCS))
480 (setq buffer-read-only t)))
481
482 \f
483 ;;;
484 ;;; Internal functions
485 ;;;
486
487 ;; This function is wrapped with `progn' so that the autoload cookie
488 ;; copies the whole function itself into loaddefs.el rather than just placing
489 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
490 ;; help us avoid loading vc-sccs.
491 ;;;###autoload
492 (progn (defun vc-sccs-search-project-dir (dirname basename)
493 "Return the name of a master file in the SCCS project directory.
494 Does not check whether the file exists but returns nil if it does not
495 find any project directory."
496 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
497 (when project-dir
498 (if (file-name-absolute-p project-dir)
499 (setq dirs '("SCCS" ""))
500 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
501 (setq project-dir (expand-file-name (concat "~" project-dir))))
502 (while (and (not dir) dirs)
503 (setq dir (expand-file-name (car dirs) project-dir))
504 (unless (file-directory-p dir)
505 (setq dir nil)
506 (setq dirs (cdr dirs))))
507 (and dir (expand-file-name (concat "s." basename) dir))))))
508
509 (defun vc-sccs-lock-file (file)
510 "Generate lock file name corresponding to FILE."
511 (let ((master (vc-name file)))
512 (and
513 master
514 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
515 (replace-match "p." t t master 2))))
516
517 (defun vc-sccs-parse-locks ()
518 "Parse SCCS locks in current buffer.
519 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
520 (let (master-locks)
521 (goto-char (point-min))
522 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
523 nil t)
524 (setq master-locks
525 (cons (cons (match-string 1) (match-string 2)) master-locks)))
526 ;; FIXME: is it really necessary to reverse ?
527 (nreverse master-locks)))
528
529 (defun vc-sccs-add-triple (name file rev)
530 (with-current-buffer
531 (find-file-noselect
532 (expand-file-name vc-sccs-name-assoc-file
533 (file-name-directory (vc-name file))))
534 (goto-char (point-max))
535 (insert name "\t:\t" file "\t" rev "\n")
536 (basic-save-buffer)
537 (kill-buffer (current-buffer))))
538
539 (defun vc-sccs-lookup-triple (file name)
540 "Return the numeric revision corresponding to a named tag of FILE.
541 If NAME is nil or a revision number string it's just passed through."
542 (if (or (null name)
543 (let ((firstchar (aref name 0)))
544 (and (>= firstchar ?0) (<= firstchar ?9))))
545 name
546 (with-temp-buffer
547 (vc-insert-file
548 (expand-file-name vc-sccs-name-assoc-file
549 (file-name-directory (vc-name file))))
550 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
551
552 (provide 'vc-sccs)
553
554 ;;; vc-sccs.el ends here