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