10

When I run hg commit, Mercurial generates a file for my commit message that looks like this :

HG: Enter commit message.  Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: Henri Wiechers <[email protected]>
HG: branch 'default'
HG: added a.txt

Is there a way to customize this file? I'd like to include if the working copy has any unknown files.

1

6 Answers 6

7

It's possible to specify this in the [committemplate] config section (see hg help config.committemplate):

 "committemplate"
----------------

"changeset"
    String: configuration in this section is used as the template to
    customize the text shown in the editor when committing.

In addition to pre-defined template keywords, commit log specific one
below can be used for customization:

"extramsg"
    String: Extra message (typically 'Leave message empty to abort
    commit.'). This may be changed by some commands or extensions.

For example, the template configuration below shows as same text as one
shown by default:

  [committemplate]
  changeset = {desc}\n\n
      HG: Enter commit message.  Lines beginning with 'HG:' are removed.
      HG: {extramsg}
      HG: --
      HG: user: {author}\n{ifeq(p2rev, "-1", "",
     "HG: branch merge\n")
     }HG: branch '{branch}'\n{if(activebookmark,
     "HG: bookmark '{activebookmark}'\n")   }{subrepos %
     "HG: subrepo {subrepo}\n"              }{file_adds %
     "HG: added {file}\n"                   }{file_mods %
     "HG: changed {file}\n"                 }{file_dels %
     "HG: removed {file}\n"                 }{if(files, "",
     "HG: no files changed\n")}

"diff()"
    String: show the diff (see 'hg help templates' for detail)
Sign up to request clarification or add additional context in comments.

Comments

3

There's no official way to do it w/o modifying mercurial itself (not terribly intimidating, it's very clean Python), but here's a way to do it by tweaking the editor setting the [ui] section of your ~/.hgrc:

editor = hg status --unknown >! /tmp/unknown_list ; /usr/bin/vim -c "r /tmp/unknown_list"

That is, of course vim on Linux specific, but the same could be done for any decent editor on any OS.

4 Comments

This doesn't really work, does it? You need to let the editor open the file generated by Mercurial -- editing /tmp/unknown_list is not enough. Also, beware of symlink attacks when you write to files in /tmp!
It does work. The editor opens the file created by mercurial and then -c "r /tmp/unknown_list" tells vim to read in the contents of that listing file at the top of the tmpfile mercurial created and vim loaded. You're right that I'd be using ~/tmp and a $$ in the real world though I'm usually on machines where I'm the only user.
When I try it, I see abort: /tmp/unknown_list not under root and a file called "!" is created instead. I've never seen the >! style of redirection, and it doesn't work in sh or bash on my system... was that a typo?
You can do the same thing much simpler with editor = /usr/bin/vim -c 'r !hg status --unknown', although for this to be useful in practice, you'd probably want to get fancier and put it at the bottom of the editor with "HG: " at the beginning: editor = /usr/bin/vim -c '$r !hg status --unknown | sed -e"s|^|HG: |"' -c "norm 1G" (the "norm 1G" is to get the cursor back up to the top).
2

I wanted to do this under windows. The idea of customising the editor setting in the ini/.hgrc file made me think of replacing the editor command with a command file.

e.g. if you set this in mercurial.ini:

[ui]
editor = c:\path\to\hgedit.cmd

then hg will call the command file and pass the name of the temp file on the command line. The temp file name can then be accessed in the command file using the %1 parameter.

hgedit.cmd could be something like:

@echo off
hg status --unknown>>%1
notepad %1

If you want to append the output of hg as comments you could do this:

@echo off
echo HG: -->>%1
echo HG: Unknown files:>>%1
for /f "tokens=*" %%a in ('hg st --unknown') do echo HG: %%a>>%1
notepad %1

(You don't have to use notepad of course.)

Comments

2

A Mac/Linux variant of Jim Eggleston's answer... I made a script called hg-commit-editor:

#!/bin/sh
hg status --unknown | sed -e 's|^|HG: |' >> $1
editor $1

and then set it as my commit editor in my hgrc:

[ui]
editor = hg-commit-editor

Comments

1

Use hg commit -m "My message here". You can also set up an editor in your Mercurial.ini or ~/.hgrc file. Add the following:

[ui]
editor = /path/to/your/favorite/editor

Comments

0

There are many ways to do this. Some are even listed on the official wiki. This expands on @Ry4an answer. You can add this to your ~/.hgrc

[ui]
editor = function hgvi { hg status --unknown | sed 's/^/HG: /g' >> "$1"; vi "$1"; }; hgvi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.