如何在提交后的钩子脚本中获取项目路径?(git)

发布于 2024-10-20 23:05:23 字数 144 浏览 4 评论 0原文

我想调用位于存储库中的脚本。

我当然可以执行以下操作:

#!/bin/sh
../../myscript.sh

但我认为这不太好;)

那么如何在提交后脚本中获取项目的路径呢?

I want to call a Script that is located in the repository.

I could of course do the following:

#!/bin/sh
../../myscript.sh

but I think thats not nice ;)

so how do I get the path of my project within the post-commit script?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆追雨的时光 2024-10-27 23:05:23

当您处理非裸存储库时,post-commit1 挂钩将使用存储库工作树的当前工作目录运行。因此,您不需要 ../../

如果您想要脚本的完整路径,您可以随时执行以下操作:

SCRIPT=$(readlink -nf myscript.sh)

... 或者您可以使用 git rev-parse --show-toplevel 来获取工作目录的绝对路径:

SCRIPT=$(git rev-parse --show-toplevel)/myscript.sh

1 ...但请注意,其他一些钩子并非如此,例如在非裸存储库中的 post-receive 钩子中,当前工作目录是 .git 目录。

When you're dealing with a non-bare repository, the post-commit1 hook is run with a current working directory of the working tree of the repository. So, you don't need the ../../.

If you want the full path of the script, you could always do:

SCRIPT=$(readlink -nf myscript.sh)

... or you could use git rev-parse --show-toplevel to get an absolute path to the working directory:

SCRIPT=$(git rev-parse --show-toplevel)/myscript.sh

1 ... but note that this is not true of some other hooks, e.g. in a post-receive hook in a non-bare repository, the current working directory is the .git directory.

俯瞰星空 2024-10-27 23:05:23

这仍然有效吗?
我的提交后包含

#!/bin/sh
exec ./../../myscript.sh

并有效。
仅使用 exec myscript.shmyscript.sh 不起作用。

此外,如果要求输入密码,即使 myscript.sh 也会返回 /.git/hooks 文件夹。
使用 $GIT_WORK_TREE 指向 ~.我目前没有看到任何指向存储库根的可靠方法。

Is this still vaild?
my post-commit contains

#!/bin/sh
exec ./../../myscript.sh

and works.
Using just exec myscript.sh or myscript.sh doesn't work.

Moreover, even myscript.sh returns the /.git/hooks folder if asked for pwd.
Using $GIT_WORK_TREE points to ~. I currently see no robust way pointing to the repo root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文