Mercurial 版本控制入门 | Linux 中国

2019 年 4 月 26 日 Linux中国
了解 Mercurial 的基础知识,它是一个用 Python 写的分布式版本控制系统。
-- Moshe Zadka

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

官方文档中[2]说明了几种安装 Mercurial 的方法。我最喜欢的一种方法不在里面:使用 pip。这是开发本地扩展的最合适方式!

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

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

为了让命令简短一些,以及满足人们对化学幽默的渴望,该命令称之为 hg

   
   
     
  1. $ source mercurial-env/bin/activate
  2. (mercurial-env)$ mkdir test-dir
  3. (mercurial-env)$ cd test-dir
  4. (mercurial-env)$ hg init
  5. (mercurial-env)$ hg status
  6. (mercurial-env)$

由于还没有任何文件,因此状态为空。添加几个文件:

   
   
     
  1. (mercurial-env)$ echo 1 > one
  2. (mercurial-env)$ echo 2 > two
  3. (mercurial-env)$ hg status
  4. ? one
  5. ? two
  6. (mercurial-env)$ hg addremove
  7. adding one
  8. adding two
  9. (mercurial-env)$ hg commit -m 'Adding stuff'
  10. (mercurial-env)$ hg log
  11. changeset: 0:1f1befb5d1e9
  12. tag: tip
  13. user: Moshe Zadka <[moshez@zadka.club][4]>
  14. date: Fri Mar 29 12:42:43 2019 -0700
  15. summary: Adding stuff

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

如我所说,Mercurial 扩展用 Python 写成,它们只是常规的 Python 模块。

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

   
   
     
  1. from mercurial import registrar
  2. from mercurial.i18n import _
  3. cmdtable = {}
  4. command = registrar.command(cmdtable)
  5. @command('say-hello',
  6. [('w', 'whom', '', _('Whom to greet'))])
  7. def say_hello(ui, repo, `opts):
  8. ui.write("hello ", opts['whom'], "\n")

简单的测试方法是将它手动加入虚拟环境中的文件中:

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

然后你需要启用扩展。你可以仅在当前仓库中启用它:

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

现在,问候有了:

   
   
     
  1. (mercurial-env)$ hg say-hello --whom world
  2. hello world

大多数扩展会做更多有用的东西,甚至可能与 Mercurial 有关。repo 对象是 mercurial.hg.repository 的对象。

有关 Mercurial API 的更多信息,请参阅官方文档[3]。并访问官方仓库[4]获取更多示例和灵感。


via: https://opensource.com/article/19/4/getting-started-mercurial

作者:Moshe Zadka[6] 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

登录查看更多
0

相关内容

Mercurial 是一个供开发者使用的跨平台的分布式版本控制工具,主要由 Python 实现,支持在 Windows 和类 Unix 系统(如 FreeBSD、Mac OS X 和 Linux)上使用。 mercurial.selenic.com
【2020新书】使用高级C# 提升你的编程技能,412页pdf
专知会员服务
56+阅读 · 2020年6月26日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【实用书】Python技术手册,第三版767页pdf
专知会员服务
229+阅读 · 2020年5月21日
Python导论,476页pdf,现代Python计算
专知会员服务
255+阅读 · 2020年5月17日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
MIT新书《强化学习与最优控制》
专知会员服务
272+阅读 · 2019年10月9日
Python 3.8.0来了!
数据派THU
5+阅读 · 2019年10月22日
用 Python 开发 Excel 宏脚本的神器
私募工场
26+阅读 · 2019年9月8日
C# 10分钟完成百度人脸识别
DotNet
3+阅读 · 2019年2月17日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
Monocular Plan View Networks for Autonomous Driving
Arxiv
6+阅读 · 2019年5月16日
Arxiv
135+阅读 · 2018年10月8日
Feature Selection Library (MATLAB Toolbox)
Arxiv
7+阅读 · 2018年8月6日
VIP会员
相关VIP内容
【2020新书】使用高级C# 提升你的编程技能,412页pdf
专知会员服务
56+阅读 · 2020年6月26日
一份简明有趣的Python学习教程,42页pdf
专知会员服务
76+阅读 · 2020年6月22日
【实用书】Python技术手册,第三版767页pdf
专知会员服务
229+阅读 · 2020年5月21日
Python导论,476页pdf,现代Python计算
专知会员服务
255+阅读 · 2020年5月17日
【实用书】Python爬虫Web抓取数据,第二版,306页pdf
专知会员服务
115+阅读 · 2020年5月10日
MIT新书《强化学习与最优控制》
专知会员服务
272+阅读 · 2019年10月9日
相关资讯
Python 3.8.0来了!
数据派THU
5+阅读 · 2019年10月22日
用 Python 开发 Excel 宏脚本的神器
私募工场
26+阅读 · 2019年9月8日
C# 10分钟完成百度人脸识别
DotNet
3+阅读 · 2019年2月17日
如何编写完美的 Python 命令行程序?
CSDN
5+阅读 · 2019年1月19日
Python3.8新特性概览
Python程序员
4+阅读 · 2018年12月8日
Python | Jupyter导出PDF,自定义脚本告别G安装包
程序人生
7+阅读 · 2018年7月17日
教你用Python来玩跳一跳
七月在线实验室
6+阅读 · 2018年1月2日
Top
微信扫码咨询专知VIP会员