vc-rcs.el 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. (defun vc-rcs-update-changelog (files)
  800. "Default implementation of update-changelog.
  801. Uses `rcs2log' which only works for RCS and CVS."
  802. ;; FIXME: We (c|sh)ould add support for cvs2cl
  803. (let ((odefault default-directory)
  804. (changelog (find-change-log))
  805. ;; Presumably not portable to non-Unixy systems, along with rcs2log:
  806. (tempfile (make-temp-file
  807. (expand-file-name "vc"
  808. (or small-temporary-file-directory
  809. temporary-file-directory))))
  810. (login-name (or user-login-name
  811. (format "uid%d" (number-to-string (user-uid)))))
  812. (full-name (or add-log-full-name
  813. (user-full-name)
  814. (user-login-name)
  815. (format "uid%d" (number-to-string (user-uid)))))
  816. (mailing-address (or add-log-mailing-address
  817. user-mail-address)))
  818. (find-file-other-window changelog)
  819. (barf-if-buffer-read-only)
  820. (vc-buffer-sync)
  821. (undo-boundary)
  822. (goto-char (point-min))
  823. (push-mark)
  824. (message "Computing change log entries...")
  825. (message "Computing change log entries... %s"
  826. (unwind-protect
  827. (progn
  828. (setq default-directory odefault)
  829. (if (eq 0 (apply 'call-process
  830. (expand-file-name "rcs2log"
  831. exec-directory)
  832. nil (list t tempfile) nil
  833. "-c" changelog
  834. "-u" (concat login-name
  835. "\t" full-name
  836. "\t" mailing-address)
  837. (mapcar
  838. (lambda (f)
  839. (file-relative-name
  840. (expand-file-name f odefault)))
  841. files)))
  842. "done"
  843. (pop-to-buffer (get-buffer-create "*vc*"))
  844. (erase-buffer)
  845. (insert-file-contents tempfile)
  846. "failed"))
  847. (setq default-directory (file-name-directory changelog))
  848. (delete-file tempfile)))))
  849. (defun vc-rcs-check-headers ()
  850. "Check if the current file has any headers in it."
  851. (save-excursion
  852. (goto-char (point-min))
  853. (re-search-forward "\\$[A-Za-z\300-\326\330-\366\370-\377]+\
  854. \\(: [\t -#%-\176\240-\377]*\\)?\\$" nil t)))
  855. (defun vc-rcs-clear-headers ()
  856. "Implementation of vc-clear-headers for RCS."
  857. (let ((case-fold-search nil))
  858. (goto-char (point-min))
  859. (while (re-search-forward
  860. (concat "\\$\\(Author\\|Date\\|Header\\|Id\\|Locker\\|Name\\|"
  861. "RCSfile\\|Revision\\|Source\\|State\\): [^$\n]+\\$")
  862. nil t)
  863. (replace-match "$\\1$"))))
  864. (defun vc-rcs-rename-file (old new)
  865. ;; Just move the master file (using vc-rcs-master-templates).
  866. (vc-rename-master (vc-name old) new vc-rcs-master-templates))
  867. (defun vc-rcs-find-file-hook ()
  868. ;; If the file is locked by some other user, make
  869. ;; the buffer read-only. Like this, even root
  870. ;; cannot modify a file that someone else has locked.
  871. (and (stringp (vc-state buffer-file-name 'RCS))
  872. (setq buffer-read-only t)))
  873. ;;;
  874. ;;; Internal functions
  875. ;;;
  876. (defun vc-rcs-workfile-is-newer (file)
  877. "Return non-nil if FILE is newer than its RCS master.
  878. This likely means that FILE has been changed with respect
  879. to its master version."
  880. (let ((file-time (nth 5 (file-attributes file)))
  881. (master-time (nth 5 (file-attributes (vc-name file)))))
  882. (or (> (nth 0 file-time) (nth 0 master-time))
  883. (and (= (nth 0 file-time) (nth 0 master-time))
  884. (> (nth 1 file-time) (nth 1 master-time))))))
  885. (defun vc-rcs-find-most-recent-rev (branch)
  886. "Find most recent revision on BRANCH."
  887. (goto-char (point-min))
  888. (let ((latest-rev -1) value)
  889. (while (re-search-forward (concat "^\\(" (regexp-quote branch)
  890. "\\.\\([0-9]+\\)\\)\ndate[ \t]+[0-9.]+;")
  891. nil t)
  892. (let ((rev (string-to-number (match-string 2))))
  893. (when (< latest-rev rev)
  894. (setq latest-rev rev)
  895. (setq value (match-string 1)))))
  896. (or value
  897. (vc-branch-part branch))))
  898. (defun vc-rcs-fetch-master-state (file &optional working-revision)
  899. "Compute the master file's idea of the state of FILE.
  900. If a WORKING-REVISION is given, compute the state of that version,
  901. otherwise determine the workfile version based on the master file.
  902. This function sets the properties `vc-working-revision' and
  903. `vc-checkout-model' to their correct values, based on the master
  904. file."
  905. (with-temp-buffer
  906. (if (or (not (vc-insert-file (vc-name file) "^[0-9]"))
  907. (progn (goto-char (point-min))
  908. (not (looking-at "^head[ \t\n]+[^;]+;$"))))
  909. (error "File %s is not an RCS master file" (vc-name file)))
  910. (let ((workfile-is-latest nil)
  911. (default-branch (vc-parse-buffer "^branch[ \t\n]+\\([^;]*\\);" 1)))
  912. (vc-file-setprop file 'vc-rcs-default-branch default-branch)
  913. (unless working-revision
  914. ;; Workfile version not known yet. Determine that first. It
  915. ;; is either the head of the trunk, the head of the default
  916. ;; branch, or the "default branch" itself, if that is a full
  917. ;; revision number.
  918. (cond
  919. ;; no default branch
  920. ((or (not default-branch) (string= "" default-branch))
  921. (setq working-revision
  922. (vc-parse-buffer "^head[ \t\n]+\\([^;]+\\);" 1))
  923. (setq workfile-is-latest t))
  924. ;; default branch is actually a revision
  925. ((string-match "^[0-9]+\\.[0-9]+\\(\\.[0-9]+\\.[0-9]+\\)*$"
  926. default-branch)
  927. (setq working-revision default-branch))
  928. ;; else, search for the head of the default branch
  929. (t (vc-insert-file (vc-name file) "^desc")
  930. (setq working-revision
  931. (vc-rcs-find-most-recent-rev default-branch))
  932. (setq workfile-is-latest t)))
  933. (vc-file-setprop file 'vc-working-revision working-revision))
  934. ;; Check strict locking
  935. (goto-char (point-min))
  936. (vc-file-setprop file 'vc-checkout-model
  937. (if (re-search-forward ";[ \t\n]*strict;" nil t)
  938. 'locking 'implicit))
  939. ;; Compute state of workfile version
  940. (goto-char (point-min))
  941. (let ((locking-user
  942. (vc-parse-buffer (concat "^locks[ \t\n]+[^;]*[ \t\n]+\\([^:]+\\):"
  943. (regexp-quote working-revision)
  944. "[^0-9.]")
  945. 1)))
  946. (cond
  947. ;; not locked
  948. ((not locking-user)
  949. (if (or workfile-is-latest
  950. (vc-rcs-latest-on-branch-p file working-revision))
  951. ;; workfile version is latest on branch
  952. 'up-to-date
  953. ;; workfile version is not latest on branch
  954. 'needs-update))
  955. ;; locked by the calling user
  956. ((and (stringp locking-user)
  957. (string= locking-user (vc-user-login-name file)))
  958. ;; Don't call `vc-rcs-checkout-model' to avoid inf-looping.
  959. (if (or (eq (vc-file-getprop file 'vc-checkout-model) 'locking)
  960. workfile-is-latest
  961. (vc-rcs-latest-on-branch-p file working-revision))
  962. 'edited
  963. ;; Locking is not used for the file, but the owner does
  964. ;; have a lock, and there is a higher version on the current
  965. ;; branch. Not sure if this can occur, and if it is right
  966. ;; to use `needs-merge' in this case.
  967. 'needs-merge))
  968. ;; locked by somebody else
  969. ((stringp locking-user)
  970. locking-user)
  971. (t
  972. (error "Error getting state of RCS file")))))))
  973. (defun vc-rcs-consult-headers (file)
  974. "Search for RCS headers in FILE, and set properties accordingly.
  975. Returns: nil if no headers were found
  976. 'rev if a workfile revision was found
  977. 'rev-and-lock if revision and lock info was found"
  978. (cond
  979. ((not (get-file-buffer file)) nil)
  980. ((let (status version locking-user)
  981. (with-current-buffer (get-file-buffer file)
  982. (save-excursion
  983. (goto-char (point-min))
  984. (cond
  985. ;; search for $Id or $Header
  986. ;; -------------------------
  987. ;; The `\ 's below avoid an RCS 5.7 bug when checking in this file.
  988. ((or (and (search-forward "$Id\ : " nil t)
  989. (looking-at "[^ ]+ \\([0-9.]+\\) "))
  990. (and (progn (goto-char (point-min))
  991. (search-forward "$Header\ : " nil t))
  992. (looking-at "[^ ]+ \\([0-9.]+\\) ")))
  993. (goto-char (match-end 0))
  994. ;; if found, store the revision number ...
  995. (setq version (match-string-no-properties 1))
  996. ;; ... and check for the locking state
  997. (cond
  998. ((looking-at
  999. (concat "[0-9]+[/-][01][0-9][/-][0-3][0-9] " ; date
  1000. "[0-2][0-9]:[0-5][0-9]+:[0-6][0-9]+\\([+-][0-9:]+\\)? " ; time
  1001. "[^ ]+ [^ ]+ ")) ; author & state
  1002. (goto-char (match-end 0)) ; [0-6] in regexp handles leap seconds
  1003. (cond
  1004. ;; unlocked revision
  1005. ((looking-at "\\$")
  1006. (setq locking-user 'none)
  1007. (setq status 'rev-and-lock))
  1008. ;; revision is locked by some user
  1009. ((looking-at "\\([^ ]+\\) \\$")
  1010. (setq locking-user (match-string-no-properties 1))
  1011. (setq status 'rev-and-lock))
  1012. ;; everything else: false
  1013. (nil)))
  1014. ;; unexpected information in
  1015. ;; keyword string --> quit
  1016. (nil)))
  1017. ;; search for $Revision
  1018. ;; --------------------
  1019. ((re-search-forward (concat "\\$"
  1020. "Revision: \\([0-9.]+\\) \\$")
  1021. nil t)
  1022. ;; if found, store the revision number ...
  1023. (setq version (match-string-no-properties 1))
  1024. ;; and see if there's any lock information
  1025. (goto-char (point-min))
  1026. (if (re-search-forward (concat "\\$" "Locker:") nil t)
  1027. (cond ((looking-at " \\([^ ]+\\) \\$")
  1028. (setq locking-user (match-string-no-properties 1))
  1029. (setq status 'rev-and-lock))
  1030. ((looking-at " *\\$")
  1031. (setq locking-user 'none)
  1032. (setq status 'rev-and-lock))
  1033. (t
  1034. (setq locking-user 'none)
  1035. (setq status 'rev-and-lock)))
  1036. (setq status 'rev)))
  1037. ;; else: nothing found
  1038. ;; -------------------
  1039. (t nil))))
  1040. (if status (vc-file-setprop file 'vc-working-revision version))
  1041. (and (eq status 'rev-and-lock)
  1042. (vc-file-setprop file 'vc-state
  1043. (cond
  1044. ((eq locking-user 'none) 'up-to-date)
  1045. ((string= locking-user (vc-user-login-name file))
  1046. 'edited)
  1047. (t locking-user)))
  1048. ;; If the file has headers, we don't want to query the
  1049. ;; master file, because that would eliminate all the
  1050. ;; performance gain the headers brought us. We therefore
  1051. ;; use a heuristic now to find out whether locking is used
  1052. ;; for this file. If we trust the file permissions, and the
  1053. ;; file is not locked, then if the file is read-only we
  1054. ;; assume that locking is used for the file, otherwise
  1055. ;; locking is not used.
  1056. (not (vc-mistrust-permissions file))
  1057. (vc-up-to-date-p file)
  1058. (if (string-match ".r-..-..-." (nth 8 (file-attributes file)))
  1059. (vc-file-setprop file 'vc-checkout-model 'locking)
  1060. (vc-file-setprop file 'vc-checkout-model 'implicit)))
  1061. status))))
  1062. (defun vc-release-greater-or-equal (r1 r2)
  1063. "Compare release numbers, represented as strings.
  1064. Release components are assumed cardinal numbers, not decimal fractions
  1065. \(5.10 is a higher release than 5.9\). Omitted fields are considered
  1066. lower \(5.6.7 is earlier than 5.6.7.1\). Comparison runs till the end
  1067. of the string is found, or a non-numeric component shows up \(5.6.7 is
  1068. earlier than \"5.6.7 beta\", which is probably not what you want in
  1069. some cases\). This code is suitable for existing RCS release numbers.
  1070. CVS releases are handled reasonably, too \(1.3 < 1.4* < 1.5\)."
  1071. (let (v1 v2 i1 i2)
  1072. (catch 'done
  1073. (or (and (string-match "^\\.?\\([0-9]+\\)" r1)
  1074. (setq i1 (match-end 0))
  1075. (setq v1 (string-to-number (match-string 1 r1)))
  1076. (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
  1077. (setq i2 (match-end 0))
  1078. (setq v2 (string-to-number (match-string 1 r2)))
  1079. (if (> v1 v2) (throw 'done t)
  1080. (if (< v1 v2) (throw 'done nil)
  1081. (throw 'done
  1082. (vc-release-greater-or-equal
  1083. (substring r1 i1)
  1084. (substring r2 i2)))))))
  1085. (throw 'done t)))
  1086. (or (and (string-match "^\\.?\\([0-9]+\\)" r2)
  1087. (throw 'done nil))
  1088. (throw 'done t)))))
  1089. (defun vc-rcs-release-p (release)
  1090. "Return t if we have RELEASE or better."
  1091. (let ((installation (vc-rcs-system-release)))
  1092. (if (and installation
  1093. (not (eq installation 'unknown)))
  1094. (vc-release-greater-or-equal installation release))))
  1095. (defun vc-rcs-system-release ()
  1096. "Return the RCS release installed on this system, as a string.
  1097. Return symbol `unknown' if the release cannot be deducted. The user can
  1098. override this using variable `vc-rcs-release'.
  1099. If the user has not set variable `vc-rcs-release' and it is nil,
  1100. variable `vc-rcs-release' is set to the returned value."
  1101. (or vc-rcs-release
  1102. (setq vc-rcs-release
  1103. (or (and (zerop (vc-do-command "*vc*" nil "rcs" nil "-V"))
  1104. (with-current-buffer (get-buffer "*vc*")
  1105. (vc-parse-buffer "^RCS version \\([0-9.]+ *.*\\)" 1)))
  1106. 'unknown))))
  1107. (defun vc-rcs-set-non-strict-locking (file)
  1108. (vc-do-command "*vc*" 0 "rcs" file "-U")
  1109. (vc-file-setprop file 'vc-checkout-model 'implicit)
  1110. (set-file-modes file (logior (file-modes file) 128)))
  1111. (defun vc-rcs-set-default-branch (file branch)
  1112. (vc-do-command "*vc*" 0 "rcs" (vc-name file) (concat "-b" branch))
  1113. (vc-file-setprop file 'vc-rcs-default-branch branch))
  1114. (defun vc-rcs-parse (&optional buffer)
  1115. "Parse current buffer, presumed to be in RCS-style masterfile format.
  1116. Optional arg BUFFER specifies another buffer to parse. Return an alist
  1117. of two elements, w/ keys `headers' and `revisions' and values in turn
  1118. sub-alists. For `headers', the values unless otherwise specified are
  1119. strings and the keys are:
  1120. desc -- description
  1121. head -- latest revision
  1122. branch -- the branch the \"head revision\" lies on;
  1123. absent if the head revision lies on the trunk
  1124. access -- ???
  1125. symbols -- sub-alist of (SYMBOL . REVISION) elements
  1126. locks -- if file is checked out, something like \"ttn:1.7\"
  1127. strict -- t if \"strict locking\" is in effect, otherwise nil
  1128. comment -- may be absent; typically something like \"# \" or \"; \"
  1129. expand -- may be absent; ???
  1130. For `revisions', the car is REVISION (string), the cdr a sub-alist,
  1131. with string values (unless otherwise specified) and keys:
  1132. date -- a time value (like that returned by `encode-time'); as a
  1133. special case, a year value less than 100 is augmented by 1900
  1134. author -- username
  1135. state -- typically \"Exp\" or \"Rel\"
  1136. branches -- list of revisions that begin branches from this revision
  1137. next -- on the trunk: the chronologically-preceding revision, or \"\";
  1138. on a branch: the chronologically-following revision, or \"\"
  1139. log -- change log entry
  1140. text -- for the head revision on the trunk, the body of the file;
  1141. other revisions have `:insn' instead
  1142. :insn -- for non-head revisions, a list of parsed instructions
  1143. in one of two forms, in both cases START meaning \"first
  1144. go to line START\":
  1145. - `(START k COUNT)' -- kill COUNT lines
  1146. - `(START i TEXT)' -- insert TEXT (a string)
  1147. The list is in descending order by START.
  1148. The `:insn' key is a keyword to distinguish it as a vc-rcs.el extension."
  1149. (setq buffer (get-buffer (or buffer (current-buffer))))
  1150. (set-buffer buffer)
  1151. ;; An RCS masterfile can be viewed as containing four regular (for the
  1152. ;; most part) sections: (a) the "headers", (b) the "rev headers", (c)
  1153. ;; the "description" and (d) the "rev bodies", in that order. In the
  1154. ;; returned alist (see docstring), elements from (b) and (d) are
  1155. ;; combined pairwise to form the "revisions", while those from (a) and
  1156. ;; (c) are simply combined to form the "headers".
  1157. ;;
  1158. ;; Loosely speaking, each section contains a series of alternating
  1159. ;; "tags" and "printed representations". In the (b) and (d), many
  1160. ;; such series can appear, and a revision number on a line by itself
  1161. ;; precedes the series of tags and printed representations associated
  1162. ;; with it.
  1163. ;;
  1164. ;; In (a) and (b), the printed representations (with the exception of
  1165. ;; the `comment' tag in the headers) terminate with a semicolon, which
  1166. ;; is NOT part of the "value" finally associated with the tag. All
  1167. ;; other printed representations are in "@@-format"; there is an "@",
  1168. ;; the middle part (to be translated into the value), another "@" and
  1169. ;; a newline. Each "@@" in the middle part indicates the position of
  1170. ;; a single "@" (and consequently the requirement of an additional
  1171. ;; initial step when translating to the value).
  1172. ;;
  1173. ;; Parser state includes vars that collect parts of the return value...
  1174. (let ((desc nil) (headers nil) (revs nil)
  1175. ;; ... as well as vars that support a single-pass, tag-assisted,
  1176. ;; minimal-data-copying scan. Basically -- skirting around the
  1177. ;; grouping by revision required in (b) and (d) -- we repeatedly
  1178. ;; and context-sensitively read a tag (that MUST be present),
  1179. ;; determine the bounds of the printed representation, translate
  1180. ;; it into a value, and push the tag plus value onto one of the
  1181. ;; collection vars. Finally, we return the parse tree
  1182. ;; incorporating the values of the collection vars (see "rv").
  1183. ;;
  1184. ;; A symbol or string to keep track of context (for error messages).
  1185. context
  1186. ;; A symbol, the current tag.
  1187. tok
  1188. ;; Region (begin and end buffer positions) of the printed
  1189. ;; representation for the current tag.
  1190. b e
  1191. ;; A list of buffer positions where "@@" can be found within the
  1192. ;; printed representation region. For each location, we push two
  1193. ;; elements onto the list, 1+ and 2+ the location, respectively,
  1194. ;; with the 2+ appearing at the head. In this way, the expression
  1195. ;; `(,e ,@@-holes ,b)
  1196. ;; describes regions that can be concatenated (in reverse order)
  1197. ;; to "de-@@-format" the printed representation as the first step
  1198. ;; to translating it into some value. See internal func `gather'.
  1199. @-holes)
  1200. (flet ((sw () (skip-chars-forward " \t\n")) ; i.e., `[:space:]'
  1201. (at (tag) (save-excursion (eq tag (read buffer))))
  1202. (to-eol () (buffer-substring-no-properties
  1203. (point) (progn (forward-line 1)
  1204. (1- (point)))))
  1205. (to-semi () (setq b (point)
  1206. e (progn (search-forward ";")
  1207. (1- (point)))))
  1208. (to-one@ () (setq @-holes nil
  1209. b (progn (search-forward "@") (point))
  1210. e (progn (while (and (search-forward "@")
  1211. (= ?@ (char-after))
  1212. (progn
  1213. (push (point) @-holes)
  1214. (forward-char 1)
  1215. (push (point) @-holes))))
  1216. (1- (point)))))
  1217. (tok+val (set-b+e name &optional proc)
  1218. (unless (eq name (setq tok (read buffer)))
  1219. (error "Missing `%s' while parsing %s" name context))
  1220. (sw)
  1221. (funcall set-b+e)
  1222. (cons tok (if proc
  1223. (funcall proc)
  1224. (buffer-substring-no-properties b e))))
  1225. (k-semi (name &optional proc) (tok+val 'to-semi name proc))
  1226. (gather () (let ((pairs `(,e ,@@-holes ,b))
  1227. acc)
  1228. (while pairs
  1229. (push (buffer-substring-no-properties
  1230. (cadr pairs) (car pairs))
  1231. acc)
  1232. (setq pairs (cddr pairs)))
  1233. (apply 'concat acc)))
  1234. (k-one@ (name &optional later) (tok+val 'to-one@ name
  1235. (if later
  1236. (lambda () t)
  1237. 'gather))))
  1238. (save-excursion
  1239. (goto-char (point-min))
  1240. ;; headers
  1241. (setq context 'headers)
  1242. (flet ((hpush (name &optional proc)
  1243. (push (k-semi name proc) headers)))
  1244. (hpush 'head)
  1245. (when (at 'branch)
  1246. (hpush 'branch))
  1247. (hpush 'access)
  1248. (hpush 'symbols
  1249. (lambda ()
  1250. (mapcar (lambda (together)
  1251. (let ((two (split-string together ":")))
  1252. (setcar two (intern (car two)))
  1253. (setcdr two (cadr two))
  1254. two))
  1255. (split-string
  1256. (buffer-substring-no-properties b e)))))
  1257. (hpush 'locks))
  1258. (push `(strict . ,(when (at 'strict)
  1259. (search-forward ";")
  1260. t))
  1261. headers)
  1262. (when (at 'comment)
  1263. (push (k-one@ 'comment) headers)
  1264. (search-forward ";"))
  1265. (when (at 'expand)
  1266. (push (k-one@ 'expand) headers)
  1267. (search-forward ";"))
  1268. (setq headers (nreverse headers))
  1269. ;; rev headers
  1270. (sw) (setq context 'rev-headers)
  1271. (while (looking-at "[0-9]")
  1272. (push `(,(to-eol)
  1273. ,(k-semi 'date
  1274. (lambda ()
  1275. (let ((ls (mapcar 'string-to-number
  1276. (split-string
  1277. (buffer-substring-no-properties
  1278. b e)
  1279. "\\."))))
  1280. ;; Hack the year -- verified to be the
  1281. ;; same algorithm used in RCS 5.7.
  1282. (when (< (car ls) 100)
  1283. (setcar ls (+ 1900 (car ls))))
  1284. (apply 'encode-time (nreverse ls)))))
  1285. ,@(mapcar 'k-semi '(author state))
  1286. ,(k-semi 'branches
  1287. (lambda ()
  1288. (split-string
  1289. (buffer-substring-no-properties b e))))
  1290. ,(k-semi 'next))
  1291. revs)
  1292. (sw))
  1293. (setq revs (nreverse revs))
  1294. ;; desc
  1295. (sw) (setq context 'desc
  1296. desc (k-one@ 'desc))
  1297. ;; rev bodies
  1298. (let (acc
  1299. ;; Element of `revs' that initially holds only header info.
  1300. ;; "Pairwise combination" occurs when we add body info.
  1301. rev
  1302. ;; Components of the editing commands (aside from the actual
  1303. ;; text) that comprise the `text' printed representations
  1304. ;; (not including the "head" revision).
  1305. cmd start act
  1306. ;; Ascending (reversed) `@-holes' which the internal func
  1307. ;; `incg' pops to effect incremental gathering.
  1308. asc
  1309. ;; Function to extract text (for the `a' command), either
  1310. ;; `incg' or `buffer-substring-no-properties'. (This is
  1311. ;; for speed; strictly speaking, it is sufficient to use
  1312. ;; only the former since it behaves identically to the
  1313. ;; latter in the absence of "@@".)
  1314. sub)
  1315. (flet ((incg (beg end) (let ((b beg) (e end) @-holes)
  1316. (while (and asc (< (car asc) e))
  1317. (push (pop asc) @-holes))
  1318. ;; Self-deprecate when work is done.
  1319. ;; Folding many dimensions into one.
  1320. ;; Thanks B.Mandelbrot, for complex sum.
  1321. ;; O beauteous math! --the Unvexed Bum
  1322. (unless asc
  1323. (setq sub 'buffer-substring-no-properties))
  1324. (gather))))
  1325. (while (and (sw)
  1326. (not (eobp))
  1327. (setq context (to-eol)
  1328. rev (or (assoc context revs)
  1329. (error "Rev `%s' has body but no head"
  1330. context))))
  1331. (push (k-one@ 'log) (cdr rev))
  1332. ;; For rev body `text' tags, delay translation slightly...
  1333. (push (k-one@ 'text t) (cdr rev))
  1334. ;; ... until we decide which tag and value is appropriate to
  1335. ;; collect. For the "head" revision, compute the value of the
  1336. ;; `text' printed representation by simple `gather'. For all
  1337. ;; other revisions, replace the `text' tag+value with `:insn'
  1338. ;; plus value, always scanning in-place.
  1339. (if (string= context (cdr (assq 'head headers)))
  1340. (setcdr (cadr rev) (gather))
  1341. (if @-holes
  1342. (setq asc (nreverse @-holes)
  1343. sub 'incg)
  1344. (setq sub 'buffer-substring-no-properties))
  1345. (goto-char b)
  1346. (setq acc nil)
  1347. (while (< (point) e)
  1348. (forward-char 1)
  1349. (setq cmd (char-before)
  1350. start (read (current-buffer))
  1351. act (read (current-buffer)))
  1352. (forward-char 1)
  1353. (push (case cmd
  1354. (?d
  1355. ;; `d' means "delete lines".
  1356. ;; For Emacs spirit, we use `k' for "kill".
  1357. `(,start k ,act))
  1358. (?a
  1359. ;; `a' means "append after this line" but
  1360. ;; internally we normalize it so that START
  1361. ;; specifies the actual line for insert, thus
  1362. ;; requiring less hair in the realization algs.
  1363. ;; For Emacs spirit, we use `i' for "insert".
  1364. `(,(1+ start) i
  1365. ,(funcall sub (point) (progn (forward-line act)
  1366. (point)))))
  1367. (t (error "Bad command `%c' in `text' for rev `%s'"
  1368. cmd context)))
  1369. acc))
  1370. (goto-char (1+ e))
  1371. (setcar (cdr rev) (cons :insn acc)))))))
  1372. ;; rv
  1373. `((headers ,desc ,@headers)
  1374. (revisions ,@revs)))))
  1375. (provide 'vc-rcs)
  1376. ;;; vc-rcs.el ends here