把 .git/hooks
放进项目库(例如使用符号链接)是否被认为是一种不好的做法?如果是,那么向不同的Git用户提供相同的hooks的最佳方法是什么?
现在你可以通过以下步骤将一个版本控制下的目录设为Git Hooks目录,例如: MY_REPO_DIR/.githooks
即可。
git config --local core.hooksPath .githooks/
虽然它仍然不能直接执行,但是如果您在自己的README(或其他地方)添加一条注释,每个开发者都需要付出最少的努力。
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"
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
bin/create-hook-symlinks
中添加了chmod +x .git/hooks/*
来使其工作。 - guneysusHOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks
。 - Arnold Daniels不,将它们放入存储库是可以的。我甚至建议这样做(如果它们对其他人也有用)。用户必须明确启用它们(正如您所说,例如通过符号链接),这在一方面有些麻烦,但另一方面可以保护用户免受未经他们同意运行任意代码的风险。
正如其他人在他们的答案中所述,如果你的钩子是特定于你的项目的,则将它们包含在由Git管理的项目本身中。我会更进一步地说,鉴于使用单个脚本或命令构建项目是一个好的实践,你的钩子应该在构建期间安装。
我写了一篇管理Git钩子的文章,如果你有兴趣了解更多。
完全声明;我编写了下面描述的Maven插件。
如果你正在使用Maven处理Java项目的构建管理,则以下Maven插件可以处理从项目中的位置安装钩子。
https://github.com/rudikershaw/git-build-hook
将所有的Git钩子放在项目中的一个目录中,然后配置你的pom.xml
以包含以下插件声明、目标和配置。<build>
<plugins>
<plugin>
<groupId>com.rudikershaw.gitbuildhook</groupId>
<artifactId>git-build-hook-maven-plugin</artifactId>
<configuration>
<gitConfig>
<!-- The location of the directory you are using to store the Git hooks in your project. -->
<core.hooksPath>hooks-directory/</core.hooksPath>
</gitConfig>
</configuration>
<executions>
<execution>
<goals>
<!-- Sets git config specified under configuration > gitConfig. -->
<goal>configure</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- ... etc ... -->
</plugins>
</build>
// package.json
{
"husky": {
"hooks": {
"pre-commit": "npm test",
"pre-push": "npm test",
"...": "..."
}
}
}
此外,还有许多不同的钩子管理应用程序/插件,包括Python项目的pre-commit,Ruby项目的Overcommit,以及Ruby 或 Node.js 项目的Lefthook。
模板目录包含将复制到$GIT_DIR创建后的文件和目录。
模板目录将按顺序为以下内容之一:
使用--template选项给的参数;
$GIT_TEMPLATE_DIR环境变量的内容;
init.templateDir配置变量;或者
默认模板目录:/usr/share/git-core/templates。
hooks
文件夹,然后在您的composer.json文件中: },
"scripts": {
"post-install-cmd": [
"cp -r 'hooks/' '.git/hooks/'",
"php -r \"copy('hooks/pre-commit', '.git/hooks/pre-commit');\"",
"php -r \"copy('hooks/commit-msg', '.git/hooks/commit-msg');\"",
"php -r \"chmod('.git/hooks/pre-commit', 0777);\"",
"php -r \"chmod('.git/hooks/commit-msg', 0777);\"",
],
然后,随着每个人定期运行composer install
,您甚至可以在项目继续进行时更新它们。
#!/usr/bin/bash
# Adds the git-hook described below. Appends to the hook file
# if it already exists or creates the file if it does not.
# Note: CWD must be inside target repository
HOOK_DIR=$(git rev-parse --show-toplevel)/.git/hooks
HOOK_FILE="$HOOK_DIR"/post-commit
# Create script file if doesn't exist
if [ ! -e "$HOOK_FILE" ] ; then
echo '#!/usr/bin/bash' >> "$HOOK_FILE"
chmod 700 "$HOOK_FILE"
fi
# Append hook code into script
cat >> "$HOOK_FILE" <<EOF
########################################
# ... post-commit hook script here ... #
########################################
EOF
看起来很多帖子都已经过时了,至少如果你在Python生态系统中使用pre-commit的话(我发现在较旧版本的git中更改git hook路径会失败,例如2.3)。有了一个.pre-commit-config.yaml文件,放在仓库根目录下的hooks目录中,最简单的解决方案是运行:
pre-commit install -f --config hooks/.pre-commit-config.yaml
您可以使用托管解决方案,例如pre-commit进行提交前钩子管理。
或者使用中央化的解决方案,例如Datree.io用于服务器端git-hooks。
它具有内置策略,例如:
它不会替换所有钩子,但它可能会帮助您的开发人员处理最明显的问题,而无需在每个开发人员的计算机/仓库上安装钩子的配置问题。
免责声明:我是Datree的创始人之一。
.git-suggested
目录,提示用户是否希望将其内容复制到.git/{hooks,config,info,(任何安全的目录)}
中。然而,对建议的钩子进行平滑处理可能会有些棘手。 - undefined