如何在遥控器上挂接 git pull ?
当远程发生 git pull 时,有没有办法挂钩(类似于预接收或后接收)。基本上,我希望能够在拉动时使遥控器提交它所拥有的任何内容。
在我的情况下,远程上的任何内容都是权威来源,可以在没有 git commit 的情况下进行修改。我想确保当我拉动时我总是能够获得最新的实时内容。
Is there a way to hook when a git pull happens on the remote (similar to a pre-receive or post-receive). Basically I'd like to be able to cause the remote to commit whatever it has when there is a pull.
In my situation, whatever is live on the remote is an authoritative source which may get modified without a git commit. I want to make sure when I pull I'm always able to get the latest of whatever is live.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,回答您的实际问题:当有人获取时,远程端不会调用任何钩子。 (当有人拉取时,远程只知道他们是从它那里获取的 - 它不知道他们是否运行了 git pull 、 git fetch 、 git 远程更新...)
至于你的实际情况:我同意 Magnus 的观点,最好是在编辑后进行提交,或者如果失败的话,有某种定期任务(cronjob?)来检查修改并提交(如果发现)。如果您不喜欢这些选择中的任何一个,那么您就需要简化事情,以便您可以在拉取之前快速轻松地在远程存储库中触发提交。
另外,我建议不要将带有工作树的存储库视为规范存储库。如果你需要推动它,那就是自找麻烦。相反,您可以拥有一个裸露的规范存储库,您的实时存储库在提交后将其推送到该存储库,并在该裸存储库中安装一个 post-receive 挂钩,以在必要时更新实时存储库。
First, to answer your actual question: there are no hooks invoked on the remote side when someone fetches. (When someone pulls, all the remote knows is that they fetched from it - it doesn't know whether they ran
git pull
,git fetch
,git remote update
...)As for your actual situation: I agree with Magnus that it'd be best to simply have commits take place after edits, or failing that, have some sort of periodic task (cronjob?) that checks for modifications and commits if it finds any. If you don't like either of those choices, you're left with streamlining things so that it's quick and easy for you to trigger a commit in the remote repository just before pulling.
Also, I'd suggest not regarding a repository with a work tree as your canonical repository. It's asking for trouble if you ever need to push to it. Instead, you could have a bare canonical repository which your live repository pushes to after committing, and install a post-receive hook in that bare repository to update the live repository when necessary.
不幸的是,git 本身并不为此提供钩子,因为在允许拉/取之前想要检查某些内容是一个完美的用例。
我不知道你的“远程”是否在本地计算机上,但如果不是,请查看 gitolite,它具有专门用于此目的的钩子:
来自 v2 gitolite 文档:
对于 v3 gitolite,这里是有关触发器的文档。该文档有点神秘,但它的工作原理如下:
在 ~/.gitolite.rc 添加(在全局范围内):
<代码>PRE_GIT =>'
[
'
]
在同一个文件中查看允许设置 LOCAL_CODE 的行并将其设置为您喜欢的任何位置
在为 LOCAL_CODE 设置的目录中,创建一个名为“triggers”的子目录,并创建一个名为
的脚本,其中包含您当时想要执行的任何操作...(确保执行以下操作:chmod +x
运行
gitolite setup
测试&祝你有美好的一天
更新:实际使用gitolite,我认为你可以让 pre-git 触发器执行一些操作,例如推送到不存在的分支,然后挂钩到非裸存储库中的预接收以拒绝推送,但要执行 git add --all 。 && git commit -m"自动提交" &&在此过程中推送 gitolite。当您不想允许 gitolite 托管用户权限直接在非裸存储库中运行命令时,这非常方便。
Unfortunately git does not natively supply hooks for this, as it is a perfect use-case to want to check something before allowing a pull/fetch.
I don't know if your 'remote' is on a local machine, but if it isn't, have a look at gitolite which has hooks that are meant exactly for this:
From the v2 gitolite docs:
For v3 gitolite, here are the docs about triggers. The documentation is a bit cryptic, but here is how it works:
in ~/.gitolite.rc add (on the global scope):
PRE_GIT =>
[
'<pre_git_trigger_script_name>'
]
in the same file look at the line allowing to set LOCAL_CODE and set it to where ever you like
In the directory that you set for LOCAL_CODE, create a subdirectory called 'triggers' and create a script called
<pre_git_trigger_script_name>
with whatever you want to do at that point... (make sure do to:chmod +x <pre_git_trigger_script_name>
run
gitolite setup
test && have a good day
update: Actually using gitolite, I think you could have the pre-git trigger do something like pushing to an unexisting branch and then hook to pre-receive in your non-bare repository to reject the push, but to do
git add --all . && git commit -m"autocommit" && push gitolite
in the process. This is handy when you don't want to allow the gitolite hosting user privileges to run commands directly in your non-bare repository.我对 git hooks 没有直接的经验,此页面可能有帮助,但没有帮助看起来你能做到。
更简单(也是更好的 IMO)解决方案是使用生产环境以外的存储库作为权威来源。你能做到吗?生产环境很少被用作权威来源,因为最新和最稳定是两个截然不同的东西......
仅供参考,我只在生产环境中执行 git pull 或 git status 。任何更改都会在我的本地存储库上进行,经过测试、提交、推送到 github,然后拉到生产环境。
更新
我应该指出,git 的一大优势和特点是它是一个分布式源代码控制系统。因此,实际上不存在任何权威来源。
I have no direct experience with git hooks, and this page may help, but it doesn't look like you're going to be able to do it.
The easier (and better IMO) solution would be to use a repo other than the production environment as the authoritative source. Can you do this? A production environment is very rarely used as the authoritative source because latest and most stable are two very different things...
FYI, I only ever perform a git pull or git status when in a production environment. Any changes are made on my local repo, tested, committed, pushed to github, then pulled down to the production environment.
UPDATE
I should point out that one of the great strengths and features of git is that it is a distributed source control system. As such, there isn't really any such thing as an authoritative source.
我认为你不能用钩子来做到这一点,根据我阅读钩子文档的理解,没有适合你的要求的钩子。
如果我需要像你想要的东西,我会在“远程”上创建一个每小时运行一次的脚本,并检查是否有任何文件发生更改(git status)并提交所有内容(git commit - a -m“自动提交”)。
I think you can't do this with hooks, for what I understand reading the hooks' doc there's no hook that fits into your requirement.
If I need something like what you want I'd create a script on 'remote' that runs every hour and checks if any file was changes (git status) and commit all (git commit -a -m "Auto commit").
这不是我以前做过的事情,但你可以从 php 内部运行 bash 脚本:
http: //www.devx.com/opensource/Article/40785
https://www.php.net/function.exec
这应该允许您提交并通过 PHP 脚本推送一组更改。在其上添加一个界面或将其集成到您当前的编辑过程中,您就可以开始了。
It's not something I've ever done before, but you can run bash scripts from inside php:
http://www.devx.com/opensource/Article/40785
https://www.php.net/function.exec
Which should allow you to commit and push a set of changes via a PHP script. Chuck an interface over it or integrate it into your current editing process and you should be good to go.
谁/什么在编辑这个文件?如果当有人更改网站上的某些内容时,某些内容就会发生更改,则必须由某些内容触发,这意味着您可以自动执行此操作。当发生这种情况并且保存文件时,您应该触发提交。提交必须在某个时候完成,最好就在那个时间。
Who/what is editing this file? If it's something being changed when someone changes something on a site, that must be triggered by something, which means you can automate this. When this something happens and the file is saved, there you should trigger the commit. The commit has to be done sometime and it might as well be at that time.