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