vc-rcs.el 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  1. ;;; vc-rcs.el --- support for RCS 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. ;; See vc.el
  19. ;; Some features will not work with ancient RCS versions. Where
  20. ;; appropriate, VC finds out which version you have, and allows or
  21. ;; disallows those features.
  22. ;; You can support the RCS -x option by customizing vc-rcs-master-templates.
  23. ;;; Code:
  24. ;;;
  25. ;;; Customization options
  26. ;;;
  27. (eval-when-compile
  28. (require 'cl)
  29. (require 'vc))
  30. (defgroup vc-rcs nil
  31. "VC RCS backend."
  32. :version "24.1"
  33. :group 'vc)
  34. (defcustom vc-rcs-release nil
  35. "The release number of your RCS installation, as a string.
  36. If nil, VC itself computes this value when it is first needed."
  37. :type '(choice (const :tag "Auto" nil)
  38. (string :tag "Specified")
  39. (const :tag "Unknown" unknown))
  40. :group 'vc-rcs)
  41. (defcustom vc-rcs-register-switches nil
  42. "Switches for registering a file in RCS.
  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-rcs)
  52. (defcustom vc-rcs-diff-switches nil
  53. "String or list of strings specifying switches for RCS 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-rcs)
  61. (defcustom vc-rcs-header '("\$Id\$")
  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-rcs)
  66. (defcustom vc-rcsdiff-knows-brief nil
  67. "Indicates whether rcsdiff understands the --brief option.
  68. The value is either `yes', `no', or nil. If it is nil, VC tries
  69. to use --brief and sets this variable to remember whether it worked."
  70. :type '(choice (const :tag "Work out" nil) (const yes) (const no))
  71. :group 'vc-rcs)
  72. ;;;###autoload
  73. (defcustom vc-rcs-master-templates
  74. (purecopy '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
  75. "Where to look for RCS master files.
  76. For a description of possible values, see `vc-check-master-templates'."
  77. :type '(choice (const :tag "Use standard RCS file names"
  78. '("%sRCS/%s,v" "%s%s,v" "%sRCS/%s"))
  79. (repeat :tag "User-specified"
  80. (choice string
  81. function)))
  82. :version "21.1"
  83. :group 'vc-rcs)
  84. ;;; Properties of the backend
  85. (defun vc-rcs-revision-granularity () 'file)
  86. (defun vc-rcs-checkout-model (files)
  87. "RCS-specific version of `vc-checkout-model'."
  88. (let ((file (if (consp files) (car files) files))
  89. result)
  90. (when vc-consult-headers
  91. (vc-file-setprop file 'vc-checkout-model nil)
  92. (vc-rcs-consult-headers file)
  93. (setq result (vc-file-getprop file 'vc-checkout-model)))
  94. (or result
  95. (progn (vc-rcs-fetch-master-state file)
  96. (vc-file-getprop file 'vc-checkout-model)))))
  97. ;;;
  98. ;;; State-querying functions
  99. ;;;
  100. ;; The autoload cookie below places vc-rcs-registered directly into
  101. ;; loaddefs.el, so that vc-rcs.el does not need to be loaded for
  102. ;; every file that is visited.
  103. ;;;###autoload
  104. (progn
  105. (defun vc-rcs-registered (f) (vc-default-registered 'RCS f)))
  106. (defun vc-rcs-state (file)
  107. "Implementation of `vc-state' for RCS."
  108. (if (not (vc-rcs-registered file))
  109. 'unregistered
  110. (or (boundp 'vc-rcs-headers-result)
  111. (and vc-consult-headers
  112. (vc-rcs-consult-headers file)))
  113. (let ((state
  114. ;; vc-working-revision might not be known; in that case the
  115. ;; property is nil. vc-rcs-fetch-master-state knows how to
  116. ;; handle that.
  117. (vc-rcs-fetch-master-state file
  118. (vc-file-getprop file
  119. 'vc-working-revision))))
  120. (if (not (eq state 'up-to-date))
  121. state
  122. (if (vc-workfile-unchanged-p file)
  123. 'up-to-date
  124. (if (eq (vc-rcs-checkout-model (list file)) 'locking)
  125. 'unlocked-changes
  126. 'edited))))))
  127. (defun vc-rcs-state-heuristic (file)
  128. "State heuristic for RCS."
  129. (let (vc-rcs-headers-result)
  130. (if (and vc-consult-headers
  131. (setq vc-rcs-headers-result
  132. (vc-rcs-consult-headers file))
  133. (eq vc-rcs-headers-result 'rev-and-lock))
  134. (let ((state (vc-file-getprop file 'vc-state)))
  135. ;; If the headers say that the file is not locked, the
  136. ;; permissions can tell us whether locking is used for
  137. ;; the file or not.
  138. (if (and (eq state 'up-to-date)
  139. (not (vc-mistrust-permissions file))
  140. (file-exists-p file))
  141. (cond
  142. ((string-match ".rw..-..-." (nth 8 (file-attributes file)))
  143. (vc-file-setprop file 'vc-checkout-model 'implicit)
  144. (setq state
  145. (if (vc-rcs-workfile-is-newer file)
  146. 'edited
  147. 'up-to-date)))
  148. ((string-match ".r-..-..-." (nth 8 (file-attributes file)))
  149. (vc-file-setprop file 'vc-checkout-model 'locking))))
  150. state)
  151. (if (not (vc-mistrust-permissions file))
  152. (let* ((attributes (file-attributes file 'string))
  153. (owner-name (nth 2 attributes))
  154. (permissions (nth 8 attributes)))
  155. (cond ((and permissions (string-match ".r-..-..-." permissions))
  156. (vc-file-setprop file 'vc-checkout-model 'locking)
  157. 'up-to-date)
  158. ((and permissions (string-match ".rw..-..-." permissions))
  159. (if (eq (vc-rcs-checkout-model file) 'locking)
  160. (if (file-ownership-preserved-p file)
  161. 'edited
  162. owner-name)
  163. (if (vc-rcs-workfile-is-newer file)
  164. 'edited
  165. 'up-to-date)))
  166. (t
  167. ;; Strange permissions. Fall through to
  168. ;; expensive state computation.
  169. (vc-rcs-state file))))
  170. (vc-rcs-state file)))))
  171. (defun vc-rcs-dir-status (dir update-function)
  172. ;; FIXME: this function should be rewritten or `vc-expand-dirs'
  173. ;; should be changed to take a backend parameter. Using
  174. ;; `vc-expand-dirs' is not TRTD because it returns files from
  175. ;; multiple backends. It should also return 'unregistered files.
  176. ;; Doing individual vc-state calls is painful but there
  177. ;; is no better way in RCS-land.
  178. (let ((flist (vc-expand-dirs (list dir)))
  179. (result nil))
  180. (dolist (file flist)
  181. (let ((state (vc-state file))
  182. (frel (file-relative-name file)))
  183. (when (and (eq (vc-backend file) 'RCS)
  184. (not (eq state 'up-to-date)))
  185. (push (list frel state) result))))
  186. (funcall update-function result)))
  187. (defun vc-rcs-working-revision (file)
  188. "RCS-specific version of `vc-working-revision'."
  189. (or (and vc-consult-headers
  190. (vc-rcs-consult-headers file)
  191. (vc-file-getprop file 'vc-working-revision))
  192. (progn
  193. (vc-rcs-fetch-master-state file)
  194. (vc-file-getprop file 'vc-working-revision))))
  195. (defun vc-rcs-latest-on-branch-p (file &optional version)
  196. "Return non-nil if workfile version of FILE is the latest on its branch.
  197. When VERSION is given, perform check for that version."
  198. (unless version (setq version (vc-working-revision file)))
  199. (with-temp-buffer
  200. (string= version
  201. (if (vc-rcs-trunk-p version)
  202. (progn
  203. ;; Compare VERSION to the head version number.
  204. (vc-insert-file (vc-name file) "^[0-9]")
  205. (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
  206. ;; If we are not on the trunk, we need to examine the
  207. ;; whole current branch.
  208. (vc-insert-file (vc-name file) "^desc")
  209. (vc-rcs-find-most-recent-rev (vc-branch-part version))))))
  210. (defun vc-rcs-workfile-unchanged-p (file)
  211. "RCS-specific implementation of `vc-workfile-unchanged-p'."
  212. ;; Try to use rcsdiff --brief. If rcsdiff does not understand that,
  213. ;; do a double take and remember the fact for the future
  214. (let* ((version (concat "-r" (vc-working-revision file)))
  215. (status (if (eq vc-rcsdiff-knows-brief 'no)
  216. (vc-do-command "*vc*" 1 "rcsdiff" file version)
  217. (vc-do-command "*vc*" 2 "rcsdiff" file "--brief" version))))
  218. (if (eq status 2)
  219. (if (not vc-rcsdiff-knows-brief)
  220. (setq vc-rcsdiff-knows-brief 'no
  221. status (vc-do-command "*vc*" 1 "rcsdiff" file version))
  222. (error "rcsdiff failed"))
  223. (if (not vc-rcsdiff-knows-brief) (setq vc-rcsdiff-knows-brief 'yes)))
  224. ;; The workfile is unchanged if rcsdiff found no differences.
  225. (zerop status)))
  226. ;;;
  227. ;;; State-changing functions
  228. ;;;
  229. (defun vc-rcs-create-repo ()
  230. "Create a new RCS repository."
  231. ;; RCS is totally file-oriented, so all we have to do is make the directory.
  232. (make-directory "RCS"))
  233. (defun vc-rcs-register (files &optional rev comment)
  234. "Register FILES into the RCS version-control system.
  235. REV is the optional revision number for the files. COMMENT can be used
  236. to provide an initial description for each FILES.
  237. Passes either `vc-rcs-register-switches' or `vc-register-switches'
  238. to the RCS command.
  239. Automatically retrieve a read-only version of the file with keywords
  240. expanded if `vc-keep-workfiles' is non-nil, otherwise, delete the workfile."
  241. (let (subdir name)
  242. ;; When REV is specified, we need to force using "-t-".
  243. (when rev (unless comment (setq comment "")))
  244. (dolist (file files)
  245. (and (not (file-exists-p
  246. (setq subdir (expand-file-name "RCS"
  247. (file-name-directory file)))))
  248. (not (directory-files (file-name-directory file)
  249. nil ".*,v$" t))
  250. (yes-or-no-p "Create RCS subdirectory? ")
  251. (make-directory subdir))
  252. (apply 'vc-do-command "*vc*" 0 "ci" file
  253. ;; if available, use the secure registering option
  254. (and (vc-rcs-release-p "5.6.4") "-i")
  255. (concat (if vc-keep-workfiles "-u" "-r") rev)
  256. (and comment (concat "-t-" comment))
  257. (vc-switches 'RCS 'register))
  258. ;; parse output to find master file name and workfile version
  259. (with-current-buffer "*vc*"
  260. (goto-char (point-min))
  261. (if (not (setq name
  262. (if (looking-at (concat "^\\(.*\\) <-- "
  263. (file-name-nondirectory file)))
  264. (match-string 1))))
  265. ;; if we couldn't find the master name,
  266. ;; run vc-rcs-registered to get it
  267. ;; (will be stored into the vc-name property)
  268. (vc-rcs-registered file)
  269. (vc-file-setprop file 'vc-name
  270. (if (file-name-absolute-p name)
  271. name
  272. (expand-file-name
  273. name
  274. (file-name-directory file))))))
  275. (vc-file-setprop file 'vc-working-revision
  276. (if (re-search-forward
  277. "^initial revision: \\([0-9.]+\\).*\n"
  278. nil t)
  279. (match-string 1))))))
  280. (defun vc-rcs-responsible-p (file)
  281. "Return non-nil if RCS thinks it would be responsible for registering FILE."
  282. ;; TODO: check for all the patterns in vc-rcs-master-templates
  283. (file-directory-p (expand-file-name "RCS"
  284. (if (file-directory-p file)
  285. file
  286. (file-name-directory file)))))
  287. (defun vc-rcs-receive-file (file rev)
  288. "Implementation of receive-file for RCS."
  289. (let ((checkout-model (vc-rcs-checkout-model (list file))))
  290. (vc-rcs-register file rev "")
  291. (when (eq checkout-model 'implicit)
  292. (vc-rcs-set-non-strict-locking file))
  293. (vc-rcs-set-default-branch file (concat rev ".1"))))
  294. (defun vc-rcs-unregister (file)
  295. "Unregister FILE from RCS.
  296. If this leaves the RCS subdirectory empty, ask the user
  297. whether to remove it."
  298. (let* ((master (vc-name file))
  299. (dir (file-name-directory master))
  300. (backup-info (find-backup-file-name master)))
  301. (if (not backup-info)
  302. (delete-file master)
  303. (rename-file master (car backup-info) 'ok-if-already-exists)
  304. (dolist (f (cdr backup-info)) (ignore-errors (delete-file f))))
  305. (and (string= (file-name-nondirectory (directory-file-name dir)) "RCS")
  306. ;; check whether RCS dir is empty, i.e. it does not
  307. ;; contain any files except "." and ".."
  308. (not (directory-files dir nil
  309. "^\\([^.]\\|\\.[^.]\\|\\.\\.[^.]\\).*"))
  310. (yes-or-no-p (format "Directory %s is empty; remove it? " dir))
  311. (delete-directory dir))))
  312. (defun vc-rcs-checkin (files rev comment)
  313. "RCS-specific version of `vc-backend-checkin'."
  314. (let ((switches (vc-switches 'RCS 'checkin)))
  315. ;; Now operate on the files
  316. (dolist (file (vc-expand-dirs files))
  317. (let ((old-version (vc-working-revision file)) new-version
  318. (default-branch (vc-file-getprop file 'vc-rcs-default-branch)))
  319. ;; Force branch creation if an appropriate
  320. ;; default branch has been set.
  321. (and (not rev)
  322. default-branch
  323. (string-match (concat "^" (regexp-quote old-version) "\\.")
  324. default-branch)
  325. (setq rev default-branch)
  326. (setq switches (cons "-f" switches)))
  327. (if (and (not rev) old-version)
  328. (setq rev (vc-branch-part old-version)))
  329. (apply 'vc-do-command "*vc*" 0 "ci" (vc-name file)
  330. ;; if available, use the secure check-in option
  331. (and (vc-rcs-release-p "5.6.4") "-j")
  332. (concat (if vc-keep-workfiles "-u" "-r") rev)
  333. (concat "-m" comment)
  334. switches)
  335. (vc-file-setprop file 'vc-working-revision nil)
  336. ;; determine the new workfile version
  337. (set-buffer "*vc*")
  338. (goto-char (point-min))
  339. (when (or (re-search-forward
  340. "new revision: \\([0-9.]+\\);" nil t)
  341. (re-search-forward
  342. "reverting to previous revision \\([0-9.]+\\)" nil t))
  343. (setq new-version (match-string 1))
  344. (vc-file-setprop file 'vc-working-revision new-version))
  345. ;; if we got to a different branch, adjust the default
  346. ;; branch accordingly
  347. (cond
  348. ((and old-version new-version
  349. (not (string= (vc-branch-part old-version)
  350. (vc-branch-part new-version))))
  351. (vc-rcs-set-default-branch file
  352. (if (vc-rcs-trunk-p new-version) nil
  353. (vc-branch-part new-version)))
  354. ;; If this is an old (pre-1992!) RCS release, we might have
  355. ;; to remove a remaining lock.
  356. (if (not (vc-rcs-release-p "5.6.2"))
  357. ;; exit status of 1 is also accepted.
  358. ;; It means that the lock was removed before.
  359. (vc-do-command "*vc*" 1 "rcs" (vc-name file)
  360. (concat "-u" old-version)))))))))
  361. (defun vc-rcs-find-revision (file rev buffer)
  362. (apply 'vc-do-command
  363. (or buffer "*vc*") 0 "co" (vc-name file)
  364. "-q" ;; suppress diagnostic output
  365. (concat "-p" rev)
  366. (vc-switches 'RCS 'checkout)))
  367. (defun vc-rcs-checkout (file &optional editable rev)
  368. "Retrieve a copy of a saved version of FILE. If FILE is a directory,
  369. attempt the checkout for all registered files beneath it."
  370. (if (file-directory-p file)
  371. (mapc 'vc-rcs-checkout (vc-expand-dirs (list file)))
  372. (let ((file-buffer (get-file-buffer file))
  373. switches)
  374. (message "Checking out %s..." file)
  375. (save-excursion
  376. ;; Change buffers to get local value of vc-checkout-switches.
  377. (if file-buffer (set-buffer file-buffer))
  378. (setq switches (vc-switches 'RCS 'checkout))
  379. ;; Save this buffer's default-directory
  380. ;; and use save-excursion to make sure it is restored
  381. ;; in the same buffer it was saved in.
  382. (let ((default-directory default-directory))
  383. (save-excursion
  384. ;; Adjust the default-directory so that the check-out creates
  385. ;; the file in the right place.
  386. (setq default-directory (file-name-directory file))
  387. (let (new-version)
  388. ;; if we should go to the head of the trunk,
  389. ;; clear the default branch first
  390. (and rev (string= rev "")
  391. (vc-rcs-set-default-branch file nil))
  392. ;; now do the checkout
  393. (apply 'vc-do-command
  394. "*vc*" 0 "co" (vc-name file)
  395. ;; If locking is not strict, force to overwrite
  396. ;; the writable workfile.
  397. (if (eq (vc-rcs-checkout-model (list file)) 'implicit) "-f")
  398. (if editable "-l")
  399. (if (stringp rev)
  400. ;; a literal revision was specified
  401. (concat "-r" rev)
  402. (let ((workrev (vc-working-revision file)))
  403. (if workrev
  404. (concat "-r"
  405. (if (not rev)
  406. ;; no revision specified:
  407. ;; use current workfile version
  408. workrev
  409. ;; REV is t ...
  410. (if (not (vc-rcs-trunk-p workrev))
  411. ;; ... go to head of current branch
  412. (vc-branch-part workrev)
  413. ;; ... go to head of trunk
  414. (vc-rcs-set-default-branch file
  415. nil)
  416. ""))))))
  417. switches)
  418. ;; determine the new workfile version
  419. (with-current-buffer "*vc*"
  420. (setq new-version
  421. (vc-parse-buffer "^revision \\([0-9.]+\\).*\n" 1)))
  422. (vc-file-setprop file 'vc-working-revision new-version)
  423. ;; if necessary, adjust the default branch
  424. (and rev (not (string= rev ""))
  425. (vc-rcs-set-default-branch
  426. file
  427. (if (vc-rcs-latest-on-branch-p file new-version)
  428. (if (vc-rcs-trunk-p new-version) nil
  429. (vc-branch-part new-version))
  430. new-version)))))
  431. (message "Checking out %s...done" file))))))
  432. (defun vc-rcs-rollback (files)
  433. "Roll back, undoing the most recent checkins of FILES. Directories are
  434. expanded to all registered subfiles in them."
  435. (if (not files)
  436. (error "RCS backend doesn't support directory-level rollback"))
  437. (dolist (file (vc-expand-dirs files))
  438. (let* ((discard (vc-working-revision file))
  439. (previous (if (vc-rcs-trunk-p discard) "" (vc-branch-part discard)))
  440. (config (current-window-configuration))
  441. (done nil))
  442. (if (null (yes-or-no-p (format "Remove version %s from %s history? "
  443. discard file)))
  444. (error "Aborted"))
  445. (message "Removing revision %s from %s." discard file)
  446. (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-o" discard))
  447. ;; Check out the most recent remaining version. If it
  448. ;; fails, because the whole branch got deleted, do a
  449. ;; double-take and check out the version where the branch
  450. ;; started.
  451. (while (not done)
  452. (condition-case err
  453. (progn
  454. (vc-do-command "*vc*" 0 "co" (vc-name file) "-f"
  455. (concat "-u" previous))
  456. (setq done t))
  457. (error (set-buffer "*vc*")
  458. (goto-char (point-min))
  459. (if (search-forward "no side branches present for" nil t)
  460. (progn (setq previous (vc-branch-part previous))
  461. (vc-rcs-set-default-branch file previous)
  462. ;; vc-do-command popped up a window with
  463. ;; the error message. Get rid of it, by
  464. ;; restoring the old window configuration.
  465. (set-window-configuration config))
  466. ;; No, it was some other error: re-signal it.
  467. (signal (car err) (cdr err)))))))))
  468. (defun vc-rcs-revert (file &optional contents-done)
  469. "Revert FILE to the version it was based on. If FILE is a directory,
  470. revert all registered files beneath it."
  471. (if (file-directory-p file)
  472. (mapc 'vc-rcs-revert (vc-expand-dirs (list file)))
  473. (vc-do-command "*vc*" 0 "co" (vc-name file) "-f"
  474. (concat (if (eq (vc-state file) 'edited) "-u" "-r")
  475. (vc-working-revision file)))))
  476. (defun vc-rcs-merge (file first-version &optional second-version)
  477. "Merge changes into current working copy of FILE.
  478. The changes are between FIRST-VERSION and SECOND-VERSION."
  479. (vc-do-command "*vc*" 1 "rcsmerge" (vc-name file)
  480. "-kk" ; ignore keyword conflicts
  481. (concat "-r" first-version)
  482. (if second-version (concat "-r" second-version))))
  483. (defun vc-rcs-steal-lock (file &optional rev)
  484. "Steal the lock on the current workfile for FILE and revision REV.
  485. If FILE is a directory, steal the lock on all registered files beneath it.
  486. Needs RCS 5.6.2 or later for -M."
  487. (if (file-directory-p file)
  488. (mapc 'vc-rcs-steal-lock (vc-expand-dirs (list file)))
  489. (vc-do-command "*vc*" 0 "rcs" (vc-name file) "-M" (concat "-u" rev))
  490. ;; Do a real checkout after stealing the lock, so that we see
  491. ;; expanded headers.
  492. (vc-do-command "*vc*" 0 "co" (vc-name file) "-f" (concat "-l" rev))))
  493. (defun vc-rcs-modify-change-comment (files rev comment)
  494. "Modify the change comments change on FILES on a specified REV. If FILE is a
  495. directory the operation is applied to all registered files beneath it."
  496. (dolist (file (vc-expand-dirs files))
  497. (vc-do-command "*vc*" 0 "rcs" (vc-name file)
  498. (concat "-m" rev ":" comment))))
  499. ;;;
  500. ;;; History functions
  501. ;;;
  502. (defun vc-rcs-print-log-cleanup ()
  503. (let ((inhibit-read-only t))
  504. (goto-char (point-max))
  505. (forward-line -1)
  506. (while (looking-at "=*\n")
  507. (delete-char (- (match-end 0) (match-beginning 0)))
  508. (forward-line -1))
  509. (goto-char (point-min))
  510. (when (looking-at "[\b\t\n\v\f\r ]+")
  511. (delete-char (- (match-end 0) (match-beginning 0))))))
  512. (defun vc-rcs-print-log (files buffer &optional shortlog start-revision-ignored limit)
  513. "Get change log associated with FILE. If FILE is a
  514. directory the operation is applied to all registered files beneath it."
  515. (vc-do-command (or buffer "*vc*") 0 "rlog" (mapcar 'vc-name (vc-expand-dirs files)))
  516. (with-current-buffer (or buffer "*vc*")
  517. (vc-rcs-print-log-cleanup))
  518. (when limit 'limit-unsupported))
  519. (defun vc-rcs-diff (files &optional oldvers newvers buffer)
  520. "Get a difference report using RCS between two sets of files."
  521. (apply 'vc-do-command (or buffer "*vc-diff*")
  522. 1 ;; Always go synchronous, the repo is local
  523. "rcsdiff" (vc-expand-dirs files)
  524. (append (list "-q"
  525. (and oldvers (concat "-r" oldvers))
  526. (and newvers (concat "-r" newvers)))
  527. (vc-switches 'RCS 'diff))))
  528. (defun vc-rcs-comment-history (file)
  529. "Return a string with all log entries stored in BACKEND for FILE."
  530. (with-current-buffer "*vc*"
  531. ;; Has to be written this way, this function is used by the CVS backend too
  532. (vc-call-backend (vc-backend file) 'print-log (list file))
  533. ;; Remove cruft
  534. (let ((separator (concat "^-+\nrevision [0-9.]+\ndate: .*\n"
  535. "\\(branches: .*;\n\\)?"
  536. "\\(\\*\\*\\* empty log message \\*\\*\\*\n\\)?")))
  537. (goto-char (point-max)) (forward-line -1)
  538. (while (looking-at "=*\n")
  539. (delete-char (- (match-end 0) (match-beginning 0)))
  540. (forward-line -1))
  541. (goto-char (point-min))
  542. (if (looking-at "[\b\t\n\v\f\r ]+")
  543. (delete-char (- (match-end 0) (match-beginning 0))))
  544. (goto-char (point-min))
  545. (re-search-forward separator nil t)
  546. (delete-region (point-min) (point))
  547. (while (re-search-forward separator nil t)
  548. (delete-region (match-beginning 0) (match-end 0))))
  549. ;; Return the de-crufted comment list
  550. (buffer-string)))
  551. (defun vc-rcs-annotate-command (file buffer &optional revision)
  552. "Annotate FILE, inserting the results in BUFFER.
  553. Optional arg REVISION is a revision to annotate from."
  554. (vc-setup-buffer buffer)
  555. ;; Aside from the "head revision on the trunk", the instructions for
  556. ;; each revision on the trunk are an ordered list of kill and insert
  557. ;; commands necessary to go from the chronologically-following
  558. ;; revision to this one. That is, associated with revision N are
  559. ;; edits that applied to revision N+1 would result in revision N.
  560. ;;
  561. ;; On a branch, however, (some) things are inverted: the commands
  562. ;; listed are those necessary to go from the chronologically-preceding
  563. ;; revision to this one. That is, associated with revision N are
  564. ;; edits that applied to revision N-1 would result in revision N.
  565. ;;
  566. ;; So, to get per-line history info, we apply reverse-chronological
  567. ;; edits, starting with the head revision on the trunk, all the way
  568. ;; back through the initial revision (typically "1.1" or similar),
  569. ;; then apply forward-chronological edits -- keeping track of which
  570. ;; revision is associated with each inserted line -- until we reach
  571. ;; the desired revision for display (which may be either on the trunk
  572. ;; or on a branch).
  573. (let* ((tree (with-temp-buffer
  574. (insert-file-contents (vc-rcs-registered file))
  575. (vc-rcs-parse)))
  576. (revisions (cdr (assq 'revisions tree)))
  577. ;; The revision N whose instructions we currently are processing.
  578. (cur (cdr (assq 'head (cdr (assq 'headers tree)))))
  579. ;; Alist from the parse tree for N.
  580. (meta (cdr (assoc cur revisions)))
  581. ;; Point and temporary string, respectively.
  582. p s
  583. ;; "Next-branch list". Nil means the desired revision to
  584. ;; display lives on the trunk. Non-nil means it lives on a
  585. ;; branch, in which case the value is a list of revision pairs
  586. ;; (PARENT . CHILD), the first PARENT being on the trunk, that
  587. ;; links each series of revisions in the path from the initial
  588. ;; revision to the desired revision to display.
  589. nbls
  590. ;; "Path-accumulate-predicate plus revision/date/author".
  591. ;; Until set, forward-chronological edits are not accumulated.
  592. ;; Once set, its value (updated every revision) is used for
  593. ;; the text property `:vc-rcs-r/d/a' for inserts during
  594. ;; processing of forward-chronological instructions for N.
  595. ;; See internal func `r/d/a'.
  596. prda
  597. ;; List of forward-chronological instructions, each of the
  598. ;; form: (POS . ACTION), where POS is a buffer position. If
  599. ;; ACTION is a string, it is inserted, otherwise it is taken as
  600. ;; the number of characters to be deleted.
  601. path
  602. ;; N+1. When `cur' is "", this is the initial revision.
  603. pre)
  604. (unless revision
  605. (setq revision cur))
  606. (unless (assoc revision revisions)
  607. (error "No such revision: %s" revision))
  608. ;; Find which branches (if any) must be included in the edits.
  609. (let ((par revision)
  610. bpt kids)
  611. (while (setq bpt (vc-branch-part par)
  612. par (vc-branch-part bpt))
  613. (setq kids (cdr (assq 'branches (cdr (assoc par revisions)))))
  614. ;; A branchpoint may have multiple children. Find the right one.
  615. (while (not (string= bpt (vc-branch-part (car kids))))
  616. (setq kids (cdr kids)))
  617. (push (cons par (car kids)) nbls)))
  618. ;; Start with the full text.
  619. (set-buffer buffer)
  620. (insert (cdr (assq 'text meta)))
  621. ;; Apply reverse-chronological edits on the trunk, computing and
  622. ;; accumulating forward-chronological edits after some point, for
  623. ;; later.
  624. (flet ((r/d/a () (vector pre
  625. (cdr (assq 'date meta))
  626. (cdr (assq 'author meta)))))
  627. (while (when (setq pre cur cur (cdr (assq 'next meta)))
  628. (not (string= "" cur)))
  629. (setq
  630. ;; Start accumulating the forward-chronological edits when N+1
  631. ;; on the trunk is either the desired revision to display, or
  632. ;; the appropriate branchpoint for it. Do this before
  633. ;; updating `meta' since `r/d/a' uses N+1's `meta' value.
  634. prda (when (or prda (string= (if nbls (caar nbls) revision) pre))
  635. (r/d/a))
  636. meta (cdr (assoc cur revisions)))
  637. ;; Edits in the parse tree specify a line number (in the buffer
  638. ;; *BEFORE* editing occurs) to start from, but line numbers
  639. ;; change as a result of edits. To DTRT, we apply edits in
  640. ;; order of descending buffer position so that edits further
  641. ;; down in the buffer occur first w/o corrupting specified
  642. ;; buffer positions of edits occurring towards the beginning of
  643. ;; the buffer. In this way we avoid using markers. A pleasant
  644. ;; property of this approach is ability to push instructions
  645. ;; onto `path' directly, w/o need to maintain rev boundaries.
  646. (dolist (insn (cdr (assq :insn meta)))
  647. (goto-char (point-min))
  648. (forward-line (1- (pop insn)))
  649. (setq p (point))
  650. (case (pop insn)
  651. (k (setq s (buffer-substring-no-properties
  652. p (progn (forward-line (car insn))
  653. (point))))
  654. (when prda
  655. (push `(,p . ,(propertize s :vc-rcs-r/d/a prda)) path))
  656. (delete-region p (point)))
  657. (i (setq s (car insn))
  658. (when prda
  659. (push `(,p . ,(length s)) path))
  660. (insert s)))))
  661. ;; For the initial revision, setting `:vc-rcs-r/d/a' directly is
  662. ;; equivalent to pushing an insert instruction (of the entire buffer
  663. ;; contents) onto `path' then erasing the buffer, but less wasteful.
  664. (put-text-property (point-min) (point-max) :vc-rcs-r/d/a (r/d/a))
  665. ;; Now apply the forward-chronological edits for the trunk.
  666. (dolist (insn path)
  667. (goto-char (pop insn))
  668. (if (stringp insn)
  669. (insert insn)
  670. (delete-char insn)))
  671. ;; Now apply the forward-chronological edits (directly from the
  672. ;; parse-tree) for the branch(es), if necessary. We re-use vars
  673. ;; `pre' and `meta' for the sake of internal func `r/d/a'.
  674. (while nbls
  675. (setq pre (cdr (pop nbls)))
  676. (while (progn
  677. (setq meta (cdr (assoc pre revisions))
  678. prda nil)
  679. (dolist (insn (cdr (assq :insn meta)))
  680. (goto-char (point-min))
  681. (forward-line (1- (pop insn)))
  682. (case (pop insn)
  683. (k (delete-region
  684. (point) (progn (forward-line (car insn))
  685. (point))))
  686. (i (insert (propertize
  687. (car insn)
  688. :vc-rcs-r/d/a
  689. (or prda (setq prda (r/d/a))))))))
  690. (prog1 (not (string= (if nbls (caar nbls) revision) pre))
  691. (setq pre (cdr (assq 'next meta)))))))))
  692. ;; Lastly, for each line, insert at bol nicely-formatted history info.
  693. ;; We do two passes to collect summary information used to minimize
  694. ;; the annotation's usage of screen real-estate: (1) Consider rendered
  695. ;; width of revision plus author together as a unit; and (2) Omit
  696. ;; author entirely if all authors are the same as the user.
  697. (let ((ht (make-hash-table :test 'eq))
  698. (me (user-login-name))
  699. (maxw 0)
  700. (all-me t)
  701. rda w a)
  702. (goto-char (point-max))
  703. (while (not (bobp))
  704. (forward-line -1)
  705. (setq rda (get-text-property (point) :vc-rcs-r/d/a))
  706. (unless (gethash rda ht)
  707. (setq a (aref rda 2)
  708. all-me (and all-me (string= a me)))
  709. (puthash rda (setq w (+ (length (aref rda 0))
  710. (length a)))
  711. ht)
  712. (setq maxw (max w maxw))))
  713. (let ((padding (make-string maxw 32)))
  714. (flet ((pad (w) (substring-no-properties padding w))
  715. (render (rda &rest ls)
  716. (propertize
  717. (apply 'concat
  718. (format-time-string "%Y-%m-%d" (aref rda 1))
  719. " "
  720. (aref rda 0)
  721. ls)
  722. :vc-annotate-prefix t
  723. :vc-rcs-r/d/a rda)))
  724. (maphash
  725. (if all-me
  726. (lambda (rda w)
  727. (puthash rda (render rda (pad w) ": ") ht))
  728. (lambda (rda w)
  729. (puthash rda (render rda " " (pad w) " " (aref rda 2) ": ") ht)))
  730. ht)))
  731. (while (not (eobp))
  732. (insert (gethash (get-text-property (point) :vc-rcs-r/d/a) ht))
  733. (forward-line 1))))
  734. (declare-function vc-annotate-convert-time "vc-annotate" (time))
  735. (defun vc-rcs-annotate-current-time ()
  736. "Return the current time, based at midnight of the current day, and
  737. encoded as fractional days."
  738. (vc-annotate-convert-time
  739. (apply 'encode-time 0 0 0 (nthcdr 3 (decode-time (current-time))))))
  740. (defun vc-rcs-annotate-time ()
  741. "Return the time of the next annotation (as fraction of days)
  742. systime, or nil if there is none. Also, reposition point."
  743. (unless (eobp)
  744. (prog1 (vc-annotate-convert-time
  745. (aref (get-text-property (point) :vc-rcs-r/d/a) 1))
  746. (goto-char (next-single-property-change (point) :vc-annotate-prefix)))))
  747. (defun vc-rcs-annotate-extract-revision-at-line ()
  748. (aref (get-text-property (point) :vc-rcs-r/d/a) 0))
  749. ;;;
  750. ;;; Tag system
  751. ;;;
  752. (defun vc-rcs-create-tag (dir name branchp)
  753. (when branchp
  754. (error "RCS backend does not support module branches"))
  755. (let ((result (vc-tag-precondition dir)))
  756. (if (stringp result)
  757. (error "File %s is not up-to-date" result)
  758. (vc-file-tree-walk
  759. dir
  760. (lambda (f)
  761. (vc-do-command "*vc*" 0 "rcs" (vc-name f) (concat "-n" name ":")))))))
  762. ;;;
  763. ;;; Miscellaneous
  764. ;;;
  765. (defun vc-rcs-trunk-p (rev)
  766. "Return t if REV is a revision on the trunk."
  767. (not (eq nil (string-match "\\`[0-9]+\\.[0-9]+\\'" rev))))
  768. (defun vc-rcs-minor-part (rev)
  769. "Return the minor revision number of a revision number REV."
  770. (string-match "[0-9]+\\'" rev)
  771. (substring rev (match-beginning 0) (match-end 0)))
  772. (defun vc-rcs-previous-revision (file rev)
  773. "Return the revision number immediately preceding REV for FILE,
  774. or nil if there is no previous revision. This default
  775. implementation works for MAJOR.MINOR-style revision numbers as
  776. used by RCS and CVS."
  777. (let ((branch (vc-branch-part rev))
  778. (minor-num (string-to-number (vc-rcs-minor-part rev))))
  779. (when branch
  780. (if (> minor-num 1)
  781. ;; revision does probably not start a branch or release
  782. (concat branch "." (number-to-string (1- minor-num)))
  783. (if (vc-rcs-trunk-p rev)
  784. ;; we are at the beginning of the trunk --
  785. ;; don't know anything to return here
  786. nil
  787. ;; we are at the beginning of a branch --
  788. ;; return revision of starting point
  789. (vc-branch-part branch))))))
  790. (defun vc-rcs-next-revision (file rev)
  791. "Return the revision number immediately following REV for FILE,
  792. or nil if there is no next revision. This default implementation
  793. works for MAJOR.MINOR-style revision numbers as used by RCS
  794. and CVS."
  795. (when (not (string= rev (vc-working-revision file)))
  796. (let ((branch (vc-branch-part rev))
  797. (minor-num (string-to-number (vc-rcs-minor-part rev))))
  798. (concat branch "." (number-to-string (1+ minor-num))))))
  799. ;; Note that most GNU/Linux distributions seem to supply rcs2log in a
  800. ;; standard bin directory. Eg both Red Hat and Debian include it in
  801. ;; their cvs packages. It's not obvious why Emacs still needs to
  802. ;; provide it as well...
  803. (defvar vc-rcs-rcs2log-program
  804. (let (exe)
  805. (cond ((file-executable-p
  806. (setq exe (expand-file-name "rcs2log" exec-directory)))
  807. exe)
  808. ;; In the unlikely event that someone is running an
  809. ;; uninstalled Emacs and wants to do something RCS-related.
  810. ((file-executable-p
  811. (setq exe (expand-file-name "lib-src/rcs2log" source-directory)))
  812. exe)
  813. (t "rcs2log")))
  814. "Path to the `rcs2log' program (normally in `exec-directory').")
  815. (defun vc-rcs-update-changelog (files)
  816. "Default implementation of update-changelog.
  817. Uses `rcs2log' which only works for RCS and CVS."
  818. ;; FIXME: We (c|sh)ould add support for cvs2cl
  819. (let ((odefault default-directory)
  820. (changelog (find-change-log))
  821. ;; Presumably not portable to non-Unixy systems, along with rcs2log:
  822. (tempfile (make-temp-file
  823. (expand-file-name "vc"
  824. (or small-temporary-file-directory
  825. temporary-file-directory))))
  826. (login-name (or user-login-name
  827. (format "uid%d" (number-to-string (user-uid)))))
  828. (full-name (or add-log-full-name
  829. (user-full-name)
  830. (user-login-name)
  831. (format "uid%d" (number-to-string (user-uid)))))
  832. (mailing-address (or add-log-mailing-address
  833. user-mail-address)))
  834. (find-file-other-window changelog)
  835. (barf-if-buffer-read-only)
  836. (vc-buffer-sync)
  837. (undo-boundary)
  838. (goto-char (point-min))
  839. (push-mark)
  840. (message "Computing change log entries...")
  841. (message "Computing change log entries... %s"
  842. (unwind-protect
  843. (progn
  844. (setq default-directory odefault)
  845. (if (eq 0 (apply 'call-process vc-rcs-rcs2log-program
  846. nil (list t tempfile) nil
  847. "-c" changelog
  848. "-u" (concat login-name
  849. "\t" full-name
  850. "\t" mailing-address)
  851. (mapcar
  852. (lambda (f)
  853. (file-relative-name
  854. (expand-file-name f odefault)))
  855. files)))
  856. "done"
  857. (pop-to-buffer (get-buffer-create "*vc*"))
  858. (erase-buffer)
  859. (insert-file-contents tempfile)
  860. "failed"))
  861. (setq default-directory (file-name-directory changelog))
  862. (delete-file tempfile)))))
  863. (defun vc-rcs-check-headers ()
  864. "Check if the current file has any headers in it."
  865. (save-excursion
  866. (goto-char (point-min))
  867. (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
  868. \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
  869. (defun vc-rcs-clear-headers ()
  870. "Implementation of vc-clear-headers for RCS."
  871. (let ((case-fold-search nil))
  872. (goto-char (point-min))
  873. (while (re-search-forward
  874. (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
  875. "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
  876. nil t)
  877. (replace-match "$\\1$"))))
  878. (defun vc-rcs-rename-file (old new)
  879. ;; Just move the master file (using vc-rcs-master-templates).
  880. (vc-rename-master (vc-name old) new vc-rcs-master-templates))
  881. (defun vc-rcs-find-file-hook ()
  882. ;; If the file is locked by some other user, make
  883. ;; the buffer read-only. Like this, even root
  884. ;; cannot modify a file that someone else has locked.
  885. (and (stringp (vc-state buffer-file-name 'RCS))
  886. (setq buffer-read-only t)))
  887. ;;;
  888. ;;; Internal functions
  889. ;;;
  890. (defun vc-rcs-workfile-is-newer (file)
  891. "Return non-nil if FILE is newer than its RCS master.
  892. This likely means that FILE has been changed with respect
  893. to its master version."
  894. (let ((file-time (nth 5 (file-attributes file)))
  895. (master-time (nth 5 (file-attributes (vc-name file)))))
  896. (or (> (nth 0 file-time) (nth 0 master-time))
  897. (and (= (nth 0 file-time) (nth 0 master-time))
  898. (> (nth 1 file-time) (nth 1 master-time))))))
  899. (defun vc-rcs-find-most-recent-rev (branch)
  900. "Find most recent revision on BRANCH."
  901. (goto-char (point-min))
  902. (let ((latest-rev -1) value)
  903. (while (re-search-forward (concat "^\\(" (regexp-quote branch)
  904. "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
  905. nil t)
  906. (let ((rev (string-to-number (match-string 2))))
  907. (when (< latest-rev rev)
  908. (setq latest-rev rev)
  909. (setq value (match-string 1)))))
  910. (or value
  911. (vc-branch-part branch))))
  912. (defun vc-rcs-fetch-master-state (file &optional working-revision)
  913. "Compute the master file's idea of the state of FILE.
  914. If a WORKING-REVISION is given, compute the state of that version,
  915. otherwise determine the workfile version based on the master file.
  916. This function sets the properties `vc-working-revision' and
  917. `vc-checkout-model' to their correct values, based on the master
  918. file."
  919. (with-temp-buffer
  920. (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
  921. (progn (goto-char (point-min))
  922. (not (looking-at "^head[ \t\n]+[^;]+;$"))))
  923. (error "File %s is not an RCS master file" (vc-name file)))
  924. (let ((workfile-is-latest nil)
  925. (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
  926. (vc-file-setprop file 'vc-rcs-default-branch default-branch)
  927. (unless working-revision
  928. ;; Workfile version not known yet. Determine that first. It
  929. ;; is either the head of the trunk, the head of the default
  930. ;; branch, or the "default branch" itself, if that is a full
  931. ;; revision number.
  932. (cond
  933. ;; no default branch
  934. ((or (not default-branch) (string= "" default-branch))
  935. (setq working-revision
  936. (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
  937. (setq workfile-is-latest t))
  938. ;; default branch is actually a revision
  939. ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
  940. default-branch)
  941. (setq working-revision default-branch))
  942. ;; else, search for the head of the default branch
  943. (t (vc-insert-file (vc-name file) "^desc")
  944. (setq working-revision
  945. (vc-rcs-find-most-recent-rev default-branch))
  946. (setq workfile-is-latest t)))
  947. (vc-file-setprop file 'vc-working-revision working-revision))
  948. ;; Check strict locking
  949. (goto-char (point-min))
  950. (vc-file-setprop file 'vc-checkout-model
  951. (if (re-search-forward ";[ \t\n]*strict;" nil t)
  952. 'locking 'implicit))
  953. ;; Compute state of workfile version
  954. (goto-char (point-min))
  955. (let ((locking-user
  956. (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
  957. (regexp-quote working-revision)
  958. "[^0-9.]")
  959. 1)))
  960. (cond
  961. ;; not locked
  962. ((not locking-user)
  963. (if (or workfile-is-latest
  964. (vc-rcs-latest-on-branch-p file working-revision))
  965. ;; workfile version is latest on branch
  966. 'up-to-date
  967. ;; workfile version is not latest on branch
  968. 'needs-update))
  969. ;; locked by the calling user
  970. ((and (stringp locking-user)
  971. (string= locking-user (vc-user-login-name file)))
  972. ;; Don't call `vc-rcs-checkout-model' to avoid inf-looping.
  973. (if (or (eq (vc-file-getprop file 'vc-checkout-model) 'locking)
  974. workfile-is-latest
  975. (vc-rcs-latest-on-branch-p file working-revision))
  976. 'edited
  977. ;; Locking is not used for the file, but the owner does
  978. ;; have a lock, and there is a higher version on the current
  979. ;; branch. Not sure if this can occur, and if it is right
  980. ;; to use `needs-merge' in this case.
  981. 'needs-merge))
  982. ;; locked by somebody else
  983. ((stringp locking-user)
  984. locking-user)
  985. (t
  986. (error "Error getting state of RCS file")))))))
  987. (defun vc-rcs-consult-headers (file)
  988. "Search for RCS headers in FILE, and set properties accordingly.
  989. Returns: nil if no headers were found
  990. 'rev if a workfile revision was found
  991. 'rev-and-lock if revision and lock info was found"
  992. (cond
  993. ((not (get-file-buffer file)) nil)
  994. ((let (status version locking-user)
  995. (with-current-buffer (get-file-buffer file)
  996. (save-excursion
  997. (goto-char (point-min))
  998. (cond
  999. ;; search for $Id or $Header
  1000. ;; -------------------------
  1001. ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
  1002. ((or (and (search-forward "$Id\ : " nil t)
  1003. (looking-at "[^ ]+ \\([0-9.]+\\) "))
  1004. (and (progn (goto-char (point-min))
  1005. (search-forward "$Header\ : " nil t))
  1006. (looking-at "[^ ]+ \\([0-9.]+\\) ")))
  1007. (goto-char (match-end 0))
  1008. ;; if found, store the revision number ...
  1009. (setq version (match-string-no-properties 1))
  1010. ;; ... and check for the locking state
  1011. (cond
  1012. ((looking-at
  1013. (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
  1014. "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
  1015. "[^ ]+ [^ ]+ ")) ; author & state
  1016. (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
  1017. (cond
  1018. ;; unlocked revision
  1019. ((looking-at "\\$")
  1020. (setq locking-user 'none)
  1021. (setq status 'rev-and-lock))
  1022. ;; revision is locked by some user
  1023. ((looking-at "\\([^ ]+\\) \\$")
  1024. (setq locking-user (match-string-no-properties 1))
  1025. (setq status 'rev-and-lock))
  1026. ;; everything else: false
  1027. (nil)))
  1028. ;; unexpected information in
  1029. ;; keyword string --> quit
  1030. (nil)))
  1031. ;; search for $Revision
  1032. ;; --------------------
  1033. ((re-search-forward (concat "\\$"
  1034. "Revision: \\([0-9.]+\\) \\$")
  1035. nil t)
  1036. ;; if found, store the revision number ...
  1037. (setq version (match-string-no-properties 1))
  1038. ;; and see if there's any lock information
  1039. (goto-char (point-min))
  1040. (if (re-search-forward (concat "\\$" "Locker:") nil t)
  1041. (cond ((looking-at " \\([^ ]+\\) \\$")
  1042. (setq locking-user (match-string-no-properties 1))
  1043. (setq status 'rev-and-lock))
  1044. ((looking-at " *\\$")
  1045. (setq locking-user 'none)
  1046. (setq status 'rev-and-lock))
  1047. (t
  1048. (setq locking-user 'none)
  1049. (setq status 'rev-and-lock)))
  1050. (setq status 'rev)))
  1051. ;; else: nothing found
  1052. ;; -------------------
  1053. (t nil))))
  1054. (if status (vc-file-setprop file 'vc-working-revision version))
  1055. (and (eq status 'rev-and-lock)
  1056. (vc-file-setprop file 'vc-state
  1057. (cond
  1058. ((eq locking-user 'none) 'up-to-date)
  1059. ((string= locking-user (vc-user-login-name file))
  1060. 'edited)
  1061. (t locking-user)))
  1062. ;; If the file has headers, we don't want to query the
  1063. ;; master file, because that would eliminate all the
  1064. ;; performance gain the headers brought us. We therefore
  1065. ;; use a heuristic now to find out whether locking is used
  1066. ;; for this file. If we trust the file permissions, and the
  1067. ;; file is not locked, then if the file is read-only we
  1068. ;; assume that locking is used for the file, otherwise
  1069. ;; locking is not used.
  1070. (not (vc-mistrust-permissions file))
  1071. (vc-up-to-date-p file)
  1072. (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
  1073. (vc-file-setprop file 'vc-checkout-model 'locking)
  1074. (vc-file-setprop file 'vc-checkout-model 'implicit)))
  1075. status))))
  1076. (defun vc-release-greater-or-equal (r1 r2)
  1077. "Compare release numbers, represented as strings.
  1078. Release components are assumed cardinal numbers, not decimal fractions
  1079. \(5.10 is a higher release than 5.9\). Omitted fields are considered
  1080. lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
  1081. of the string is found, or a non-numeric component shows up \(5.6.7 is
  1082. earlier than \"5.6.7 beta\", which is probably not what you want in
  1083. some cases\). This code is suitable for existing RCS release numbers.
  1084. CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
  1085. (let (v1 v2 i1 i2)
  1086. (catch 'done
  1087. (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
  1088. (setq i1 (match-end 0))
  1089. (setq v1 (string-to-number (match-string 1 r1)))
  1090. (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
  1091. (setq i2 (match-end 0))
  1092. (setq v2 (string-to-number (match-string 1 r2)))
  1093. (if (> v1 v2) (throw 'done t)
  1094. (if (< v1 v2) (throw 'done nil)
  1095. (throw 'done
  1096. (vc-release-greater-or-equal
  1097. (substring r1 i1)
  1098. (substring r2 i2)))))))
  1099. (throw 'done t)))
  1100. (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
  1101. (throw 'done nil))
  1102. (throw 'done t)))))
  1103. (defun vc-rcs-release-p (release)
  1104. "Return t if we have RELEASE or better."
  1105. (let ((installation (vc-rcs-system-release)))
  1106. (if (and installation
  1107. (not (eq installation 'unknown)))
  1108. (vc-release-greater-or-equal installation release))))
  1109. (defun vc-rcs-system-release ()
  1110. "Return the RCS release installed on this system, as a string.
  1111. Return symbol `unknown' if the release cannot be deducted. The user can
  1112. override this using variable `vc-rcs-release'.
  1113. If the user has not set variable `vc-rcs-release' and it is nil,
  1114. variable `vc-rcs-release' is set to the returned value."
  1115. (or vc-rcs-release
  1116. (setq vc-rcs-release
  1117. (or (and (zerop (vc-do-command "*vc*" nil "rcs" nil "-V"))
  1118. (with-current-buffer (get-buffer "*vc*")
  1119. (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
  1120. 'unknown))))
  1121. (defun vc-rcs-set-non-strict-locking (file)
  1122. (vc-do-command "*vc*" 0 "rcs" file "-U")
  1123. (vc-file-setprop file 'vc-checkout-model 'implicit)
  1124. (set-file-modes file (logior (file-modes file) 128)))
  1125. (defun vc-rcs-set-default-branch (file branch)
  1126. (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-b" branch))
  1127. (vc-file-setprop file 'vc-rcs-default-branch branch))
  1128. (defun vc-rcs-parse (&optional buffer)
  1129. "Parse current buffer, presumed to be in RCS-style masterfile format.
  1130. Optional arg BUFFER specifies another buffer to parse. Return an alist
  1131. of two elements, w/ keys `headers' and `revisions' and values in turn
  1132. sub-alists. For `headers', the values unless otherwise specified are
  1133. strings and the keys are:
  1134. desc -- description
  1135. head -- latest revision
  1136. branch -- the branch the \"head revision\" lies on;
  1137. absent if the head revision lies on the trunk
  1138. access -- ???
  1139. symbols -- sub-alist of (SYMBOL . REVISION) elements
  1140. locks -- if file is checked out, something like \"ttn:1.7\"
  1141. strict -- t if \"strict locking\" is in effect, otherwise nil
  1142. comment -- may be absent; typically something like \"# \" or \"; \"
  1143. expand -- may be absent; ???
  1144. For `revisions', the car is REVISION (string), the cdr a sub-alist,
  1145. with string values (unless otherwise specified) and keys:
  1146. date -- a time value (like that returned by `encode-time'); as a
  1147. special case, a year value less than 100 is augmented by 1900
  1148. author -- username
  1149. state -- typically \"Exp\" or \"Rel\"
  1150. branches -- list of revisions that begin branches from this revision
  1151. next -- on the trunk: the chronologically-preceding revision, or \"\";
  1152. on a branch: the chronologically-following revision, or \"\"
  1153. log -- change log entry
  1154. text -- for the head revision on the trunk, the body of the file;
  1155. other revisions have `:insn' instead
  1156. :insn -- for non-head revisions, a list of parsed instructions
  1157. in one of two forms, in both cases START meaning \"first
  1158. go to line START\":
  1159. - `(START k COUNT)' -- kill COUNT lines
  1160. - `(START i TEXT)' -- insert TEXT (a string)
  1161. The list is in descending order by START.
  1162. The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
  1163. (setq buffer (get-buffer (or buffer (current-buffer))))
  1164. (set-buffer buffer)
  1165. ;; An RCS masterfile can be viewed as containing four regular (for the
  1166. ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
  1167. ;; the "description" and (d) the "rev bodies", in that order. In the
  1168. ;; returned alist (see docstring), elements from (b) and (d) are
  1169. ;; combined pairwise to form the "revisions", while those from (a) and
  1170. ;; (c) are simply combined to form the "headers".
  1171. ;;
  1172. ;; Loosely speaking, each section contains a series of alternating
  1173. ;; "tags" and "printed representations". In the (b) and (d), many
  1174. ;; such series can appear, and a revision number on a line by itself
  1175. ;; precedes the series of tags and printed representations associated
  1176. ;; with it.
  1177. ;;
  1178. ;; In (a) and (b), the printed representations (with the exception of
  1179. ;; the `comment' tag in the headers) terminate with a semicolon, which
  1180. ;; is NOT part of the "value" finally associated with the tag. All
  1181. ;; other printed representations are in "@@-format"; there is an "@",
  1182. ;; the middle part (to be translated into the value), another "@" and
  1183. ;; a newline. Each "@@" in the middle part indicates the position of
  1184. ;; a single "@" (and consequently the requirement of an additional
  1185. ;; initial step when translating to the value).
  1186. ;;
  1187. ;; Parser state includes vars that collect parts of the return value...
  1188. (let ((desc nil) (headers nil) (revs nil)
  1189. ;; ... as well as vars that support a single-pass, tag-assisted,
  1190. ;; minimal-data-copying scan. Basically -- skirting around the
  1191. ;; grouping by revision required in (b) and (d) -- we repeatedly
  1192. ;; and context-sensitively read a tag (that MUST be present),
  1193. ;; determine the bounds of the printed representation, translate
  1194. ;; it into a value, and push the tag plus value onto one of the
  1195. ;; collection vars. Finally, we return the parse tree
  1196. ;; incorporating the values of the collection vars (see "rv").
  1197. ;;
  1198. ;; A symbol or string to keep track of context (for error messages).
  1199. context
  1200. ;; A symbol, the current tag.
  1201. tok
  1202. ;; Region (begin and end buffer positions) of the printed
  1203. ;; representation for the current tag.
  1204. b e
  1205. ;; A list of buffer positions where "@@" can be found within the
  1206. ;; printed representation region. For each location, we push two
  1207. ;; elements onto the list, 1+ and 2+ the location, respectively,
  1208. ;; with the 2+ appearing at the head. In this way, the expression
  1209. ;; `(,e ,@@-holes ,b)
  1210. ;; describes regions that can be concatenated (in reverse order)
  1211. ;; to "de-@@-format" the printed representation as the first step
  1212. ;; to translating it into some value. See internal func `gather'.
  1213. @-holes)
  1214. (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
  1215. (at (tag) (save-excursion (eq tag (read buffer))))
  1216. (to-eol () (buffer-substring-no-properties
  1217. (point) (progn (forward-line 1)
  1218. (1- (point)))))
  1219. (to-semi () (setq b (point)
  1220. e (progn (search-forward ";")
  1221. (1- (point)))))
  1222. (to-one@ () (setq @-holes nil
  1223. b (progn (search-forward "@") (point))
  1224. e (progn (while (and (search-forward "@")
  1225. (= ?@ (char-after))
  1226. (progn
  1227. (push (point) @-holes)
  1228. (forward-char 1)
  1229. (push (point) @-holes))))
  1230. (1- (point)))))
  1231. (tok+val (set-b+e name &optional proc)
  1232. (unless (eq name (setq tok (read buffer)))
  1233. (error "Missing `%s' while parsing %s" name context))
  1234. (sw)
  1235. (funcall set-b+e)
  1236. (cons tok (if proc
  1237. (funcall proc)
  1238. (buffer-substring-no-properties b e))))
  1239. (k-semi (name &optional proc) (tok+val 'to-semi name proc))
  1240. (gather () (let ((pairs `(,e ,@@-holes ,b))
  1241. acc)
  1242. (while pairs
  1243. (push (buffer-substring-no-properties
  1244. (cadr pairs) (car pairs))
  1245. acc)
  1246. (setq pairs (cddr pairs)))
  1247. (apply 'concat acc)))
  1248. (k-one@ (name &optional later) (tok+val 'to-one@ name
  1249. (if later
  1250. (lambda () t)
  1251. 'gather))))
  1252. (save-excursion
  1253. (goto-char (point-min))
  1254. ;; headers
  1255. (setq context 'headers)
  1256. (flet ((hpush (name &optional proc)
  1257. (push (k-semi name proc) headers)))
  1258. (hpush 'head)
  1259. (when (at 'branch)
  1260. (hpush 'branch))
  1261. (hpush 'access)
  1262. (hpush 'symbols
  1263. (lambda ()
  1264. (mapcar (lambda (together)
  1265. (let ((two (split-string together ":")))
  1266. (setcar two (intern (car two)))
  1267. (setcdr two (cadr two))
  1268. two))
  1269. (split-string
  1270. (buffer-substring-no-properties b e)))))
  1271. (hpush 'locks))
  1272. (push `(strict . ,(when (at 'strict)
  1273. (search-forward ";")
  1274. t))
  1275. headers)
  1276. (when (at 'comment)
  1277. (push (k-one@ 'comment) headers)
  1278. (search-forward ";"))
  1279. (when (at 'expand)
  1280. (push (k-one@ 'expand) headers)
  1281. (search-forward ";"))
  1282. (setq headers (nreverse headers))
  1283. ;; rev headers
  1284. (sw) (setq context 'rev-headers)
  1285. (while (looking-at "[0-9]")
  1286. (push `(,(to-eol)
  1287. ,(k-semi 'date
  1288. (lambda ()
  1289. (let ((ls (mapcar 'string-to-number
  1290. (split-string
  1291. (buffer-substring-no-properties
  1292. b e)
  1293. "\\."))))
  1294. ;; Hack the year -- verified to be the
  1295. ;; same algorithm used in RCS 5.7.
  1296. (when (< (car ls) 100)
  1297. (setcar ls (+ 1900 (car ls))))
  1298. (apply 'encode-time (nreverse ls)))))
  1299. ,@(mapcar 'k-semi '(author state))
  1300. ,(k-semi 'branches
  1301. (lambda ()
  1302. (split-string
  1303. (buffer-substring-no-properties b e))))
  1304. ,(k-semi 'next))
  1305. revs)
  1306. (sw))
  1307. (setq revs (nreverse revs))
  1308. ;; desc
  1309. (sw) (setq context 'desc
  1310. desc (k-one@ 'desc))
  1311. ;; rev bodies
  1312. (let (acc
  1313. ;; Element of `revs' that initially holds only header info.
  1314. ;; "Pairwise combination" occurs when we add body info.
  1315. rev
  1316. ;; Components of the editing commands (aside from the actual
  1317. ;; text) that comprise the `text' printed representations
  1318. ;; (not including the "head" revision).
  1319. cmd start act
  1320. ;; Ascending (reversed) `@-holes' which the internal func
  1321. ;; `incg' pops to effect incremental gathering.
  1322. asc
  1323. ;; Function to extract text (for the `a' command), either
  1324. ;; `incg' or `buffer-substring-no-properties'. (This is
  1325. ;; for speed; strictly speaking, it is sufficient to use
  1326. ;; only the former since it behaves identically to the
  1327. ;; latter in the absence of "@@".)
  1328. sub)
  1329. (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
  1330. (while (and asc (< (car asc) e))
  1331. (push (pop asc) @-holes))
  1332. ;; Self-deprecate when work is done.
  1333. ;; Folding many dimensions into one.
  1334. ;; Thanks B.Mandelbrot, for complex sum.
  1335. ;; O beauteous math! --the Unvexed Bum
  1336. (unless asc
  1337. (setq sub 'buffer-substring-no-properties))
  1338. (gather))))
  1339. (while (and (sw)
  1340. (not (eobp))
  1341. (setq context (to-eol)
  1342. rev (or (assoc context revs)
  1343. (error "Rev `%s' has body but no head"
  1344. context))))
  1345. (push (k-one@ 'log) (cdr rev))
  1346. ;; For rev body `text' tags, delay translation slightly...
  1347. (push (k-one@ 'text t) (cdr rev))
  1348. ;; ... until we decide which tag and value is appropriate to
  1349. ;; collect. For the "head" revision, compute the value of the
  1350. ;; `text' printed representation by simple `gather'. For all
  1351. ;; other revisions, replace the `text' tag+value with `:insn'
  1352. ;; plus value, always scanning in-place.
  1353. (if (string= context (cdr (assq 'head headers)))
  1354. (setcdr (cadr rev) (gather))
  1355. (if @-holes
  1356. (setq asc (nreverse @-holes)
  1357. sub 'incg)
  1358. (setq sub 'buffer-substring-no-properties))
  1359. (goto-char b)
  1360. (setq acc nil)
  1361. (while (< (point) e)
  1362. (forward-char 1)
  1363. (setq cmd (char-before)
  1364. start (read (current-buffer))
  1365. act (read (current-buffer)))
  1366. (forward-char 1)
  1367. (push (case cmd
  1368. (?d
  1369. ;; `d' means "delete lines".
  1370. ;; For Emacs spirit, we use `k' for "kill".
  1371. `(,start k ,act))
  1372. (?a
  1373. ;; `a' means "append after this line" but
  1374. ;; internally we normalize it so that START
  1375. ;; specifies the actual line for insert, thus
  1376. ;; requiring less hair in the realization algs.
  1377. ;; For Emacs spirit, we use `i' for "insert".
  1378. `(,(1+ start) i
  1379. ,(funcall sub (point) (progn (forward-line act)
  1380. (point)))))
  1381. (t (error "Bad command `%c' in `text' for rev `%s'"
  1382. cmd context)))
  1383. acc))
  1384. (goto-char (1+ e))
  1385. (setcar (cdr rev) (cons :insn acc)))))))
  1386. ;; rv
  1387. `((headers ,desc ,@headers)
  1388. (revisions ,@revs)))))
  1389. (provide 'vc-rcs)
  1390. ;;; vc-rcs.el ends here