简体   繁体   English

从 Git 预接收挂钩获取脚本

[英]Sourcing a script from a Git pre-receive hook

I'm testing a git pre-receive hook in a server.我正在服务器中测试 git 预接收挂钩。 This are the contents of the hooks/pre-receive :这是hooks/pre-receive的内容:

#!/bin/bash
echo "Hi $USER"
source pre-receive-hooks/bye

The contents of hooks/pre-receive-hooks/bye are: hooks/pre-receive-hooks/bye的内容是:

#!/bin/bash
echo "Bye $USER"

Both hooks/pre-receive and hooks/pre-receive-hooks/bye files are executable, have the same permissions, and same owner and group. hooks/pre-receivehooks/pre-receive-hooks/bye文件都是可执行的,具有相同的权限,以及相同的所有者和组。

When I push from a client to the server I get the message:当我从客户端推送到服务器时,我收到消息:

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Hi sergioro
remote: hooks/pre-receive: line 10: pre-receive-hooks/bye: No such file or directory
To drop:/git/hooks_practice.git
   3fbfa15..c15d08d  master -> master

The first echo is working but then the source command fails.第一个echo显正在工作,但随后source命令失败。 Why is the source is failing?为什么source是失败的? Or more generally, how to source a script from within a Git hook?或者更一般地说,如何从 Git 挂钩中获取脚本?

I also tried the following commands but all return the same error when I push:我还尝试了以下命令,但推送时都返回相同的错误:

source ./pre-receive-hooks/bye   # relative path
. pre-receive-hooks/bye          # source using the dot command
./pre-receive-hooks/bye          # run script directly

Changing source pre-receive-hooks/bye to source hooks/pre-receive-hooks/bye solved the problem.source pre-receive-hooks/bye更改为source hooks/pre-receive-hooks/bye解决了这个问题。 Apparently Git hooks have $PWD equal to the root directory of the repository, not the hooks directory.显然 Git 挂钩的$PWD等于存储库的根目录,而不是挂钩目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM