vc-hg.el 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. ;;; vc-hg.el --- VC backend for the mercurial version control system
  2. ;; Copyright (C) 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Ivan Kanis
  4. ;; Keywords: vc tools
  5. ;; Package: vc
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This is a mercurial version control backend
  19. ;;; Thanks:
  20. ;;; Bugs:
  21. ;;; Installation:
  22. ;;; Todo:
  23. ;; 1) Implement the rest of the vc interface. See the comment at the
  24. ;; beginning of vc.el. The current status is:
  25. ;; FUNCTION NAME STATUS
  26. ;; BACKEND PROPERTIES
  27. ;; * revision-granularity OK
  28. ;; STATE-QUERYING FUNCTIONS
  29. ;; * registered (file) OK
  30. ;; * state (file) OK
  31. ;; - state-heuristic (file) NOT NEEDED
  32. ;; - dir-status (dir update-function) OK
  33. ;; - dir-status-files (dir files ds uf) OK
  34. ;; - dir-extra-headers (dir) OK
  35. ;; - dir-printer (fileinfo) OK
  36. ;; * working-revision (file) OK
  37. ;; - latest-on-branch-p (file) ??
  38. ;; * checkout-model (files) OK
  39. ;; - workfile-unchanged-p (file) OK
  40. ;; - mode-line-string (file) NOT NEEDED
  41. ;; STATE-CHANGING FUNCTIONS
  42. ;; * register (files &optional rev comment) OK
  43. ;; * create-repo () OK
  44. ;; - init-revision () NOT NEEDED
  45. ;; - responsible-p (file) OK
  46. ;; - could-register (file) OK
  47. ;; - receive-file (file rev) ?? PROBABLY NOT NEEDED
  48. ;; - unregister (file) COMMENTED OUT, MAY BE INCORRECT
  49. ;; * checkin (files rev comment) OK
  50. ;; * find-revision (file rev buffer) OK
  51. ;; * checkout (file &optional editable rev) OK
  52. ;; * revert (file &optional contents-done) OK
  53. ;; - rollback (files) ?? PROBABLY NOT NEEDED
  54. ;; - merge (file rev1 rev2) NEEDED
  55. ;; - merge-news (file) NEEDED
  56. ;; - steal-lock (file &optional revision) NOT NEEDED
  57. ;; HISTORY FUNCTIONS
  58. ;; * print-log (files buffer &optional shortlog start-revision limit) OK
  59. ;; - log-view-mode () OK
  60. ;; - show-log-entry (revision) NOT NEEDED, DEFAULT IS GOOD
  61. ;; - comment-history (file) NOT NEEDED
  62. ;; - update-changelog (files) NOT NEEDED
  63. ;; * diff (files &optional rev1 rev2 buffer) OK
  64. ;; - revision-completion-table (files) OK?
  65. ;; - annotate-command (file buf &optional rev) OK
  66. ;; - annotate-time () OK
  67. ;; - annotate-current-time () NOT NEEDED
  68. ;; - annotate-extract-revision-at-line () OK
  69. ;; TAG SYSTEM
  70. ;; - create-tag (dir name branchp) NEEDED
  71. ;; - retrieve-tag (dir name update) NEEDED
  72. ;; MISCELLANEOUS
  73. ;; - make-version-backups-p (file) ??
  74. ;; - repository-hostname (dirname) ??
  75. ;; - previous-revision (file rev) OK
  76. ;; - next-revision (file rev) OK
  77. ;; - check-headers () ??
  78. ;; - clear-headers () ??
  79. ;; - delete-file (file) TEST IT
  80. ;; - rename-file (old new) OK
  81. ;; - find-file-hook () PROBABLY NOT NEEDED
  82. ;; 2) Implement Stefan Monnier's advice:
  83. ;; vc-hg-registered and vc-hg-state
  84. ;; Both of those functions should be super extra careful to fail gracefully in
  85. ;; unexpected circumstances. The reason this is important is that any error
  86. ;; there will prevent the user from even looking at the file :-(
  87. ;; Ideally, just like in vc-arch and vc-cvs, checking that the file is under
  88. ;; mercurial's control and extracting the current revision should be done
  89. ;; without even using `hg' (this way even if you don't have `hg' installed,
  90. ;; Emacs is able to tell you this file is under mercurial's control).
  91. ;;; History:
  92. ;;
  93. ;;; Code:
  94. (eval-when-compile
  95. (require 'cl)
  96. (require 'vc)
  97. (require 'vc-dir))
  98. ;;; Customization options
  99. (defgroup vc-hg nil
  100. "VC Mercurial (hg) backend."
  101. :version "24.1"
  102. :group 'vc)
  103. (defcustom vc-hg-global-switches nil
  104. "Global switches to pass to any Hg command."
  105. :type '(choice (const :tag "None" nil)
  106. (string :tag "Argument String")
  107. (repeat :tag "Argument List" :value ("") string))
  108. :version "22.2"
  109. :group 'vc-hg)
  110. (defcustom vc-hg-diff-switches t ; Hg doesn't support common args like -u
  111. "String or list of strings specifying switches for Hg diff under VC.
  112. If nil, use the value of `vc-diff-switches'. If t, use no switches."
  113. :type '(choice (const :tag "Unspecified" nil)
  114. (const :tag "None" t)
  115. (string :tag "Argument String")
  116. (repeat :tag "Argument List" :value ("") string))
  117. :version "23.1"
  118. :group 'vc-hg)
  119. (defcustom vc-hg-program "hg"
  120. "Name of the Mercurial executable (excluding any arguments)."
  121. :type 'string
  122. :group 'vc-hg)
  123. (defcustom vc-hg-root-log-format
  124. '("{rev}:{tags}: {author|person} {date|shortdate} {desc|firstline}\\n"
  125. "^\\([0-9]+\\):\\([^:]*\\): \\(.*?\\)[ \t]+\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}\\)"
  126. ((1 'log-view-message-face)
  127. (2 'change-log-list)
  128. (3 'change-log-name)
  129. (4 'change-log-date)))
  130. "Mercurial log template for `vc-print-root-log'.
  131. This should be a list (TEMPLATE REGEXP KEYWORDS), where TEMPLATE
  132. is the \"--template\" argument string to pass to Mercurial,
  133. REGEXP is a regular expression matching the resulting Mercurial
  134. output, and KEYWORDS is a list of `font-lock-keywords' for
  135. highlighting the Log View buffer."
  136. :type '(list string string (repeat sexp))
  137. :group 'vc-hg
  138. :version "24.1")
  139. ;;; Properties of the backend
  140. (defvar vc-hg-history nil)
  141. (defun vc-hg-revision-granularity () 'repository)
  142. (defun vc-hg-checkout-model (files) 'implicit)
  143. ;;; State querying functions
  144. ;;;###autoload (defun vc-hg-registered (file)
  145. ;;;###autoload "Return non-nil if FILE is registered with hg."
  146. ;;;###autoload (if (vc-find-root file ".hg") ; short cut
  147. ;;;###autoload (progn
  148. ;;;###autoload (load "vc-hg")
  149. ;;;###autoload (vc-hg-registered file))))
  150. ;; Modeled after the similar function in vc-bzr.el
  151. (defun vc-hg-registered (file)
  152. "Return non-nil if FILE is registered with hg."
  153. (when (vc-hg-root file) ; short cut
  154. (let ((state (vc-hg-state file))) ; expensive
  155. (and state (not (memq state '(ignored unregistered)))))))
  156. (defun vc-hg-state (file)
  157. "Hg-specific version of `vc-state'."
  158. (let*
  159. ((status nil)
  160. (default-directory (file-name-directory file))
  161. (out
  162. (with-output-to-string
  163. (with-current-buffer
  164. standard-output
  165. (setq status
  166. (condition-case nil
  167. ;; Ignore all errors.
  168. (let ((process-environment
  169. ;; Avoid localization of messages so we
  170. ;; can parse the output.
  171. (append (list "TERM=dumb" "LANGUAGE=C")
  172. process-environment)))
  173. (process-file
  174. vc-hg-program nil t nil
  175. "--config" "alias.status=status"
  176. "--config" "defaults.status="
  177. "status" "-A" (file-relative-name file)))
  178. ;; Some problem happened. E.g. We can't find an `hg'
  179. ;; executable.
  180. (error nil)))))))
  181. (when (eq 0 status)
  182. (when (null (string-match ".*: No such file or directory$" out))
  183. (let ((state (aref out 0)))
  184. (cond
  185. ((eq state ?=) 'up-to-date)
  186. ((eq state ?A) 'added)
  187. ((eq state ?M) 'edited)
  188. ((eq state ?I) 'ignored)
  189. ((eq state ?R) 'removed)
  190. ((eq state ?!) 'missing)
  191. ((eq state ??) 'unregistered)
  192. ((eq state ?C) 'up-to-date) ;; Older mercurial versions use this.
  193. (t 'up-to-date)))))))
  194. (defun vc-hg-working-revision (file)
  195. "Hg-specific version of `vc-working-revision'."
  196. (let ((default-directory (if (file-directory-p file)
  197. (file-name-as-directory file)
  198. (file-name-directory file))))
  199. (ignore-errors
  200. (with-output-to-string
  201. (process-file vc-hg-program nil standard-output nil
  202. "log" "-l" "1" "--template" "{rev}"
  203. (file-relative-name file))))))
  204. ;;; History functions
  205. (defcustom vc-hg-log-switches nil
  206. "String or list of strings specifying switches for hg log under VC."
  207. :type '(choice (const :tag "None" nil)
  208. (string :tag "Argument String")
  209. (repeat :tag "Argument List" :value ("") string))
  210. :group 'vc-hg)
  211. (defun vc-hg-print-log (files buffer &optional shortlog start-revision limit)
  212. "Get change log associated with FILES."
  213. ;; `vc-do-command' creates the buffer, but we need it before running
  214. ;; the command.
  215. (vc-setup-buffer buffer)
  216. ;; If the buffer exists from a previous invocation it might be
  217. ;; read-only.
  218. (let ((inhibit-read-only t))
  219. (with-current-buffer
  220. buffer
  221. (apply 'vc-hg-command buffer 0 files "log"
  222. (nconc
  223. (when start-revision (list (format "-r%s:" start-revision)))
  224. (when limit (list "-l" (format "%s" limit)))
  225. (when shortlog (list "--template" (car vc-hg-root-log-format)))
  226. vc-hg-log-switches)))))
  227. (defvar log-view-message-re)
  228. (defvar log-view-file-re)
  229. (defvar log-view-font-lock-keywords)
  230. (defvar log-view-per-file-logs)
  231. (defvar log-view-expanded-log-entry-function)
  232. (define-derived-mode vc-hg-log-view-mode log-view-mode "Hg-Log-View"
  233. (require 'add-log) ;; we need the add-log faces
  234. (set (make-local-variable 'log-view-file-re) "\\`a\\`")
  235. (set (make-local-variable 'log-view-per-file-logs) nil)
  236. (set (make-local-variable 'log-view-message-re)
  237. (if (eq vc-log-view-type 'short)
  238. (cadr vc-hg-root-log-format)
  239. "^changeset:[ \t]*\\([0-9]+\\):\\(.+\\)"))
  240. ;; Allow expanding short log entries
  241. (when (eq vc-log-view-type 'short)
  242. (setq truncate-lines t)
  243. (set (make-local-variable 'log-view-expanded-log-entry-function)
  244. 'vc-hg-expanded-log-entry))
  245. (set (make-local-variable 'log-view-font-lock-keywords)
  246. (if (eq vc-log-view-type 'short)
  247. (list (cons (nth 1 vc-hg-root-log-format)
  248. (nth 2 vc-hg-root-log-format)))
  249. (append
  250. log-view-font-lock-keywords
  251. '(
  252. ;; Handle the case:
  253. ;; user: FirstName LastName <foo@bar>
  254. ("^user:[ \t]+\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]"
  255. (1 'change-log-name)
  256. (2 'change-log-email))
  257. ;; Handle the cases:
  258. ;; user: foo@bar
  259. ;; and
  260. ;; user: foo
  261. ("^user:[ \t]+\\([A-Za-z0-9_.+-]+\\(?:@[A-Za-z0-9_.-]+\\)?\\)"
  262. (1 'change-log-email))
  263. ("^date: \\(.+\\)" (1 'change-log-date))
  264. ("^tag: +\\([^ ]+\\)$" (1 'highlight))
  265. ("^summary:[ \t]+\\(.+\\)" (1 'log-view-message)))))))
  266. (defun vc-hg-diff (files &optional oldvers newvers buffer)
  267. "Get a difference report using hg between two revisions of FILES."
  268. (let* ((firstfile (car files))
  269. (working (and firstfile (vc-working-revision firstfile))))
  270. (when (and (equal oldvers working) (not newvers))
  271. (setq oldvers nil))
  272. (when (and (not oldvers) newvers)
  273. (setq oldvers working))
  274. (apply #'vc-hg-command (or buffer "*vc-diff*") nil files "diff"
  275. (append
  276. (vc-switches 'hg 'diff)
  277. (when oldvers
  278. (if newvers
  279. (list "-r" oldvers "-r" newvers)
  280. (list "-r" oldvers)))))))
  281. (defun vc-hg-expanded-log-entry (revision)
  282. (with-temp-buffer
  283. (vc-hg-command t nil nil "log" "-r" revision)
  284. (goto-char (point-min))
  285. (unless (eobp)
  286. ;; Indent the expanded log entry.
  287. (indent-region (point-min) (point-max) 2)
  288. (goto-char (point-max))
  289. (buffer-string))))
  290. (defun vc-hg-revision-table (files)
  291. (let ((default-directory (file-name-directory (car files))))
  292. (with-temp-buffer
  293. (vc-hg-command t nil files "log" "--template" "{rev} ")
  294. (split-string
  295. (buffer-substring-no-properties (point-min) (point-max))))))
  296. ;; Modeled after the similar function in vc-cvs.el
  297. (defun vc-hg-revision-completion-table (files)
  298. (lexical-let ((files files)
  299. table)
  300. (setq table (lazy-completion-table
  301. table (lambda () (vc-hg-revision-table files))))
  302. table))
  303. (defun vc-hg-annotate-command (file buffer &optional revision)
  304. "Execute \"hg annotate\" on FILE, inserting the contents in BUFFER.
  305. Optional arg REVISION is a revision to annotate from."
  306. (vc-hg-command buffer 0 file "annotate" "-d" "-n" "--follow"
  307. (when revision (concat "-r" revision))))
  308. (declare-function vc-annotate-convert-time "vc-annotate" (time))
  309. ;; The format for one line output by "hg annotate -d -n" looks like this:
  310. ;;215 Wed Jun 20 21:22:58 2007 -0700: CONTENTS
  311. ;; i.e: VERSION_NUMBER DATE: CONTENTS
  312. ;; If the user has set the "--follow" option, the output looks like:
  313. ;;215 Wed Jun 20 21:22:58 2007 -0700 foo.c: CONTENTS
  314. ;; i.e. VERSION_NUMBER DATE FILENAME: CONTENTS
  315. (defconst vc-hg-annotate-re
  316. "^[ \t]*\\([0-9]+\\) \\(.\\{30\\}\\)\\(?:\\(: \\)\\|\\(?: +\\(.+\\): \\)\\)")
  317. (defun vc-hg-annotate-time ()
  318. (when (looking-at vc-hg-annotate-re)
  319. (goto-char (match-end 0))
  320. (vc-annotate-convert-time
  321. (date-to-time (match-string-no-properties 2)))))
  322. (defun vc-hg-annotate-extract-revision-at-line ()
  323. (save-excursion
  324. (beginning-of-line)
  325. (when (looking-at vc-hg-annotate-re)
  326. (if (match-beginning 3)
  327. (match-string-no-properties 1)
  328. (cons (match-string-no-properties 1)
  329. (expand-file-name (match-string-no-properties 4)
  330. (vc-hg-root default-directory)))))))
  331. (defun vc-hg-previous-revision (file rev)
  332. (let ((newrev (1- (string-to-number rev))))
  333. (when (>= newrev 0)
  334. (number-to-string newrev))))
  335. (defun vc-hg-next-revision (file rev)
  336. (let ((newrev (1+ (string-to-number rev)))
  337. (tip-revision
  338. (with-temp-buffer
  339. (vc-hg-command t 0 nil "tip")
  340. (goto-char (point-min))
  341. (re-search-forward "^changeset:[ \t]*\\([0-9]+\\):")
  342. (string-to-number (match-string-no-properties 1)))))
  343. ;; We don't want to exceed the maximum possible revision number, ie
  344. ;; the tip revision.
  345. (when (<= newrev tip-revision)
  346. (number-to-string newrev))))
  347. ;; Modeled after the similar function in vc-bzr.el
  348. (defun vc-hg-delete-file (file)
  349. "Delete FILE and delete it in the hg repository."
  350. (condition-case ()
  351. (delete-file file)
  352. (file-error nil))
  353. (vc-hg-command nil 0 file "remove" "--after" "--force"))
  354. ;; Modeled after the similar function in vc-bzr.el
  355. (defun vc-hg-rename-file (old new)
  356. "Rename file from OLD to NEW using `hg mv'."
  357. (vc-hg-command nil 0 new "mv" old))
  358. (defun vc-hg-register (files &optional rev comment)
  359. "Register FILES under hg.
  360. REV is ignored.
  361. COMMENT is ignored."
  362. (vc-hg-command nil 0 files "add"))
  363. (defun vc-hg-create-repo ()
  364. "Create a new Mercurial repository."
  365. (vc-hg-command nil 0 nil "init"))
  366. (defalias 'vc-hg-responsible-p 'vc-hg-root)
  367. ;; Modeled after the similar function in vc-bzr.el
  368. (defun vc-hg-could-register (file)
  369. "Return non-nil if FILE could be registered under hg."
  370. (and (vc-hg-responsible-p file) ; shortcut
  371. (condition-case ()
  372. (with-temp-buffer
  373. (vc-hg-command t nil file "add" "--dry-run"))
  374. ;; The command succeeds with no output if file is
  375. ;; registered.
  376. (error))))
  377. ;; FIXME: This would remove the file. Is that correct?
  378. ;; (defun vc-hg-unregister (file)
  379. ;; "Unregister FILE from hg."
  380. ;; (vc-hg-command nil nil file "remove"))
  381. (declare-function log-edit-extract-headers "log-edit" (headers string))
  382. (defun vc-hg-checkin (files rev comment)
  383. "Hg-specific version of `vc-backend-checkin'.
  384. REV is ignored."
  385. (apply 'vc-hg-command nil 0 files
  386. (nconc (list "commit" "-m")
  387. (log-edit-extract-headers '(("Author" . "--user")
  388. ("Date" . "--date"))
  389. comment))))
  390. (defun vc-hg-find-revision (file rev buffer)
  391. (let ((coding-system-for-read 'binary)
  392. (coding-system-for-write 'binary))
  393. (if rev
  394. (vc-hg-command buffer 0 file "cat" "-r" rev)
  395. (vc-hg-command buffer 0 file "cat"))))
  396. ;; Modeled after the similar function in vc-bzr.el
  397. (defun vc-hg-checkout (file &optional editable rev)
  398. "Retrieve a revision of FILE.
  399. EDITABLE is ignored.
  400. REV is the revision to check out into WORKFILE."
  401. (let ((coding-system-for-read 'binary)
  402. (coding-system-for-write 'binary))
  403. (with-current-buffer (or (get-file-buffer file) (current-buffer))
  404. (if rev
  405. (vc-hg-command t 0 file "cat" "-r" rev)
  406. (vc-hg-command t 0 file "cat")))))
  407. ;; Modeled after the similar function in vc-bzr.el
  408. (defun vc-hg-workfile-unchanged-p (file)
  409. (eq 'up-to-date (vc-hg-state file)))
  410. ;; Modeled after the similar function in vc-bzr.el
  411. (defun vc-hg-revert (file &optional contents-done)
  412. (unless contents-done
  413. (with-temp-buffer (vc-hg-command t 0 file "revert"))))
  414. ;;; Hg specific functionality.
  415. (defvar vc-hg-extra-menu-map
  416. (let ((map (make-sparse-keymap)))
  417. map))
  418. (defun vc-hg-extra-menu () vc-hg-extra-menu-map)
  419. (defun vc-hg-extra-status-menu () vc-hg-extra-menu-map)
  420. (defvar log-view-vc-backend)
  421. (defstruct (vc-hg-extra-fileinfo
  422. (:copier nil)
  423. (:constructor vc-hg-create-extra-fileinfo (rename-state extra-name))
  424. (:conc-name vc-hg-extra-fileinfo->))
  425. rename-state ;; rename or copy state
  426. extra-name) ;; original name for copies and rename targets, new name for
  427. (declare-function vc-default-dir-printer "vc-dir" (backend fileentry))
  428. (defun vc-hg-dir-printer (info)
  429. "Pretty-printer for the vc-dir-fileinfo structure."
  430. (let ((extra (vc-dir-fileinfo->extra info)))
  431. (vc-default-dir-printer 'Hg info)
  432. (when extra
  433. (insert (propertize
  434. (format " (%s %s)"
  435. (case (vc-hg-extra-fileinfo->rename-state extra)
  436. (copied "copied from")
  437. (renamed-from "renamed from")
  438. (renamed-to "renamed to"))
  439. (vc-hg-extra-fileinfo->extra-name extra))
  440. 'face 'font-lock-comment-face)))))
  441. (defun vc-hg-after-dir-status (update-function)
  442. (let ((status-char nil)
  443. (file nil)
  444. (translation '((?= . up-to-date)
  445. (?C . up-to-date)
  446. (?A . added)
  447. (?R . removed)
  448. (?M . edited)
  449. (?I . ignored)
  450. (?! . missing)
  451. (? . copy-rename-line)
  452. (?? . unregistered)))
  453. (translated nil)
  454. (result nil)
  455. (last-added nil)
  456. (last-line-copy nil))
  457. (goto-char (point-min))
  458. (while (not (eobp))
  459. (setq translated (cdr (assoc (char-after) translation)))
  460. (setq file
  461. (buffer-substring-no-properties (+ (point) 2)
  462. (line-end-position)))
  463. (cond ((not translated)
  464. (setq last-line-copy nil))
  465. ((eq translated 'up-to-date)
  466. (setq last-line-copy nil))
  467. ((eq translated 'copy-rename-line)
  468. ;; For copied files the output looks like this:
  469. ;; A COPIED_FILE_NAME
  470. ;; ORIGINAL_FILE_NAME
  471. (setf (nth 2 last-added)
  472. (vc-hg-create-extra-fileinfo 'copied file))
  473. (setq last-line-copy t))
  474. ((and last-line-copy (eq translated 'removed))
  475. ;; For renamed files the output looks like this:
  476. ;; A NEW_FILE_NAME
  477. ;; ORIGINAL_FILE_NAME
  478. ;; R ORIGINAL_FILE_NAME
  479. ;; We need to adjust the previous entry to not think it is a copy.
  480. (setf (vc-hg-extra-fileinfo->rename-state (nth 2 last-added))
  481. 'renamed-from)
  482. (push (list file translated
  483. (vc-hg-create-extra-fileinfo
  484. 'renamed-to (nth 0 last-added))) result)
  485. (setq last-line-copy nil))
  486. (t
  487. (setq last-added (list file translated nil))
  488. (push last-added result)
  489. (setq last-line-copy nil)))
  490. (forward-line))
  491. (funcall update-function result)))
  492. (defun vc-hg-dir-status (dir update-function)
  493. (vc-hg-command (current-buffer) 'async dir "status" "-C")
  494. (vc-exec-after
  495. `(vc-hg-after-dir-status (quote ,update-function))))
  496. (defun vc-hg-dir-status-files (dir files default-state update-function)
  497. (apply 'vc-hg-command (current-buffer) 'async dir "status" "-C" files)
  498. (vc-exec-after
  499. `(vc-hg-after-dir-status (quote ,update-function))))
  500. (defun vc-hg-dir-extra-header (name &rest commands)
  501. (concat (propertize name 'face 'font-lock-type-face)
  502. (propertize
  503. (with-temp-buffer
  504. (apply 'vc-hg-command (current-buffer) 0 nil commands)
  505. (buffer-substring-no-properties (point-min) (1- (point-max))))
  506. 'face 'font-lock-variable-name-face)))
  507. (defun vc-hg-dir-extra-headers (dir)
  508. "Generate extra status headers for a Mercurial tree."
  509. (let ((default-directory dir))
  510. (concat
  511. (vc-hg-dir-extra-header "Root : " "root") "\n"
  512. (vc-hg-dir-extra-header "Branch : " "id" "-b") "\n"
  513. (vc-hg-dir-extra-header "Tags : " "id" "-t") ; "\n"
  514. ;; these change after each commit
  515. ;; (vc-hg-dir-extra-header "Local num : " "id" "-n") "\n"
  516. ;; (vc-hg-dir-extra-header "Global id : " "id" "-i")
  517. )))
  518. (defun vc-hg-log-incoming (buffer remote-location)
  519. (vc-hg-command buffer 1 nil "incoming" "-n" (unless (string= remote-location "")
  520. remote-location)))
  521. (defun vc-hg-log-outgoing (buffer remote-location)
  522. (vc-hg-command buffer 1 nil "outgoing" "-n" (unless (string= remote-location "")
  523. remote-location)))
  524. (declare-function log-view-get-marked "log-view" ())
  525. ;; XXX maybe also add key bindings for these functions.
  526. (defun vc-hg-push ()
  527. (interactive)
  528. (let ((marked-list (log-view-get-marked)))
  529. (if marked-list
  530. (apply #'vc-hg-command
  531. nil 0 nil
  532. "push"
  533. (apply 'nconc
  534. (mapcar (lambda (arg) (list "-r" arg)) marked-list)))
  535. (error "No log entries selected for push"))))
  536. (defun vc-hg-pull (prompt)
  537. "Issue a Mercurial pull command.
  538. If called interactively with a set of marked Log View buffers,
  539. call \"hg pull -r REVS\" to pull in the specified revisions REVS.
  540. With a prefix argument or if PROMPT is non-nil, prompt for a
  541. specific Mercurial pull command. The default is \"hg pull -u\",
  542. which fetches changesets from the default remote repository and
  543. then attempts to update the working directory."
  544. (interactive "P")
  545. (let (marked-list)
  546. ;; The `vc-hg-pull' command existed before the `pull' VC action
  547. ;; was implemented. Keep it for backward compatibility.
  548. (if (and (called-interactively-p 'interactive)
  549. (setq marked-list (log-view-get-marked)))
  550. (apply #'vc-hg-command
  551. nil 0 nil
  552. "pull"
  553. (apply 'nconc
  554. (mapcar (lambda (arg) (list "-r" arg))
  555. marked-list)))
  556. (let* ((root (vc-hg-root default-directory))
  557. (buffer (format "*vc-hg : %s*" (expand-file-name root)))
  558. (command "pull")
  559. (hg-program vc-hg-program)
  560. ;; Fixme: before updating the working copy to the latest
  561. ;; state, should check if it's visiting an old revision.
  562. (args '("-u")))
  563. ;; If necessary, prompt for the exact command.
  564. (when prompt
  565. (setq args (split-string
  566. (read-shell-command "Run Hg (like this): "
  567. (format "%s pull -u" hg-program)
  568. 'vc-hg-history)
  569. " " t))
  570. (setq hg-program (car args)
  571. command (cadr args)
  572. args (cddr args)))
  573. (apply 'vc-do-async-command buffer root hg-program
  574. command args)
  575. (vc-set-async-update buffer)))))
  576. (defun vc-hg-merge-branch ()
  577. "Merge incoming changes into the current working directory.
  578. This runs the command \"hg merge\"."
  579. (let* ((root (vc-hg-root default-directory))
  580. (buffer (format "*vc-hg : %s*" (expand-file-name root))))
  581. (apply 'vc-do-async-command buffer root vc-hg-program '("merge"))
  582. (vc-set-async-update buffer)))
  583. ;;; Internal functions
  584. (defun vc-hg-command (buffer okstatus file-or-list &rest flags)
  585. "A wrapper around `vc-do-command' for use in vc-hg.el.
  586. This function differs from vc-do-command in that it invokes
  587. `vc-hg-program', and passes `vc-hg-global-switches' to it before FLAGS."
  588. (apply 'vc-do-command (or buffer "*vc*") okstatus vc-hg-program file-or-list
  589. (if (stringp vc-hg-global-switches)
  590. (cons vc-hg-global-switches flags)
  591. (append vc-hg-global-switches
  592. flags))))
  593. (defun vc-hg-root (file)
  594. (vc-find-root file ".hg"))
  595. (provide 'vc-hg)
  596. ;;; vc-hg.el ends here