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