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