4

When I use mercurial command (hg commit --amend) it always opens up the editor window. In git I can avoid this by git commit -a --amend -CHEAD, is there something equivalent for mercurial?

peak
  • 105,803
  • 17
  • 152
  • 177
A. K.
  • 34,395
  • 15
  • 52
  • 89

3 Answers3

5

Given that you have, and are using, hg commit --amend, the easiest way is to trick Mercurial into using an editor that does nothing, leaving the message file unchanged and hence usable:

$ hg --config ui.editor=true commit --amend
saved backup bundle to [long path redacted]

That is, Mercurial did open up an "editor window" (or editor command, anyway), it's just that this one—/bin/true—immediately said "everything worked" without touching the interactive terminal.


Note: if you have HGEDITOR set in your environment, this will actually override ui.editor:

def geteditor(self):
    '''return editor to use'''
    if pycompat.sysplatform == 'plan9':
        # vi is the MIPS instruction simulator on Plan 9. We
        # instead default to E to plumb commit messages to
        # avoid confusion.
        editor = 'E'
    else:
        editor = 'vi'
    return (encoding.environ.get("HGEDITOR") or
            self.config("ui", "editor") or
            encoding.environ.get("VISUAL") or
            encoding.environ.get("EDITOR", editor))

so:

HGEDITOR=true hg commit --amend

will do the same thing more reliably (and with a bit less typing), but it's not called out this way in the documentation, which says only that HGEDITOR is deprecated.

torek
  • 448,244
  • 59
  • 642
  • 775
  • For reasons which I cannot explain, this does not work for me on a Mac: `hg --config ui.editor=true commit --amend` produces /bin/sh: aqua: command not found abort: edit failed: aqua exited with status 127 Same thing with other variants, e.g. `hg --config ui.editor=/usr/bin/true commit --amend` ; `HGRCPATH= hg --config ui.editor=/usr/bin/true commit --amend` – peak Dec 17 '18 at 10:40
  • @peak: Something is clearly overriding `ui.editor`. It appears (from the source) that `HGEDITOR` takes priority over the `ui.config` setting, despite this being documented as deprecated. – torek Dec 17 '18 at 11:17
  • Thanks for the explanation. – peak Dec 17 '18 at 11:28
5

There is an experimental extension that was added to mercurial 4.3.1 called 'amend'. It performs the same action as commit --amend with the exception of not opening an edit window for the comments.

hg amend

It is marked as experimental, but I use it fairly often without any problems.

Also there is an unamend command that will 'undo the most recent amend operation on a current changeset'.

peak
  • 105,803
  • 17
  • 152
  • 177
Craig
  • 756
  • 3
  • 8
3

Although hg amend is still marked experimental, I've never run into any problems using it. However, it is worth noting that when using hg commit, one can always avoid the editor window by specifying a message. In the case of an amendment, it might make sense to retain the previous message, which can be done in several ways, but two robust methods are illustrated by the following:

hg commit --amend -m "$(hg log -l 1 --template '{desc}')" --date now

hg commit --amend -m "$(cat $(hg root)/.hg/last-message.txt)" --date now

For practical purposes, however, if your platform supports it, using HGEDITOR would be simpler:

HGEDITOR=true hg commit --amend --date now
peak
  • 105,803
  • 17
  • 152
  • 177
  • Also worth noting: there's an extension called "evolve" that adds `hg amend` and uses the evolve feature's obsolescence markers to provide a safe way to edit history. This is a nice feature set but it still has not yet been put into any official Mercurial distribution. – torek Dec 17 '18 at 11:40