Mercurial路径模式扩展
项目描述
不要在多个仓库中重复定义[路径],在~/.hgrc中一次性指定通用规则。
路径模式是用于定义默认远程路径别名的Mercurial扩展。如果您在几台机器上维护一致布局的仓库树,可能会发现它很有用。
路径模式通常在幕后工作,使标准命令如 hg pull、hg push 和 hg incoming 了解额外的路径。尽管如此,它还实现了一些命令,特别是 hg cloneto «path-alias»(克隆到由短名指定的远程地址)。
1 使用路径模式
按照以下说明安装扩展。
1.1 简单示例
在您的 ~/.hgrc 中编写
[extensions] mercurial_path_pattern = [path_pattern] lagrange.local = ~/devel/{repo} lagrange.remote = ssh://johny@lagrange.mekk.net/sources/{repo} bbssh.local = ~/devel/public/{below} bbssh.remote = ssh://hg@bitbucket.org/Johny/{below:/=-}
假设 ~/devel/personal/blog/drafts 和 ~/devel/public/pymods/acme 都是某些 Mercurial 仓库。那么
cd ~/devel/personal/blog/drafts hg push lagrange # Works, pushes to ssh://johny@lagrange.mekk.net/sources/personal/blog/drafts cd ~/devel/public/pymods/acme hg pull lagrange # Works, pulls from ssh://johny@lagrange.mekk.net/sources/public/pymods/acme hg pull bbssh # Works too, pulls from ssh://hg@bitbucket.org/Johny/pymods-acme
尽管这些仓库缺少 .hg/hgrc,但这仍然有效。
对于两个仓库来说,这并不是很有用,但一旦你有上百个仓库,管理单个 .hg/hgrc 就变得麻烦(想象一下在所有地方将 lagrange.mekk.net 更改为 lagrange.mekk.com,或者可能为新的开发机器添加第二个远程别名)。
1.2 覆盖仓库级别路径
默认情况下,路径模式比每个仓库的路径优先级低,因此如果你在仓库级别定义了 lagrange 路径,它不会被模式覆盖。你可以通过添加 .enforce 来增强它
[path_pattern] lagrange.local = ~/devel/{repo} lagrange.remote = ssh://johny@lagrange.mekk.net/sources/{repo} lagrange.enforce = true
具有这样的配置,模式将战胜 .hg/hgrc 中的任何路径(通常不推荐,但如果你在仓库中有一些散布的错误路径,它可能很有用)。
1.3 重用相同的别名
要在几个不同的位置使用相同的别名,请使用 ALIAS.XTRA.local 和 ALIAS.XTRA.remote 键,其中 XTRA 是某个唯一标识符。例如
[path_pattern] production.main.local = ~/devel/{repo} production.main.remote = ssh://www-owner@www.acme.org/public/{repo} production.beta.local = ~/experiments/{repo} production.beta.remote = ssh://www-owner@beta.acme.org/public/{repo}
将允许你不仅在 ~/devel/website/blog 中使用 hg push production,还可以在 ~/experiments/website/qagame 中使用(前者推送到 www.acme.org,后者推送到 beta.acme.org)。是否这是一个好主意,取决于你。
2 支持克隆的命令
2.1 使用cloneto
cloneto 命令使得将仓库克隆到远程 URL 更加容易
hg cloneto lagrange # Equivalent to # hg clone . ssh://johny@lagrange.mekk.net/sources/pymodules/acme # but noticeably shorter
这既适用于正常路径,也适用于从模式派生的路径,但与模式一起使用时特别方便。特别是,它使得将新创建的仓库推送到远程 URL 成为可能,例如
cd ~/devel/libs hg init xyz cd xyz hg cloneto lagrange # Works, creates sources/libs/xyz on johny@lagrange.mekk.net
2.2 而不是clonefrom
目前没有 clonefrom 命令,但这并不是真正需要的。以下工作(想象 libs/zzz 存在于 lagrange.mekk.net,但尚未在此处克隆)
cd ~/devel/libs hg init zzz cd zzz hg pull lagrange
3 测试模式配置
标准
hg paths
命令列出为当前仓库定义的路径,在模式展开后。在几个不同的仓库中使用它(用于验证你的模式是否生成正确的路径)。
命令
hg list_path_patterns
命令打印配置中找到的所有模式。使用它来检测由于打字错误而导致的某些模式被忽略,并检查配置处理的结果。
4 模式语法
4.1 介绍
模式在 Mercurial 配置文件的 [path_pattern] 部分中定义(通常为 ~/.hgrc)。你可以定义任意多的模式。以下是一个说明各种语法元素的示例
[path_pattern] lagrange.local = ~/devel/{repo} lagrange.remote = ssh://johny@lagrange.mekk.net/sources/{repo} euler.local = ~/devel/{repo} euler.remote = ssh://johny@euler.mekk.net/devel/{repo:/=.}/hg wrk.local = ~/work/{what} wrk.remote = https://tim@devel-department.local/{what:/=__:\=__} ugly.local = ~/(topic)/sources/{subpath}/repo ugly.remote = ssh://hg{topic}@devel.local/{topic}/{subpath} cfg.dotcfg.local = ~/.config/{repo} cfg.dotcfg.remote = ssh://hgrepos@central.com/configs/riemann-config/{repo} cfg.dotshr.local = ~/.local/share/{repo} cfg.dotshr.remote = ssh://hgrepos@central.com/configs/riemann-local/{repo} official.hgstable.local = ~/tracked/mercurial/hg-stable official.hgstable.remote = http://selenic.com/repo/hg-stable official.thg.local = ~/tracked/mercurial/tortoisehg-stable official.thg.remote = https://bitbucket.org/tortoisehg/thg official.evolve.local = ~/tracked/mercurial/mutable-history official.evolve.remote = https://bitbucket.org/marmoute/mutable-history
4.2 模式定义
每个模式都由一对键定义 - 「别名」.local 和 「别名」.remote - 或者,如果要在几个地方使用相同的别名,则由 「别名」.「某物」.local 和 「别名」.「某物」.remote 定义(其中 「某物」 是使键唯一的任何东西)。
在处理模式时,扩展会将当前仓库根路径与 local 模式进行匹配,如果匹配,则通过填充那里的标记来计算远程路径,并定义路径别名。
.local 部分应指定绝对仓库路径(允许使用 ~ 和 ~user)。路径的某些部分可以用 {花括号} 或 (括号) 标记替换
{花括号} 会贪婪地匹配一切(直到结尾,除非后面有某些固定文本),
(括号) 仅限于单个路径项,并且不会跨越 / 或 \\ 字符。
这些部分将从本地仓库路径中提取出来,并可用于定义远程路径。
标记是可选的,如果没有使用标记(参见上面的 official),则规则适用于恰好一个仓库。如果你更愿意集中管理你的远程路径列表(或者如果你经常丢弃这些仓库以便稍后重新克隆它们),这可能是有意义的。
.remote 部分定义适当的远程地址。这是典型的 Mercurial 远程路径,但可以使用 {标记} 来引用从本地路径中提取的值:{某物} 将被替换为本地路径中匹配 {某物} 或 (某物) 的任何内容。
支持简单的修改 - {某物:x=y} 的意思是 将提取的内容作为 某物 使用,并将任何 x 替换为 y。这主要用于将 / 替换为其他字符(特别是 {below:/=-} 处理 BitBucket 习惯,将斜杠替换为减号)。替换可以是多字母的,例如 {某物:lib=library}。
如果需要,替换可以链式使用 - {某物:x=y:v=z} 的意思是 将提取的内容作为 某物,将任何 x 替换为 y,然后将任何 v 替换为 z,最后使用最终结果。
4.3 解析示例
根据上述定义,如果你在 ~/devel/python/libs/webby 仓库内工作,该扩展将
发现lagrange.local匹配,并且{repo}是python/libs/webby。将lagrange.remote填充为该值生成ssh://johny@lagrange.mekk.net/sources/python/libs/webby,因此创建了以下路径别名:lagrange=ssh://johny@lagrange.mekk.net/sources/python/libs/webby
还发现euler.local也匹配,并且{repo}仍然是python/libs/webby。将/-s替换为.-s后,将别名euler=ssh://johny@euler.mekk.net/devel/python.libs.webby/hg
忽略剩余的匹配模式。
或者,在~/tracked/mercurial/tortoisehg-stable中,该扩展将
请注意,official.thg.local匹配(目前没有定义任何内容),从official.thg.remote提取匹配路径,并最终为该存储库路径生成official = https://bitbucket.org/tortoisehg/thg(因此hg pull official在那里可以正常工作)。请注意,路径别名只是official,.thg.部分仅用于分组适当的配置项。
忽略剩余的匹配模式。
4.4 旧语法
出于兼容性原因,存在另一种方法来重用相同的路径别名。例如,而不是(目前推荐)
[path_pattern] production.web.local = ~/devel/web/{repo} production.web.remote = ssh://product@acme.org/www/{repo} production.db.local = ~/devel/database/{repo} production.db.remote = ssh://product@backend.acme.org/db/{repo} production.monit.local = ~/devel/monitoring/{repo} production.monit.remote = ssh://product@monit.acme.org/{repo}
可以使用.alias
[path_pattern] production.local = ~/devel/web/{repo} production.remote = ssh://product@acme.org/www/{repo} dbproduction.db.local = ~/devel/database/{repo} dbproduction.db.remote = ssh://product@backend.acme.org/db/{repo} dbproduction.alias = production monproduction.local = ~/devel/monitoring/{repo} monproduction.remote = ssh://product@monit.acme.org/{repo} monproduction.alias = production
这两种语法都给所有匹配的存储库中的hg push production赋予了意义。
5 模式优先级
可以编写模式,使它们发生_冲突_(存在一些路径的多个定义)。虽然不常见,但有时这种方法有其用途。
路径别名具有以下优先级
强制模式(设置.enforce的模式),
每个存储库的别名(在.hg/hgrc中定义的标准[paths]),
非强制模式。
因此,例如,使用
[path_pattern] acme.local = ~/devel/{repo} acme.remote = ssh://johny@apps.mekk.net/code/{repo} acme.enforce = true acme.alt.local = ~/devel/libs/{repo} acme.alt.remote = ssh://johny@libs.mekk.net/{repo}
(两个模式定义了相同的别名acme)在~/devel/libs/calc中执行hg push acme将推送到ssh://johny@apps.mekk.net/code/libs/calc,因为强制模式优先于非强制模式。即使acme在单个存储库的.hg/hgrc中定义(在标准[paths]中),也会发生这种情况。
如果有多个相同强度的模式匹配,扩展将尽力选择具有更具体本地路径的模式,例如,如果我们从上面的例子中删除acme.enforce(或者如果我们添加acme.alt.enforce),在~/devel/libs/calc中执行hg push acme将推送到ssh://johny@libs.mekk.net/calc,因为更具体的模式获胜。
6 技巧和窍门
6.1 default作为路径模式
您可以通过路径模式来定义默认设置
[path_pattern] default.hobby.local = ~/hobby/{repo} default.hobby.remote = ssh://hg@bitbucket.org/Johny/{below:/=-} default.wrk.local = ~/work/{what} default.wrk.remote = https://tim@devel-department.local/{what}
(在这里,我在~/hobby默认推送到bitbucket,但在~/work推送到部门服务器)。
6.2 特定仓库的特殊处理
有些仓库(或少数几个)不匹配通用规则。在这种情况下,可以在仓库级别简单覆盖给定的别名,或使用模式优先级。
我的真实例子是Keyring Extension仓库。虽然我通常使用连字符(-)作为路径分隔符(因此路径模式位于/Mekk/mercurial-path_pattern,而Dynamic Username位于/Mekk/mercurial-dynamic_username),但keyring仓库在先前的约定之前,其名称为/Mekk/mercurial_keyring。因此,我通过以下方式解决这个问题:
[path_pattern] # By default bitbucket mirrors my dir structure replacing / with - bbssh.local = ~/devel/{below} bbssh.remote = ssh://hg@bitbucket.org/Mekk/{below:/=-} # … but there are overrides bbssh.keyring.local = ~/devel/mercurial/keyring bbssh.keyring.remote = ssh://hg@bitbucket.org/Mekk/mercurial_keyring
当然,我可以在~/devel/mercurial/keyring/.hg/hgrc文件的[paths]部分中定义bbssh,但模式技术有一些优点
因为我会在机器之间共享和同步我的~/.hgrc片段,所以这个定义会自动传播到每个地方,我无需记住为新克隆添加路径,
它将所有路径放在一个地方,我可以一起审查它们,
如果需要,可以将其扩展到整个子树。
6.3 将非标准远程路径作为模式保留
同样的技巧可以用于维护远程列表。例如,以下是我在各种跟踪的仓库中方便使用hg pull official的方法
[path_pattern] official.hgstable.local = ~/tracked/hg-stable official.hgstable.remote = http://selenic.com/repo/hg-stable official.thg.local = ~/tracked/tortoise-hg official.thg.remote = https://bitbucket.org/tortoisehg/thg/ # …
当然,我可以在.hg/hgrc内部直接输入这些路径,但这些定义可以在机器之间同步,并在一段时间内保留我对负责的仓库的丢弃。
6.4 分离(并共享)模式配置
如果模式列表变得更大,我建议将模式移动到单独的配置文件中。例如,在~/.hgrc中写入
%include ~/configs/mercurial/path_pattern.hgrc
然后将所有模式存储在path_pattern.hgrc中
[path_pattern] …
这种方法的额外好处是使文件共享更容易(在我的情况下,~/configs/mercurial本身就是一个Mercurial仓库,我将其共享在我的各种开发机器上,它包含我Mercurial配置的所有非机器特定片段)。
7 安装
7.1 Linux/Unix(来自PyPI)
如果您有工作的pip或easy_install
pip install --user mercurial_path_pattern
或者也许
sudo pip install mercurial_path_pattern
(或者用easy_install代替pip)。然后通过以下方式激活
[extensions] mercurial_path_pattern =
要升级,使用带有--upgrade选项的相同命令,例如
pip install --user --upgrade mercurial_path_pattern
7.2 Linux/Unix(来自源代码)
如果您没有pip,或者希望更密切地跟踪开发
克隆这两个仓库和mercurial_extension_utils,并将它们放在同一个目录中,例如
cd ~/sources hg clone https://foss.heptapod.net/mercurial/mercurial-extension_utils/ hg clone https://foss.heptapod.net/mercurial/mercurial-path_pattern/
更新到最新标签,
激活方式
[extensions] mercurial_path_pattern = ~/sources/mercurial-path_pattern/mercurial_path_pattern.py
要升级,请拉取并更新。
请注意目录名称很重要。有关此类安装的更详细描述,请参阅mercurial_extension_utils。
7.3 Windows
如果您已安装任何Python,可以使用pip安装
pip install mercurial_path_pattern
然而,由于Mercurial(无论是从TortoiseHg获取,还是使用自己的包)使用它自己的捆绑Python,您必须通过指定路径来激活
[extensions] mercurial_path_pattern = C:/Python27/Lib/site-packages/mercurial_path_pattern.py ;; Or wherever pip installed it
升级到新版本
pip --upgrade mercurial_path_pattern
如果您没有Python,请克隆仓库
cd c:\hgplugins hg clone https://foss.heptapod.net/mercurial/mercurial-extension_utils/ hg clone https://foss.heptapod.net/mercurial/mercurial-path_pattern/
更新到标记版本并通过路径激活
[extensions] mercurial_path_pattern = C:/hgplugins/mercurial-path_pattern/mercurial_path_pattern.py ;; Or wherever you cloned
有关Windows安装的更多详细信息,请参阅mercurial_extension_utils文档。
9 历史
10 仓库、错误报告、增强建议
在HeptaPod上跟踪开发,请参阅https://foss.heptapod.net/mercurial/mercurial-path_pattern/
使用那里的问题跟踪器报告错误和建议改进。
感谢Octobus和Clever Cloud托管此服务。
11 附加说明
有关此扩展的信息也可在Mercurial Wiki上找到:http://mercurial.selenic.com/wiki/PathPatternExtension
项目详情
下载文件
下载适用于您平台的应用程序。如果您不确定选择哪一个,请了解有关安装包的更多信息。
源分布
mercurial_path_pattern-1.4.3.tar.gz的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | ff5a237deec2e40ba093694b377f8a3e3515c24f7b853ab6bbfcc745f1875153 |
|
MD5 | 8856b29f429735f4eb605f5a64307044 |
|
BLAKE2b-256 | eef32d9d3a8ca720a758221d90e264c8b446ce66a5468b6918b94340e12ca176 |