xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

git hooks in action All In One

git hooks in action All In One

git hooks 实战 / git 钩子实战

https://git-scm.com/docs/githooks

git hooks

钩子都被存储在 Git 目录下的 hooks 子目录中, 即绝大部分项目中的 .git/hooks 文件夹下

$ cd ./.git && ls -al

$ cd ./hooks && ls -al

total 120
drwxr-xr-x  15 xgqfrms-mbp  staff   480 Oct 17 21:28 .
drwxr-xr-x  17 xgqfrms-mbp  staff   544 Oct 24 21:56 ..
-rwxr-xr-x   1 xgqfrms-mbp  staff   478 Oct 17 21:28 applypatch-msg.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff   896 Oct 17 21:28 commit-msg.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  4655 Oct 17 21:28 fsmonitor-watchman.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff   189 Oct 17 21:28 post-update.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff   424 Oct 17 21:28 pre-applypatch.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  1643 Oct 17 21:28 pre-commit.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff   416 Oct 17 21:28 pre-merge-commit.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  1374 Oct 17 21:28 pre-push.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  4898 Oct 17 21:28 pre-rebase.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff   544 Oct 17 21:28 pre-receive.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  1492 Oct 17 21:28 prepare-commit-msg.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  2783 Oct 17 21:28 push-to-checkout.sample
-rwxr-xr-x   1 xgqfrms-mbp  staff  3650 Oct 17 21:28 update.sample

$ code ./pre-commit.sample
# OR
$ cat ./pre-commit.sample

$ vim ./pre-commit.sample
# ESC + :q, exit vim
# ESC + :!q, exit vim
$ vi ./pre-commit.sample
$ nano ./pre-commit.sample
# Control + X, exit nano

$ cat ./pre-commit.sample

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
then
        against=HEAD
else
        # Initial commit: diff against an empty tree object
        against=$(git hash-object -t tree /dev/null)
fi

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
        # Note that the use of brackets around a tr range is ok here, (it's
        # even required, for portability to Solaris 10's /usr/bin/tr), since
        # the square bracket bytes happen to fall in the designated range.
        test $(git diff --cached --name-only --diff-filter=A -z $against |
          LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
then
        cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
EOF
        exit 1
fi

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

https://git-scm.com/book/zh/v2/自定义-Git-Git-钩子

https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

https://git-scm.com/docs/githooks

ESLint


husky

Git hooks made easy 🐶 woof!

https://typicode.github.io/husky/#/

https://github.com/typicode/husky

demos

Python Script

#!/usr/bin/python3 

# 这是一个单行注释
print("Hello, World!");

'''
这是多行注释,用三个单引号
这是多行注释,用三个单引号 
这是多行注释,用三个单引号
'''

"""
这是多行注释,用三个双引号
这是多行注释,用三个双引号 
这是多行注释,用三个双引号
"""

必须使用无扩展名的 pre-commit 脚本文件名,不然 git 不知道扩展名是什么!

脚本内部可以指定不同的脚本执行环境,如 Perl, Python 等

??? js script / Node.js script ??? ❓

👻为什么 git 不可以忽略扩展名,因为 .sample 文件名也相同,无法区分出来!

git hook 的脚本不能使用扩展名

pre-commit

pre-commit.py

https://github.com/web-full-stack/git

https://github.com/xgqfrms/git/tree/master/git-hooks

ESLint

refs

https://www.cnblogs.com/xgqfrms/p/14194242.html

https://www.atlassian.com/git/tutorials/git-hooks

https://askubuntu.com/questions/932713/what-is-the-difference-between-chmod-x-and-chmod-755



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-10-24 18:03  xgqfrms  阅读(28)  评论(5编辑  收藏  举报