将Git挂钩放入存储库中 [英] Putting Git hooks into a repository

查看:26
本文介绍了将Git挂钩放入存储库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否认为将.git/hooks放入项目存储库(例如,使用符号链接)是一种糟糕的做法。如果是,向不同的Git用户提供相同钩子的最佳方式是什么?

推荐答案

我大体上同意with Scy,另外还有几个建议,足以值得单独回答。

首先,您应该编写一个脚本来创建适当的符号链接,特别是在这些挂钩是关于实施策略或创建有用的通知的情况下。如果人们只需要键入bin/create-hook-symlinks就会使用钩子,而不是必须自己动手。

其次,直接符号链接挂钩可防止用户添加他们自己的个人挂钩。例如,我相当喜欢示例的预提交挂钩,它可以确保我没有任何空格错误。解决这个问题的一个很好的方法是在存储库中放入一个挂钩包装器脚本,并将所有的挂钩符号链接到它。

然后,包装器可以检查$0(假设它是一个Bash脚本;否则与argv[0]类似),以确定它是作为哪个钩子调用的,然后调用存储库中的适当钩子,以及必须重命名的适当用户的钩子,并将所有参数传递给每个钩子。快速示例:

#!/bin/bash
if [ -x $0.local ]; then
    $0.local "$@" || exit $?
fi
if [ -x tracked_hooks/$(basename $0) ]; then
    tracked_hooks/$(basename $0) "$@" || exit $?
fi

安装脚本将把所有预先存在的钩子移到一边(将.local追加到它们的名称),并将所有已知的钩子名称符号链接到上面的脚本:

#!/bin/bash
HOOK_NAMES="applypatch-msg pre-applypatch post-applypatch pre-commit prepare-commit-msg commit-msg post-commit pre-rebase post-checkout post-merge pre-receive update post-receive post-update pre-auto-gc"
# assuming the script is in a bin directory, one level into the repo
HOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks

for hook in $HOOK_NAMES; do
    # If the hook already exists, is executable, and is not a symlink
    if [ ! -h $HOOK_DIR/$hook -a -x $HOOK_DIR/$hook ]; then
        mv $HOOK_DIR/$hook $HOOK_DIR/$hook.local
    fi
    # create the symlink, overwriting the file if it exists
    # probably the only way this would happen is if you're using an old version of git
    # -- back when the sample hooks were not executable, instead of being named ____.sample
    ln -s -f ../../bin/hooks-wrapper $HOOK_DIR/$hook
done

这篇关于将Git挂钩放入存储库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆