Remove logentry primitive from backend API.
[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 (defun vc-sccs-checkout-model (files) 'locking)
106
107 ;;;
108 ;;; State-querying functions
109 ;;;
110
111 ;; The autoload cookie below places vc-sccs-registered directly into
112 ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
113 ;; every file that is visited. The definition is repeated below
114 ;; so that Help and etags can find it.
115
116 ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
117 (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
118
119 (defun vc-sccs-state (file)
120 "SCCS-specific function to compute the version control state."
121 (if (not (vc-sccs-registered file))
122 'unregistered
123 (with-temp-buffer
124 (if (vc-insert-file (vc-sccs-lock-file file))
125 (let* ((locks (vc-sccs-parse-locks))
126 (working-revision (vc-working-revision file))
127 (locking-user (cdr (assoc working-revision locks))))
128 (if (not locking-user)
129 (if (vc-workfile-unchanged-p file)
130 'up-to-date
131 'unlocked-changes)
132 (if (string= locking-user (vc-user-login-name file))
133 'edited
134 locking-user)))
135 'up-to-date))))
136
137 (defun vc-sccs-state-heuristic (file)
138 "SCCS-specific state heuristic."
139 (if (not (vc-mistrust-permissions file))
140 ;; This implementation assumes that any file which is under version
141 ;; control and has -rw-r--r-- is locked by its owner. This is true
142 ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
143 ;; We have to be careful not to exclude files with execute bits on;
144 ;; scripts can be under version control too. Also, we must ignore the
145 ;; group-read and other-read bits, since paranoid users turn them off.
146 (let* ((attributes (file-attributes file 'string))
147 (owner-name (nth 2 attributes))
148 (permissions (nth 8 attributes)))
149 (if (string-match ".r-..-..-." permissions)
150 'up-to-date
151 (if (string-match ".rw..-..-." permissions)
152 (if (file-ownership-preserved-p file)
153 'edited
154 owner-name)
155 ;; Strange permissions.
156 ;; Fall through to real state computation.
157 (vc-sccs-state file))))
158 (vc-sccs-state file)))
159
160 (defun vc-sccs-dir-status (dir update-function)
161 ;; Doing loys of undividual 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 (push (list frel state) result)))
169 (funcall update-function result)))
170
171 (defun vc-sccs-working-revision (file)
172 "SCCS-specific version of `vc-working-revision'."
173 (with-temp-buffer
174 ;; The working revision is always the latest revision number.
175 ;; To find this number, search the entire delta table,
176 ;; rather than just the first entry, because the
177 ;; first entry might be a deleted ("R") revision.
178 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
179 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
180
181 (defun vc-sccs-workfile-unchanged-p (file)
182 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
183 (zerop (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
184 (list "--brief" "-q"
185 (concat "-r" (vc-working-revision file))))))
186
187 \f
188 ;;;
189 ;;; State-changing functions
190 ;;;
191
192 (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
193 ;; (let ((load-path (append vc-sccs-path load-path)))
194 ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
195 (apply 'vc-do-command buffer okstatus "sccs" file-or-list command flags))
196
197 (defun vc-sccs-create-repo ()
198 "Create a new SCCS repository."
199 ;; SCCS is totally file-oriented, so all we have to do is make the directory
200 (make-directory "SCCS"))
201
202 (defun vc-sccs-register (files &optional rev comment)
203 "Register FILES into the SCCS version-control system.
204 REV is the optional revision number for the file. COMMENT can be used
205 to provide an initial description of FILES.
206
207 `vc-register-switches' and `vc-sccs-register-switches' are passed to
208 the SCCS command (in that order).
209
210 Automatically retrieve a read-only version of the files with keywords
211 expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
212 (dolist (file files)
213 (let* ((dirname (or (file-name-directory file) ""))
214 (basename (file-name-nondirectory file))
215 (project-file (vc-sccs-search-project-dir dirname basename)))
216 (let ((vc-name
217 (or project-file
218 (format (car vc-sccs-master-templates) dirname basename))))
219 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
220 (and rev (not (string= rev "")) (concat "-r" rev))
221 "-fb"
222 (concat "-i" (file-relative-name file))
223 (and comment (concat "-y" comment))
224 (vc-switches 'SCCS 'register)))
225 (delete-file file)
226 (if vc-keep-workfiles
227 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
228
229 (defun vc-sccs-responsible-p (file)
230 "Return non-nil if SCCS thinks it would be responsible for registering FILE."
231 ;; TODO: check for all the patterns in vc-sccs-master-templates
232 (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
233 (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
234 (file-name-nondirectory file)))))
235
236 (defun vc-sccs-checkin (files rev comment)
237 "SCCS-specific version of `vc-backend-checkin'."
238 (dolist (file files)
239 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
240 (if rev (concat "-r" rev))
241 (concat "-y" comment)
242 (vc-switches 'SCCS 'checkin))
243 (if vc-keep-workfiles
244 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
245
246 (defun vc-sccs-find-revision (file rev buffer)
247 (apply 'vc-sccs-do-command
248 buffer 0 "get" (vc-name file)
249 "-s" ;; suppress diagnostic output
250 "-p"
251 (and rev
252 (concat "-r"
253 (vc-sccs-lookup-triple file rev)))
254 (vc-switches 'SCCS 'checkout)))
255
256 (defun vc-sccs-checkout (file &optional editable rev)
257 "Retrieve a copy of a saved revision of SCCS controlled FILE.
258 EDITABLE non-nil means that the file should be writable and
259 locked. REV is the revision to check out."
260 (let ((file-buffer (get-file-buffer file))
261 switches)
262 (message "Checking out %s..." file)
263 (save-excursion
264 ;; Change buffers to get local value of vc-checkout-switches.
265 (if file-buffer (set-buffer file-buffer))
266 (setq switches (vc-switches 'SCCS 'checkout))
267 ;; Save this buffer's default-directory
268 ;; and use save-excursion to make sure it is restored
269 ;; in the same buffer it was saved in.
270 (let ((default-directory default-directory))
271 (save-excursion
272 ;; Adjust the default-directory so that the check-out creates
273 ;; the file in the right place.
274 (setq default-directory (file-name-directory file))
275
276 (and rev (or (string= rev "")
277 (not (stringp rev)))
278 (setq rev nil))
279 (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
280 (if editable "-e")
281 (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
282 switches))))
283 (message "Checking out %s...done" file)))
284
285 (defun vc-sccs-rollback (files)
286 "Roll back, undoing the most recent checkins of FILES."
287 (if (not files)
288 (error "SCCS backend doesn't support directory-level rollback."))
289 (dolist (file files)
290 (let ((discard (vc-working-revision file)))
291 (if (null (yes-or-no-p (format "Remove version %s from %s history? "
292 discard file)))
293 (error "Aborted"))
294 (message "Removing revision %s from %s..." discard file)
295 (vc-sccs-do-command nil 0 "rmdel"
296 (vc-name file) (concat "-r" discard))
297 (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
298
299 (defun vc-sccs-revert (file &optional contents-done)
300 "Revert FILE to the version it was based on."
301 (vc-sccs-do-command nil 0 "unget" (vc-name file))
302 (vc-sccs-do-command nil 0 "get" (vc-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 (vc-sccs-do-command nil 0 "unget"
311 (vc-name file) "-n" (if rev (concat "-r" rev)))
312 (vc-sccs-do-command nil 0 "get"
313 (vc-name file) "-g" (if rev (concat "-r" rev))))
314
315 (defun vc-sccs-modify-change-comment (files rev comment)
316 "Modify (actually, append to) the change comments for FILES on a specified REV."
317 (dolist (file files)
318 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
319 (concat "-y" comment) (concat "-r" rev))))
320
321 \f
322 ;;;
323 ;;; History functions
324 ;;;
325
326 (defun vc-sccs-print-log (files &optional buffer)
327 "Get change log associated with FILES."
328 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files)))
329
330 (defun vc-sccs-wash-log ()
331 "Remove all non-comment information from log output."
332 ;; FIXME: not implemented for SCCS
333 nil)
334
335 (defun vc-sccs-diff (files &optional oldvers newvers buffer)
336 "Get a difference report using SCCS between two filesets."
337 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
338 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
339 (apply 'vc-do-command (or buffer "*vc-diff*")
340 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
341 (append (list "-q"
342 (and oldvers (concat "-r" oldvers))
343 (and newvers (concat "-r" newvers)))
344 (vc-switches 'SCCS 'diff))))
345
346 \f
347 ;;;
348 ;;; Snapshot system
349 ;;;
350
351 (defun vc-sccs-assign-name (file name)
352 "Assign to FILE's latest revision a given NAME."
353 (vc-sccs-add-triple name file (vc-working-revision file)))
354
355 \f
356 ;;;
357 ;;; Miscellaneous
358 ;;;
359
360 (defun vc-sccs-check-headers ()
361 "Check if the current file has any headers in it."
362 (save-excursion
363 (goto-char (point-min))
364 (re-search-forward "%[A-Z]%" nil t)))
365
366 (defun vc-sccs-rename-file (old new)
367 ;; Move the master file (using vc-rcs-master-templates).
368 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
369 ;; Update the snapshot file.
370 (with-current-buffer
371 (find-file-noselect
372 (expand-file-name vc-sccs-name-assoc-file
373 (file-name-directory (vc-name old))))
374 (goto-char (point-min))
375 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
376 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
377 (replace-match (concat ":" new) nil nil))
378 (basic-save-buffer)
379 (kill-buffer (current-buffer))))
380
381 \f
382 ;;;
383 ;;; Internal functions
384 ;;;
385
386 (defun vc-sccs-root (dir)
387 (vc-find-root dir "SCCS" t))
388
389 ;; This function is wrapped with `progn' so that the autoload cookie
390 ;; copies the whole function itself into loaddefs.el rather than just placing
391 ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
392 ;; help us avoid loading vc-sccs.
393 ;;;###autoload
394 (progn (defun vc-sccs-search-project-dir (dirname basename)
395 "Return the name of a master file in the SCCS project directory.
396 Does not check whether the file exists but returns nil if it does not
397 find any project directory."
398 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
399 (when project-dir
400 (if (file-name-absolute-p project-dir)
401 (setq dirs '("SCCS" ""))
402 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
403 (setq project-dir (expand-file-name (concat "~" project-dir))))
404 (while (and (not dir) dirs)
405 (setq dir (expand-file-name (car dirs) project-dir))
406 (unless (file-directory-p dir)
407 (setq dir nil)
408 (setq dirs (cdr dirs))))
409 (and dir (expand-file-name (concat "s." basename) dir))))))
410
411 (defun vc-sccs-lock-file (file)
412 "Generate lock file name corresponding to FILE."
413 (let ((master (vc-name file)))
414 (and
415 master
416 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
417 (replace-match "p." t t master 2))))
418
419 (defun vc-sccs-parse-locks ()
420 "Parse SCCS locks in current buffer.
421 The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
422 (let (master-locks)
423 (goto-char (point-min))
424 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
425 nil t)
426 (setq master-locks
427 (cons (cons (match-string 1) (match-string 2)) master-locks)))
428 ;; FIXME: is it really necessary to reverse ?
429 (nreverse master-locks)))
430
431 (defun vc-sccs-add-triple (name file rev)
432 (with-current-buffer
433 (find-file-noselect
434 (expand-file-name vc-sccs-name-assoc-file
435 (file-name-directory (vc-name file))))
436 (goto-char (point-max))
437 (insert name "\t:\t" file "\t" rev "\n")
438 (basic-save-buffer)
439 (kill-buffer (current-buffer))))
440
441 (defun vc-sccs-lookup-triple (file name)
442 "Return the numeric revision corresponding to a named snapshot of FILE.
443 If NAME is nil or a revision number string it's just passed through."
444 (if (or (null name)
445 (let ((firstchar (aref name 0)))
446 (and (>= firstchar ?0) (<= firstchar ?9))))
447 name
448 (with-temp-buffer
449 (vc-insert-file
450 (expand-file-name vc-sccs-name-assoc-file
451 (file-name-directory (vc-name file))))
452 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
453
454 (provide 'vc-sccs)
455
456 ;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
457 ;;; vc-sccs.el ends here