pcvs-info.el 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ;;; pcvs-info.el --- internal representation of a fileinfo entry
  2. ;; Copyright (C) 1991-2012 Free Software Foundation, Inc.
  3. ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
  4. ;; Keywords: pcl-cvs
  5. ;; Package: pcvs
  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. ;; The cvs-fileinfo data structure:
  19. ;;
  20. ;; When the `cvs update' is ready we parse the output. Every file
  21. ;; that is affected in some way is added to the cookie collection as
  22. ;; a "fileinfo" (as defined below in cvs-create-fileinfo).
  23. ;;; Code:
  24. (eval-when-compile (require 'cl))
  25. (require 'pcvs-util)
  26. ;;(require 'pcvs-defs)
  27. ;;;;
  28. ;;;; config variables
  29. ;;;;
  30. (define-obsolete-variable-alias 'cvs-display-full-path
  31. 'cvs-display-full-name "22.1")
  32. (defcustom cvs-display-full-name t
  33. "Specifies how the filenames should be displayed in the listing.
  34. If non-nil, their full filename name will be displayed, else only the
  35. non-directory part."
  36. :group 'pcl-cvs
  37. :type '(boolean))
  38. (defcustom cvs-allow-dir-commit nil
  39. "Allow `cvs-mode-commit' on directories.
  40. If you commit without any marked file and with the cursor positioned
  41. on a directory entry, cvs would commit the whole directory. This seems
  42. to confuse some users sometimes."
  43. :group 'pcl-cvs
  44. :type '(boolean))
  45. ;;;;
  46. ;;;; Faces for fontification
  47. ;;;;
  48. (defface cvs-header
  49. '((((class color) (background dark))
  50. (:foreground "lightyellow" :weight bold))
  51. (((class color) (background light))
  52. (:foreground "blue4" :weight bold))
  53. (t (:weight bold)))
  54. "PCL-CVS face used to highlight directory changes."
  55. :group 'pcl-cvs)
  56. (define-obsolete-face-alias 'cvs-header-face 'cvs-header "22.1")
  57. (defface cvs-filename
  58. '((((class color) (background dark))
  59. (:foreground "lightblue"))
  60. (((class color) (background light))
  61. (:foreground "blue4"))
  62. (t ()))
  63. "PCL-CVS face used to highlight file names."
  64. :group 'pcl-cvs)
  65. (define-obsolete-face-alias 'cvs-filename-face 'cvs-filename "22.1")
  66. (defface cvs-unknown
  67. '((((class color) (background dark))
  68. (:foreground "red1"))
  69. (((class color) (background light))
  70. (:foreground "red1"))
  71. (t (:slant italic)))
  72. "PCL-CVS face used to highlight unknown file status."
  73. :group 'pcl-cvs)
  74. (define-obsolete-face-alias 'cvs-unknown-face 'cvs-unknown "22.1")
  75. (defface cvs-handled
  76. '((((class color) (background dark))
  77. (:foreground "pink"))
  78. (((class color) (background light))
  79. (:foreground "pink"))
  80. (t ()))
  81. "PCL-CVS face used to highlight handled file status."
  82. :group 'pcl-cvs)
  83. (define-obsolete-face-alias 'cvs-handled-face 'cvs-handled "22.1")
  84. (defface cvs-need-action
  85. '((((class color) (background dark))
  86. (:foreground "orange"))
  87. (((class color) (background light))
  88. (:foreground "orange"))
  89. (t (:slant italic)))
  90. "PCL-CVS face used to highlight status of files needing action."
  91. :group 'pcl-cvs)
  92. (define-obsolete-face-alias 'cvs-need-action-face 'cvs-need-action "22.1")
  93. (defface cvs-marked
  94. '((((min-colors 88) (class color) (background dark))
  95. (:foreground "green1" :weight bold))
  96. (((class color) (background dark))
  97. (:foreground "green" :weight bold))
  98. (((class color) (background light))
  99. (:foreground "green3" :weight bold))
  100. (t (:weight bold)))
  101. "PCL-CVS face used to highlight marked file indicator."
  102. :group 'pcl-cvs)
  103. (define-obsolete-face-alias 'cvs-marked-face 'cvs-marked "22.1")
  104. (defface cvs-msg
  105. '((t (:slant italic)))
  106. "PCL-CVS face used to highlight CVS messages."
  107. :group 'pcl-cvs)
  108. (define-obsolete-face-alias 'cvs-msg-face 'cvs-msg "22.1")
  109. (defvar cvs-fi-up-to-date-face 'cvs-handled)
  110. (defvar cvs-fi-unknown-face 'cvs-unknown)
  111. (defvar cvs-fi-conflict-face 'font-lock-warning-face)
  112. ;; There is normally no need to alter the following variable, but if
  113. ;; your site has installed CVS in a non-standard way you might have
  114. ;; to change it.
  115. (defvar cvs-bakprefix ".#"
  116. "The prefix that CVS prepends to files when rcsmerge'ing.")
  117. (easy-mmode-defmap cvs-status-map
  118. '(([(mouse-2)] . cvs-mode-toggle-mark))
  119. "Local keymap for text properties of status")
  120. ;; Constructor:
  121. (defstruct (cvs-fileinfo
  122. (:constructor nil)
  123. (:copier nil)
  124. (:constructor -cvs-create-fileinfo (type dir file full-log
  125. &key marked subtype
  126. merge
  127. base-rev
  128. head-rev))
  129. (:conc-name cvs-fileinfo->))
  130. marked ;; t/nil.
  131. type ;; See below
  132. subtype ;; See below
  133. dir ;; Relative directory the file resides in.
  134. ;; (concat dir file) should give a valid path.
  135. file ;; The file name sans the directory.
  136. base-rev ;; During status: This is the revision that the
  137. ;; working file is based on.
  138. head-rev ;; During status: This is the highest revision in
  139. ;; the repository.
  140. merge ;; A cons cell containing the (ancestor . head) revisions
  141. ;; of the merge that resulted in the current file.
  142. ;;removed ;; t if the file no longer exists.
  143. full-log ;; The output from cvs, unparsed.
  144. ;;mod-time ;; Not used.
  145. ;; In addition to the above, the following values can be extracted:
  146. ;; handled ;; t if this file doesn't require further action.
  147. ;; full-name ;; The complete relative filename.
  148. ;; pp-name ;; The printed file name
  149. ;; backup-file;; For MERGED and CONFLICT files after a \"cvs update\",
  150. ;; this is a full path to the backup file where the
  151. ;; untouched version resides.
  152. ;; The meaning of the type field:
  153. ;; Value ---Used by--- Explanation
  154. ;; update status
  155. ;; NEED-UPDATE x file needs update
  156. ;; MODIFIED x x modified by you, unchanged in repository
  157. ;; MERGED x x successful merge
  158. ;; ADDED x x added by you, not yet committed
  159. ;; MISSING x rm'd, but not yet `cvs remove'd
  160. ;; REMOVED x x removed by you, not yet committed
  161. ;; NEED-MERGE x need merge
  162. ;; CONFLICT x conflict when merging
  163. ;; ;;MOD-CONFLICT x removed locally, changed in repository.
  164. ;; DIRCHANGE x x A change of directory.
  165. ;; UNKNOWN x An unknown file.
  166. ;; UP-TO-DATE x The file is up-to-date.
  167. ;; UPDATED x x file copied from repository
  168. ;; PATCHED x x diff applied from repository
  169. ;; COMMITTED x x cvs commit'd
  170. ;; DEAD An entry that should be removed
  171. ;; MESSAGE x x This is a special fileinfo that is used
  172. ;; to display a text that should be in
  173. ;; full-log."
  174. ;; TEMP A temporary message that should be removed
  175. )
  176. (defun cvs-create-fileinfo (type dir file msg &rest keys)
  177. (cvs-check-fileinfo (apply #'-cvs-create-fileinfo type dir file msg keys)))
  178. ;; Fake selectors:
  179. (defun cvs-fileinfo->full-name (fileinfo)
  180. "Return the full path for the file that is described in FILEINFO."
  181. (let ((dir (cvs-fileinfo->dir fileinfo)))
  182. (if (eq (cvs-fileinfo->type fileinfo) 'DIRCHANGE)
  183. (if (string= dir "") "." (directory-file-name dir))
  184. ;; Here, I use `concat' rather than `expand-file-name' because I want
  185. ;; the resulting path to stay relative if `dir' is relative.
  186. (concat dir (cvs-fileinfo->file fileinfo)))))
  187. (define-obsolete-function-alias 'cvs-fileinfo->full-path
  188. 'cvs-fileinfo->full-name "22.1")
  189. (defun cvs-fileinfo->pp-name (fi)
  190. "Return the filename of FI as it should be displayed."
  191. (if cvs-display-full-name
  192. (cvs-fileinfo->full-name fi)
  193. (cvs-fileinfo->file fi)))
  194. (defun cvs-fileinfo->backup-file (fileinfo)
  195. "Construct the file name of the backup file for FILEINFO."
  196. (let* ((dir (cvs-fileinfo->dir fileinfo))
  197. (file (cvs-fileinfo->file fileinfo))
  198. (default-directory (file-name-as-directory (expand-file-name dir)))
  199. (files (directory-files "." nil
  200. (concat "\\`" (regexp-quote cvs-bakprefix)
  201. (regexp-quote file) "\\(\\.[0-9]+\\.[0-9]+\\)+\\'")))
  202. bf)
  203. (dolist (f files)
  204. (when (and (file-readable-p f)
  205. (or (null bf) (file-newer-than-file-p f bf)))
  206. (setq bf f)))
  207. (concat dir bf)))
  208. ;; (defun cvs-fileinfo->handled (fileinfo)
  209. ;; "Tell if this requires further action"
  210. ;; (memq (cvs-fileinfo->type fileinfo) '(UP-TO-DATE DEAD)))
  211. ;; Predicate:
  212. (defun cvs-check-fileinfo (fi)
  213. "Check FI's conformance to some conventions."
  214. (let ((check 'none)
  215. (type (cvs-fileinfo->type fi))
  216. (subtype (cvs-fileinfo->subtype fi))
  217. (marked (cvs-fileinfo->marked fi))
  218. (dir (cvs-fileinfo->dir fi))
  219. (file (cvs-fileinfo->file fi))
  220. (base-rev (cvs-fileinfo->base-rev fi))
  221. (head-rev (cvs-fileinfo->head-rev fi))
  222. (full-log (cvs-fileinfo->full-log fi)))
  223. (if (and (setq check 'marked) (memq marked '(t nil))
  224. (setq check 'base-rev) (or (null base-rev) (stringp base-rev))
  225. (setq check 'head-rev) (or (null head-rev) (stringp head-rev))
  226. (setq check 'full-log) (stringp full-log)
  227. (setq check 'dir)
  228. (and (stringp dir)
  229. (not (file-name-absolute-p dir))
  230. (or (string= dir "")
  231. (string= dir (file-name-as-directory dir))))
  232. (setq check 'file)
  233. (and (stringp file)
  234. (string= file (file-name-nondirectory file)))
  235. (setq check 'type) (symbolp type)
  236. (setq check 'consistency)
  237. (case type
  238. (DIRCHANGE (and (null subtype) (string= "." file)))
  239. ((NEED-UPDATE ADDED MISSING DEAD MODIFIED MESSAGE UP-TO-DATE
  240. REMOVED NEED-MERGE CONFLICT UNKNOWN MESSAGE)
  241. t)))
  242. fi
  243. (error "Invalid :%s in cvs-fileinfo %s" check fi))))
  244. ;;;;
  245. ;;;; State table to indicate what you can do when.
  246. ;;;;
  247. (defconst cvs-states
  248. `((NEED-UPDATE update diff ignore)
  249. (UP-TO-DATE update nil remove diff safe-rm revert)
  250. (MODIFIED update commit undo remove diff merge diff-base)
  251. (ADDED update commit remove)
  252. (MISSING remove undo update safe-rm revert)
  253. (REMOVED commit add undo safe-rm)
  254. (NEED-MERGE update undo diff diff-base)
  255. (CONFLICT merge remove undo commit diff diff-base)
  256. (DIRCHANGE remove update diff ,(if cvs-allow-dir-commit 'commit) tag)
  257. (UNKNOWN ignore add remove)
  258. (DEAD )
  259. (MESSAGE))
  260. "Fileinfo state descriptions for pcl-cvs.
  261. This is an assoc list. Each element consists of (STATE . FUNS)
  262. - STATE (described in `cvs-create-fileinfo') is the key
  263. - FUNS is the list of applicable operations.
  264. The first one (if any) should be the \"default\" action.
  265. Most of the actions have the obvious meaning.
  266. `safe-rm' indicates that the file can be removed without losing
  267. any information.")
  268. ;;;;
  269. ;;;; Utility functions
  270. ;;;;
  271. (defun cvs-applicable-p (fi-or-type func)
  272. "Check if FUNC is applicable to FI-OR-TYPE.
  273. If FUNC is nil, always return t.
  274. FI-OR-TYPE can either be a symbol (a fileinfo-type) or a fileinfo."
  275. (let ((type (if (symbolp fi-or-type) fi-or-type
  276. (cvs-fileinfo->type fi-or-type))))
  277. (and (not (eq type 'MESSAGE))
  278. (eq (car (memq func (cdr (assq type cvs-states)))) func))))
  279. (defun cvs-add-face (str face &optional keymap &rest props)
  280. (when keymap
  281. (when (keymapp keymap)
  282. (setq props (list* 'keymap keymap props)))
  283. (setq props (list* 'mouse-face 'highlight props)))
  284. (add-text-properties 0 (length str) (list* 'font-lock-face face props) str)
  285. str)
  286. (defun cvs-fileinfo-pp (fileinfo)
  287. "Pretty print FILEINFO. Insert a printed representation in current buffer.
  288. For use by the cookie package."
  289. (cvs-check-fileinfo fileinfo)
  290. (let ((type (cvs-fileinfo->type fileinfo))
  291. (subtype (cvs-fileinfo->subtype fileinfo)))
  292. (insert
  293. (case type
  294. (DIRCHANGE (concat "In directory "
  295. (cvs-add-face (cvs-fileinfo->full-name fileinfo)
  296. 'cvs-header t 'cvs-goal-column t)
  297. ":"))
  298. (MESSAGE
  299. (cvs-add-face (format "Message: %s" (cvs-fileinfo->full-log fileinfo))
  300. 'cvs-msg))
  301. (t
  302. (let* ((status (if (cvs-fileinfo->marked fileinfo)
  303. (cvs-add-face "*" 'cvs-marked)
  304. " "))
  305. (file (cvs-add-face (cvs-fileinfo->pp-name fileinfo)
  306. 'cvs-filename t 'cvs-goal-column t))
  307. (base (or (cvs-fileinfo->base-rev fileinfo) ""))
  308. (head (cvs-fileinfo->head-rev fileinfo))
  309. (type
  310. (let ((str (case type
  311. ;;(MOD-CONFLICT "Not Removed")
  312. (DEAD "")
  313. (t (capitalize (symbol-name type)))))
  314. (face (let ((sym (intern
  315. (concat "cvs-fi-"
  316. (downcase (symbol-name type))
  317. "-face"))))
  318. (or (and (boundp sym) (symbol-value sym))
  319. 'cvs-need-action))))
  320. (cvs-add-face str face cvs-status-map)))
  321. (side (or
  322. ;; maybe a subtype
  323. (when subtype (downcase (symbol-name subtype)))
  324. ;; or the head-rev
  325. (when (and head (not (string= head base))) head)
  326. ;; or nothing
  327. "")))
  328. (format "%-11s %s %-11s %-11s %s"
  329. side status type base file))))
  330. "\n")))
  331. (defun cvs-fileinfo-update (fi fi-new)
  332. "Update FI with the information provided in FI-NEW."
  333. (let ((type (cvs-fileinfo->type fi-new))
  334. (merge (cvs-fileinfo->merge fi-new)))
  335. (setf (cvs-fileinfo->type fi) type)
  336. (setf (cvs-fileinfo->subtype fi) (cvs-fileinfo->subtype fi-new))
  337. (setf (cvs-fileinfo->full-log fi) (cvs-fileinfo->full-log fi-new))
  338. (setf (cvs-fileinfo->base-rev fi) (cvs-fileinfo->base-rev fi-new))
  339. (setf (cvs-fileinfo->head-rev fi) (cvs-fileinfo->head-rev fi-new))
  340. (cond
  341. (merge (setf (cvs-fileinfo->merge fi) merge))
  342. ((memq type '(UP-TO-DATE NEED-UPDATE))
  343. (setf (cvs-fileinfo->merge fi) nil)))))
  344. (defun cvs-fileinfo< (a b)
  345. "Compare fileinfo A with fileinfo B and return t if A is `less'.
  346. The ordering defined by this function is such that directories are
  347. sorted alphabetically, and inside every directory the DIRCHANGE
  348. fileinfo will appear first, followed by all files (alphabetically)."
  349. (let ((subtypea (cvs-fileinfo->subtype a))
  350. (subtypeb (cvs-fileinfo->subtype b)))
  351. (cond
  352. ;; Sort according to directories.
  353. ((string< (cvs-fileinfo->dir a) (cvs-fileinfo->dir b)) t)
  354. ((not (string= (cvs-fileinfo->dir a) (cvs-fileinfo->dir b))) nil)
  355. ;; The DIRCHANGE entry is always first within the directory.
  356. ((eq (cvs-fileinfo->type b) 'DIRCHANGE) nil)
  357. ((eq (cvs-fileinfo->type a) 'DIRCHANGE) t)
  358. ;; All files are sorted by file name.
  359. ((string< (cvs-fileinfo->file a) (cvs-fileinfo->file b))))))
  360. ;;;
  361. ;;; Look at CVS/Entries to quickly find a first approximation of the status
  362. ;;;
  363. (defun cvs-fileinfo-from-entries (dir &optional all)
  364. "List of fileinfos for DIR, extracted from CVS/Entries.
  365. Unless ALL is optional, returns only the files that are not up-to-date.
  366. DIR can also be a file."
  367. (let* ((singlefile
  368. (cond
  369. ((equal dir "") nil)
  370. ((file-directory-p dir) (setq dir (file-name-as-directory dir)) nil)
  371. (t (prog1 (file-name-nondirectory dir)
  372. (setq dir (or (file-name-directory dir) ""))))))
  373. (file (expand-file-name "CVS/Entries" dir))
  374. (fis nil))
  375. (if (not (file-readable-p file))
  376. (push (cvs-create-fileinfo (if singlefile 'UNKNOWN 'DIRCHANGE)
  377. dir (or singlefile ".") "") fis)
  378. (with-temp-buffer
  379. (insert-file-contents file)
  380. (goto-char (point-min))
  381. ;; Select the single file entry in case we're only interested in a file.
  382. (cond
  383. ((not singlefile)
  384. (push (cvs-create-fileinfo 'DIRCHANGE dir "." "") fis))
  385. ((re-search-forward
  386. (concat "^[^/]*/" (regexp-quote singlefile) "/.*") nil t)
  387. (setq all t)
  388. (goto-char (match-beginning 0))
  389. (narrow-to-region (point) (match-end 0)))
  390. (t
  391. (push (cvs-create-fileinfo 'UNKNOWN dir singlefile "") fis)
  392. (narrow-to-region (point-min) (point-min))))
  393. (while (looking-at "\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/\\([^/]*\\)/")
  394. (if (/= (match-beginning 1) (match-end 1))
  395. (setq fis (append (cvs-fileinfo-from-entries
  396. (concat dir (file-name-as-directory
  397. (match-string 2)))
  398. all)
  399. fis))
  400. (let ((f (match-string 2))
  401. (rev (match-string 3))
  402. (date (match-string 4))
  403. timestamp
  404. (type 'MODIFIED)
  405. (subtype nil))
  406. (cond
  407. ((equal (substring rev 0 1) "-")
  408. (setq type 'REMOVED rev (substring rev 1)))
  409. ((not (file-exists-p (concat dir f))) (setq type 'MISSING))
  410. ((equal rev "0") (setq type 'ADDED rev nil))
  411. ((equal date "Result of merge") (setq subtype 'MERGED))
  412. ((let ((mtime (nth 5 (file-attributes (concat dir f))))
  413. (system-time-locale "C"))
  414. (setq timestamp (format-time-string "%c" mtime 'utc))
  415. ;; Solaris sometimes uses "Wed Sep 05", not "Wed Sep 5".
  416. ;; See "grep '[^a-z_]ctime' cvs/src/*.c" for reference.
  417. (if (= (aref timestamp 8) ?0)
  418. (setq timestamp (concat (substring timestamp 0 8)
  419. " " (substring timestamp 9))))
  420. (equal timestamp date))
  421. (setq type (if all 'UP-TO-DATE)))
  422. ((equal date (concat "Result of merge+" timestamp))
  423. (setq type 'CONFLICT)))
  424. (when type
  425. (push (cvs-create-fileinfo type dir f ""
  426. :base-rev rev :subtype subtype)
  427. fis))))
  428. (forward-line 1))))
  429. fis))
  430. (provide 'pcvs-info)
  431. ;;; pcvs-info.el ends here