An example about git hook

hustnzj發表於2022-09-06

Hook into Git’s Internals

A hook is a script that Git automatically executes every time a particular event occurs in a repository.

The hooks reside in the .git/hooks directory. Renaming the <action>.sample to <action> will cause the script to be executed when corresponding event occurs.

For example, change post-update.sample to post-update and modify its content as below:


#!/bin/sh

# Output a friendly message

echo "Publishing master branch!" >&2

# Remove the old `my-website` directory (if necessary)

rm -rf ../my-website

# Create a new `my-website` directory

mkdir ../my-website

# Archive the `master` branch

git archive master --format=tar --output=../my-website.tar

# Decompress the archive into the `my-website` directory

tar -xf ../my-website.tar -C ../my-website

exit 0

Note: >&2 is equal to 1>&2 which means that redirecting stdout (file descriptor 1) to stderr (file descriptor 2).

The new post-update script creates an archive of the master branch, then exports it into a directory called my-website.

We can see the script in action by pushing a branch to the central-repo.git repository.


$ git push ../central-repo.git master

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), 317 bytes | 317.00 KiB/s, done.

Total 3 (delta 1), reused 0 (delta 0), pack-reused 0

remote: Publishing master branch!

To ../central-repo.git

e3e4b9d..db77e58 master -> master

After the central repository receives the new master branch, our post-update script is executed. You should see the Publishing master branch! message echoed from the script, along with a new my-website folder in the same directory as my-git-repo. You can open index.html in a web browser to verify that it contains all the files from our master branch, and you can also see the intermediate .tar archive produced by the hook.

If there are some conflict or divergent warning messages, you need to use git rebase or git pull –rebase to fix them.

Note:

  • Before rebasing, you need to fetch the latest remote branches into your local repo, i.e., git fetch <remote>

  • git rebase <remote>/<branch> is basing on <remote>/<branch> and put the commits on current branch but not on *<remote>/<branch>* one by one.

  • If you choose to rebase blindly, just use git pull <remote> <branch> --rebase.

Each of the .sample scripts in the hooks directory represents a different event that you can listen for, and each of them can do anything from automatically creating and publishing releases to enforcing a commit policy, making sure a project compiles, and of course, publishing websites (that means no more clunky FTP uploads). Hooks are even configured on a per-repository basis, which means you can run different scripts in your local repository than your central repository.

For a detailed description of the available hooks, please consult the official Git documentation.

References

Chapter Some tips & tricks in Git in Ry’s Git Tutorial

command line - What does >&2 mean in a shell script? - Ask Ubuntu

本作品採用《CC 協議》,轉載必須註明作者和本文連結
日拱一卒

相關文章