Last edit

Summary: Removed section on breaking hardlinks in clones since it is incorrect and not needed. Mercurial *wont* hardlink files in the working copy, it only hardlinks (binary) files inside the .hg/store/ directory. You cannot edit the binary files in .hg/store/ with Emacs in a meaningful way.


< == Breaking hardlinks in local clones ==
< When you create clones of local Mercurial repositories, Mercurial will try to create hardlinks
< to save space [ HardlinkedClones]. This works
< fine as long as Emacs is setup to break links. The problem I have is that some hardlinks
< on my system need to stay hardlinked, and I cannot globally set 'break-hardlink-on-save to t.
< This hook below, gets around the problem by ask what to do when saving a file that has hardlinks.
< (defun ask-to-break-hardlink ()
< (if (and (file-exists-p buffer-file-name) (> (file-nlinks buffer-file-name) 1))
< (if (yes-or-no-p
< (format "File %s is has more than one hardlink; Break the hardlink? "
< (file-name-nondirectory
< buffer-file-name)))
< (set (make-local-variable 'break-hardlink-on-save) t)
< (set (make-local-variable 'break-hardlink-on-save) nil)
< )
< )
< )
< (add-hook 'before-save-hook 'ask-to-break-hardlink)
< This is a different way to force breaking of hardlinks in Mercurial repositories, it forces break-hardlink-on-save to t when vc recognizes the file:
< (defadvice vc-hg-registered (after vc-hg-registered-break-hardlink activate)
< "Break hardlinks when editing in a Mercurial repository"
< (if ad-return-value
< (progn
< (set (make-local-variable 'break-hardlink-on-save) t)
< ))
< ad-return-value)

Mercurial (aka hg) is a distributed version control system much like Git, Bazaar and DaRcs.

Mercurial is available from


vc-hg-push and vc-hg-pull are broken in vc-hg.el (emacs23). vc-hg-command has to be called via an apply.

Using diff-mode for MQ patches

Mercurial’s MQ extension uses patch files which are more convenient to read with DiffMode. diff-mode can not, however, guess the correct base directory, so applying and reverting hunks won’t work out of the box. Here’s a hook that fixes the problem:

  (add-to-list 'auto-mode-alist '("\\.hg/patches\\(?:-[^/]+\\)?/" . diff-mode))
  (defun mq-patch-set-default-directory ()
    (when (string= ".hg" (nth 2 (reverse (split-string default-directory "/"))))
      (setq default-directory (expand-file-name (concat default-directory "../../")))))
  (add-hook 'diff-mode-hook 'mq-patch-set-default-directory)

Emacs from Mercurial

see EmacsFromMercurial