Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

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

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

问题

以下是翻译好的部分:

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?
为什么我的git挂钩脚本(pre-commit,post-commit,pre-receive,pre-push等)都不运行?

Note: this question is not a duplicate;
注意:这个问题不是重复的;

I have try the answer to each of the other questions but none of them work.
我已经尝试了其他问题的答案,但它们都不起作用。

I did chmod +x, added the path to hook. rename script, neither of them solve my issue.
我已经使用chmod +x,添加了挂钩的路径,重命名脚本,但都没有解决我的问题。

Inside my git
在我的git中
branches config description HEAD hooks info objects refs

Inside hooks:
在hooks文件夹中:
applypatch-msg.sample fsmonitor-watchman.sample post-update.sample pre-commit prepare-commit-msg.sample pre-rebase.sample update.sample
commit-msg post-merge.sh pre-applypatch.sample pre-commit.sample pre-push pre-receive

I run them manually and they are all working fine.:
我手动运行它们,它们都正常工作。

$ bash pre-commit
您即将提交到主分支
您真的要这么做吗?[y/n] y

pre-commit script
pre-commit脚本

#!/bin/sh

An example hook script to verify what is about to be committed.

Called by "git commit" with no arguments. The hook should

exit with non-zero status after issuing an appropriate message if

it wants to stop the commit.

To enable this hook, rename this file to "pre-commit".

echo "You are about to commit" $(git diff --cached --name-only --diff-filter=ACM)
echo "to" $(git branch --show-current)

while : ; do
read -p "Do you really want to do this? [y/n] " RESPONSE < /dev/tty
case "${RESPONSE}" in
[Yy]* ) exit 0; break;;
[Nn]* ) exit 1;;
esac
done

But when I git commit and git push to the repository none of the scripts work.
但是当我使用git commit和git push到存储库时,没有任何脚本运行。

$ git commit -m "Test hooks"
[master] Test hooks 1 file
changed, 1 insertion(+)

My git version is 2.39.1
我的git版本是2.39.1

I created the repository on a VM with Ubuntu 18.04.6 LTS installed
我在安装了Ubuntu 18.04.6 LTS的虚拟机上创建了存储库。

Here was the procedure for creating the repo.
以下是创建存储库的过程:

mkdir project1.git
cd project1.git
git init --bare

After the creation I clone the repo to my local computer (windows).
创建后,我将存储库克隆到我的本地计算机(Windows)。

Clone the git repository
克隆git存储库

git clone git@{ip}:/home/git/git_repositories/project1.git/

英文:

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

Note:
this question is not a duplicate;

I have try the answer to each of the other questions but none of them work.

I did chmod +x, added the path to hook. rename script, neither of them solve my issue.

> Inside my git

branches config description HEAD hooks info objects refs

> Inside hooks:

applypatch-msg.sample  fsmonitor-watchman.sample  post-update.sample     pre-commit         prepare-commit-msg.sample  pre-rebase.sample  update.sample
commit-msg             post-merge.sh              pre-applypatch.sample  pre-commit.sample  pre-push                   pre-receive

I run them manually and they are all working fine.:

$ bash pre-commit
  You are about to commit to master
  Do you really want to do this? [y/n] y

pre-commit script

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by &quot;git commit&quot; with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to &quot;pre-commit&quot;.

echo &quot;You are about to commit&quot; $(git diff --cached --name-only --diff-filter=ACM)
echo &quot;to&quot; $(git branch --show-current)

while : ; do
    read -p &quot;Do you really want to do this? [y/n] &quot; RESPONSE &lt; /dev/tty
    case &quot;${RESPONSE}&quot; in
        [Yy]* ) exit 0; break;;
        [Nn]* ) exit 1;;
    esac
done

But when i git commit and git push to the repository none of the scripts work.

$git commit -m &quot;Test hooks&quot;
[master] Test hooks  1 file
changed, 1 insertion(+)

My git version is 2.39.1

I created the repository on a VM with Ubuntu 18.04.6 LTS installed
Here was the procedure fro creating the repo.

mkdir project1.git
cd project1.git
git init --bare

After the creation i clone the repo to my local computer (windows).

Clone the git repository

git clone git@{ip}:/home/git/git_repositories/project1.git/

答案1

得分: 1

但我想要使用project1.git/hooks中的脚本来使其工作。

例如,一个预提交钩子在裸仓库中不会工作(裸仓库没有工作树)。
它只会在您的克隆仓库中工作,在Windows上,您可以创建一个myClonedRepo/.git/hook/pre-commit脚本(没有扩展名,也没有.sh),它将在每次提交之前运行。

来自评论:

  • 所有用户都可以从共享的Git模板仓库中创建/克隆其仓库。文章“创建自定义Git模板”提供了这种方法的示例,但这意味着:

    • 每个用户必须访问相同的共享文件夹
    • 他们需要在全局Git配置中激活init.templateDir配置设置
  • 任何必须强制执行的检查,特别是对于分布式团队的所有成员,最好由**服务器端钩子**来管理。

英文:

> But I want use the scripts in project1.git/hooks to make this work.

A pre-commit hook for instance would not work in a bare repository (which has no working tree).
It would work only in your cloned repository, on Windows, where you can create a myClonedRepo/.git/hook/pre-commit script (no extension, no .sh), which will be run before each commit.

From the comments:

  • all users could create/clone their repositories from a shared Git template repository. The article "Creating a Custom Git Template" gives an illustration of that approach, but means that:

    • every user must access the same shared folder
    • they need to activate the init.templateDir config setting in their global Git configuration
  • any check which must be enforced for all the team members, especially for a distributed team, is best managed by server-side hooks instead.

huangapple
  • 本文由 发表于 2023年2月6日 09:46:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356705.html
匿名

发表评论

匿名网友

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

确定