如何在 HG / Mercurial 中设置由存储库指定的挂钩?

发布于 2024-12-05 13:49:08 字数 185 浏览 0 评论 0原文

我需要一个在更新后运行的挂钩(这将构建他们已更新的解决方案),并且我不想为每个克隆我的中央存储库的人手动添加该挂钩。

当有人第一次克隆我的中央存储库时,是否可以将挂钩包含到该克隆中? .hgrc 文件似乎不会自动克隆。

我确实读过有关站点范围挂钩的信息,但据我了解,它们适用于每个创建的存储库,我只想在某些存储库上使用挂钩。

I have a need for a hook to run after update (this will build the solution they have updated) and I don't want to have to add that hook manually for each person that clones my central repository.

When someone first clones my central repository, is it possible to include hooks into that clone? It seems that the .hgrc file doesn't get cloned automatically.

I did read about site-wide hooks, but as far as I understand it, they work on each created repository, where I only want to have the hooks on some repos.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅忆流年 2024-12-12 13:49:08

作为鲁迪已经说过,出于安全原因,这是不可能的(谢天谢地)。

但是,您可以减少每个克隆手动设置挂钩的工作量:将挂钩脚本作为存储库的一部分发送,例如在目录 .hghooks 中,并另外在存储库中包含一个脚本,该脚本设置在克隆的 hgrc 中添加这些钩子。现在,每个同事只需为每个克隆调用一次设置脚本。

As Rudi already said, this is (thankfully) not possible for security reasons.

However, you can reduce the per-clone workload to set up hooks manually: Ship the hook scripts as part of your repository, e.g. in a directory .hghooks, and additionally include a script in your repo which sets up these hooks in a clone's hgrc. Each coworker now only needs to call the setup script once per clone.

痴意少年 2024-12-12 13:49:08

这是不可能的,因为钩子不会传播到克隆是一种安全措施。如果可能的话,人们可以设置一个 rouge 存储库,它可以在克隆该存储库的任何计算机上运行任意命令。

请参阅http://hgbook.red-bean。 com/read/handling-repository-events-with-hooks.html#id402330 了解更多详细信息。

This is not possible, since that hooks do not propagate to clones is a security measure. If this were possible, one could set up a rouge repository, which runs arbitrary commands on any machine where the repo is cloned.

See http://hgbook.red-bean.com/read/handling-repository-events-with-hooks.html#id402330 for more details.

撑一把青伞 2024-12-12 13:49:08

这将允许集中的每个存储库挂钩,每个用户只需一个设置步骤。然而,这会给与网络断开连接的用户带来问题。如果您倾向于断开连接的开发人员(或通过高延迟/低带宽链接的开发人员),另一种选择是拥有一个包含挂钩的存储库,并设置每个用户的全局 hgrc 以指向该存储库(并且需要定期从中央挂钩回购)。

请注意,我将第一次提交的 ID 视为“存储库 ID”——这假设每个存储库中的第一次提交在某种程度上是唯一的——内容或提交消息。如果不是这种情况,您可以做同样的事情,但将其应用于前 N 次提交 - 但您必须考虑少于 N 次提交的存储库 - 不能只采用 repo[:5] 例如,较新的提交将更改存储库 ID。我个人建议第一次提交应该是一个标准的 .ignore 文件,其中包含该存储库特有的提交消息。

  1. 有一个可从网络共享(或在钩子存储库中)访问的中央shared_hgrc文件。

  2. 每个用户的全局 hgrc 有:

    %include /path/to/shared_hgrc
    
  3. 创建 python 钩子模块的共享存储库。钩子必须用Python编写。

  4. 创建您的挂钩函数。在每个函数中,通过检查第一次提交的 ID 来检查钩子已被调用的存储库:

    <前><代码># hooktest.py

    导入 Mercurial.util

    FOOBAR_REPO = 'b88c69276866d73310be679b6a4b40d875e26d84'

    ALLOW_PRECOMMIT_REPOS = 设置((
    FOOBAR_REPO,
    ))

    def precommit_deny_if_wrong_repo(ui, repo, **kwargs):
    """如果不允许存储库执行此操作,则中止。
    存储库 ID 是第一次提交到存储库的 ID。"""

    repo_id = repo[0].hex().lower()

    如果 repo_id 不在 ALLOW_PRECOMMIT_REPOS 中:
    raise Mercurial.util.Abort('存储库被拒绝:%s' % (repo_id,))

    ui.status('允许的存储库: %s\n' % (repo_id,))

    def precommit_skip_if_wrong_repo(ui, repo, **kwargs):
    """如果仓库不允许执行此操作,则跳过挂钩。
    存储库 ID 是第一次提交到存储库的 ID。"""

    repo_id = repo[0].hex().lower()

    如果 repo_id 不在 ALLOW_PRECOMMIT_REPOS 中:
    ui.debug('跳过存储库挂钩:%s\n' % (repo_id,))
    返回

    ui.status('允许存储库挂钩:%s\n' % (repo_id,))

  5. 在shared_hgrc 文件中,设置所需的钩子(确保限定钩子名称以防止冲突):< /p>

    <前><代码>[挂钩]
    pre-commit.00_skip = python:/path/to/hooktest.py:precommit_skip_if_wrong_repo
    pre-commit.01_deny = python:/path/to/hooktest.py:precommit_deny_if_wrong_repo

This will allow for centralised per-repo hooks, with a single setup step per user. It will however cause problems for users who are disconnected from the network. An alternative if you tend to have disconnected developers (or ones over high-latency/low bandwidth links) would be to have a repo containing the hooks, and set up each user's global hgrc to point into that repo (and require regular pulls from a central hook repo).

Note that I treat the ID of the first commit as the "repo ID" - this assumes that the first commit in each repository is unique in some way - contents or commit message. If this is not the case you could do the same thing but applying it over the first N commits - but you would then have to account for repos that have fewer than N commits - can't just take repo[:5] for example as newer commits would then change the repo ID. I'd personally suggest that the first commit should probably be a standard .ignore file with a commit message unique to that repo.

  1. Have a central shared_hgrc file, accessible from a network share (or in a hook repo).

  2. Each user's global hgrc has:

    %include /path/to/shared_hgrc
    
  3. Create a shared repository of python hook modules. The hooks must be written in python.

  4. Create your hook functions. In each function, check which repo the hook has been called on by checking the ID of the first commit:

    # hooktest.py
    
    import mercurial.util
    
    FOOBAR_REPO = 'b88c69276866d73310be679b6a4b40d875e26d84'
    
    ALLOW_PRECOMMIT_REPOS = set((
        FOOBAR_REPO,
    ))
    
    def precommit_deny_if_wrong_repo(ui, repo, **kwargs):
        """Aborts if the repo is not allowed to do this.
        The repo ID is the ID of the first commit to the repo."""
    
        repo_id = repo[0].hex().lower()
    
        if repo_id not in ALLOW_PRECOMMIT_REPOS:
            raise mercurial.util.Abort('Repository denied: %s' % (repo_id,))
    
        ui.status('Repository allowed: %s\n' % (repo_id,))
    
    def precommit_skip_if_wrong_repo(ui, repo, **kwargs):
        """Skips the hook if the repo is not allowed to do this.
        The repo ID is the ID of the first commit to the repo."""
    
        repo_id = repo[0].hex().lower()
    
        if repo_id not in ALLOW_PRECOMMIT_REPOS:
            ui.debug('Repository hook skipped: %s\n' % (repo_id,))
            return
    
        ui.status('Repository hook allowed: %s\n' % (repo_id,))
    
  5. In the shared_hgrc file, set up the hooks you need (make sure you qualify the hook names to prevent conflicts):

    [hooks]
    pre-commit.00_skip = python:/path/to/hooktest.py:precommit_skip_if_wrong_repo
    pre-commit.01_deny = python:/path/to/hooktest.py:precommit_deny_if_wrong_repo
    
丑疤怪 2024-12-12 13:49:08

正如@Rudi 首先所说的,出于安全原因这是无法完成的。

通过一些事先的设置,您可以使钩子在克隆上运行,但将带有存储库相对路径的钩子放在 /etc/mercurial 或每个用户的 ~/.hgrc< /code>,在公司环境中可以通过系统管理工具或构建自定义 Mercurial 安装程序来完成。在非公司环境中,请遵循@Oben 的建议并提供脚本和自述文件。

As @Rudi said first, it can't be done for security reasons.

With some prior setup you can make it so that hooks are run on clone, but putting a hook with a repo-relative path in /etc/mercurial or in each user's ~/.hgrc, which in a corporate setting can be done via your system management tools or by building a custom Mercurial installer. In a non-corporate setting follow @Oben's advice and provide the scripts and a readme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文