pre-receive钩子只在服务器端起作用吗?

huangapple go评论68阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年7月13日 15:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定