使用 Graphlog

作者:Steve Losh
日期:2009-10-03

hg log 用起来一直感觉不错,可是如果碰上了分支的话那它可就令人抓狂了。这时候 Graphlog 能够帮到你。

hg log 是你最先学会的 Mercurial 命令之一,它能让你查看到一个代码库的所有提交的记录。这是一个它使用的输出示例:

$ hg log
changeset:   2:7b393734ccf7
tag:         tip
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:13 2009 -0400
summary:     Do some more work.

changeset:   1:26ac605206b4
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:08 2009 -0400
summary:     Do some work.

changeset:   0:ef4afb3d9699
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:00 2009 -0400
summary:     Initial commit.

$

还不错啊!如果你的变更呈一条直线没有分支的话那它还能工作良好,可一但出现分支的话那它便变得有些难以理解了。让我们来看看更多提交后的输出:

$ hg log
changeset:   8:b58f37a6f0af
tag:         tip
parent:      3:fee36cf81486
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:48:55 2009 -0400
summary:     Oh god the server is on fire fix it now.

changeset:   7:8f54b4d0d885
parent:      5:ce29b9e5288f
parent:      6:1dff6084cda4
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:48:20 2009 -0400
summary:     Go back to the old wording.

changeset:   6:1dff6084cda4
parent:      4:3c52ba0b77af
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:47:23 2009 -0400
summary:     Actually, the wording was fine.

changeset:   5:ce29b9e5288f
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:46:21 2009 -0400
summary:     Add some content.

changeset:   4:3c52ba0b77af
parent:      2:7b393734ccf7
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:46:04 2009 -0400
summary:     Change some wording around.

changeset:   3:fee36cf81486
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:45:50 2009 -0400
summary:     Fix a horrific bug.

changeset:   2:7b393734ccf7
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:13 2009 -0400
summary:     Do some more work.

changeset:   1:26ac605206b4
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:08 2009 -0400
summary:     Do some work.

changeset:   0:ef4afb3d9699
user:        Steve Losh <steve@stevelosh.com>
date:        Fri Oct 02 23:42:00 2009 -0400
summary:     Initial commit.

$

现在便不是那么容易的完整的看出后面几次变更的动作。这里至少有一次合并的存在 (因为变更集 7 拥有两个父节点) 所以一定存在一个分支。难道你仅瞄一眼就能看出其中奥妙,或是试图把它搞清楚直至你抓狂?

为了使记录更容易理解,你可以通过 编辑你的~/.hgrc文件 包含以下内容来开启 graphlog 扩展

[extensions]
graphlog =

现在你可以使用 hg glog 来查看一个包含了 ASII 字符画的记录,就像这样:

$ hg glog
@  changeset:   8:b58f37a6f0af
|  tag:         tip
|  parent:      3:fee36cf81486
|  user:        Steve Losh <steve@stevelosh.com>
|  date:        Fri Oct 02 23:48:55 2009 -0400
|  summary:     Oh god the server is on fire fix it now.
|
| o    changeset:   7:8f54b4d0d885
| |\   parent:      5:ce29b9e5288f
| | |  parent:      6:1dff6084cda4
| | |  user:        Steve Losh <steve@stevelosh.com>
| | |  date:        Fri Oct 02 23:48:20 2009 -0400
| | |  summary:     Go back to the old wording.
| | |
| | o  changeset:   6:1dff6084cda4
| | |  parent:      4:3c52ba0b77af
| | |  user:        Steve Losh <steve@stevelosh.com>
| | |  date:        Fri Oct 02 23:47:23 2009 -0400
| | |  summary:     Actually, the wording was fine.
| | |
| o |  changeset:   5:ce29b9e5288f
| |/   user:        Steve Losh <steve@stevelosh.com>
| |    date:        Fri Oct 02 23:46:21 2009 -0400
| |    summary:     Add some content.
| |
| o  changeset:   4:3c52ba0b77af
| |  parent:      2:7b393734ccf7
| |  user:        Steve Losh <steve@stevelosh.com>
| |  date:        Fri Oct 02 23:46:04 2009 -0400
| |  summary:     Change some wording around.
| |
o |  changeset:   3:fee36cf81486
|/   user:        Steve Losh <steve@stevelosh.com>
|    date:        Fri Oct 02 23:45:50 2009 -0400
|    summary:     Fix a horrific bug.
|
o  changeset:   2:7b393734ccf7
|  user:        Steve Losh <steve@stevelosh.com>
|  date:        Fri Oct 02 23:42:13 2009 -0400
|  summary:     Do some more work.
|
o  changeset:   1:26ac605206b4
|  user:        Steve Losh <steve@stevelosh.com>
|  date:        Fri Oct 02 23:42:08 2009 -0400
|  summary:     Do some work.
|
o  changeset:   0:ef4afb3d9699
   user:        Steve Losh <steve@stevelosh.com>
   date:        Fri Oct 02 23:42:00 2009 -0400
   summary:     Initial commit.

$

这样的话更容易看出哪里出现了分支了吧。

我个人的话喜欢 hg glog 的输出内容更简洁一些,因为通常我并不需要那么多的提交信息。我将我的 ~/.hgrc 文件修改包含如下内容:

[defaults]
glog = --template 'changeset:   {rev}:{node|short} {tags}\nsummary:     {desc|firstline|fill68|tabindent|tabindent}\n\n'

通过将这个模板设为默认,我的 hg glog 输出内容就像这样:

$ hg glog
@  changeset:   8:b58f37a6f0af tip
|  summary:     Oh god the server is on fire fix it now.
|
| o    changeset:   7:8f54b4d0d885
| |\   summary:     Go back to the old wording.
| | |
| | o  changeset:   6:1dff6084cda4
| | |  summary:     Actually, the wording was fine.
| | |
| o |  changeset:   5:ce29b9e5288f
| |/   summary:     Add some content.
| |
| o  changeset:   4:3c52ba0b77af
| |  summary:     Change some wording around.
| |
o |  changeset:   3:fee36cf81486
|/   summary:     Fix a horrific bug.
|
o  changeset:   2:7b393734ccf7
|  summary:     Do some more work.
|
o  changeset:   1:26ac605206b4
|  summary:     Do some work.
|
o  changeset:   0:ef4afb3d9699
   summary:     Initial commit.

$

对我来说,这更容易理解并且包含了所有我所需要的信息。假如对某个变更集我想了解更多的话,我会使用 hg log -r REV 或是 hg show REV