]> code.delx.au - gnu-emacs/blob - lisp/vc/vc-sccs.el
Clean up a longstanding to-do item.
[gnu-emacs] / lisp / vc / vc-sccs.el
1 ;;; vc-sccs.el --- support for SCCS version-control -*- lexical-binding:t -*-
2
3 ;; Copyright (C) 1992-2014 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 ;; This needs to be autoloaded because vc-sccs-registered uses it (via
78 ;; vc-default-registered), and vc-hooks needs to be able to check
79 ;; for a registered backend without loading every backend.
80 ;;;###autoload
81 (defcustom vc-sccs-master-templates
82 (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
83 "Where to look for SCCS master files.
84 For a description of possible values, see `vc-check-master-templates'."
85 :type '(choice (const :tag "Use standard SCCS file names"
86 ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
87 (repeat :tag "User-specified"
88 (choice string
89 function)))
90 :version "21.1"
91 :group 'vc-sccs)
92
93 \f
94 ;;;
95 ;;; Internal variables
96 ;;;
97
98 (defconst vc-sccs-name-assoc-file "VC-names")
99
100 \f
101 ;;; Properties of the backend
102
103 (defun vc-sccs-revision-granularity () 'file)
104 (defun vc-sccs-checkout-model (_files) 'locking)
105
106 ;;;
107 ;;; State-querying functions
108 ;;;
109
110 ;; The autoload cookie below places vc-sccs-registered directly into
111 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
112 ;; every file that is visited.
113 ;;;###autoload
114 (progn
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-sccs-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 (autoload 'vc-expand-dirs "vc")
136
137 (defun vc-sccs-dir-status (dir update-function)
138 ;; Doing lots of individual VC-state calls is painful, but
139 ;; there is no better option in SCCS-land.
140 (let ((flist (vc-expand-dirs (list dir) 'SCCS))
141 (result nil))
142 (dolist (file flist)
143 (let ((state (vc-state file))
144 (frel (file-relative-name file)))
145 (when (and (eq (vc-backend file) 'SCCS)
146 (not (eq state 'up-to-date)))
147 (push (list frel state) result))))
148 (funcall update-function result)))
149
150 (autoload 'vc-master-name "vc-filewise")
151
152 (defun vc-sccs-working-revision (file)
153 "SCCS-specific version of `vc-working-revision'."
154 (with-temp-buffer
155 ;; The working revision is always the latest revision number.
156 ;; To find this number, search the entire delta table,
157 ;; rather than just the first entry, because the
158 ;; first entry might be a deleted ("R") revision.
159 (vc-insert-file (vc-master-name file) "^\001e\n\001[^s]")
160 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
161
162 ;; Cf vc-sccs-find-revision.
163 (defun vc-sccs-write-revision (file outfile &optional rev)
164 "Write the SCCS version of input file FILE to output file OUTFILE.
165 Optional string REV is a revision."
166 (with-temp-buffer
167 (apply 'vc-sccs-do-command t 0 "get" (vc-master-name file)
168 (append '("-s" "-p" "-k") ; -k: no keyword expansion
169 (if rev (list (concat "-r" rev)))))
170 (write-region nil nil outfile nil 'silent)))
171
172 (defun vc-sccs-workfile-unchanged-p (file)
173 "Has FILE remained unchanged since last checkout?"
174 (let ((tempfile (make-temp-file "vc-sccs")))
175 (unwind-protect
176 (progn
177 (vc-sccs-write-revision file tempfile (vc-working-revision file))
178 (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
179 (delete-file tempfile))))
180
181 \f
182 ;;;
183 ;;; State-changing functions
184 ;;;
185
186 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
187 ;; (let ((load-path (append vc-sccs-path load-path)))
188 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
189 (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
190
191 (defun vc-sccs-create-repo ()
192 "Create a new SCCS repository."
193 ;; SCCS is totally file-oriented, so all we have to do is make the directory
194 (make-directory "SCCS"))
195
196 (autoload 'vc-switches "vc")
197
198 (defun vc-sccs-register (files &optional comment)
199 "Register FILES into the SCCS version-control system.
200 COMMENT can be used to provide an initial description of FILES.
201 Passes either `vc-sccs-register-switches' or `vc-register-switches'
202 to the SCCS command.
203
204 Automatically retrieve a read-only version of the files with keywords
205 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
206 (dolist (file files)
207 (let* ((dirname (or (file-name-directory file) ""))
208 (basename (file-name-nondirectory file))
209 (project-file (vc-sccs-search-project-dir dirname basename)))
210 (let ((vc-master-name
211 (or project-file
212 (format (car vc-sccs-master-templates) dirname basename))))
213 (apply 'vc-sccs-do-command nil 0 "admin" vc-master-name
214 "-fb"
215 (concat "-i" (file-relative-name file))
216 (and comment (concat "-y" comment))
217 (vc-switches 'SCCS 'register)))
218 (delete-file file)
219 (if vc-keep-workfiles
220 (vc-sccs-do-command nil 0 "get" (vc-master-name file))))))
221
222 (defun vc-sccs-responsible-p (file)
223 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
224 ;; TODO: check for all the patterns in vc-sccs-master-templates
225 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
226 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
227 (file-name-nondirectory file)))))
228
229 (defun vc-sccs-checkin (files comment)
230 "SCCS-specific version of `vc-backend-checkin'."
231 (dolist (file (vc-expand-dirs files 'SCCS))
232 (apply 'vc-sccs-do-command nil 0 "delta" (vc-master-name file)
233 (concat "-y" comment)
234 (vc-switches 'SCCS 'checkin))
235 (if vc-keep-workfiles
236 (vc-sccs-do-command nil 0 "get" (vc-master-name file)))))
237
238 (defun vc-sccs-find-revision (file rev buffer)
239 (apply 'vc-sccs-do-command
240 buffer 0 "get" (vc-master-name file)
241 "-s" ;; suppress diagnostic output
242 "-p"
243 (and rev
244 (concat "-r"
245 (vc-sccs-lookup-triple file rev)))
246 (vc-switches 'SCCS 'checkout)))
247
248 (defun vc-sccs-checkout (file &optional rev)
249 "Retrieve a copy of a saved revision of SCCS controlled FILE.
250 If FILE is a directory, all version-controlled files beneath are checked out.
251 EDITABLE non-nil means that the file should be writable and
252 locked. REV is the revision to check out."
253 (if (file-directory-p file)
254 (mapc 'vc-sccs-checkout (vc-expand-dirs (list file) 'SCCS))
255 (let ((file-buffer (get-file-buffer file))
256 switches)
257 (message "Checking out %s..." file)
258 (save-excursion
259 ;; Change buffers to get local value of vc-checkout-switches.
260 (if file-buffer (set-buffer file-buffer))
261 (setq switches (vc-switches 'SCCS 'checkout))
262 ;; Save this buffer's default-directory
263 ;; and use save-excursion to make sure it is restored
264 ;; in the same buffer it was saved in.
265 (let ((default-directory default-directory))
266 (save-excursion
267 ;; Adjust the default-directory so that the check-out creates
268 ;; the file in the right place.
269 (setq default-directory (file-name-directory file))
270
271 (and rev (or (string= rev "")
272 (not (stringp rev)))
273 (setq rev nil))
274 (apply 'vc-sccs-do-command nil 0 "get" (vc-master-name file)
275 "-e"
276 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
277 switches))))
278 (message "Checking out %s...done" file))))
279
280 (defun vc-sccs-rollback (files)
281 "Roll back, undoing the most recent checkins of FILES. Directories
282 are expanded to all version-controlled subfiles."
283 (setq files (vc-expand-dirs files 'SCCS))
284 (if (not files)
285 (error "SCCS backend doesn't support directory-level rollback"))
286 (dolist (file files)
287 (let ((discard (vc-working-revision file)))
288 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
289 discard file)))
290 (error "Aborted"))
291 (message "Removing revision %s from %s..." discard file)
292 (vc-sccs-do-command nil 0 "rmdel"
293 (vc-master-name file) (concat "-r" discard))
294 (vc-sccs-do-command nil 0 "get" (vc-master-name file) nil))))
295
296 (defun vc-sccs-revert (file &optional _contents-done)
297 "Revert FILE to the version it was based on. If FILE is a directory,
298 revert all subfiles."
299 (if (file-directory-p file)
300 (mapc 'vc-sccs-revert (vc-expand-dirs (list file) 'SCCS))
301 (vc-sccs-do-command nil 0 "unget" (vc-master-name file))
302 (vc-sccs-do-command nil 0 "get" (vc-master-name file))
303 ;; Checking out explicit revisions is not supported under SCCS, yet.
304 ;; We always "revert" to the latest revision; therefore
305 ;; vc-working-revision is cleared here so that it gets recomputed.
306 (vc-file-setprop file 'vc-working-revision nil)))
307
308 (defun vc-sccs-steal-lock (file &optional rev)
309 "Steal the lock on the current workfile for FILE and revision REV."
310 (if (file-directory-p file)
311 (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file) 'SCCS))
312 (vc-sccs-do-command nil 0 "unget"
313 (vc-master-name file) "-n" (if rev (concat "-r" rev)))
314 (vc-sccs-do-command nil 0 "get"
315 (vc-master-name file) "-g" (if rev (concat "-r" rev)))))
316
317 (defun vc-sccs-modify-change-comment (files rev comment)
318 "Modify (actually, append to) the change comments for FILES on a specified REV."
319 (dolist (file (vc-expand-dirs files 'SCCS))
320 (vc-sccs-do-command nil 0 "cdc" (vc-master-name file)
321 (concat "-y" comment) (concat "-r" rev))))
322
323 \f
324 ;;;
325 ;;; History functions
326 ;;;
327
328 (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
329 "Print commit log associated with FILES into specified BUFFER.
330 Remaining arguments are ignored."
331 (setq files (vc-expand-dirs files 'SCCS))
332 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-master-name files))
333 (when limit 'limit-unsupported))
334
335 (autoload 'vc-setup-buffer "vc-dispatcher")
336 (autoload 'vc-delistify "vc-dispatcher")
337
338 (defvar w32-quote-process-args)
339
340 ;; FIXME use sccsdiff if present?
341 (defun vc-sccs-diff (files &optional _async oldvers newvers buffer)
342 "Get a difference report using SCCS between two filesets."
343 (setq files (vc-expand-dirs files 'SCCS))
344 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
345 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
346 (or buffer (setq buffer "*vc-diff*"))
347 ;; We have to reimplement pieces of vc-do-command, because
348 ;; we want to run multiple external commands, and only do the setup
349 ;; and exit pieces once.
350 (save-current-buffer
351 (unless (or (eq buffer t)
352 (and (stringp buffer) (string= (buffer-name) buffer))
353 (eq buffer (current-buffer)))
354 (vc-setup-buffer buffer))
355 (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
356 (if oldvers (list (concat " -r" oldvers)))
357 (if newvers (list (concat " -r" newvers)))))
358 (fake-command
359 (format "diff%s %s"
360 (if fake-flags
361 (concat " " (mapconcat 'identity fake-flags " "))
362 "")
363 (vc-delistify files)))
364 (status 0)
365 (oldproc (get-buffer-process (current-buffer))))
366 (when vc-command-messages
367 (message "Running %s in foreground..." fake-command))
368 (if oldproc (delete-process oldproc))
369 (dolist (file files)
370 (let ((oldfile (make-temp-file "vc-sccs"))
371 newfile)
372 (unwind-protect
373 (progn
374 (vc-sccs-write-revision file oldfile oldvers)
375 (if newvers
376 (vc-sccs-write-revision file (setq newfile
377 (make-temp-file "vc-sccs"))
378 newvers))
379 (let* ((inhibit-read-only t)
380 (buffer-undo-list t)
381 (process-environment
382 (cons "LC_MESSAGES=C" process-environment))
383 (w32-quote-process-args t)
384 (this-status
385 (apply 'process-file "diff" nil t nil
386 (append (vc-switches 'SCCS 'diff)
387 (list oldfile
388 (or newfile
389 (file-relative-name file)))))))
390 (or (integerp this-status) (setq status 'error))
391 (and (integerp status)
392 (> this-status status)
393 (setq status this-status))))
394 (delete-file oldfile)
395 (if newfile (delete-file newfile)))))
396 (when (or (not (integerp status)) (> status 1))
397 (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
398 (pop-to-buffer (current-buffer))
399 (goto-char (point-min))
400 (shrink-window-if-larger-than-buffer))
401 (error "Running %s...FAILED (%s)" fake-command
402 (if (integerp status) (format "status %d" status) status)))
403 (when vc-command-messages
404 (message "Running %s...OK = %d" fake-command status))
405 ;; Should we pretend we ran sccsdiff instead?
406 ;; This might not actually be a valid diff command.
407 (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
408 status)))
409
410 \f
411 ;;;
412 ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
413 ;;; our own set of name-to-revision mappings.
414 ;;;
415
416 (autoload 'vc-tag-precondition "vc")
417 (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
418
419 (defun vc-sccs-create-tag (dir name branchp)
420 (when branchp
421 (error "SCCS backend does not support module branches"))
422 (let ((result (vc-tag-precondition dir)))
423 (if (stringp result)
424 (error "File %s is not up-to-date" result)
425 (vc-file-tree-walk
426 dir
427 (lambda (f)
428 (vc-sccs-add-triple name f (vc-working-revision f)))))))
429
430 \f
431 ;;;
432 ;;; Miscellaneous
433 ;;;
434
435 (defun vc-sccs-previous-revision (file rev)
436 (vc-call-backend 'RCS 'previous-revision file rev))
437
438 (defun vc-sccs-next-revision (file rev)
439 (vc-call-backend 'RCS 'next-revision file rev))
440
441 (defun vc-sccs-check-headers ()
442 "Check if the current file has any headers in it."
443 (save-excursion
444 (goto-char (point-min))
445 (re-search-forward "%[A-Z]%" nil t)))
446
447 (autoload 'vc-rename-master "vc-filewise")
448
449 (defun vc-sccs-rename-file (old new)
450 ;; Move the master file (using vc-rcs-master-templates).
451 (vc-rename-master (vc-master-name old) new vc-sccs-master-templates)
452 ;; Update the tag file.
453 (with-current-buffer
454 (find-file-noselect
455 (expand-file-name vc-sccs-name-assoc-file
456 (file-name-directory (vc-master-name old))))
457 (goto-char (point-min))
458 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
459 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
460 (replace-match (concat ":" new) nil nil))
461 (basic-save-buffer)
462 (kill-buffer (current-buffer))))
463
464 (defun vc-sccs-find-file-hook ()
465 ;; If the file is locked by some other user, make
466 ;; the buffer read-only. Like this, even root
467 ;; cannot modify a file that someone else has locked.
468 (and (stringp (vc-state buffer-file-name 'SCCS))
469 (setq buffer-read-only t)))
470
471 \f
472 ;;;
473 ;;; Internal functions
474 ;;;
475
476 ;; This function is wrapped with `progn' so that the autoload cookie
477 ;; copies the whole function itself into loaddefs.el rather than just placing
478 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
479 ;; help us avoid loading vc-sccs.
480 ;;;###autoload
481 (progn (defun vc-sccs-search-project-dir (_dirname basename)
482 "Return the name of a master file in the SCCS project directory.
483 Does not check whether the file exists but returns nil if it does not
484 find any project directory."
485 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
486 (when project-dir
487 (if (file-name-absolute-p project-dir)
488 (setq dirs '("SCCS" ""))
489 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
490 (setq project-dir (expand-file-name (concat "~" project-dir))))
491 (while (and (not dir) dirs)
492 (setq dir (expand-file-name (car dirs) project-dir))
493 (unless (file-directory-p dir)
494 (setq dir nil)
495 (setq dirs (cdr dirs))))
496 (and dir (expand-file-name (concat "s." basename) dir))))))
497
498 (defun vc-sccs-lock-file (file)
499 "Generate lock file name corresponding to FILE."
500 (let ((master (vc-master-name file)))
501 (and
502 master
503 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
504 (replace-match "p." t t master 2))))
505
506 (defun vc-sccs-parse-locks ()
507 "Parse SCCS locks in current buffer.
508 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
509 (let (master-locks)
510 (goto-char (point-min))
511 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
512 nil t)
513 (setq master-locks
514 (cons (cons (match-string 1) (match-string 2)) master-locks)))
515 ;; FIXME: is it really necessary to reverse ?
516 (nreverse master-locks)))
517
518 (defun vc-sccs-add-triple (name file rev)
519 (with-current-buffer
520 (find-file-noselect
521 (expand-file-name vc-sccs-name-assoc-file
522 (file-name-directory (vc-master-name file))))
523 (goto-char (point-max))
524 (insert name "\t:\t" file "\t" rev "\n")
525 (basic-save-buffer)
526 (kill-buffer (current-buffer))))
527
528 (defun vc-sccs-lookup-triple (file name)
529 "Return the numeric revision corresponding to a named tag of FILE.
530 If NAME is nil or a revision number string it's just passed through."
531 (if (or (null name)
532 (let ((firstchar (aref name 0)))
533 (and (>= firstchar ?0) (<= firstchar ?9))))
534 name
535 (with-temp-buffer
536 (vc-insert-file
537 (expand-file-name vc-sccs-name-assoc-file
538 (file-name-directory (vc-master-name file))))
539 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
540
541 (provide 'vc-sccs)
542
543 ;;; vc-sccs.el ends here