vc-bzr.el 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  1. ;;; vc-bzr.el --- VC backend for the bzr revision control system
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Dave Love <fx@gnu.org>
  4. ;; Riccardo Murri <riccardo.murri@gmail.com>
  5. ;; Maintainer: FSF
  6. ;; Keywords: vc tools
  7. ;; Created: Sept 2006
  8. ;; Package: vc
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; See <URL:http://bazaar.canonical.com/> concerning bzr.
  22. ;; This library provides bzr support in VC.
  23. ;; Known bugs
  24. ;; ==========
  25. ;; When editing a symlink and *both* the symlink and its target
  26. ;; are bzr-versioned, `vc-bzr` presently runs `bzr status` on the
  27. ;; symlink, thereby not detecting whether the actual contents
  28. ;; (that is, the target contents) are changed.
  29. ;; See https://bugs.launchpad.net/vc-bzr/+bug/116607
  30. ;;; Properties of the backend
  31. (defun vc-bzr-revision-granularity () 'repository)
  32. (defun vc-bzr-checkout-model (files) 'implicit)
  33. ;;; Code:
  34. (eval-when-compile
  35. (require 'cl)
  36. (require 'vc) ;; for vc-exec-after
  37. (require 'vc-dir))
  38. ;; Clear up the cache to force vc-call to check again and discover
  39. ;; new functions when we reload this file.
  40. (put 'Bzr 'vc-functions nil)
  41. (defgroup vc-bzr nil
  42. "VC Bazaar (bzr) backend."
  43. :version "22.2"
  44. :group 'vc)
  45. (defcustom vc-bzr-program "bzr"
  46. "Name of the bzr command (excluding any arguments)."
  47. :group 'vc-bzr
  48. :type 'string)
  49. (defcustom vc-bzr-sha1-program '("sha1sum")
  50. "Name of program to compute SHA1.
  51. It must be a string \(program name\) or list of strings \(name and its args\)."
  52. :type '(repeat string)
  53. :group 'vc-bzr)
  54. (define-obsolete-variable-alias 'sha1-program 'vc-bzr-sha1-program "24.1")
  55. (defcustom vc-bzr-diff-switches nil
  56. "String or list of strings specifying switches for bzr diff under VC.
  57. If nil, use the value of `vc-diff-switches'. If t, use no switches."
  58. :type '(choice (const :tag "Unspecified" nil)
  59. (const :tag "None" t)
  60. (string :tag "Argument String")
  61. (repeat :tag "Argument List" :value ("") string))
  62. :group 'vc-bzr)
  63. (defcustom vc-bzr-log-switches nil
  64. "String or list of strings specifying switches for bzr log under VC."
  65. :type '(choice (const :tag "None" nil)
  66. (string :tag "Argument String")
  67. (repeat :tag "Argument List" :value ("") string))
  68. :group 'vc-bzr)
  69. (defcustom vc-bzr-status-switches
  70. (ignore-errors
  71. (with-temp-buffer
  72. (call-process vc-bzr-program nil t nil "help" "status")
  73. (if (search-backward "--no-classify" nil t)
  74. "--no-classify")))
  75. "String or list of strings specifying switches for bzr status under VC.
  76. The option \"--no-classify\" should be present if your bzr supports it."
  77. :type '(choice (const :tag "None" nil)
  78. (string :tag "Argument String")
  79. (repeat :tag "Argument List" :value ("") string))
  80. :group 'vc-bzr
  81. :version "24.1")
  82. ;; since v0.9, bzr supports removing the progress indicators
  83. ;; by setting environment variable BZR_PROGRESS_BAR to "none".
  84. (defun vc-bzr-command (bzr-command buffer okstatus file-or-list &rest args)
  85. "Wrapper round `vc-do-command' using `vc-bzr-program' as COMMAND.
  86. Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
  87. `LC_MESSAGES=C' to the environment. If BZR-COMMAND is \"status\",
  88. prepends `vc-bzr-status-switches' to ARGS."
  89. (let ((process-environment
  90. (list* "BZR_PROGRESS_BAR=none" ; Suppress progress output (bzr >=0.9)
  91. "LC_MESSAGES=C" ; Force English output
  92. process-environment)))
  93. (apply 'vc-do-command (or buffer "*vc*") okstatus vc-bzr-program
  94. file-or-list bzr-command
  95. (if (and (string-equal "status" bzr-command)
  96. vc-bzr-status-switches)
  97. (append (if (stringp vc-bzr-status-switches)
  98. (list vc-bzr-status-switches)
  99. vc-bzr-status-switches)
  100. args)
  101. args))))
  102. (defun vc-bzr-async-command (bzr-command &rest args)
  103. "Wrapper round `vc-do-async-command' using `vc-bzr-program' as COMMAND.
  104. Invoke the bzr command adding `BZR_PROGRESS_BAR=none' and
  105. `LC_MESSAGES=C' to the environment.
  106. Use the current Bzr root directory as the ROOT argument to
  107. `vc-do-async-command', and specify an output buffer named
  108. \"*vc-bzr : ROOT*\". Return this buffer."
  109. (let* ((process-environment
  110. (list* "BZR_PROGRESS_BAR=none" "LC_MESSAGES=C"
  111. process-environment))
  112. (root (vc-bzr-root default-directory))
  113. (buffer (format "*vc-bzr : %s*" (expand-file-name root))))
  114. (apply 'vc-do-async-command buffer root
  115. vc-bzr-program bzr-command args)
  116. buffer))
  117. ;;;###autoload
  118. (defconst vc-bzr-admin-dirname ".bzr"
  119. "Name of the directory containing Bzr repository status files.")
  120. ;; Used in the autoloaded vc-bzr-registered; see below.
  121. ;;;###autoload
  122. (defconst vc-bzr-admin-checkout-format-file
  123. (concat vc-bzr-admin-dirname "/checkout/format")
  124. "Name of the format file in a .bzr directory.")
  125. (defconst vc-bzr-admin-dirstate
  126. (concat vc-bzr-admin-dirname "/checkout/dirstate"))
  127. (defconst vc-bzr-admin-branch-format-file
  128. (concat vc-bzr-admin-dirname "/branch/format"))
  129. (defconst vc-bzr-admin-revhistory
  130. (concat vc-bzr-admin-dirname "/branch/revision-history"))
  131. (defconst vc-bzr-admin-lastrev
  132. (concat vc-bzr-admin-dirname "/branch/last-revision"))
  133. (defconst vc-bzr-admin-branchconf
  134. (concat vc-bzr-admin-dirname "/branch/branch.conf"))
  135. ;;;###autoload (defun vc-bzr-registered (file)
  136. ;;;###autoload (if (vc-find-root file vc-bzr-admin-checkout-format-file)
  137. ;;;###autoload (progn
  138. ;;;###autoload (load "vc-bzr")
  139. ;;;###autoload (vc-bzr-registered file))))
  140. (defun vc-bzr-root (file)
  141. "Return the root directory of the bzr repository containing FILE."
  142. ;; Cache technique copied from vc-arch.el.
  143. (or (vc-file-getprop file 'bzr-root)
  144. (let ((root (vc-find-root file vc-bzr-admin-checkout-format-file)))
  145. (when root (vc-file-setprop file 'bzr-root root)))))
  146. (defun vc-bzr-branch-conf (file)
  147. "Return the Bazaar branch settings for file FILE, as an alist.
  148. Each element of the returned alist has the form (NAME . VALUE),
  149. which are the name and value of a Bazaar setting, as strings.
  150. The settings are read from the file \".bzr/branch/branch.conf\"
  151. in the repository root directory of FILE."
  152. (let (settings)
  153. (with-temp-buffer
  154. (insert-file-contents
  155. (expand-file-name vc-bzr-admin-branchconf (vc-bzr-root file)))
  156. (while (re-search-forward "^\\([^#=][^=]*?\\) *= *\\(.*\\)$" nil t)
  157. (push (cons (match-string 1) (match-string 2)) settings)))
  158. settings))
  159. (defun vc-bzr-sha1 (file)
  160. (with-temp-buffer
  161. (set-buffer-multibyte nil)
  162. (let ((prog vc-bzr-sha1-program)
  163. (args nil)
  164. process-file-side-effects)
  165. (when (consp prog)
  166. (setq args (cdr prog))
  167. (setq prog (car prog)))
  168. (apply 'process-file prog (file-relative-name file) t nil args)
  169. (buffer-substring (point-min) (+ (point-min) 40)))))
  170. (defun vc-bzr-state-heuristic (file)
  171. "Like `vc-bzr-state' but hopefully without running Bzr."
  172. ;; `bzr status' was excruciatingly slow with large histories and
  173. ;; pending merges, so try to avoid using it until they fix their
  174. ;; performance problems.
  175. ;; This function tries first to parse Bzr internal file
  176. ;; `checkout/dirstate', but it may fail if Bzr internal file format
  177. ;; has changed. As a safeguard, the `checkout/dirstate' file is
  178. ;; only parsed if it contains the string `#bazaar dirstate flat
  179. ;; format 3' in the first line.
  180. ;; If the `checkout/dirstate' file cannot be parsed, fall back to
  181. ;; running `vc-bzr-state'."
  182. ;;
  183. ;; The format of the dirstate file is explained in bzrlib/dirstate.py
  184. ;; in the bzr distribution. Basically:
  185. ;; header-line giving the version of the file format in use.
  186. ;; a few lines of stuff
  187. ;; entries, one per line, with null-separated fields. Each line:
  188. ;; entry_key = dirname (may be empty), basename, file-id
  189. ;; current = common ( = kind, fingerprint, size, executable )
  190. ;; + working ( = packed_stat )
  191. ;; parent = common ( as above ) + history ( = rev_id )
  192. ;; kinds = (r)elocated, (a)bsent, (d)irectory, (f)ile, (l)ink
  193. (lexical-let ((root (vc-bzr-root file)))
  194. (when root ; Short cut.
  195. (lexical-let ((dirstate (expand-file-name vc-bzr-admin-dirstate root)))
  196. (condition-case nil
  197. (with-temp-buffer
  198. (insert-file-contents dirstate)
  199. (goto-char (point-min))
  200. (if (not (looking-at "#bazaar dirstate flat format 3"))
  201. (vc-bzr-state file) ; Some other unknown format?
  202. (let* ((relfile (file-relative-name file root))
  203. (reldir (file-name-directory relfile)))
  204. (if (re-search-forward
  205. (concat "^\0"
  206. (if reldir (regexp-quote
  207. (directory-file-name reldir)))
  208. "\0"
  209. (regexp-quote (file-name-nondirectory relfile))
  210. "\0"
  211. "[^\0]*\0" ;id?
  212. "\\([^\0]*\\)\0" ;"a/f/d", a=removed?
  213. "\\([^\0]*\\)\0" ;sha1 (empty if conflicted)?
  214. "\\([^\0]*\\)\0" ;size?p
  215. ;; y/n. Whether or not the current copy
  216. ;; was executable the last time bzr checked?
  217. "[^\0]*\0"
  218. "[^\0]*\0" ;?
  219. ;; Parent information. Absent in a new repo.
  220. "\\(?:\\([^\0]*\\)\0" ;"a/f/d" a=added?
  221. "\\([^\0]*\\)\0" ;sha1 again?
  222. "\\([^\0]*\\)\0" ;size again?
  223. ;; y/n. Whether or not the repo thinks
  224. ;; the file should be executable?
  225. "\\([^\0]*\\)\0"
  226. "[^\0]*\0\\)?" ;last revid?
  227. ;; There are more fields when merges are pending.
  228. )
  229. nil t)
  230. ;; Apparently the second sha1 is the one we want: when
  231. ;; there's a conflict, the first sha1 is absent (and the
  232. ;; first size seems to correspond to the file with
  233. ;; conflict markers).
  234. (cond
  235. ((eq (char-after (match-beginning 1)) ?a) 'removed)
  236. ;; If there is no parent, this must be a new repo.
  237. ;; If file is in dirstate, can only be added (b#8025).
  238. ((or (not (match-beginning 4))
  239. (eq (char-after (match-beginning 4)) ?a)) 'added)
  240. ((or (and (eq (string-to-number (match-string 3))
  241. (nth 7 (file-attributes file)))
  242. (equal (match-string 5)
  243. (vc-bzr-sha1 file))
  244. ;; For a file, does the executable state match?
  245. ;; (Bug#7544)
  246. (or (not
  247. (eq (char-after (match-beginning 1)) ?f))
  248. (let ((exe
  249. (memq
  250. ?x
  251. (mapcar
  252. 'identity
  253. (nth 8 (file-attributes file))))))
  254. (if (eq (char-after (match-beginning 7))
  255. ?y)
  256. exe
  257. (not exe)))))
  258. (and
  259. ;; It looks like for lightweight
  260. ;; checkouts \2 is empty and we need to
  261. ;; look for size in \6.
  262. (eq (match-beginning 2) (match-end 2))
  263. (eq (string-to-number (match-string 6))
  264. (nth 7 (file-attributes file)))
  265. (equal (match-string 5)
  266. (vc-bzr-sha1 file))))
  267. 'up-to-date)
  268. (t 'edited))
  269. 'unregistered))))
  270. ;; Either the dirstate file can't be read, or the sha1
  271. ;; executable is missing, or ...
  272. ;; In either case, recent versions of Bzr aren't that slow
  273. ;; any more.
  274. (error (vc-bzr-state file)))))))
  275. (defun vc-bzr-registered (file)
  276. "Return non-nil if FILE is registered with bzr."
  277. (let ((state (vc-bzr-state-heuristic file)))
  278. (not (memq state '(nil unregistered ignored)))))
  279. (defconst vc-bzr-state-words
  280. "added\\|ignored\\|kind changed\\|modified\\|removed\\|renamed\\|unknown"
  281. "Regexp matching file status words as reported in `bzr' output.")
  282. ;; History of Bzr commands.
  283. (defvar vc-bzr-history nil)
  284. (defun vc-bzr-file-name-relative (filename)
  285. "Return file name FILENAME stripped of the initial Bzr repository path."
  286. (lexical-let*
  287. ((filename* (expand-file-name filename))
  288. (rootdir (vc-bzr-root filename*)))
  289. (when rootdir
  290. (file-relative-name filename* rootdir))))
  291. (defvar vc-bzr-error-regex-alist
  292. '(("^\\( M[* ]\\|+N \\|-D \\|\\| \\*\\|R[M ] \\) \\(.+\\)" 2 nil nil 1)
  293. ("^C \\(.+\\)" 2)
  294. ("^Text conflict in \\(.+\\)" 1 nil nil 2)
  295. ("^Using saved parent location: \\(.+\\)" 1 nil nil 0))
  296. "Value of `compilation-error-regexp-alist' in *vc-bzr* buffers.")
  297. (defun vc-bzr-pull (prompt)
  298. "Pull changes into the current Bzr branch.
  299. Normally, this runs \"bzr pull\". However, if the branch is a
  300. bound branch, run \"bzr update\" instead. If there is no default
  301. location from which to pull or update, or if PROMPT is non-nil,
  302. prompt for the Bzr command to run."
  303. (let* ((vc-bzr-program vc-bzr-program)
  304. (branch-conf (vc-bzr-branch-conf default-directory))
  305. ;; Check whether the branch is bound.
  306. (bound (assoc "bound" branch-conf))
  307. (bound (and bound (equal "true" (downcase (cdr bound)))))
  308. ;; If we need to do a "bzr pull", check for a parent. If it
  309. ;; does not exist, bzr will need a pull location.
  310. (has-parent (unless bound
  311. (assoc "parent_location" branch-conf)))
  312. (command (if bound "update" "pull"))
  313. args)
  314. ;; If necessary, prompt for the exact command.
  315. (when (or prompt (not (or bound has-parent)))
  316. (setq args (split-string
  317. (read-shell-command
  318. "Bzr pull command: "
  319. (concat vc-bzr-program " " command)
  320. 'vc-bzr-history)
  321. " " t))
  322. (setq vc-bzr-program (car args)
  323. command (cadr args)
  324. args (cddr args)))
  325. (let ((buf (apply 'vc-bzr-async-command command args)))
  326. (with-current-buffer buf
  327. (vc-exec-after
  328. `(progn
  329. (let ((compilation-error-regexp-alist
  330. vc-bzr-error-regex-alist))
  331. (compilation-mode))
  332. (set (make-local-variable 'compilation-error-regexp-alist)
  333. vc-bzr-error-regex-alist))))
  334. (vc-set-async-update buf))))
  335. (defun vc-bzr-merge-branch ()
  336. "Merge another Bzr branch into the current one.
  337. Prompt for the Bzr command to run, providing a pre-defined merge
  338. source (an upstream branch or a previous merge source) as a
  339. default if it is available."
  340. (let* ((branch-conf (vc-bzr-branch-conf default-directory))
  341. ;; "bzr merge" without an argument defaults to submit_branch,
  342. ;; then parent_location. Extract the specific location and
  343. ;; add it explicitly to the command line.
  344. (setting nil)
  345. (location
  346. (cond
  347. ((setq setting (assoc "submit_branch" branch-conf))
  348. (cdr setting))
  349. ((setq setting (assoc "parent_location" branch-conf))
  350. (cdr setting))))
  351. (cmd
  352. (split-string
  353. (read-shell-command
  354. "Bzr merge command: "
  355. (concat vc-bzr-program " merge --pull"
  356. (if location (concat " " location) ""))
  357. 'vc-bzr-history)
  358. " " t))
  359. (vc-bzr-program (car cmd))
  360. (command (cadr cmd))
  361. (args (cddr cmd)))
  362. (let ((buf (apply 'vc-bzr-async-command command args)))
  363. (with-current-buffer buf
  364. (vc-exec-after
  365. `(progn
  366. (let ((compilation-error-regexp-alist
  367. vc-bzr-error-regex-alist))
  368. (compilation-mode))
  369. (set (make-local-variable 'compilation-error-regexp-alist)
  370. vc-bzr-error-regex-alist))))
  371. (vc-set-async-update buf))))
  372. (defun vc-bzr-status (file)
  373. "Return FILE status according to Bzr.
  374. Return value is a cons (STATUS . WARNING), where WARNING is a
  375. string or nil, and STATUS is one of the symbols: `added',
  376. `ignored', `kindchanged', `modified', `removed', `renamed', `unknown',
  377. which directly correspond to `bzr status' output, or 'unchanged
  378. for files whose copy in the working tree is identical to the one
  379. in the branch repository, or nil for files that are not
  380. registered with Bzr.
  381. If any error occurred in running `bzr status', then return nil."
  382. (with-temp-buffer
  383. (let ((ret (condition-case nil
  384. (vc-bzr-command "status" t 0 file)
  385. (file-error nil))) ; vc-bzr-program not found.
  386. (status 'unchanged))
  387. ;; the only secure status indication in `bzr status' output
  388. ;; is a couple of lines following the pattern::
  389. ;; | <status>:
  390. ;; | <file name>
  391. ;; if the file is up-to-date, we get no status report from `bzr',
  392. ;; so if the regexp search for the above pattern fails, we consider
  393. ;; the file to be up-to-date.
  394. (goto-char (point-min))
  395. (when (re-search-forward
  396. ;; bzr prints paths relative to the repository root.
  397. (concat "^\\(" vc-bzr-state-words "\\):[ \t\n]+"
  398. (regexp-quote (vc-bzr-file-name-relative file))
  399. ;; Bzr appends a '/' to directory names and
  400. ;; '*' to executable files
  401. (if (file-directory-p file) "/?" "\\*?")
  402. "[ \t\n]*$")
  403. nil t)
  404. (lexical-let ((statusword (match-string 1)))
  405. ;; Erase the status text that matched.
  406. (delete-region (match-beginning 0) (match-end 0))
  407. (setq status
  408. (intern (replace-regexp-in-string " " "" statusword)))))
  409. (when status
  410. (goto-char (point-min))
  411. (skip-chars-forward " \n\t") ;Throw away spaces.
  412. (cons status
  413. ;; "bzr" will output warnings and informational messages to
  414. ;; stderr; due to Emacs's `vc-do-command' (and, it seems,
  415. ;; `start-process' itself) limitations, we cannot catch stderr
  416. ;; and stdout into different buffers. So, if there's anything
  417. ;; left in the buffer after removing the above status
  418. ;; keywords, let us just presume that any other message from
  419. ;; "bzr" is a user warning, and display it.
  420. (unless (eobp) (buffer-substring (point) (point-max))))))))
  421. (defun vc-bzr-state (file)
  422. (lexical-let ((result (vc-bzr-status file)))
  423. (when (consp result)
  424. (let ((warnings (cdr result)))
  425. (when warnings
  426. ;; bzr 2.3.0 returns info about shelves, which is not really a warning
  427. (when (string-match "[0-9]+ shel\\(f\\|ves\\) exists?\\..*?\n" warnings)
  428. (setq warnings (replace-match "" nil nil warnings)))
  429. (unless (string= warnings "")
  430. (message "Warnings in `bzr' output: %s" warnings))))
  431. (cdr (assq (car result)
  432. '((added . added)
  433. (kindchanged . edited)
  434. (renamed . edited)
  435. (modified . edited)
  436. (removed . removed)
  437. (ignored . ignored)
  438. (unknown . unregistered)
  439. (unchanged . up-to-date)))))))
  440. (defun vc-bzr-resolve-when-done ()
  441. "Call \"bzr resolve\" if the conflict markers have been removed."
  442. (save-excursion
  443. (goto-char (point-min))
  444. (unless (re-search-forward "^<<<<<<< " nil t)
  445. (vc-bzr-command "resolve" nil 0 buffer-file-name)
  446. ;; Remove the hook so that it is not called multiple times.
  447. (remove-hook 'after-save-hook 'vc-bzr-resolve-when-done t))))
  448. (defun vc-bzr-find-file-hook ()
  449. (when (and buffer-file-name
  450. ;; FIXME: We should check that "bzr status" says "conflict".
  451. (file-exists-p (concat buffer-file-name ".BASE"))
  452. (file-exists-p (concat buffer-file-name ".OTHER"))
  453. (file-exists-p (concat buffer-file-name ".THIS"))
  454. ;; If "bzr status" says there's a conflict but there are no
  455. ;; conflict markers, it's not clear what we should do.
  456. (save-excursion
  457. (goto-char (point-min))
  458. (re-search-forward "^<<<<<<< " nil t)))
  459. ;; TODO: the merge algorithm used in `bzr merge' is nicely configurable,
  460. ;; but the one in `bzr pull' isn't, so it would be good to provide an
  461. ;; elisp function to remerge from the .BASE/OTHER/THIS files.
  462. (smerge-start-session)
  463. (add-hook 'after-save-hook 'vc-bzr-resolve-when-done nil t)
  464. (message "There are unresolved conflicts in this file")))
  465. (defun vc-bzr-workfile-unchanged-p (file)
  466. (eq 'unchanged (car (vc-bzr-status file))))
  467. (defun vc-bzr-working-revision (file)
  468. ;; Together with the code in vc-state-heuristic, this makes it possible
  469. ;; to get the initial VC state of a Bzr file even if Bzr is not installed.
  470. (lexical-let*
  471. ((rootdir (vc-bzr-root file))
  472. (branch-format-file (expand-file-name vc-bzr-admin-branch-format-file
  473. rootdir))
  474. (revhistory-file (expand-file-name vc-bzr-admin-revhistory rootdir))
  475. (lastrev-file (expand-file-name vc-bzr-admin-lastrev rootdir)))
  476. ;; This looks at internal files to avoid forking a bzr process.
  477. ;; May break if they change their format.
  478. (if (and (file-exists-p branch-format-file)
  479. ;; For lightweight checkouts (obtained with bzr checkout --lightweight)
  480. ;; the branch-format-file does not contain the revision
  481. ;; information, we need to look up the branch-format-file
  482. ;; in the place where the lightweight checkout comes
  483. ;; from. We only do that if it's a local file.
  484. (let ((location-fname (expand-file-name
  485. (concat vc-bzr-admin-dirname
  486. "/branch/location") rootdir)))
  487. ;; The existence of this file is how we distinguish
  488. ;; lightweight checkouts.
  489. (if (file-exists-p location-fname)
  490. (with-temp-buffer
  491. (insert-file-contents location-fname)
  492. ;; If the lightweight checkout points to a
  493. ;; location in the local file system, then we can
  494. ;; look there for the version information.
  495. (when (re-search-forward "file://\\(.+\\)" nil t)
  496. (let ((l-c-parent-dir (match-string 1)))
  497. (when (and (memq system-type '(ms-dos windows-nt))
  498. (string-match-p "^/[[:alpha:]]:" l-c-parent-dir))
  499. ;;; The non-Windows code takes a shortcut by using the host/path
  500. ;;; separator slash as the start of the absolute path. That
  501. ;;; does not work on Windows, so we must remove it (bug#5345)
  502. (setq l-c-parent-dir (substring l-c-parent-dir 1)))
  503. (setq branch-format-file
  504. (expand-file-name vc-bzr-admin-branch-format-file
  505. l-c-parent-dir))
  506. (setq lastrev-file
  507. (expand-file-name vc-bzr-admin-lastrev l-c-parent-dir))
  508. ;; FIXME: maybe it's overkill to check if both these files exist.
  509. (and (file-exists-p branch-format-file)
  510. (file-exists-p lastrev-file)))))
  511. t)))
  512. (with-temp-buffer
  513. (insert-file-contents branch-format-file)
  514. (goto-char (point-min))
  515. (cond
  516. ((or
  517. (looking-at "Bazaar-NG branch, format 0.0.4")
  518. (looking-at "Bazaar-NG branch format 5"))
  519. ;; count lines in .bzr/branch/revision-history
  520. (insert-file-contents revhistory-file)
  521. (number-to-string (count-lines (line-end-position) (point-max))))
  522. ((or
  523. (looking-at "Bazaar Branch Format 6 (bzr 0.15)")
  524. (looking-at "Bazaar Branch Format 7 (needs bzr 1.6)"))
  525. ;; revno is the first number in .bzr/branch/last-revision
  526. (insert-file-contents lastrev-file)
  527. (when (re-search-forward "[0-9]+" nil t)
  528. (buffer-substring (match-beginning 0) (match-end 0))))))
  529. ;; fallback to calling "bzr revno"
  530. (lexical-let*
  531. ((result (vc-bzr-command-discarding-stderr
  532. vc-bzr-program "revno" (file-relative-name file)))
  533. (exitcode (car result))
  534. (output (cdr result)))
  535. (cond
  536. ((eq exitcode 0) (substring output 0 -1))
  537. (t nil))))))
  538. (defun vc-bzr-create-repo ()
  539. "Create a new Bzr repository."
  540. (vc-bzr-command "init" nil 0 nil))
  541. (defun vc-bzr-init-revision (&optional file)
  542. "Always return nil, as Bzr cannot register explicit versions."
  543. nil)
  544. (defun vc-bzr-previous-revision (file rev)
  545. (if (string-match "\\`[0-9]+\\'" rev)
  546. (number-to-string (1- (string-to-number rev)))
  547. (concat "before:" rev)))
  548. (defun vc-bzr-next-revision (file rev)
  549. (if (string-match "\\`[0-9]+\\'" rev)
  550. (number-to-string (1+ (string-to-number rev)))
  551. (error "Don't know how to compute the next revision of %s" rev)))
  552. (defun vc-bzr-register (files &optional rev comment)
  553. "Register FILES under bzr.
  554. Signal an error unless REV is nil.
  555. COMMENT is ignored."
  556. (if rev (error "Can't register explicit revision with bzr"))
  557. (vc-bzr-command "add" nil 0 files))
  558. ;; Could run `bzr status' in the directory and see if it succeeds, but
  559. ;; that's relatively expensive.
  560. (defalias 'vc-bzr-responsible-p 'vc-bzr-root
  561. "Return non-nil if FILE is (potentially) controlled by bzr.
  562. The criterion is that there is a `.bzr' directory in the same
  563. or a superior directory.")
  564. (defun vc-bzr-could-register (file)
  565. "Return non-nil if FILE could be registered under bzr."
  566. (and (vc-bzr-responsible-p file) ; shortcut
  567. (condition-case ()
  568. (with-temp-buffer
  569. (vc-bzr-command "add" t 0 file "--dry-run")
  570. ;; The command succeeds with no output if file is
  571. ;; registered (in bzr 0.8).
  572. (goto-char (point-min))
  573. (looking-at "added "))
  574. (error))))
  575. (defun vc-bzr-unregister (file)
  576. "Unregister FILE from bzr."
  577. (vc-bzr-command "remove" nil 0 file "--keep"))
  578. (declare-function log-edit-extract-headers "log-edit" (headers string))
  579. (defun vc-bzr-checkin (files rev comment)
  580. "Check FILES in to bzr with log message COMMENT.
  581. REV non-nil gets an error."
  582. (if rev (error "Can't check in a specific revision with bzr"))
  583. (apply 'vc-bzr-command "commit" nil 0
  584. files (cons "-m" (log-edit-extract-headers '(("Author" . "--author")
  585. ("Date" . "--commit-time")
  586. ("Fixes" . "--fixes"))
  587. comment))))
  588. (defun vc-bzr-find-revision (file rev buffer)
  589. "Fetch revision REV of file FILE and put it into BUFFER."
  590. (with-current-buffer buffer
  591. (if (and rev (stringp rev) (not (string= rev "")))
  592. (vc-bzr-command "cat" t 0 file "-r" rev)
  593. (vc-bzr-command "cat" t 0 file))))
  594. (defun vc-bzr-checkout (file &optional editable rev)
  595. (if rev (error "Operation not supported")
  596. ;; Else, there's nothing to do.
  597. nil))
  598. (defun vc-bzr-revert (file &optional contents-done)
  599. (unless contents-done
  600. (with-temp-buffer (vc-bzr-command "revert" t 0 file))))
  601. (defvar log-view-message-re)
  602. (defvar log-view-file-re)
  603. (defvar log-view-font-lock-keywords)
  604. (defvar log-view-current-tag-function)
  605. (defvar log-view-per-file-logs)
  606. (defvar log-view-expanded-log-entry-function)
  607. (define-derived-mode vc-bzr-log-view-mode log-view-mode "Bzr-Log-View"
  608. (remove-hook 'log-view-mode-hook 'vc-bzr-log-view-mode) ;Deactivate the hack.
  609. (require 'add-log)
  610. (set (make-local-variable 'log-view-per-file-logs) nil)
  611. (set (make-local-variable 'log-view-file-re) "\\`a\\`")
  612. (set (make-local-variable 'log-view-message-re)
  613. (if (eq vc-log-view-type 'short)
  614. "^ *\\([0-9.]+\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)\\( \\[merge\\]\\)?"
  615. "^ *\\(?:revno: \\([0-9.]+\\)\\|merged: .+\\)"))
  616. ;; Allow expanding short log entries
  617. (when (eq vc-log-view-type 'short)
  618. (setq truncate-lines t)
  619. (set (make-local-variable 'log-view-expanded-log-entry-function)
  620. 'vc-bzr-expanded-log-entry))
  621. (set (make-local-variable 'log-view-font-lock-keywords)
  622. ;; log-view-font-lock-keywords is careful to use the buffer-local
  623. ;; value of log-view-message-re only since Emacs-23.
  624. (if (eq vc-log-view-type 'short)
  625. (append `((,log-view-message-re
  626. (1 'log-view-message-face)
  627. (2 'change-log-name)
  628. (3 'change-log-date)
  629. (4 'change-log-list nil lax))))
  630. (append `((,log-view-message-re . 'log-view-message-face))
  631. ;; log-view-font-lock-keywords
  632. '(("^ *\\(?:committer\\|author\\): \
  633. \\([^<(]+?\\)[ ]*[(<]\\([[:alnum:]_.+-]+@[[:alnum:]_.-]+\\)[>)]"
  634. (1 'change-log-name)
  635. (2 'change-log-email))
  636. ("^ *timestamp: \\(.*\\)" (1 'change-log-date-face)))))))
  637. (defun vc-bzr-print-log (files buffer &optional shortlog start-revision limit)
  638. "Get bzr change log for FILES into specified BUFFER."
  639. ;; `vc-do-command' creates the buffer, but we need it before running
  640. ;; the command.
  641. (vc-setup-buffer buffer)
  642. ;; If the buffer exists from a previous invocation it might be
  643. ;; read-only.
  644. ;; FIXME: `vc-bzr-command' runs `bzr log' with `LC_MESSAGES=C', so
  645. ;; the log display may not what the user wants - but I see no other
  646. ;; way of getting the above regexps working.
  647. (with-current-buffer buffer
  648. (apply 'vc-bzr-command "log" buffer 'async files
  649. (append
  650. (when shortlog '("--line"))
  651. (when start-revision (list (format "-r..%s" start-revision)))
  652. (when limit (list "-l" (format "%s" limit)))
  653. (if (stringp vc-bzr-log-switches)
  654. (list vc-bzr-log-switches)
  655. vc-bzr-log-switches)))))
  656. (defun vc-bzr-expanded-log-entry (revision)
  657. (with-temp-buffer
  658. (apply 'vc-bzr-command "log" t nil nil
  659. (list (format "-r%s" revision)))
  660. (goto-char (point-min))
  661. (when (looking-at "^-+\n")
  662. ;; Indent the expanded log entry.
  663. (indent-region (match-end 0) (point-max) 2)
  664. (buffer-substring (match-end 0) (point-max)))))
  665. (defun vc-bzr-log-incoming (buffer remote-location)
  666. (apply 'vc-bzr-command "missing" buffer 'async nil
  667. (list "--theirs-only" (unless (string= remote-location "") remote-location))))
  668. (defun vc-bzr-log-outgoing (buffer remote-location)
  669. (apply 'vc-bzr-command "missing" buffer 'async nil
  670. (list "--mine-only" (unless (string= remote-location "") remote-location))))
  671. (defun vc-bzr-show-log-entry (revision)
  672. "Find entry for patch name REVISION in bzr change log buffer."
  673. (goto-char (point-min))
  674. (when revision
  675. (let (case-fold-search
  676. found)
  677. (if (re-search-forward
  678. ;; "revno:" can appear either at the beginning of a line,
  679. ;; or indented.
  680. (concat "^[ ]*-+\n[ ]*revno: "
  681. ;; The revision can contain ".", quote it so that it
  682. ;; does not interfere with regexp matching.
  683. (regexp-quote revision) "$") nil t)
  684. (progn
  685. (beginning-of-line 0)
  686. (setq found t))
  687. (goto-char (point-min)))
  688. found)))
  689. (defun vc-bzr-diff (files &optional rev1 rev2 buffer)
  690. "VC bzr backend for diff."
  691. (let* ((switches (vc-switches 'bzr 'diff))
  692. (args
  693. (append
  694. ;; Only add --diff-options if there are any diff switches.
  695. (unless (zerop (length switches))
  696. (list "--diff-options" (mapconcat 'identity switches " ")))
  697. ;; This `when' is just an optimization because bzr-1.2 is *much*
  698. ;; faster when the revision argument is not given.
  699. (when (or rev1 rev2)
  700. (list "-r" (format "%s..%s"
  701. (or rev1 "revno:-1")
  702. (or rev2 "")))))))
  703. ;; `bzr diff' exits with code 1 if diff is non-empty.
  704. (apply #'vc-bzr-command "diff" (or buffer "*vc-diff*")
  705. (if vc-disable-async-diff 1 'async) files
  706. args)))
  707. ;; FIXME: vc-{next,previous}-revision need fixing in vc.el to deal with
  708. ;; straight integer revisions.
  709. (defun vc-bzr-delete-file (file)
  710. "Delete FILE and delete it in the bzr repository."
  711. (condition-case ()
  712. (delete-file file)
  713. (file-error nil))
  714. (vc-bzr-command "remove" nil 0 file))
  715. (defun vc-bzr-rename-file (old new)
  716. "Rename file from OLD to NEW using `bzr mv'."
  717. (setq old (expand-file-name old))
  718. (setq new (expand-file-name new))
  719. (vc-bzr-command "mv" nil 0 new old)
  720. (message "Renamed %s => %s" old new))
  721. (defvar vc-bzr-annotation-table nil
  722. "Internal use.")
  723. (make-variable-buffer-local 'vc-bzr-annotation-table)
  724. (defun vc-bzr-annotate-command (file buffer &optional revision)
  725. "Prepare BUFFER for `vc-annotate' on FILE.
  726. Each line is tagged with the revision number, which has a `help-echo'
  727. property containing author and date information."
  728. (apply #'vc-bzr-command "annotate" buffer 'async file "--long" "--all"
  729. (if revision (list "-r" revision)))
  730. (lexical-let ((table (make-hash-table :test 'equal)))
  731. (set-process-filter
  732. (get-buffer-process buffer)
  733. (lambda (proc string)
  734. (when (process-buffer proc)
  735. (with-current-buffer (process-buffer proc)
  736. (setq string (concat (process-get proc :vc-left-over) string))
  737. ;; Eg: 102020 Gnus developers 20101020 | regexp."
  738. ;; As of bzr 2.2.2, no email address in whoami (which can
  739. ;; lead to spaces in the author field) is allowed but discouraged.
  740. ;; See bug#7792.
  741. (while (string-match "^\\( *[0-9.]+ *\\) \\(.+?\\) +\\([0-9]\\{8\\}\\)\\( |.*\n\\)" string)
  742. (let* ((rev (match-string 1 string))
  743. (author (match-string 2 string))
  744. (date (match-string 3 string))
  745. (key (substring string (match-beginning 0)
  746. (match-beginning 4)))
  747. (line (match-string 4 string))
  748. (tag (gethash key table))
  749. (inhibit-read-only t))
  750. (setq string (substring string (match-end 0)))
  751. (unless tag
  752. (setq tag
  753. (propertize
  754. (format "%s %-7.7s" rev author)
  755. 'help-echo (format "Revision: %d, author: %s, date: %s"
  756. (string-to-number rev)
  757. author date)
  758. 'mouse-face 'highlight))
  759. (puthash key tag table))
  760. (goto-char (process-mark proc))
  761. (insert tag line)
  762. (move-marker (process-mark proc) (point))))
  763. (process-put proc :vc-left-over string)))))))
  764. (declare-function vc-annotate-convert-time "vc-annotate" (time))
  765. (defun vc-bzr-annotate-time ()
  766. (when (re-search-forward "^ *[0-9.]+ +.+? +|" nil t)
  767. (let ((prop (get-text-property (line-beginning-position) 'help-echo)))
  768. (string-match "[0-9]+\\'" prop)
  769. (let ((str (match-string-no-properties 0 prop)))
  770. (vc-annotate-convert-time
  771. (encode-time 0 0 0
  772. (string-to-number (substring str 6 8))
  773. (string-to-number (substring str 4 6))
  774. (string-to-number (substring str 0 4))))))))
  775. (defun vc-bzr-annotate-extract-revision-at-line ()
  776. "Return revision for current line of annotation buffer, or nil.
  777. Return nil if current line isn't annotated."
  778. (save-excursion
  779. (beginning-of-line)
  780. (if (looking-at "^ *\\([0-9.]+\\) +.* +|")
  781. (match-string-no-properties 1))))
  782. (defun vc-bzr-command-discarding-stderr (command &rest args)
  783. "Execute shell command COMMAND (with ARGS); return its output and exitcode.
  784. Return value is a cons (EXITCODE . OUTPUT), where EXITCODE is
  785. the (numerical) exit code of the process, and OUTPUT is a string
  786. containing whatever the process sent to its standard output
  787. stream. Standard error output is discarded."
  788. (with-temp-buffer
  789. (cons
  790. (apply #'process-file command nil (list (current-buffer) nil) nil args)
  791. (buffer-substring (point-min) (point-max)))))
  792. (defstruct (vc-bzr-extra-fileinfo
  793. (:copier nil)
  794. (:constructor vc-bzr-create-extra-fileinfo (extra-name))
  795. (:conc-name vc-bzr-extra-fileinfo->))
  796. extra-name) ;; original name for rename targets, new name for
  797. (defun vc-bzr-dir-printer (info)
  798. "Pretty-printer for the vc-dir-fileinfo structure."
  799. (let ((extra (vc-dir-fileinfo->extra info)))
  800. (vc-default-dir-printer 'Bzr info)
  801. (when extra
  802. (insert (propertize
  803. (format " (renamed from %s)"
  804. (vc-bzr-extra-fileinfo->extra-name extra))
  805. 'face 'font-lock-comment-face)))))
  806. ;; FIXME: this needs testing, it's probably incomplete.
  807. (defun vc-bzr-after-dir-status (update-function relative-dir)
  808. (let ((status-str nil)
  809. (translation '(("+N " . added)
  810. ("-D " . removed)
  811. (" M " . edited) ;; file text modified
  812. (" *" . edited) ;; execute bit changed
  813. (" M*" . edited) ;; text modified + execute bit changed
  814. ("I " . ignored)
  815. (" D " . missing)
  816. ;; For conflicts, should we list the .THIS/.BASE/.OTHER?
  817. ("C " . conflict)
  818. ("? " . unregistered)
  819. ;; No such state, but we need to distinguish this case.
  820. ("R " . renamed)
  821. ("RM " . renamed)
  822. ;; For a non existent file FOO, the output is:
  823. ;; bzr: ERROR: Path(s) do not exist: FOO
  824. ("bzr" . not-found)
  825. ;; If the tree is not up to date, bzr will print this warning:
  826. ;; working tree is out of date, run 'bzr update'
  827. ;; ignore it.
  828. ;; FIXME: maybe this warning can be put in the vc-dir header...
  829. ("wor" . not-found)
  830. ;; Ignore "P " and "P." for pending patches.
  831. ("P " . not-found)
  832. ("P. " . not-found)
  833. ))
  834. (translated nil)
  835. (result nil))
  836. (goto-char (point-min))
  837. (while (not (eobp))
  838. ;; Bzr 2.3.0 added this if there are shelves. (Bug#8170)
  839. (unless (looking-at "[0-9]+ shel\\(f\\|ves\\) exists?\\.")
  840. (setq status-str
  841. (buffer-substring-no-properties (point) (+ (point) 3)))
  842. (setq translated (cdr (assoc status-str translation)))
  843. (cond
  844. ((eq translated 'conflict)
  845. ;; For conflicts the file appears twice in the listing: once
  846. ;; with the M flag and once with the C flag, so take care
  847. ;; not to add it twice to `result'. Ugly.
  848. (let* ((file
  849. (buffer-substring-no-properties
  850. ;;For files with conflicts the format is:
  851. ;;C Text conflict in FILENAME
  852. ;; Bah.
  853. (+ (point) 21) (line-end-position)))
  854. (entry (assoc file result)))
  855. (when entry
  856. (setf (nth 1 entry) 'conflict))))
  857. ((eq translated 'renamed)
  858. (re-search-forward "R[ M] \\(.*\\) => \\(.*\\)$" (line-end-position) t)
  859. (let ((new-name (file-relative-name (match-string 2) relative-dir))
  860. (old-name (file-relative-name (match-string 1) relative-dir)))
  861. (push (list new-name 'edited
  862. (vc-bzr-create-extra-fileinfo old-name)) result)))
  863. ;; do nothing for non existent files
  864. ((memq translated '(not-found ignored)))
  865. (t
  866. (push (list (file-relative-name
  867. (buffer-substring-no-properties
  868. (+ (point) 4)
  869. (line-end-position)) relative-dir)
  870. translated) result))))
  871. (forward-line))
  872. (funcall update-function result)))
  873. (defun vc-bzr-dir-status (dir update-function)
  874. "Return a list of conses (file . state) for DIR."
  875. (vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S")
  876. (vc-exec-after
  877. `(vc-bzr-after-dir-status (quote ,update-function)
  878. ;; "bzr status" results are relative to
  879. ;; the bzr root directory, NOT to the
  880. ;; directory "bzr status" was invoked in.
  881. ;; Ugh.
  882. ;; We pass the relative directory here so
  883. ;; that `vc-bzr-after-dir-status' can
  884. ;; frob the results accordingly.
  885. (file-relative-name ,dir (vc-bzr-root ,dir)))))
  886. (defun vc-bzr-dir-status-files (dir files default-state update-function)
  887. "Return a list of conses (file . state) for DIR."
  888. (apply 'vc-bzr-command "status" (current-buffer) 'async dir "-v" "-S" files)
  889. (vc-exec-after
  890. `(vc-bzr-after-dir-status (quote ,update-function)
  891. (file-relative-name ,dir (vc-bzr-root ,dir)))))
  892. (defvar vc-bzr-shelve-map
  893. (let ((map (make-sparse-keymap)))
  894. ;; Turn off vc-dir marking
  895. (define-key map [mouse-2] 'ignore)
  896. (define-key map [down-mouse-3] 'vc-bzr-shelve-menu)
  897. (define-key map "\C-k" 'vc-bzr-shelve-delete-at-point)
  898. (define-key map "=" 'vc-bzr-shelve-show-at-point)
  899. (define-key map "\C-m" 'vc-bzr-shelve-show-at-point)
  900. (define-key map "A" 'vc-bzr-shelve-apply-and-keep-at-point)
  901. (define-key map "P" 'vc-bzr-shelve-apply-at-point)
  902. (define-key map "S" 'vc-bzr-shelve-snapshot)
  903. map))
  904. (defvar vc-bzr-shelve-menu-map
  905. (let ((map (make-sparse-keymap "Bzr Shelve")))
  906. (define-key map [de]
  907. '(menu-item "Delete Shelf" vc-bzr-shelve-delete-at-point
  908. :help "Delete the current shelf"))
  909. (define-key map [ap]
  910. '(menu-item "Apply and Keep Shelf" vc-bzr-shelve-apply-and-keep-at-point
  911. :help "Apply the current shelf and keep it"))
  912. (define-key map [po]
  913. '(menu-item "Apply and Remove Shelf (Pop)" vc-bzr-shelve-apply-at-point
  914. :help "Apply the current shelf and remove it"))
  915. (define-key map [sh]
  916. '(menu-item "Show Shelve" vc-bzr-shelve-show-at-point
  917. :help "Show the contents of the current shelve"))
  918. map))
  919. (defvar vc-bzr-extra-menu-map
  920. (let ((map (make-sparse-keymap)))
  921. (define-key map [bzr-sn]
  922. '(menu-item "Shelve a Snapshot" vc-bzr-shelve-snapshot
  923. :help "Shelve the current state of the tree and keep the current state"))
  924. (define-key map [bzr-sh]
  925. '(menu-item "Shelve..." vc-bzr-shelve
  926. :help "Shelve changes"))
  927. map))
  928. (defun vc-bzr-extra-menu () vc-bzr-extra-menu-map)
  929. (defun vc-bzr-extra-status-menu () vc-bzr-extra-menu-map)
  930. (defun vc-bzr-dir-extra-headers (dir)
  931. (let*
  932. ((str (with-temp-buffer
  933. (vc-bzr-command "info" t 0 dir)
  934. (buffer-string)))
  935. (shelve (vc-bzr-shelve-list))
  936. (shelve-help-echo "Use M-x vc-bzr-shelve to create shelves")
  937. (root-dir (vc-bzr-root dir))
  938. (pending-merge
  939. ;; FIXME: looking for .bzr/checkout/merge-hashes is not a
  940. ;; reliable method to detect pending merges, disable this
  941. ;; until a proper solution is implemented.
  942. (and nil
  943. (file-exists-p
  944. (expand-file-name ".bzr/checkout/merge-hashes" root-dir))))
  945. (pending-merge-help-echo
  946. (format "A merge has been performed.\nA commit from the top-level directory (%s)\nis required before being able to check in anything else" root-dir))
  947. (light-checkout
  948. (when (string-match ".+light checkout root: \\(.+\\)$" str)
  949. (match-string 1 str)))
  950. (light-checkout-branch
  951. (when light-checkout
  952. (when (string-match ".+checkout of branch: \\(.+\\)$" str)
  953. (match-string 1 str)))))
  954. (concat
  955. (propertize "Parent branch : " 'face 'font-lock-type-face)
  956. (propertize
  957. (if (string-match "parent branch: \\(.+\\)$" str)
  958. (match-string 1 str)
  959. "None")
  960. 'face 'font-lock-variable-name-face)
  961. "\n"
  962. (when light-checkout
  963. (concat
  964. (propertize "Light checkout root: " 'face 'font-lock-type-face)
  965. (propertize light-checkout 'face 'font-lock-variable-name-face)
  966. "\n"))
  967. (when light-checkout-branch
  968. (concat
  969. (propertize "Checkout of branch : " 'face 'font-lock-type-face)
  970. (propertize light-checkout-branch 'face 'font-lock-variable-name-face)
  971. "\n"))
  972. (when pending-merge
  973. (concat
  974. (propertize "Warning : " 'face 'font-lock-warning-face
  975. 'help-echo pending-merge-help-echo)
  976. (propertize "Pending merges, commit recommended before any other action"
  977. 'help-echo pending-merge-help-echo
  978. 'face 'font-lock-warning-face)
  979. "\n"))
  980. (if shelve
  981. (concat
  982. (propertize "Shelves :\n" 'face 'font-lock-type-face
  983. 'help-echo shelve-help-echo)
  984. (mapconcat
  985. (lambda (x)
  986. (propertize x
  987. 'face 'font-lock-variable-name-face
  988. 'mouse-face 'highlight
  989. 'help-echo "mouse-3: Show shelve menu\nA: Apply and keep shelf\nP: Apply and remove shelf (pop)\nS: Snapshot to a shelf\nC-k: Delete shelf"
  990. 'keymap vc-bzr-shelve-map))
  991. shelve "\n"))
  992. (concat
  993. (propertize "Shelves : " 'face 'font-lock-type-face
  994. 'help-echo shelve-help-echo)
  995. (propertize "No shelved changes"
  996. 'help-echo shelve-help-echo
  997. 'face 'font-lock-variable-name-face))))))
  998. (defun vc-bzr-shelve (name)
  999. "Create a shelve."
  1000. (interactive "sShelf name: ")
  1001. (let ((root (vc-bzr-root default-directory)))
  1002. (when root
  1003. (vc-bzr-command "shelve" nil 0 nil "--all" "-m" name)
  1004. (vc-resynch-buffer root t t))))
  1005. (defun vc-bzr-shelve-show (name)
  1006. "Show the contents of shelve NAME."
  1007. (interactive "sShelve name: ")
  1008. (vc-setup-buffer "*vc-diff*")
  1009. ;; FIXME: how can you show the contents of a shelf?
  1010. (vc-bzr-command "unshelve" "*vc-diff*" 'async nil "--preview" name)
  1011. (set-buffer "*vc-diff*")
  1012. (diff-mode)
  1013. (setq buffer-read-only t)
  1014. (pop-to-buffer (current-buffer)))
  1015. (defun vc-bzr-shelve-apply (name)
  1016. "Apply shelve NAME and remove it afterwards."
  1017. (interactive "sApply (and remove) shelf: ")
  1018. (vc-bzr-command "unshelve" nil 0 nil "--apply" name)
  1019. (vc-resynch-buffer (vc-bzr-root default-directory) t t))
  1020. (defun vc-bzr-shelve-apply-and-keep (name)
  1021. "Apply shelve NAME and keep it afterwards."
  1022. (interactive "sApply (and keep) shelf: ")
  1023. (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep" name)
  1024. (vc-resynch-buffer (vc-bzr-root default-directory) t t))
  1025. (defun vc-bzr-shelve-snapshot ()
  1026. "Create a stash with the current tree state."
  1027. (interactive)
  1028. (vc-bzr-command "shelve" nil 0 nil "--all" "-m"
  1029. (let ((ct (current-time)))
  1030. (concat
  1031. (format-time-string "Snapshot on %Y-%m-%d" ct)
  1032. (format-time-string " at %H:%M" ct))))
  1033. (vc-bzr-command "unshelve" nil 0 nil "--apply" "--keep")
  1034. (vc-resynch-buffer (vc-bzr-root default-directory) t t))
  1035. (defun vc-bzr-shelve-list ()
  1036. (with-temp-buffer
  1037. (vc-bzr-command "shelve" (current-buffer) 1 nil "--list" "-q")
  1038. (delete
  1039. ""
  1040. (split-string
  1041. (buffer-substring (point-min) (point-max))
  1042. "\n"))))
  1043. (defun vc-bzr-shelve-get-at-point (point)
  1044. (save-excursion
  1045. (goto-char point)
  1046. (beginning-of-line)
  1047. (if (looking-at "^ +\\([0-9]+\\):")
  1048. (match-string 1)
  1049. (error "Cannot find shelf at point"))))
  1050. (defun vc-bzr-shelve-delete-at-point ()
  1051. (interactive)
  1052. (let ((shelve (vc-bzr-shelve-get-at-point (point))))
  1053. (when (y-or-n-p (format "Remove shelf %s ? " shelve))
  1054. (vc-bzr-command "unshelve" nil 0 nil "--delete-only" shelve)
  1055. (vc-dir-refresh))))
  1056. (defun vc-bzr-shelve-show-at-point ()
  1057. (interactive)
  1058. (vc-bzr-shelve-show (vc-bzr-shelve-get-at-point (point))))
  1059. (defun vc-bzr-shelve-apply-at-point ()
  1060. (interactive)
  1061. (vc-bzr-shelve-apply (vc-bzr-shelve-get-at-point (point))))
  1062. (defun vc-bzr-shelve-apply-and-keep-at-point ()
  1063. (interactive)
  1064. (vc-bzr-shelve-apply-and-keep (vc-bzr-shelve-get-at-point (point))))
  1065. (defun vc-bzr-shelve-menu (e)
  1066. (interactive "e")
  1067. (vc-dir-at-event e (popup-menu vc-bzr-shelve-menu-map e)))
  1068. (defun vc-bzr-revision-table (files)
  1069. (let ((vc-bzr-revisions '())
  1070. (default-directory (file-name-directory (car files))))
  1071. (with-temp-buffer
  1072. (vc-bzr-command "log" t 0 files "--line")
  1073. (let ((start (point-min))
  1074. (loglines (buffer-substring-no-properties (point-min) (point-max))))
  1075. (while (string-match "^\\([0-9]+\\):" loglines)
  1076. (push (match-string 1 loglines) vc-bzr-revisions)
  1077. (setq start (+ start (match-end 0)))
  1078. (setq loglines (buffer-substring-no-properties start (point-max))))))
  1079. vc-bzr-revisions))
  1080. (defun vc-bzr-conflicted-files (dir)
  1081. (let ((default-directory (vc-bzr-root dir))
  1082. (files ()))
  1083. (with-temp-buffer
  1084. (vc-bzr-command "status" t 0 default-directory)
  1085. (goto-char (point-min))
  1086. (when (re-search-forward "^conflicts:\n" nil t)
  1087. (while (looking-at " \\(?:Text conflict in \\(.*\\)\\|.*\\)\n")
  1088. (if (match-end 1)
  1089. (push (expand-file-name (match-string 1)) files))
  1090. (goto-char (match-end 0)))))
  1091. files))
  1092. ;;; Revision completion
  1093. (eval-and-compile
  1094. (defconst vc-bzr-revision-keywords
  1095. ;; bzr help revisionspec | sed -ne 's/^\([a-z]*\):$/"\1"/p' | sort -u
  1096. '("ancestor" "annotate" "before" "branch" "date" "last" "mainline" "revid"
  1097. "revno" "submit" "tag")))
  1098. (defun vc-bzr-revision-completion-table (files)
  1099. (lexical-let ((files files))
  1100. ;; What about using `files'?!? --Stef
  1101. (lambda (string pred action)
  1102. (cond
  1103. ((string-match "\\`\\(ancestor\\|branch\\|\\(revno:\\)?[-0-9]+:\\):"
  1104. string)
  1105. (completion-table-with-context (substring string 0 (match-end 0))
  1106. (apply-partially
  1107. 'completion-table-with-predicate
  1108. 'completion-file-name-table
  1109. 'file-directory-p t)
  1110. (substring string (match-end 0))
  1111. pred
  1112. action))
  1113. ((string-match "\\`\\(before\\):" string)
  1114. (completion-table-with-context (substring string 0 (match-end 0))
  1115. (vc-bzr-revision-completion-table files)
  1116. (substring string (match-end 0))
  1117. pred
  1118. action))
  1119. ((string-match "\\`\\(tag\\):" string)
  1120. (let ((prefix (substring string 0 (match-end 0)))
  1121. (tag (substring string (match-end 0)))
  1122. (table nil)
  1123. process-file-side-effects)
  1124. (with-temp-buffer
  1125. ;; "bzr-1.2 tags" is much faster with --show-ids.
  1126. (process-file vc-bzr-program nil '(t) nil "tags" "--show-ids")
  1127. ;; The output is ambiguous, unless we assume that revids do not
  1128. ;; contain spaces.
  1129. (goto-char (point-min))
  1130. (while (re-search-forward "^\\(.*[^ \n]\\) +[^ \n]*$" nil t)
  1131. (push (match-string-no-properties 1) table)))
  1132. (completion-table-with-context prefix table tag pred action)))
  1133. ((string-match "\\`annotate:" string)
  1134. (completion-table-with-context
  1135. (substring string 0 (match-end 0))
  1136. (apply-partially #'completion-table-with-terminator '(":" . "\\`a\\`")
  1137. #'completion-file-name-table)
  1138. (substring string (match-end 0)) pred action))
  1139. ((string-match "\\`date:" string)
  1140. (completion-table-with-context
  1141. (substring string 0 (match-end 0))
  1142. '("yesterday" "today" "tomorrow")
  1143. (substring string (match-end 0)) pred action))
  1144. ((string-match "\\`\\([a-z]+\\):" string)
  1145. ;; no actual completion for the remaining keywords.
  1146. (completion-table-with-context (substring string 0 (match-end 0))
  1147. (if (member (match-string 1 string)
  1148. vc-bzr-revision-keywords)
  1149. ;; If it's a valid keyword,
  1150. ;; use a non-empty table to
  1151. ;; indicate it.
  1152. '("") nil)
  1153. (substring string (match-end 0))
  1154. pred
  1155. action))
  1156. (t
  1157. ;; Could use completion-table-with-terminator, except that it
  1158. ;; currently doesn't work right w.r.t pcm and doesn't give
  1159. ;; the *Completions* output we want.
  1160. (complete-with-action action (eval-when-compile
  1161. (mapcar (lambda (s) (concat s ":"))
  1162. vc-bzr-revision-keywords))
  1163. string pred))))))
  1164. (provide 'vc-bzr)
  1165. ;;; vc-bzr.el ends here