【问题标题】:How do I customize the 'commit message file' generated by `hg commit`?如何自定义“hg commit”生成的“提交消息文件”?
【发布时间】:2011-05-16 13:32:00
【问题描述】:

当我运行hg commit 时,Mercurial 会为我的提交消息生成一个文件,如下所示:

HG: Enter commit message.  Lines beginning with 'HG:' are removed.
HG: Leave message empty to abort commit.
HG: --
HG: user: Henri Wiechers <hwiechers@gmail.com>
HG: branch 'default'
HG: added a.txt

有没有办法自定义这个文件?如果工作副本有,我想包括 任何未知文件。

【问题讨论】:

标签: mercurial


【解决方案1】:

使用hg commit -m "My message here"。您还可以在 Mercurial.ini~/.hgrc 文件中设置编辑器。添加以下内容:

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

【讨论】:

    【解决方案2】:

    没有修改 mercurial 本身的官方方法(不是很吓人,它是非常干净的 Python),但这里有一种方法可以通过调整 editor 设置 [ui] 部分来做到这一点@987654323 @:

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

    这当然是特定于 Linux 的 vim,但对于任何操作系统上的任何体面的编辑器都可以这样做。

    【讨论】:

    • 这真的行不通,不是吗?您需要让编辑器打开 Mercurial 生成的文件 -- 编辑 /tmp/unknown_list 是不够的。此外,当您写入 /tmp! 中的文件时,请注意符号链接攻击!
    • 它确实有效。编辑器打开由 mercurial 创建的文件,然后 -c "r /tmp/unknown_list" 告诉 vim 读入 mercurial 创建并加载 vim 的 tmpfile 顶部的列表文件的内容。你说得对,我会在现实世界中使用 ~/tmp 和 $$,尽管我通常在我是唯一用户的机器上。
    • 当我尝试它时,我看到abort: /tmp/unknown_list not under root 和一个名为“!”的文件而是创建。我从未见过&gt;! 重定向样式,并且它在我的系统上的 sh 或 bash 中不起作用……这是一个错字吗?
    • 你可以用editor = /usr/bin/vim -c 'r !hg status --unknown'做同样的事情更简单,虽然为了在实践中有用,你可能想更漂亮,把它放在编辑器底部,用“HG :" 开头:editor = /usr/bin/vim -c '$r !hg status --unknown | sed -e"s|^|HG: |"' -c "norm 1G"(“norm 1G”是让光标回到顶部)。
    【解决方案3】:

    我想在 Windows 下执行此操作。在 ini/.hgrc 文件中自定义编辑器设置的想法让我想到了用命令文件替换编辑器命令。

    例如如果你在 mercurial.ini 中设置:

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

    然后 hg 将调用命令文件并在命令行上传递临时文件的名称。然后可以使用 %1 参数在命令文件中访问临时文件名。

    hgedit.cmd 可能类似于:

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

    如果您想将 hg 的输出附加为 cmets,您可以这样做:

    @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
    

    (当然你不必使用记事本。)

    【讨论】:

      【解决方案4】:

      Jim Eggleston 的答案的 Mac/Linux 变体...我制作了一个名为 hg-commit-editor 的脚本:

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

      然后在我的 hgrc 中将其设置为我的提交编辑器:

      [ui]
      editor = hg-commit-editor
      

      【讨论】:

        【解决方案5】:

        有很多方法可以做到这一点。有些甚至是listed on the official wiki。这扩展了@Ry4an 的答案。您可以将此添加到您的~/.hgrc

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

        【讨论】:

          【解决方案6】:

          可以在[committemplate] 配置部分中指定它(参见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)
          

          【讨论】:

            猜你喜欢
            • 2016-05-02
            • 2021-11-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-05-11
            • 1970-01-01
            • 2021-12-17
            相关资源
            最近更新 更多