59

How can I specify a multiline commit message for mercurial on the command line?

hg commit -m "* add foo\n* fix bar"

does not work. The log shows:

changeset:   13:f2c6526e5911
tag:         tip
date:        Fri Jan 23 23:22:36 2009 +0100
files:       foobar.cpp
description:
    * add foo\n*fix bar
dbr
  • 165,801
  • 69
  • 278
  • 343
Martin
  • 12,408
  • 6
  • 34
  • 30
  • I guess this has to be solved through the command shell itself. If your command shell can give you a way to supply some multi-line text as a single parameter (maybe by having a '\' at the end of the line), then HG shold have no problem with it... – Hosam Aly Jan 24 '09 at 12:44
  • Yes, I guess you are right. However, this is the only usecase for me personally, where I would use it on a regular basis, that's why I didn't ask it as a bash-question. – Martin Jan 24 '09 at 12:50

7 Answers7

84

Mercurial: multiline commit message on the command line?

Hit enter.

$ hg commit -m "Did some work
> Now I'm done"

One of the things is that only the first line shows up in hg log:

$ hg log
changeset:   0:d2dc019450a2
tag:         tip
user:        Aaron Maenpaa <zacherates@gmail.com>
date:        Sat Jan 24 07:46:23 2009 -0500
summary:     Did some work

... but if you fire up "hg view" you can see that the whole message is there.

Edited to add:

... but hg -v log shows the whole message:

$ hg -v log
changeset:   0:d2dc019450a2
tag:         tip
user:        Aaron Maenpaa <zacherates@gmail.com>
date:        Sat Jan 24 07:46:23 2009 -0500
files:       work
description:
Did some work
Now I'm done
Aaron Maenpaa
  • 119,832
  • 11
  • 95
  • 108
  • 5
    The "Enter" behavior is dependant on UNIX-like shells. Is there a way to do it with Windows CMD? – Hosam Aly Jan 24 '09 at 12:52
  • @Hosam Other than installing cygwin (or mingwin) and using bash I can't think of anything. – Aaron Maenpaa Jan 24 '09 at 13:02
  • @Hosam Aly: Can you use \n or \r\n or \r? I'm not sure if the shellexpands it, but Mercurial might. – Lucas Jones Jan 24 '09 at 13:40
  • @person-b: The shell does not expand it AFAIK, and I don't have Mercurial to check it. But @Jason has a good answer below. – Hosam Aly Jan 25 '09 at 07:12
  • 3
    Thanks! `hg help log` doesn't have the `v` flag (at-least not for me). Full summary is exactly what I was looking for :) – Justin Jul 09 '12 at 14:33
  • FYI: Even all *NIX shells won't work. My fav, 'tcsh', gives "Unmatched '." I temporarily switch to bash when I need the behavior described in the solution above. – JS. Oct 26 '12 at 17:43
  • @Justin: full help including global options is hidden behind `hg -v help log`. – Robert Siemer May 07 '16 at 12:58
17

From Windows cmd, do this command for a multiline commit.

hg commit -l con

This allows you to enter a multiple line commit message straight from the command line. To end your message, hit Enter, and on a line by itself hit Ctrl + Z and Enter again.

Why? The -l option to hg commit says to read the commit message from a file, and con specifies that the file is actually the console.

Cody Piersall
  • 8,312
  • 2
  • 43
  • 57
  • 1
    Still looking find a way in the docs to do this for the "hg tag" command because `-l` is `--local` and not "--logfile" (like it is for hg commit). – zanlok Aug 17 '15 at 18:42
9

If you're doing it interactively (vs. from a script), just do hg commit without the -m flag. I'm not sure what the behavior is on Linux or Mac, but on Windows it pops up Notepad with a file that you fill out and save for a multiline message.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • Is there some way to save and close notepad all in one keystroke? That's what annoys me about this approach. – Doug McClean Oct 09 '09 at 23:57
  • 2
    I think there's a way (via an .ini file) to tell Mercurial to call some other application rather than Notepad, then you just need to find your favorite editor. – Jason S Oct 10 '09 at 21:42
  • 1
    It uses the editor specified in the hgrc. On Windows, it defaults to notepad if none is specified. – Clayton Hughes Mar 22 '12 at 00:09
  • @DougMcClean I just go `Alt, F, S, Alt, F, X`. It's 6 key presses, but you can do it fairly fast because of the pattern. (Note that I don't mean `Alt+F, S, Alt+F, X` which seems harder to type.) – Dave Cousineau May 07 '13 at 19:57
5

I know, there is already a solution for linux users, but I needed another solution for windows command line, so I was looking for it...

And I found one: https://www.mercurial-scm.org/pipermail/mercurial/2011-November/040855.html

hg commit -l filename.txt

I hope, it's useful for someone out there ,)

[EDIT] oO - it has already been added to the help

-l --logfile FILE read commit message from file

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
kapsiR
  • 2,720
  • 28
  • 36
2

In bash (since version 2.0):

hg commit -m $'foo\nbar'

(I.e. put a dollar sign before an opening single quote to make it parse escape sequences (like \n) within the string — note that it doesn't work with double quotes.)

Sasha
  • 3,599
  • 1
  • 31
  • 52
2

Here's another way that is more close to what you tried at first:

hg commit -m "$(echo -e 'foo\nbar')"
Leslie P. Polzer
  • 2,998
  • 21
  • 18
1

For Windows with PowerShell:

 hg commit --message "foo`nbar"
CodeFox
  • 3,321
  • 1
  • 29
  • 41