【问题标题】:Mercurial "hg status" and relative pathsMercurial“汞状态”和相对路径
【发布时间】:2009-05-07 07:36:47
【问题描述】:

我将 mercurial 和 git 用于不同的项目,并且都喜欢它们。我觉得 mercurial 有点烦人的是,“hg status”显示了相对于存储库根目录的路径,而不是当前目录(与 git 不同)。可以以某种方式调整这种行为吗?

【问题讨论】:

标签: mercurial


【解决方案1】:

要查看相对于当前目录的工作区状态,您始终可以使用“.”。 (单点)作为“hg status”的参数,即:

% hg root                   # root of the workspace
/work/foo

% pwd                       # current directory is <root>/src
/work/foo/src

% hg status                 # no argument, see all files
M etc/foo.conf              # all files are shown with paths
M src/foosetup.c            # relative to workspace root
%

当你明确询问当前工作目录时的不同之处在于相对文件名路径使用它作为它们的起点:

% hg status .               # see only files under current path
M foosetup.c
%

【讨论】:

  • 不幸的是,与公认的解决方案不完全相同,更受限制。因此,并非普遍更好和更清洁。 “汞状况。”仅使用相对路径报告 . 下面的子树的状态。 “hg status $(hg root)”使用相对于 CWD 的路径报告整个 repo 的状态。因为很难在评论中展示这一点,所以我正在添加一个带有示例的新答案。
【解决方案2】:

通常的解决方法是运行:

hg status $(hg root)

对于 1.7 之前的旧版 Mercurial,您可以使用此 hack,将其添加到您的存储库的“.hg/hgrc”文件中:

[alias]
 sst = status /path/to/root

这需要启用别名扩展,因此您可能需要将“alias=”添加到您的 ~/.hgrc 文件中。

从 Mercurial 1.7 开始,别名扩展了解了“!”转义以使用 shell 命令,因此您现在可以拥有一个全局别名来执行此操作:

[alias]
sst = !hg status $($HG root) $HG_ARGS

不要使用st = !hg status $(hg root),因为这会创建一个无限循环,一遍又一遍地运行 hg status。它看起来像别名解析中的一个错误 - 如果您想别名 hg status 以显示从根目录开始的路径,那么以下咒语在全局 $HOME/.hgrc 中有效:

[alias]
__mystatus = status
st = !hg __mystatus $($HG root) $HG_ARGS

【讨论】:

  • 谢谢!很遗憾,别名分机。目前不允许使用 shell 命令,所以我打算将“hg status $(hg root)”设为 bash 别名。
  • 您的最后一个别名示例不会将任何选项传递给状态(如 --quiet)。最后一行应该是st = !hg __mystatus $($HG root) $HG_ARGS
  • @krupan 谢谢!我已经用那个提示更新了答案。我很少使用状态参数,所以我没有遇到问题。
  • 请看@muxator 的回答——干净多了!
【解决方案3】:

自旧的 mercurial 版本(2008 年的>= 1.1)以来的标准工作方式是:

hg status re:

产生:

M ../contrib/buildrpm
M ../hg
M httprepo.py

从 mercurial 1.3 (2009) 开始,您可以根据需要定义 alias

[alias]
sst = status re:

这种行为最终记录在 3.4(2015 年)中:

$ hg help status --verbose
[...]
- show changes in the working directory relative to the current directory
  (see 'hg help patterns' for more information):

    hg status re:

自 mercurial 4.2(2017 年 5 月)以来,您可以告诉状态命令始终打印相对路径,并将其放入您的 hgrc

[commands]
status.relative = True

直接在命令行上测试(hg >= 4.2):

$ hg --config commands.status.relative=True status
M ../contrib/buildrpm
M ../hg
M httprepo.py

最后,mercurial 4.3 引入了ui.tweakdefaults,其中包括将一些默认设置更改为更现代的:

[ui]
# hg status prints relative paths
# hg diff produces patches in git format
tweakdefaults = True

如果您使用的是现代 mercurial (>=4.3),这是官方推荐的方式。

【讨论】:

  • 这是正确的答案。其他答案是过时的解决方法。
  • 我认为 muxator 出现的时间很晚——但 IMO 的答案要好得多。
【解决方案4】:

随着时间的推移,汞会越来越好: 使用 hg 2.2.3,我可以定义 st 别名。

[alias]
st = !hg status $($HG root) $HG_ARGS

因此:

  • hg st 将为您提供相对于当前目录的路径
  • hg status 将为您提供相对于 hg 根目录的路径

【讨论】:

    【解决方案5】:

    我添加这个答案不是因为它比公认的答案更好,而是因为它澄清了“hg status”之间的区别。和“汞状态 $(汞根)”。这可能会让一些评论者感到困惑 - 更糟糕的是,这可能会导致忘记检查必要的东西。

    “汞状态。”仅使用相对路径报告 . 下面的子树的状态。

    "hg status $(hg root)" 使用相对于 CWD 的路径报告整个 repo 的状态。

    两者都有用。

    (一般来说,“hg status path”显示路径下子树的状态(如果 path = $(hg root),则整个 repo”,但相对于 CWD。(我必须承认我觉得这很混乱,因为有发生了两件事:获取状态的子树和显示相对路径的 cwd。))

    下面嵌入的 shell 会话示例显示了这一点。

    $ bash [~/hack] 562 $> mcd hg-test
    ./hg-test
    $ bash [~/hack/hg-test] 563 $> hg init .
    $ bash [~/hack/hg-test] 564 $> mkdir subdir
    $ bash [~/hack/hg-test] 565 $> touch foo
    $ bash [~/hack/hg-test] 566 $> touch subdir/bar
    $ bash [~/hack/hg-test] 567 $> hg status
    ? foo
    ? subdir/bar
    $ bash [~/hack/hg-test] 552 $> hg status $(hg root)
    ? foo
    ? subdir/bar
    $ bash [~/hack/hg-test] 552 $> cd subdir
    ./subdir
    $ bash [~/hack/hg-test/subdir] 553 $> hg status
    ? foo
    ? subdir/bar
    $ bash [~/hack/hg-test/subdir] 553 $> hg status .
    ? bar
    $ bash [~/hack/hg-test/subdir] 513 $> hg status $(hg root)
    ? ../foo
    ? bar
    $ bash [~/hack/hg-test/subdir] 523 $> hg status
    ? foo
    ? subdir/bar
    

    因此,如果你想做一些事情,比如在本地子树中备份文件,而不签入,然后恢复(我在使用“hg lock”时经常需要这样做,因为我使用的 FrameMaker 文件不能在 hg 中进行差异或合并(或几乎没有):

    $ bash [~/hack/hg-test/subdir] 523 $> mkdir bak; hg status -n . | xargs cp --target-directory bak
    $ bash [~/hack/hg-test/subdir] 524 $> ls bak
    bar
    

    但是如果要备份树中所有按状态报告的文件

    $ bash [~/hack/hg-test/subdir] 528 $> mkdir bak-root; hg status -n $(hg root) | xargs cp --target-directory bak-root
    cp: will not overwrite just-created `bak-root/bar' with `bar'
    $ bash [~/hack/hg-test/subdir] 529 $> ls bak-root
    bar  foo
    

    顺便说一下,警告显示了文件名冲突的问题。我通常使用一个小工具,我必须添加一个 .bak 后缀或 xargs。但是这个例子就足够了。

    顺便说一句^2,我通常用“hg status -nm”来做这样的事情,但上面的例子就足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-15
      • 2013-07-14
      • 2010-12-17
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 2011-06-30
      • 1970-01-01
      相关资源
      最近更新 更多