开发者

Is there any git hook for pull?

开发者 https://www.devze.com 2023-01-24 23:19 出处:网络
I need to perform some actions (prepare gettext *.mo message files) on my project e开发者_Go百科verytime I run git pull. Is there any suitable git hook, which I could use for this purpose please?The g

I need to perform some actions (prepare gettext *.mo message files) on my project e开发者_Go百科verytime I run git pull. Is there any suitable git hook, which I could use for this purpose please?


The githooks man page is a complete list of hooks. If it's not on there, it doesn't exist.

That said, there is a post-merge hook, and all pulls include a merge, though not all merges are pulls. It's run after merges, and can't affect the outcome. It never gets executed if there were conflicts; you'd have to pick that up with the post-commit hook if it really matters, or invoke it manually.


post-merge - see https://git-scm.com/docs/githooks#_post_merge for more details of how to use it.


This approach works for me.

First, add a file named post-merge to /path/to/your_project/.git/hooks/

cd /path/to/your_project/.git/hooks/
touch post-merge

Then, change it's ownership to same as <your_project> folder(this is the same as nginx and php-fpm runner), in my case, I use www:www

sudo chown www:www post-merge

Then change it's file mode to 775(then it can be executed)

sudo chmod 775 post-merge

Then put the snippet below to post-merge. To understand the snippet, see here(actually that's me).

#!/bin/sh

# default owner user
OWNER="www:www"

# changed file permission
PERMISSION="664"

# web repository directory
REPO_DIR="/www/wwwroot/your_project/"

# remote repository
REMOTE_REPO="origin"

# public branch of the remote repository
REMOTE_REPO_BRANCH="master"

cd $REPO_DIR || exit
unset GIT_DIR
files="$(git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD)"

for file in $files
do
  sudo chown $OWNER $file
  sudo chmod $PERMISSION $file
done

exec git-update-server-info

Everything is done, now, go back to your_project folder

cd /path/to/your_project/

run git pull under your_project folder, remember you must run as root or sudo(I remember sudo)

sudo git pull

Now check the new file that pulled from remote repository, see if its ownership has been changed to www:www(if it was as expected, the ownership of the new pulled file should be changed to www:www).

This approach is much better than sudo chown -R www:www /www/wwwroot/your_project/, because it only change the new file's ownership, not all of then! Say I just pulled 2 new file, if you change the whole folder's ownership, it's costs more time and server resources(cpu usage, memory usage...), that's totally unnecessary.


post-merge is the closest to a true post-pull hook, as the other answers point out, but consider adding a post-checkout hook if you want to catch the changes post-merge misses.

From the docs:

This hook is invoked when a git-checkout or git-switch is run after having updated the worktree.

So anytime your working copy changes, because you are "switching what you are working on" / anyone on your team has worked on, the hook runs.

A practical example where the behavior differs is that post-merge doesn't trigger when you jump back to (check out a commit from) the past (because there is no merge)

0

精彩评论

暂无评论...
验证码 换一张
取 消