vc-hg.el 25 KB

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