vc-bzr.el 55 KB

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