vc-sccs.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. ;;; vc-sccs.el --- support for SCCS version-control
  2. ;; Copyright (C) 1992-2012 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. ;; Proper function of the SCCS diff commands requires the shellscript vcdiff
  19. ;; to be installed somewhere on Emacs's path for executables.
  20. ;;
  21. ;;; Code:
  22. (eval-when-compile
  23. (require 'vc))
  24. ;;;
  25. ;;; Customization options
  26. ;;;
  27. ;; ;; Maybe a better solution is to not use "get" but "sccs get".
  28. ;; (defcustom vc-sccs-path
  29. ;; (let ((path ()))
  30. ;; (dolist (dir '("/usr/sccs" "/usr/lib/sccs" "/usr/libexec/sccs"))
  31. ;; (if (file-directory-p dir)
  32. ;; (push dir path)))
  33. ;; path)
  34. ;; "List of extra directories to search for SCCS commands."
  35. ;; :type '(repeat directory)
  36. ;; :group 'vc)
  37. (defgroup vc-sccs nil
  38. "VC SCCS backend."
  39. :version "24.1"
  40. :group 'vc)
  41. (defcustom vc-sccs-register-switches nil
  42. "Switches for registering a file in SCCS.
  43. A string or list of strings passed to the checkin program by
  44. \\[vc-register]. If nil, use the value of `vc-register-switches'.
  45. If t, use no switches."
  46. :type '(choice (const :tag "Unspecified" nil)
  47. (const :tag "None" t)
  48. (string :tag "Argument String")
  49. (repeat :tag "Argument List" :value ("") string))
  50. :version "21.1"
  51. :group 'vc-sccs)
  52. (defcustom vc-sccs-diff-switches nil
  53. "String or list of strings specifying switches for SCCS diff under VC.
  54. If nil, use the value of `vc-diff-switches'. If t, use no switches."
  55. :type '(choice (const :tag "Unspecified" nil)
  56. (const :tag "None" t)
  57. (string :tag "Argument String")
  58. (repeat :tag "Argument List" :value ("") string))
  59. :version "21.1"
  60. :group 'vc-sccs)
  61. (defcustom vc-sccs-header '("%W%")
  62. "Header keywords to be inserted by `vc-insert-headers'."
  63. :type '(repeat string)
  64. :version "24.1" ; no longer consult the obsolete vc-header-alist
  65. :group 'vc-sccs)
  66. ;;;###autoload
  67. (defcustom vc-sccs-master-templates
  68. (purecopy '("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
  69. "Where to look for SCCS master files.
  70. For a description of possible values, see `vc-check-master-templates'."
  71. :type '(choice (const :tag "Use standard SCCS file names"
  72. ("%sSCCS/s.%s" "%ss.%s" vc-sccs-search-project-dir))
  73. (repeat :tag "User-specified"
  74. (choice string
  75. function)))
  76. :version "21.1"
  77. :group 'vc-sccs)
  78. ;;;
  79. ;;; Internal variables
  80. ;;;
  81. (defconst vc-sccs-name-assoc-file "VC-names")
  82. ;;; Properties of the backend
  83. (defun vc-sccs-revision-granularity () 'file)
  84. (defun vc-sccs-checkout-model (files) 'locking)
  85. ;;;
  86. ;;; State-querying functions
  87. ;;;
  88. ;; The autoload cookie below places vc-sccs-registered directly into
  89. ;; loaddefs.el, so that vc-sccs.el does not need to be loaded for
  90. ;; every file that is visited. The definition is repeated below
  91. ;; so that Help and etags can find it.
  92. ;;;###autoload (defun vc-sccs-registered(f) (vc-default-registered 'SCCS f))
  93. (defun vc-sccs-registered (f) (vc-default-registered 'SCCS f))
  94. (defun vc-sccs-state (file)
  95. "SCCS-specific function to compute the version control state."
  96. (if (not (vc-sccs-registered file))
  97. 'unregistered
  98. (with-temp-buffer
  99. (if (vc-insert-file (vc-sccs-lock-file file))
  100. (let* ((locks (vc-sccs-parse-locks))
  101. (working-revision (vc-working-revision file))
  102. (locking-user (cdr (assoc working-revision locks))))
  103. (if (not locking-user)
  104. (if (vc-workfile-unchanged-p file)
  105. 'up-to-date
  106. 'unlocked-changes)
  107. (if (string= locking-user (vc-user-login-name file))
  108. 'edited
  109. locking-user)))
  110. 'up-to-date))))
  111. (defun vc-sccs-state-heuristic (file)
  112. "SCCS-specific state heuristic."
  113. (if (not (vc-mistrust-permissions file))
  114. ;; This implementation assumes that any file which is under version
  115. ;; control and has -rw-r--r-- is locked by its owner. This is true
  116. ;; for both RCS and SCCS, which keep unlocked files at -r--r--r--.
  117. ;; We have to be careful not to exclude files with execute bits on;
  118. ;; scripts can be under version control too. Also, we must ignore the
  119. ;; group-read and other-read bits, since paranoid users turn them off.
  120. (let* ((attributes (file-attributes file 'string))
  121. (owner-name (nth 2 attributes))
  122. (permissions (nth 8 attributes)))
  123. (if (string-match ".r-..-..-." permissions)
  124. 'up-to-date
  125. (if (string-match ".rw..-..-." permissions)
  126. (if (file-ownership-preserved-p file)
  127. 'edited
  128. owner-name)
  129. ;; Strange permissions.
  130. ;; Fall through to real state computation.
  131. (vc-sccs-state file))))
  132. (vc-sccs-state file)))
  133. (defun vc-sccs-dir-status (dir update-function)
  134. ;; FIXME: this function should be rewritten, using `vc-expand-dirs'
  135. ;; is not TRTD because it returns files from multiple backends.
  136. ;; It should also return 'unregistered files.
  137. ;; Doing lots of individual VC-state calls is painful, but
  138. ;; there is no better option in SCCS-land.
  139. (let ((flist (vc-expand-dirs (list dir)))
  140. (result nil))
  141. (dolist (file flist)
  142. (let ((state (vc-state file))
  143. (frel (file-relative-name file)))
  144. (when (and (eq (vc-backend file) 'SCCS)
  145. (not (eq state 'up-to-date)))
  146. (push (list frel state) result))))
  147. (funcall update-function result)))
  148. (defun vc-sccs-working-revision (file)
  149. "SCCS-specific version of `vc-working-revision'."
  150. (with-temp-buffer
  151. ;; The working revision is always the latest revision number.
  152. ;; To find this number, search the entire delta table,
  153. ;; rather than just the first entry, because the
  154. ;; first entry might be a deleted ("R") revision.
  155. (vc-insert-file (vc-name file) "^\001e\n\001[^s]")
  156. (vc-parse-buffer "^\001d D \\([^ ]+\\)" 1)))
  157. (defun vc-sccs-workfile-unchanged-p (file)
  158. "SCCS-specific implementation of `vc-workfile-unchanged-p'."
  159. (zerop (apply 'vc-do-command "*vc*" 1 "vcdiff" (vc-name file)
  160. (list "--brief" "-q"
  161. (concat "-r" (vc-working-revision file))))))
  162. ;;;
  163. ;;; State-changing functions
  164. ;;;
  165. (defun vc-sccs-do-command (buffer okstatus command file-or-list &rest flags)
  166. ;; (let ((load-path (append vc-sccs-path load-path)))
  167. ;; (apply 'vc-do-command buffer okstatus command file-or-list flags))
  168. (apply 'vc-do-command (or buffer "*vc*") okstatus "sccs" file-or-list command flags))
  169. (defun vc-sccs-create-repo ()
  170. "Create a new SCCS repository."
  171. ;; SCCS is totally file-oriented, so all we have to do is make the directory
  172. (make-directory "SCCS"))
  173. (defun vc-sccs-register (files &optional rev comment)
  174. "Register FILES into the SCCS version-control system.
  175. REV is the optional revision number for the file. COMMENT can be used
  176. to provide an initial description of FILES.
  177. Passes either `vc-sccs-register-switches' or `vc-register-switches'
  178. to the SCCS command.
  179. Automatically retrieve a read-only version of the files with keywords
  180. expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
  181. (dolist (file files)
  182. (let* ((dirname (or (file-name-directory file) ""))
  183. (basename (file-name-nondirectory file))
  184. (project-file (vc-sccs-search-project-dir dirname basename)))
  185. (let ((vc-name
  186. (or project-file
  187. (format (car vc-sccs-master-templates) dirname basename))))
  188. (apply 'vc-sccs-do-command nil 0 "admin" vc-name
  189. (and rev (not (string= rev "")) (concat "-r" rev))
  190. "-fb"
  191. (concat "-i" (file-relative-name file))
  192. (and comment (concat "-y" comment))
  193. (vc-switches 'SCCS 'register)))
  194. (delete-file file)
  195. (if vc-keep-workfiles
  196. (vc-sccs-do-command nil 0 "get" (vc-name file))))))
  197. (defun vc-sccs-responsible-p (file)
  198. "Return non-nil if SCCS thinks it would be responsible for registering FILE."
  199. ;; TODO: check for all the patterns in vc-sccs-master-templates
  200. (or (file-directory-p (expand-file-name "SCCS" (file-name-directory file)))
  201. (stringp (vc-sccs-search-project-dir (or (file-name-directory file) "")
  202. (file-name-nondirectory file)))))
  203. (defun vc-sccs-checkin (files rev comment)
  204. "SCCS-specific version of `vc-backend-checkin'."
  205. (dolist (file (vc-expand-dirs files))
  206. (apply 'vc-sccs-do-command nil 0 "delta" (vc-name file)
  207. (if rev (concat "-r" rev))
  208. (concat "-y" comment)
  209. (vc-switches 'SCCS 'checkin))
  210. (if vc-keep-workfiles
  211. (vc-sccs-do-command nil 0 "get" (vc-name file)))))
  212. (defun vc-sccs-find-revision (file rev buffer)
  213. (apply 'vc-sccs-do-command
  214. buffer 0 "get" (vc-name file)
  215. "-s" ;; suppress diagnostic output
  216. "-p"
  217. (and rev
  218. (concat "-r"
  219. (vc-sccs-lookup-triple file rev)))
  220. (vc-switches 'SCCS 'checkout)))
  221. (defun vc-sccs-checkout (file &optional editable rev)
  222. "Retrieve a copy of a saved revision of SCCS controlled FILE.
  223. If FILE is a directory, all version-controlled files beneath are checked out.
  224. EDITABLE non-nil means that the file should be writable and
  225. locked. REV is the revision to check out."
  226. (if (file-directory-p file)
  227. (mapc 'vc-sccs-checkout (vc-expand-dirs (list file)))
  228. (let ((file-buffer (get-file-buffer file))
  229. switches)
  230. (message "Checking out %s..." file)
  231. (save-excursion
  232. ;; Change buffers to get local value of vc-checkout-switches.
  233. (if file-buffer (set-buffer file-buffer))
  234. (setq switches (vc-switches 'SCCS 'checkout))
  235. ;; Save this buffer's default-directory
  236. ;; and use save-excursion to make sure it is restored
  237. ;; in the same buffer it was saved in.
  238. (let ((default-directory default-directory))
  239. (save-excursion
  240. ;; Adjust the default-directory so that the check-out creates
  241. ;; the file in the right place.
  242. (setq default-directory (file-name-directory file))
  243. (and rev (or (string= rev "")
  244. (not (stringp rev)))
  245. (setq rev nil))
  246. (apply 'vc-sccs-do-command nil 0 "get" (vc-name file)
  247. (if editable "-e")
  248. (and rev (concat "-r" (vc-sccs-lookup-triple file rev)))
  249. switches))))
  250. (message "Checking out %s...done" file))))
  251. (defun vc-sccs-rollback (files)
  252. "Roll back, undoing the most recent checkins of FILES. Directories
  253. are expanded to all version-controlled subfiles."
  254. (setq files (vc-expand-dirs files))
  255. (if (not files)
  256. (error "SCCS backend doesn't support directory-level rollback"))
  257. (dolist (file files)
  258. (let ((discard (vc-working-revision file)))
  259. (if (null (yes-or-no-p (format "Remove version %s from %s history? "
  260. discard file)))
  261. (error "Aborted"))
  262. (message "Removing revision %s from %s..." discard file)
  263. (vc-sccs-do-command nil 0 "rmdel"
  264. (vc-name file) (concat "-r" discard))
  265. (vc-sccs-do-command nil 0 "get" (vc-name file) nil))))
  266. (defun vc-sccs-revert (file &optional contents-done)
  267. "Revert FILE to the version it was based on. If FILE is a directory,
  268. revert all subfiles."
  269. (if (file-directory-p file)
  270. (mapc 'vc-sccs-revert (vc-expand-dirs (list file)))
  271. (vc-sccs-do-command nil 0 "unget" (vc-name file))
  272. (vc-sccs-do-command nil 0 "get" (vc-name file))
  273. ;; Checking out explicit revisions is not supported under SCCS, yet.
  274. ;; We always "revert" to the latest revision; therefore
  275. ;; vc-working-revision is cleared here so that it gets recomputed.
  276. (vc-file-setprop file 'vc-working-revision nil)))
  277. (defun vc-sccs-steal-lock (file &optional rev)
  278. "Steal the lock on the current workfile for FILE and revision REV."
  279. (if (file-directory-p file)
  280. (mapc 'vc-sccs-steal-lock (vc-expand-dirs (list file)))
  281. (vc-sccs-do-command nil 0 "unget"
  282. (vc-name file) "-n" (if rev (concat "-r" rev)))
  283. (vc-sccs-do-command nil 0 "get"
  284. (vc-name file) "-g" (if rev (concat "-r" rev)))))
  285. (defun vc-sccs-modify-change-comment (files rev comment)
  286. "Modify (actually, append to) the change comments for FILES on a specified REV."
  287. (dolist (file (vc-expand-dirs files))
  288. (vc-sccs-do-command nil 0 "cdc" (vc-name file)
  289. (concat "-y" comment) (concat "-r" rev))))
  290. ;;;
  291. ;;; History functions
  292. ;;;
  293. (defun vc-sccs-print-log (files buffer &optional shortlog start-revision-ignored limit)
  294. "Get change log associated with FILES."
  295. (setq files (vc-expand-dirs files))
  296. (vc-sccs-do-command buffer 0 "prs" (mapcar 'vc-name files))
  297. (when limit 'limit-unsupported))
  298. (defun vc-sccs-diff (files &optional oldvers newvers buffer)
  299. "Get a difference report using SCCS between two filesets."
  300. (setq files (vc-expand-dirs files))
  301. (setq oldvers (vc-sccs-lookup-triple (car files) oldvers))
  302. (setq newvers (vc-sccs-lookup-triple (car files) newvers))
  303. (apply 'vc-do-command (or buffer "*vc-diff*")
  304. 1 "vcdiff" (mapcar 'vc-name (vc-expand-dirs files))
  305. (append (list "-q"
  306. (and oldvers (concat "-r" oldvers))
  307. (and newvers (concat "-r" newvers)))
  308. (vc-switches 'SCCS 'diff))))
  309. ;;;
  310. ;;; Tag system. SCCS doesn't have tags, so we simulate them by maintaining
  311. ;;; our own set of name-to-revision mappings.
  312. ;;;
  313. (defun vc-sccs-create-tag (dir name branchp)
  314. (when branchp
  315. (error "SCCS backend does not support module branches"))
  316. (let ((result (vc-tag-precondition dir)))
  317. (if (stringp result)
  318. (error "File %s is not up-to-date" result)
  319. (vc-file-tree-walk
  320. dir
  321. (lambda (f)
  322. (vc-sccs-add-triple name f (vc-working-revision f)))))))
  323. ;;;
  324. ;;; Miscellaneous
  325. ;;;
  326. (defun vc-sccs-previous-revision (file rev)
  327. (vc-call-backend 'RCS 'previous-revision file rev))
  328. (defun vc-sccs-next-revision (file rev)
  329. (vc-call-backend 'RCS 'next-revision file rev))
  330. (defun vc-sccs-check-headers ()
  331. "Check if the current file has any headers in it."
  332. (save-excursion
  333. (goto-char (point-min))
  334. (re-search-forward "%[A-Z]%" nil t)))
  335. (defun vc-sccs-rename-file (old new)
  336. ;; Move the master file (using vc-rcs-master-templates).
  337. (vc-rename-master (vc-name old) new vc-sccs-master-templates)
  338. ;; Update the tag file.
  339. (with-current-buffer
  340. (find-file-noselect
  341. (expand-file-name vc-sccs-name-assoc-file
  342. (file-name-directory (vc-name old))))
  343. (goto-char (point-min))
  344. ;; (replace-regexp (concat ":" (regexp-quote old) "$") (concat ":" new))
  345. (while (re-search-forward (concat ":" (regexp-quote old) "$") nil t)
  346. (replace-match (concat ":" new) nil nil))
  347. (basic-save-buffer)
  348. (kill-buffer (current-buffer))))
  349. (defun vc-sccs-find-file-hook ()
  350. ;; If the file is locked by some other user, make
  351. ;; the buffer read-only. Like this, even root
  352. ;; cannot modify a file that someone else has locked.
  353. (and (stringp (vc-state buffer-file-name 'SCCS))
  354. (setq buffer-read-only t)))
  355. ;;;
  356. ;;; Internal functions
  357. ;;;
  358. ;; This function is wrapped with `progn' so that the autoload cookie
  359. ;; copies the whole function itself into loaddefs.el rather than just placing
  360. ;; a (autoload 'vc-sccs-search-project-dir "vc-sccs") which would not
  361. ;; help us avoid loading vc-sccs.
  362. ;;;###autoload
  363. (progn (defun vc-sccs-search-project-dir (dirname basename)
  364. "Return the name of a master file in the SCCS project directory.
  365. Does not check whether the file exists but returns nil if it does not
  366. find any project directory."
  367. (let ((project-dir (getenv "PROJECTDIR")) dirs dir)
  368. (when project-dir
  369. (if (file-name-absolute-p project-dir)
  370. (setq dirs '("SCCS" ""))
  371. (setq dirs '("src/SCCS" "src" "source/SCCS" "source"))
  372. (setq project-dir (expand-file-name (concat "~" project-dir))))
  373. (while (and (not dir) dirs)
  374. (setq dir (expand-file-name (car dirs) project-dir))
  375. (unless (file-directory-p dir)
  376. (setq dir nil)
  377. (setq dirs (cdr dirs))))
  378. (and dir (expand-file-name (concat "s." basename) dir))))))
  379. (defun vc-sccs-lock-file (file)
  380. "Generate lock file name corresponding to FILE."
  381. (let ((master (vc-name file)))
  382. (and
  383. master
  384. (string-match "\\(.*/\\)\\(s\\.\\)\\(.*\\)" master)
  385. (replace-match "p." t t master 2))))
  386. (defun vc-sccs-parse-locks ()
  387. "Parse SCCS locks in current buffer.
  388. The result is a list of the form ((REVISION . USER) (REVISION . USER) ...)."
  389. (let (master-locks)
  390. (goto-char (point-min))
  391. (while (re-search-forward "^\\([0-9.]+\\) [0-9.]+ \\([^ ]+\\) .*\n?"
  392. nil t)
  393. (setq master-locks
  394. (cons (cons (match-string 1) (match-string 2)) master-locks)))
  395. ;; FIXME: is it really necessary to reverse ?
  396. (nreverse master-locks)))
  397. (defun vc-sccs-add-triple (name file rev)
  398. (with-current-buffer
  399. (find-file-noselect
  400. (expand-file-name vc-sccs-name-assoc-file
  401. (file-name-directory (vc-name file))))
  402. (goto-char (point-max))
  403. (insert name "\t:\t" file "\t" rev "\n")
  404. (basic-save-buffer)
  405. (kill-buffer (current-buffer))))
  406. (defun vc-sccs-lookup-triple (file name)
  407. "Return the numeric revision corresponding to a named tag of FILE.
  408. If NAME is nil or a revision number string it's just passed through."
  409. (if (or (null name)
  410. (let ((firstchar (aref name 0)))
  411. (and (>= firstchar ?0) (<= firstchar ?9))))
  412. name
  413. (with-temp-buffer
  414. (vc-insert-file
  415. (expand-file-name vc-sccs-name-assoc-file
  416. (file-name-directory (vc-name file))))
  417. (vc-parse-buffer (concat name "\t:\t" file "\t\\(.+\\)") 1))))
  418. (provide 'vc-sccs)
  419. ;;; vc-sccs.el ends here