Mercurial,定义我自己的模板/关键字 : hostname

标签 mercurial keyword

我需要一个 Mercurail 模板/关键字“主机名”来获取存储库所在计算机的名称(或 IP)。据我阅读wiki,即“第11章。自定义Mercurial的输出”,hg帮助模板和网络,我认为它应该类似于date关键字,动态扩展。我如何定义自己的模板/关键字?

非常感谢

最佳答案

以下扩展应该可以解决问题:

from mercurial import templatekw

testedwith = "3.5"

_ipname = None

def showipname(repo, ctx, templ, **args):
  """:ipname: String. The hostname of the machine that the repository
  resides on."""
  import socket
  global _ipname
  if not _ipname:
    _ipname = socket.gethostname()
  return _ipname

def uisetup(ui):
  templatekw.keywords["ipname"] = showipname

然后使用(比如说):

hg log -r . -T '{node|short} at {ipname}\n'

请注意,socket.gethostname() 的值可能取决于您的互联网连接。如果您需要 /etc/hostname 中的值或标识您的计算机的其他内容,请改用该方法。

要使用扩展,请将其放入文件中,例如 ipnametempl.py 某处,然后将以下行添加到 .hgrc 中:

[extensions]
ipnametempl=/path/to/ipnametempl.py

这可以是您的用户/全局 hgrc 或存储库中的 .hg/hgrc (如果您只想为特定存储库启用它,则为后者) 。请参阅 hg help hgrc 以了解用户/全局 hgrc 文件在您系统上的位置。

关于Mercurial,定义我自己的模板/关键字 : hostname,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33544656/

相关文章:

javascript - 'true' 和 'false' 和 'null' 是什么?

version-control - 到 "backport"的工作流程更改为不同的 Mercurial (Hg) 分支?

svn - 使用 Mercurial 而不是 Subversion 的优缺点

mercurial - HG : How to push some (but not all) changes to 'stable' branch?

c -开关 "unknown"案例

c++ - 在 C++ 中,为什么 "assert"不是关键字?

c - C 结构中的私有(private)关键字

visual-studio - 我应该如何在 Mercurial 存储库中设置我的 Visual Studio 项目/解决方案?

android - 带锁文件的 Mercurial 替代方案

java - lucene 索引中的单词重要性