How do I hook a git pull on the remote?

后端 未结 6 1513
陌清茗
陌清茗 2021-02-05 21:19

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 i

6条回答
  •  暖寄归人
    2021-02-05 22:02

    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:

    "gl-pre-git" hook

    Although git has lots of nice hooks you can tap into, they all run only on a push. There's nothing that runs on a fetch or a clone, and there's no way to run something before git-receive-pack or git-upload-pack, (as the case may be) are invoked.

    That's what the gl-pre-git hook is for. If an executable hook called gl-pre-git is present, it will be invoked with the current directory set to repo.git, and with a single argument which will be either R or W depending on what the client is trying to do.

    For v3 gitolite, here are the docs about triggers. The documentation is a bit cryptic, but here is how it works:

    1. in ~/.gitolite.rc add (on the global scope):

      PRE_GIT => [ '' ]

    2. in the same file look at the line allowing to set LOCAL_CODE and set it to where ever you like

    3. In the directory that you set for LOCAL_CODE, create a subdirectory called 'triggers' and create a script called with whatever you want to do at that point... (make sure do to: chmod +x

    4. run gitolite setup

    5. 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.

提交回复
热议问题