英文:
Does pre-receive hook works only on server-side?
问题
我想强制将 jira 任务包含在提交消息中。要在全局范围内执行此操作,我可以使用 pre-receive 钩子,该钩子适用于 GitHub Enterprise,但首先我想进行测试,所以在 .git/hooks
目录中,我修改了 pre-receive.sample -> pre-receive
并添加了以下内容:
HOOK_FILE=$1
COMMIT_MSG=`head -n1 $HOOK_FILE`
PATTERN="[MM\-[0-9]+]"
if [[ ! ${COMMIT_MSG} =~ $PATTERN ]]; then
echo ""
echo " 错误:提交消息不正确。"
echo " '$COMMIT_MSG' 缺少 JIRA 任务编号。"
echo " 错误:请修正提交消息。"
echo " 例子:'MM-1234 我的提交'。"
echo ""
exit 1
fi
推送后它不起作用,但当我将此代码移动到 commit-msg
文件中时,在提交无效后我会收到带有错误的消息。所以我的问题是 - 我能否只修改本地仓库中的 .git/hooks/pre-receive
来测试 pre-receive 钩子?
英文:
I would like to force including jira ticket into commit message. To do it globally I can use pre-receive hook, which is available for GitHub Enterprise, but firstly I would like to test, so in .git/hooks
directory, I modified pre-receive.sample -> pre-receive
and added:
HOOK_FILE=$1
COMMIT_MSG=`head -n1 $HOOK_FILE`
PATTERN="[MM\-[0-9]+\]"
if [[ ! ${COMMIT_MSG} =~ $PATTERN ]]; then
echo ""
echo " ERROR: Bad commit message."
echo " '$COMMIT_MSG' is missing JIRA Ticket Number."
echo " ERROR: Please fix the commit message."
echo " example: 'MM-1234 my commit'."
echo ""
exit 1
fi
and after push it doesn't work, but when I move this code into commit-msg
file, after invalid commit I get message with error. So my question is - am I able to test pre-receive hook modifying only .git/hooks/pre-receive
on my local repo?
答案1
得分: 2
The hook pre-receive does not take parameters, so it does not work to get the value from $1
. Github provides some good pre-receive samples in Bash, among which I think you can find a suitable one and modify it to meet your needs. Remember to name it pre-receive
.
You can test the pre-receive hook in a local repository which serves as a server-side repository. Here is an example.
create a bare repository that works as a remote repository
git init --bare server
Here is a very simple pre-receive.
#!/bin/bash
echo HELLO WORLD from the server
Make it executable and copy it under server/hooks
.
clone the repository and name it client
git clone server client
edit and commit
cd client
touch a.txt
git add a.txt
git commit -m "init"
push the branch to server
git push origin master
The push will invoke pre-receive
under server/hooks
if it is set up correctly. The push prints something like
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 202 bytes | 202.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: HELLO WORLD from the server
To /path/to/server/
- [new branch] master -> master
The lines that start with remote:
are sent back from the server repository. In this example, remote: HELLO WORLD from the server
is generated by the pre-receive hook.
This way, you can modify and test the hook in the local machine.
英文:
The hook pre-receive does not take parameters, so it does not work to get the value from $1
. Github provides some good pre-receive samples in Bash, among which I think you can find a suitable one and modify it to meet your needs. Remember to name it pre-receive
.
You can test the pre-receive hook in a local repository which serves as a server-side repository. Here is an example.
# create a bare repository that works as a remote repository
git init --bare server
Here is a very simple pre-receive.
#!/bin/bash
echo HELLO WORLD from the server
Make it executable and copy it under server/hooks
.
# clone the repository and name it client
git clone server client
# edit and commit
cd client
touch a.txt
git add a.txt
git commit -m "init"
# push the branch to server
git push origin master
The push will invoke pre-receive
under server/hooks
if it is setup correctly. The push prints something like
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 202 bytes | 202.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: HELLO WORLD from the server
To /path/to/server/
* [new branch] master -> master
The lines that start with remote:
are sent back from the server repository. In this example, remote: HELLO WORLD from the server
is generated by the pre-receive hook.
This way, you can modify and test the hook in the local machine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论