Remove wash-log from the VC backend API.
[bpt/emacs.git] / lisp / vc-sccs.el
CommitLineData
7a004b71
GM
1;;; vc-sccs.el --- support for SCCS version-control
2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
59465d84
GM
4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
5;; Free Software Foundation, Inc.
7a004b71
GM
6
7;; Author: FSF (see vc.el for full credits)
8;; Maintainer: Andre Spiegel <spiegel@gnu.org>
9
7a004b71
GM
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
7a004b71 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
7a004b71
GM
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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
7a004b71
GM
24
25;;; Commentary:
26
27;;; Code:
28
f1180544 29(eval-when-compile
a1e9f194
AS
30 (require 'vc))
31
f1180544 32;;;
8f98485f
AS
33;;; Customization options
34;;;
35
14b11401
SM
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
7a004b71 47(defcustom vc-sccs-register-switches nil
a7cafade
DL
48 "*Extra switches for registering a file in SCCS.
49A string or list of strings passed to the checkin program by
7a004b71
GM
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))
a7cafade 56 :version "21.1"
7a004b71
GM
57 :group 'vc)
58
a1e9f194
AS
59(defcustom vc-sccs-diff-switches nil
60 "*A string or list of strings specifying extra switches for `vcdiff',
61the 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
7a004b71
GM
70(defcustom vc-sccs-header (or (cdr (assoc 'SCCS vc-header-alist)) '("%W%"))
71 "*Header keywords to be inserted by `vc-insert-headers'."
8b7c8991 72 :type '(repeat string)
7a004b71
GM
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.
79For 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)))
a7cafade 85 :version "21.1"
7a004b71
GM
86 :group 'vc)
87
8f98485f
AS
88\f
89;;;
90;;; Internal variables
91;;;
92
099bd78a
SM
93(defconst vc-sccs-name-assoc-file "VC-names")
94
8f98485f 95\f
8cdd17b4
ER
96;;; Properties of the backend
97
14b11401 98(defun vc-sccs-revision-granularity () 'file)
70e2f6c7 99(defun vc-sccs-checkout-model (files) 'locking)
8cdd17b4 100
8f98485f
AS
101;;;
102;;; State-querying functions
103;;;
104
14b11401
SM
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.
9ef33737
AS
109
110;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
af65391b 111(defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
7a004b71
GM
112
113(defun vc-sccs-state (file)
114 "SCCS-specific function to compute the version control state."
3702367b
ER
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))))
7a004b71
GM
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.
bbd88348
AS
140 (let* ((attributes (file-attributes file 'string))
141 (owner-name (nth 2 attributes))
7a004b71
GM
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
bbd88348 148 owner-name)
8bfde4be
AS
149 ;; Strange permissions.
150 ;; Fall through to real state computation.
151 (vc-sccs-state file))))
152 (vc-sccs-state file)))
7a004b71 153
c1b51374 154(defun vc-sccs-dir-status (dir update-function)
e7290559 155 ;; Doing lots of individual VC-state calls is painful, but
5a9de6d0 156 ;; there is no better option in SCCS-land.
90e9ca17
DN
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)))
c1b51374 163 (funcall update-function result)))
90e9ca17 164
ac3f4c6f
ER
165(defun vc-sccs-working-revision (file)
166 "SCCS-specific version of `vc-working-revision'."
7a004b71 167 (with-temp-buffer
5b5afd50 168 ;; The working revision is always the latest revision number.
8bfde4be
AS
169 ;; To find this number, search the entire delta table,
170 ;; rather than just the first entry, because the
5b5afd50 171 ;; first entry might be a deleted ("R") revision.
8bfde4be 172 (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
7a004b71
GM
173 (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
174
7a004b71 175(defun vc-sccs-workfile-unchanged-p (file)
fa63cb6d 176 "SCCS-specific implementation of `vc-workfile-unchanged-p'."
a1e9f194
AS
177 (zerop (apply 'vc-do-command nil 1 "vcdiff" (vc-name file)
178 (list "--brief" "-q"
ac3f4c6f 179 (concat "-r" (vc-working-revision file))))))
7a004b71 180
7a004b71 181\f
8f98485f
AS
182;;;
183;;; State-changing functions
184;;;
7a004b71 185
14b11401
SM
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
8cdd17b4
ER
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.
7a004b71 198REV is the optional revision number for the file. COMMENT can be used
8cdd17b4 199to provide an initial description of FILES.
7a004b71
GM
200
201`vc-register-switches' and `vc-sccs-register-switches' are passed to
202the SCCS command (in that order).
203
8cdd17b4 204Automatically retrieve a read-only version of the files with keywords
7a004b71 205expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
8cdd17b4 206 (dolist (file files)
dc0f87c9 207 (let* ((dirname (or (file-name-directory file) ""))
7a004b71
GM
208 (basename (file-name-nondirectory file))
209 (project-file (vc-sccs-search-project-dir dirname basename)))
210 (let ((vc-name
211 (or project-file
dc0f87c9 212 (format (car vc-sccs-master-templates) dirname basename))))
14b11401 213 (apply 'vc-sccs-do-command nil 0 "admin" vc-name
8cdd17b4 214 (and rev (not (string= rev "")) (concat "-r" rev))
7a004b71 215 "-fb"
0cccd8ff 216 (concat "-i" (file-relative-name file))
7a004b71 217 (and comment (concat "-y" comment))
dc0f87c9 218 (vc-switches 'SCCS 'register)))
7a004b71
GM
219 (delete-file file)
220 (if vc-keep-workfiles
14b11401 221 (vc-sccs-do-command nil 0 "get" (vc-name file))))))
7a004b71 222
8f98485f
AS
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
8cdd17b4 230(defun vc-sccs-checkin (files rev comment)
8f98485f 231 "SCCS-specific version of `vc-backend-checkin'."
e7290559 232 (dolist (file (vc-expand-dirs files))
14b11401 233 (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
8cdd17b4
ER
234 (if rev (concat "-r" rev))
235 (concat "-y" comment)
236 (vc-switches 'SCCS 'checkin))
237 (if vc-keep-workfiles
14b11401 238 (vc-sccs-do-command nil 0 "get" (vc-name file)))))
8f98485f 239
ac3f4c6f 240(defun vc-sccs-find-revision (file rev buffer)
14b11401 241 (apply 'vc-sccs-do-command
52876673
SM
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)))
dc0f87c9 248 (vc-switches 'SCCS 'checkout)))
52876673
SM
249
250(defun vc-sccs-checkout (file &optional editable rev)
5b5afd50 251 "Retrieve a copy of a saved revision of SCCS controlled FILE.
e7290559 252If FILE is a directory, all version-controlled files beneath are checked out.
1862f9ef 253EDITABLE non-nil means that the file should be writable and
52876673 254locked. REV is the revision to check out."
e7290559
ER
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))))
7a004b71 281
5b5afd50 282(defun vc-sccs-rollback (files)
e7290559
ER
283 "Roll back, undoing the most recent checkins of FILES. Directories
284are expanded to all version-controlled subfiles."
285 (setq files (vc-expand-dirs files))
8cdd17b4
ER
286 (if (not files)
287 (error "SCCS backend doesn't support directory-level rollback."))
288 (dolist (file files)
ac3f4c6f 289 (let ((discard (vc-working-revision file)))
8cdd17b4
ER
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)
14b11401
SM
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))))
8cdd17b4 297
99739bbf 298(defun vc-sccs-revert (file &optional contents-done)
e7290559
ER
299 "Revert FILE to the version it was based on. If FILE is a directory,
300revert 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)))
8f98485f 309
8f98485f
AS
310(defun vc-sccs-steal-lock (file &optional rev)
311 "Steal the lock on the current workfile for FILE and revision REV."
e7290559
ER
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)))))
8f98485f 318
9b64a7f0
ER
319(defun vc-sccs-modify-change-comment (files rev comment)
320 "Modify (actually, append to) the change comments for FILES on a specified REV."
e7290559 321 (dolist (file (vc-expand-dirs files))
14b11401
SM
322 (vc-sccs-do-command nil 0 "cdc" (vc-name file)
323 (concat "-y" comment) (concat "-r" rev))))
9b64a7f0 324
8f98485f
AS
325\f
326;;;
327;;; History functions
328;;;
329
8cdd17b4
ER
330(defun vc-sccs-print-log (files &optional buffer)
331 "Get change log associated with FILES."
e7290559 332 (setq files (vc-expand-dirs files))
14b11401 333 (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files)))
8cdd17b4 334
8cdd17b4
ER
335(defun vc-sccs-diff (files &optional oldvers newvers buffer)
336 "Get a difference report using SCCS between two filesets."
e7290559 337 (setq files (vc-expand-dirs files))
e46e905a
GM
338 (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
339 (setq newvers (vc-sccs-lookup-triple (car files) newvers))
59465d84 340 (apply 'vc-do-command (or buffer "*vc-diff*")
8cdd17b4 341 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
a1e9f194
AS
342 (append (list "-q"
343 (and oldvers (concat "-r" oldvers))
344 (and newvers (concat "-r" newvers)))
dc0f87c9 345 (vc-switches 'SCCS 'diff))))
8f98485f
AS
346
347\f
348;;;
349;;; Snapshot system
350;;;
351
352(defun vc-sccs-assign-name (file name)
5b5afd50 353 "Assign to FILE's latest revision a given NAME."
ac3f4c6f 354 (vc-sccs-add-triple name file (vc-working-revision file)))
8f98485f
AS
355
356\f
357;;;
358;;; Miscellaneous
359;;;
360
361(defun vc-sccs-check-headers ()
362 "Check if the current file has any headers in it."
363 (save-excursion
364 (goto-char (point-min))
365 (re-search-forward "%[A-Z]%" nil t)))
366
367(defun vc-sccs-rename-file (old new)
368 ;; Move the master file (using vc-rcs-master-templates).
369 (vc-rename-master (vc-name old) new vc-sccs-master-templates)
370 ;; Update the snapshot file.
371 (with-current-buffer
372 (find-file-noselect
373 (expand-file-name vc-sccs-name-assoc-file
374 (file-name-directory (vc-name old))))
375 (goto-char (point-min))
376 ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
377 (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
378 (replace-match (concat ":" new) nil nil))
379 (basic-save-buffer)
380 (kill-buffer (current-buffer))))
381
382\f
383;;;
384;;; Internal functions
385;;;
386
6d587cb5
TTN
387(defun vc-sccs-root (dir)
388 (vc-find-root dir "SCCS" t))
389
8f98485f
AS
390;; This function is wrapped with `progn' so that the autoload cookie
391;; copies the whole function itself into loaddefs.el rather than just placing
392;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
393;; help us avoid loading vc-sccs.
394;;;###autoload
395(progn (defun vc-sccs-search-project-dir (dirname basename)
396 "Return the name of a master file in the SCCS project directory.
397Does not check whether the file exists but returns nil if it does not
398find any project directory."
399 (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
400 (when project-dir
401 (if (file-name-absolute-p project-dir)
402 (setq dirs '("SCCS" ""))
403 (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
404 (setq project-dir (expand-file-name (concat "~" project-dir))))
405 (while (and (not dir) dirs)
406 (setq dir (expand-file-name (car dirs) project-dir))
407 (unless (file-directory-p dir)
408 (setq dir nil)
409 (setq dirs (cdr dirs))))
410 (and dir (expand-file-name (concat "s." basename) dir))))))
411
412(defun vc-sccs-lock-file (file)
413 "Generate lock file name corresponding to FILE."
414 (let ((master (vc-name file)))
415 (and
416 master
417 (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
418 (replace-match "p." t t master 2))))
419
420(defun vc-sccs-parse-locks ()
421 "Parse SCCS locks in current buffer.
5b5afd50 422The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
8f98485f
AS
423 (let (master-locks)
424 (goto-char (point-min))
425 (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
426 nil t)
427 (setq master-locks
428 (cons (cons (match-string 1) (match-string 2)) master-locks)))
429 ;; FIXME: is it really necessary to reverse ?
430 (nreverse master-locks)))
431
432(defun vc-sccs-add-triple (name file rev)
433 (with-current-buffer
434 (find-file-noselect
435 (expand-file-name vc-sccs-name-assoc-file
436 (file-name-directory (vc-name file))))
437 (goto-char (point-max))
438 (insert name "\t:\t" file "\t" rev "\n")
439 (basic-save-buffer)
440 (kill-buffer (current-buffer))))
441
442(defun vc-sccs-lookup-triple (file name)
5b5afd50
ER
443 "Return the numeric revision corresponding to a named snapshot of FILE.
444If NAME is nil or a revision number string it's just passed through."
8f98485f
AS
445 (if (or (null name)
446 (let ((firstchar (aref name 0)))
447 (and (>= firstchar ?0) (<= firstchar ?9))))
448 name
449 (with-temp-buffer
450 (vc-insert-file
451 (expand-file-name vc-sccs-name-assoc-file
452 (file-name-directory (vc-name file))))
453 (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
7a004b71
GM
454
455(provide 'vc-sccs)
456
fa63cb6d 457;; arch-tag: d751dee3-d7b3-47e1-95e3-7ae98c052041
7a004b71 458;;; vc-sccs.el ends here