vc-sccs.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. ;;; vc-sccs.el --- support for SCCS version-control -*- lexical-binding:t -*-
  2. ;; Copyright (C) 1992-2015 Free Software Foundation, Inc.
  3. ;; Author: FSF (see vc.el for full credits)
  4. ;; Maintainer: Andre Spiegel <spiegel@gnu.org>
  5. ;; Package: vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'vc))
  21. ;;;
  22. ;;; Customization options
  23. ;;;
  24. ;; ;; Maybe a better solution is to not use "get" but "sccs get".
  25. ;; ;; Note for GNU CSSC, you can parse sccs -V to get the libexec path.
  26. ;; (defcustom vc-sccs-path
  27. ;; (prune-directory-list '("/usr/ccs/bin" "/usr/sccs" "/usr/lib/sccs"
  28. ;; "/usr/libexec/sccs"))
  29. ;; "List of extra directories to search for SCCS commands."
  30. ;; :type '(repeat directory)
  31. ;; :group 'vc)
  32. (defgroup vc-sccs nil
  33. "VC SCCS backend."
  34. :version "24.1"
  35. :group 'vc)
  36. (defcustom vc-sccs-register-switches nil
  37. "Switches for registering a file in SCCS.
  38. A string or list of strings passed to the checkin program by
  39. \\[vc-register]. If nil, use the value of `vc-register-switches'.
  40. If t, use no switches."
  41. :type '(choice (const :tag "Unspecified" nil)
  42. (const :tag "None" t)
  43. (string :tag "Argument String")
  44. (repeat :tag "Argument List" :value ("") string))
  45. :version "21.1"
  46. :group 'vc-sccs)
  47. (defcustom vc-sccs-diff-switches nil
  48. "String or list of strings specifying switches for SCCS diff under VC.
  49. If nil, use the value of `vc-diff-switches'. If t, use no switches."
  50. :type '(choice (const :tag "Unspecified" nil)
  51. (const :tag "None" t)
  52. (string :tag "Argument String")
  53. (repeat :tag "Argument List" :value ("") string))
  54. :version "21.1"
  55. :group 'vc-sccs)
  56. (defcustom vc-sccs-header '("%W%")
  57. "Header keywords to be inserted by `vc-insert-headers'."
  58. :type '(repeat string)
  59. :version "24.1" ; no longer consult the obsolete vc-header-alist
  60. :group 'vc-sccs)
  61. ;; This needs to be autoloaded because vc-sccs-registered uses it (via
  62. ;; vc-default-registered), and vc-hooks needs to be able to check
  63. ;; for a registered backend without loading every backend.
  64. ;;;###autoload
  65. (defcustom vc-sccs-master-templates
  66. (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
  67. "Where to look for SCCS master files.
  68. For a description of possible values, see `vc-check-master-templates'."
  69. :type '(choice (const :tag "Use standard SCCS file names"
  70. ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
  71. (repeat :tag "User-specified"
  72. (choice string
  73. function)))
  74. :version "21.1"
  75. :group 'vc-sccs)
  76. ;;;
  77. ;;; Internal variables
  78. ;;;
  79. (defconst vc-sccs-name-assoc-file "VC-names")
  80. ;;; Properties of the backend
  81. (defun vc-sccs-revision-granularity () 'file)
  82. (defun vc-sccs-checkout-model (_files) 'locking)
  83. ;;;
  84. ;;; State-querying functions
  85. ;;;
  86. ;; The autoload cookie below places vc-sccs-registered directly into
  87. ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
  88. ;; every file that is visited.
  89. ;;;###autoload
  90. (progn
  91. (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f)))
  92. (defun vc-sccs-state (file)
  93. "SCCS-specific function to compute the version control state."
  94. (if (not (vc-sccs-registered file))
  95. 'unregistered
  96. (with-temp-buffer
  97. (if (vc-insert-file (vc-sccs-lock-file file))
  98. (let* ((locks (vc-sccs-parse-locks))
  99. (working-revision (vc-working-revision file))
  100. (locking-user (cdr (assoc working-revision locks))))
  101. (if (not locking-user)
  102. (if (vc-sccs-workfile-unchanged-p file)
  103. 'up-to-date
  104. 'unlocked-changes)
  105. (if (string= locking-user (vc-user-login-name file))
  106. 'edited
  107. locking-user)))
  108. 'up-to-date))))
  109. (autoload 'vc-expand-dirs "vc")
  110. (defun vc-sccs-dir-status-files (dir files update-function)
  111. (if (not files) (setq files (vc-expand-dirs (list dir) 'RCS)))
  112. (let ((result nil))
  113. (dolist (file files)
  114. (let ((state (vc-state file))
  115. (frel (file-relative-name file)))
  116. (when (and (eq (vc-backend file) 'SCCS)
  117. (not (eq state 'up-to-date)))
  118. (push (list frel state) result))))
  119. (funcall update-function result)))
  120. (autoload 'vc-master-name "vc-filewise")
  121. (defun vc-sccs-working-revision (file)
  122. "SCCS-specific version of `vc-working-revision'."
  123. (when (and (file-regular-p file) (vc-master-name file))
  124. (with-temp-buffer
  125. ;; The working revision is always the latest revision number.
  126. ;; To find this number, search the entire delta table,
  127. ;; rather than just the first entry, because the
  128. ;; first entry might be a deleted ("R") revision.
  129. (vc-insert-file (vc-master-name file) "^\001e\n\001[^s]")
  130. (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1))))
  131. ;; Cf vc-sccs-find-revision.
  132. (defun vc-sccs-write-revision (file outfile &optional rev)
  133. "Write the SCCS version of input file FILE to output file OUTFILE.
  134. Optional string REV is a revision."
  135. (with-temp-buffer
  136. (apply 'vc-sccs-do-command t 0 "get" (vc-master-name file)
  137. (append '("-s" "-p" "-k") ; -k: no keyword expansion
  138. (if rev (list (concat "-r" rev)))))
  139. (write-region nil nil outfile nil 'silent)))
  140. (defun vc-sccs-workfile-unchanged-p (file)
  141. "Has FILE remained unchanged since last checkout?"
  142. (let ((tempfile (make-temp-file "vc-sccs")))
  143. (unwind-protect
  144. (progn
  145. (vc-sccs-write-revision file tempfile (vc-working-revision file))
  146. (zerop (vc-do-command "*vc*" 1 "cmp" file tempfile)))
  147. (delete-file tempfile))))
  148. ;;;
  149. ;;; State-changing functions
  150. ;;;
  151. (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
  152. ;; (let ((load-path (append vc-sccs-path load-path)))
  153. ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
  154. (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
  155. (defun vc-sccs-create-repo ()
  156. "Create a new SCCS repository."
  157. ;; SCCS is totally file-oriented, so all we have to do is make the directory
  158. (make-directory "SCCS"))
  159. (autoload 'vc-switches "vc")
  160. (defun vc-sccs-register (files &optional comment)
  161. "Register FILES into the SCCS version-control system.
  162. Automatically retrieve a read-only version of the files with keywords expanded.
  163. COMMENT can be used to provide an initial description of FILES.
  164. Passes either `vc-sccs-register-switches' or `vc-register-switches'
  165. to the SCCS command."
  166. (dolist (file files)
  167. (let* ((dirname (or (file-name-directory file) ""))
  168. (basename (file-name-nondirectory file))
  169. (project-file (vc-sccs-search-project-dir dirname basename)))
  170. (let ((vc-master-name
  171. (or project-file
  172. (format (car vc-sccs-master-templates) dirname basename))))
  173. (apply 'vc-sccs-do-command nil 0 "admin" vc-master-name
  174. "-fb"
  175. (concat "-i" (file-relative-name file))
  176. (and comment (concat "-y" comment))
  177. (vc-switches 'SCCS 'register)))
  178. (delete-file file)
  179. (vc-sccs-do-command nil 0 "get" (vc-master-name file)))))
  180. (defun vc-sccs-responsible-p (file)
  181. "Return non-nil if SCCS thinks it would be responsible for registering FILE."
  182. ;; TODO: check for all the patterns in vc-sccs-master-templates
  183. (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
  184. (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
  185. (file-name-nondirectory file)))))
  186. (defun vc-sccs-checkin (files comment)
  187. "SCCS-specific version of `vc-backend-checkin'."
  188. (dolist (file (vc-expand-dirs files 'SCCS))
  189. (apply 'vc-sccs-do-command nil 0 "delta" (vc-master-name file)
  190. (concat "-y" comment)
  191. (vc-switches 'SCCS 'checkin))
  192. (vc-sccs-do-command nil 0 "get" (vc-master-name file))))
  193. (defun vc-sccs-find-revision (file rev buffer)
  194. (apply 'vc-sccs-do-command
  195. buffer 0 "get" (vc-master-name file)
  196. "-s" ;; suppress diagnostic output
  197. "-p"
  198. (and rev
  199. (concat "-r"
  200. (vc-sccs-lookup-triple file rev)))
  201. (vc-switches 'SCCS 'checkout)))
  202. (defun vc-sccs-checkout (file &optional rev)
  203. "Retrieve a copy of a saved revision of SCCS controlled FILE.
  204. If FILE is a directory, all version-controlled files beneath are checked out.
  205. EDITABLE non-nil means that the file should be writable and
  206. locked. REV is the revision to check out."
  207. (if (file-directory-p file)
  208. (mapc 'vc-sccs-checkout (vc-expand-dirs (list file) 'SCCS))
  209. (let ((file-buffer (get-file-buffer file))
  210. switches)
  211. (message "Checking out %s..." file)
  212. (save-excursion
  213. ;; Change buffers to get local value of vc-checkout-switches.
  214. (if file-buffer (set-buffer file-buffer))
  215. (setq switches (vc-switches 'SCCS 'checkout))
  216. ;; Save this buffer's default-directory
  217. ;; and use save-excursion to make sure it is restored
  218. ;; in the same buffer it was saved in.
  219. (let ((default-directory default-directory))
  220. (save-excursion
  221. ;; Adjust the default-directory so that the check-out creates
  222. ;; the file in the right place.
  223. (setq default-directory (file-name-directory file))
  224. (and rev (or (string= rev "")
  225. (not (stringp rev)))
  226. (setq rev nil))
  227. (apply 'vc-sccs-do-command nil 0 "get" (vc-master-name file)
  228. "-e"
  229. (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
  230. switches))))
  231. (message "Checking out %s...done" file))))
  232. (defun vc-sccs-revert (file &optional _contents-done)
  233. "Revert FILE to the version it was based on. If FILE is a directory,
  234. revert all subfiles."
  235. (if (file-directory-p file)
  236. (mapc 'vc-sccs-revert (vc-expand-dirs (list file) 'SCCS))
  237. (vc-sccs-do-command nil 0 "unget" (vc-master-name file))
  238. (vc-sccs-do-command nil 0 "get" (vc-master-name file))
  239. ;; Checking out explicit revisions is not supported under SCCS, yet.
  240. ;; We always "revert" to the latest revision; therefore
  241. ;; vc-working-revision is cleared here so that it gets recomputed.
  242. (vc-file-setprop file 'vc-working-revision nil)))
  243. (defun vc-sccs-steal-lock (file &optional rev)
  244. "Steal the lock on the current workfile for FILE and revision REV."
  245. (if (file-directory-p file)
  246. (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file) 'SCCS))
  247. (vc-sccs-do-command nil 0 "unget"
  248. (vc-master-name file) "-n" (if rev (concat "-r" rev)))
  249. (vc-sccs-do-command nil 0 "get"
  250. (vc-master-name file) "-g" (if rev (concat "-r" rev)))))
  251. (defun vc-sccs-modify-change-comment (files rev comment)
  252. "Modify (actually, append to) the change comments for FILES on a specified REV."
  253. (dolist (file (vc-expand-dirs files 'SCCS))
  254. (vc-sccs-do-command nil 0 "cdc" (vc-master-name file)
  255. (concat "-y" comment) (concat "-r" rev))))
  256. ;;;
  257. ;;; History functions
  258. ;;;
  259. (defun vc-sccs-print-log (files buffer &optional _shortlog _start-revision-ignored limit)
  260. "Print commit log associated with FILES into specified BUFFER.
  261. Remaining arguments are ignored."
  262. (setq files (vc-expand-dirs files 'SCCS))
  263. (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-master-name files))
  264. (when limit 'limit-unsupported))
  265. (autoload 'vc-setup-buffer "vc-dispatcher")
  266. (autoload 'vc-delistify "vc-dispatcher")
  267. (defvar w32-quote-process-args)
  268. ;; FIXME use sccsdiff if present?
  269. (defun vc-sccs-diff (files &optional oldvers newvers buffer _async)
  270. "Get a difference report using SCCS between two filesets."
  271. (setq files (vc-expand-dirs files 'SCCS))
  272. (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
  273. (setq newvers (vc-sccs-lookup-triple (car files) newvers))
  274. (or buffer (setq buffer "*vc-diff*"))
  275. ;; We have to reimplement pieces of vc-do-command, because
  276. ;; we want to run multiple external commands, and only do the setup
  277. ;; and exit pieces once.
  278. (save-current-buffer
  279. (unless (or (eq buffer t)
  280. (and (stringp buffer) (string= (buffer-name) buffer))
  281. (eq buffer (current-buffer)))
  282. (vc-setup-buffer buffer))
  283. (let* ((fake-flags (append (vc-switches 'SCCS 'diff)
  284. (if oldvers (list (concat " -r" oldvers)))
  285. (if newvers (list (concat " -r" newvers)))))
  286. (fake-command
  287. (format "diff%s %s"
  288. (if fake-flags
  289. (concat " " (mapconcat 'identity fake-flags " "))
  290. "")
  291. (vc-delistify files)))
  292. (status 0)
  293. (oldproc (get-buffer-process (current-buffer))))
  294. (when vc-command-messages
  295. (message "Running %s in foreground..." fake-command))
  296. (if oldproc (delete-process oldproc))
  297. (dolist (file files)
  298. (let ((oldfile (make-temp-file "vc-sccs"))
  299. newfile)
  300. (unwind-protect
  301. (progn
  302. (vc-sccs-write-revision file oldfile oldvers)
  303. (if newvers
  304. (vc-sccs-write-revision file (setq newfile
  305. (make-temp-file "vc-sccs"))
  306. newvers))
  307. (let* ((inhibit-read-only t)
  308. (buffer-undo-list t)
  309. (process-environment
  310. (cons "LC_MESSAGES=C" process-environment))
  311. (w32-quote-process-args t)
  312. (this-status
  313. (apply 'process-file "diff" nil t nil
  314. (append (vc-switches 'SCCS 'diff)
  315. (list oldfile
  316. (or newfile
  317. (file-relative-name file)))))))
  318. (or (integerp this-status) (setq status 'error))
  319. (and (integerp status)
  320. (> this-status status)
  321. (setq status this-status))))
  322. (delete-file oldfile)
  323. (if newfile (delete-file newfile)))))
  324. (when (or (not (integerp status)) (> status 1))
  325. (unless (eq ?\s (aref (buffer-name (current-buffer)) 0))
  326. (pop-to-buffer (current-buffer))
  327. (goto-char (point-min))
  328. (shrink-window-if-larger-than-buffer))
  329. (error "Running %s...FAILED (%s)" fake-command
  330. (if (integerp status) (format "status %d" status) status)))
  331. (when vc-command-messages
  332. (message "Running %s...OK = %d" fake-command status))
  333. ;; Should we pretend we ran sccsdiff instead?
  334. ;; This might not actually be a valid diff command.
  335. (run-hook-with-args 'vc-post-command-functions "diff" files fake-flags)
  336. status)))
  337. ;;;
  338. ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
  339. ;;; our own set of name-to-revision mappings.
  340. ;;;
  341. (autoload 'vc-tag-precondition "vc")
  342. (declare-function vc-file-tree-walk "vc" (dirname func &rest args))
  343. (defun vc-sccs-create-tag (dir name branchp)
  344. (when branchp
  345. (error "SCCS backend does not support module branches"))
  346. (let ((result (vc-tag-precondition dir)))
  347. (if (stringp result)
  348. (error "File %s is not up-to-date" result)
  349. (vc-file-tree-walk
  350. dir
  351. (lambda (f)
  352. (vc-sccs-add-triple name f (vc-working-revision f)))))))
  353. ;;;
  354. ;;; Miscellaneous
  355. ;;;
  356. (defun vc-sccs-previous-revision (file rev)
  357. (vc-call-backend 'RCS 'previous-revision file rev))
  358. (defun vc-sccs-next-revision (file rev)
  359. (vc-call-backend 'RCS 'next-revision file rev))
  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. (autoload 'vc-rename-master "vc-filewise")
  366. (defun vc-sccs-rename-file (old new)
  367. ;; Move the master file (using vc-rcs-master-templates).
  368. (vc-rename-master (vc-master-name old) new vc-sccs-master-templates)
  369. ;; Update the tag file.
  370. (with-current-buffer
  371. (find-file-noselect
  372. (expand-file-name vc-sccs-name-assoc-file
  373. (file-name-directory (vc-master-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. (defun vc-sccs-find-file-hook ()
  381. ;; If the file is locked by some other user, make
  382. ;; the buffer read-only. Like this, even root
  383. ;; cannot modify a file that someone else has locked.
  384. (and (stringp (vc-state buffer-file-name 'SCCS))
  385. (setq buffer-read-only t)))
  386. ;;;
  387. ;;; Internal functions
  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. (defun vc-sccs-lock-file (file)
  411. "Generate lock file name corresponding to FILE."
  412. (let ((master (vc-master-name file)))
  413. (and
  414. master
  415. (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
  416. (replace-match "p." t t master 2))))
  417. (defun vc-sccs-parse-locks ()
  418. "Parse SCCS locks in current buffer.
  419. The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
  420. (let (master-locks)
  421. (goto-char (point-min))
  422. (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
  423. nil t)
  424. (setq master-locks
  425. (cons (cons (match-string 1) (match-string 2)) master-locks)))
  426. ;; FIXME: is it really necessary to reverse ?
  427. (nreverse master-locks)))
  428. (defun vc-sccs-add-triple (name file rev)
  429. (with-current-buffer
  430. (find-file-noselect
  431. (expand-file-name vc-sccs-name-assoc-file
  432. (file-name-directory (vc-master-name file))))
  433. (goto-char (point-max))
  434. (insert name "\t:\t" file "\t" rev "\n")
  435. (basic-save-buffer)
  436. (kill-buffer (current-buffer))))
  437. (defun vc-sccs-lookup-triple (file name)
  438. "Return the numeric revision corresponding to a named tag of FILE.
  439. If NAME is nil or a revision number string it's just passed through."
  440. (if (or (null name)
  441. (let ((firstchar (aref name 0)))
  442. (and (>= firstchar ?0) (<= firstchar ?9))))
  443. name
  444. (with-temp-buffer
  445. (vc-insert-file
  446. (expand-file-name vc-sccs-name-assoc-file
  447. (file-name-directory (vc-master-name file))))
  448. (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
  449. (provide 'vc-sccs)
  450. ;;; vc-sccs.el ends here