Mercurial 版本控制入门

了解用 Python 编写的分布式版本控制系统 Mercurial 的基础知识。
151 位读者喜欢这个。
5 tools to support distributed sysadmin teams

Opensource.com

Mercurial 是一个用 Python 编写的分布式版本控制系统。因为它使用高级语言编写,所以您可以使用几个 Python 函数编写 Mercurial 扩展。

有几种安装 Mercurial 的方法,在官方文档中对此进行了解释。我最喜欢的一种方法不在那里:使用 pip。这是开发本地扩展最方便的方法!

目前,Mercurial 仅支持 Python 2.7,因此您需要创建一个 Python 2.7 虚拟环境

python2 -m virtualenv mercurial-env
./mercurial-env/bin/pip install mercurial

为了有一个简短的命令,并满足每个人对基于化学的幽默的永不满足的需求,该命令称为 hg

$ source mercurial-env/bin/activate
(mercurial-env)$ mkdir test-dir
(mercurial-env)$ cd test-dir
(mercurial-env)$ hg init
(mercurial-env)$ hg status
(mercurial-env)$

状态为空,因为您没有任何文件。添加几个文件

(mercurial-env)$ echo 1 > one
(mercurial-env)$ echo 2 > two
(mercurial-env)$ hg status
? one
? two
(mercurial-env)$ hg addremove
adding one
adding two
(mercurial-env)$ hg commit -m 'Adding stuff'
(mercurial-env)$ hg log 
changeset:   0:1f1befb5d1e9
tag:         tip
user:        Moshe Zadka <moshez@zadka.club>
date:        Fri Mar 29 12:42:43 2019 -0700
summary:     Adding stuff

addremove 命令很有用:它将任何未被忽略的新文件添加到托管文件列表中,并删除任何已删除的文件。

正如我提到的,Mercurial 扩展是用 Python 编写的——它们只是普通的 Python 模块。

这是一个简短的 Mercurial 扩展示例

from mercurial import registrar
from mercurial.i18n import _

cmdtable = {}
command = registrar.command(cmdtable)

@command('say-hello',
    [('w', 'whom', '', _('Whom to greet'))])
def say_hello(ui, repo, **opts):
    ui.write("hello ", opts['whom'], "\n")

测试它的一个简单方法是手动将其放在虚拟环境中的文件中

$ vi ../mercurial-env/lib/python2.7/site-packages/hello_ext.py

然后您需要启用扩展。您可以先仅在当前存储库中启用它

$ cat >> .hg/hgrc
[extensions]
hello_ext =

现在,可以进行问候了

(mercurial-env)$ hg say-hello --whom world
hello world

大多数扩展将执行更有用的操作——甚至可能与 Mercurial 相关。repo 对象是一个 mercurial.hg.repository 对象。

有关 Mercurial API 的更多信息,请参阅官方文档。并访问官方 repo 以获取更多示例和灵感。

标签
Moshe sitting down, head slightly to the side. His t-shirt has Guardians of the Galaxy silhoutes against a background of sound visualization bars.
自 1998 年以来,Moshe 一直参与 Linux 社区,在 Linux“安装聚会”中提供帮助。自 1999 年以来,他一直在编写 Python 程序,并为核心 Python 解释器做出了贡献。Moshe 在那些术语存在之前就一直是 DevOps/SRE,他非常关心软件可靠性、构建可重现性以及其他此类事情。

评论已关闭。

Creative Commons License本作品根据 Creative Commons Attribution-Share Alike 4.0 International License 获得许可。
© . All rights reserved.