2019-04-17 11:49:10 +08:00
|
|
|
|
[#]: collector: (lujun9972)
|
2019-04-19 08:44:27 +08:00
|
|
|
|
[#]: translator: (geekpi)
|
2019-04-17 11:49:10 +08:00
|
|
|
|
[#]: reviewer: ( )
|
|
|
|
|
[#]: publisher: ( )
|
|
|
|
|
[#]: url: ( )
|
|
|
|
|
[#]: subject: (Getting started with Mercurial for version control)
|
|
|
|
|
[#]: via: (https://opensource.com/article/19/4/getting-started-mercurial)
|
2019-04-20 21:29:54 +08:00
|
|
|
|
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
开始使用 Mercurial 进行版本控制
|
2019-04-17 11:49:10 +08:00
|
|
|
|
======
|
2019-04-23 08:56:10 +08:00
|
|
|
|
了解 Mercurial 的基础知识,它是一个用 Python 写的分布式版本控制系统。
|
|
|
|
|
|
2019-04-17 11:49:10 +08:00
|
|
|
|
![][1]
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
[Mercurial][2] 是一个用 Python 编写的分布式版本控制系统。因为它是用高级语言编写的,所以你可以用 Python 函数编写一个 Mercurial 扩展。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
在[官方文档中][3]说明了几种安装 Mercurial 的方法。我最喜欢的一种方法不在里面:使用 **pip**。这是开发本地扩展的最合适方式!
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
目前,Mercurial 仅支持 Python 2.7,因此你需要创建一个 Python 2.7 虚拟环境:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
python2 -m virtualenv mercurial-env
|
|
|
|
|
./mercurial-env/bin/pip install mercurial
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
为了有一个简短的命令,以及满足人们对化学幽默的无法满足的需求,命令称之为 **hg**。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ 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)$
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
由于还没有任何文件,因此状态为空。添加几个文件:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
(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][4]>
|
|
|
|
|
date: Fri Mar 29 12:42:43 2019 -0700
|
|
|
|
|
summary: Adding stuff
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
**addremove** 命令很有用:它将任何未被忽略的新文件添加到托管文件列表中,并移除任何已删除的文件。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
如我所说,Mercurial 扩展用 Python 写成,它们只是常规的 Python 模块。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
这是一个简短的 Mercurial 扩展示例:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
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")
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
一个简单的测试方法是将它手动加入虚拟环境中的文件中:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
`$ vi ../mercurial-env/lib/python2.7/site-packages/hello_ext.py`
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
然后你需要_启用_扩展。你可以仅在当前仓库中启用它开始:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
$ cat >> .hg/hgrc
|
|
|
|
|
[extensions]
|
|
|
|
|
hello_ext =
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
现在,问候有了:
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
(mercurial-env)$ hg say-hello --whom world
|
|
|
|
|
hello world
|
|
|
|
|
```
|
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
大多数扩展会做更多有用的东西,甚至可能与 Mercurial 有关。 **repo** 对象是 **mercurial.hg.repository** 的对象。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
2019-04-23 08:56:10 +08:00
|
|
|
|
有关 Mercurial API 的更多信息,请参阅[官方文档][5]。并访问[官方仓库][6]获取更多示例和灵感。
|
2019-04-17 11:49:10 +08:00
|
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
via: https://opensource.com/article/19/4/getting-started-mercurial
|
|
|
|
|
|
|
|
|
|
作者:[Moshe Zadka (Community Moderator)][a]
|
|
|
|
|
选题:[lujun9972][b]
|
2019-04-23 08:56:10 +08:00
|
|
|
|
译者:[geekpi](https://github.com/geekpi)
|
2019-04-17 11:49:10 +08:00
|
|
|
|
校对:[校对者ID](https://github.com/校对者ID)
|
|
|
|
|
|
|
|
|
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
|
|
|
|
|
|
|
|
[a]: https://opensource.com/users/moshez
|
|
|
|
|
[b]: https://github.com/lujun9972
|
|
|
|
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/rh_003499_01_cloud21x_cc.png?itok=5UwC92dO
|
|
|
|
|
[2]: https://www.mercurial-scm.org/
|
|
|
|
|
[3]: https://www.mercurial-scm.org/wiki/UnixInstall
|
|
|
|
|
[4]: mailto:moshez@zadka.club
|
|
|
|
|
[5]: https://www.mercurial-scm.org/wiki/MercurialApi#Repositories
|
|
|
|
|
[6]: https://www.mercurial-scm.org/repo/hg/file/tip/hgext
|