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