还是把参考文献放到最上面,因为很清楚我所写只是一种“记录”;

瞎折腾!

  1. 8.3 自定义 Git - Git 钩子

everything-is-local 一切都是本地的

我喜欢git这句话!(自己注意官方文档的左上部分,这句话貌似随机的)

  1. Pro git 2nd Edition (2014)

macOS 用户推荐下载epub格式的,用自带软件阅读(主要是有个计时功能能记录阅读时间,反正我看到显示今天阅读已经30分钟,就感觉又是个小小小小小提升),win及Linux推荐用Chrome等浏览器阅读PDF,感觉现在浏览器的PDF阅读体验不错;

  1. 一文带你彻底学会 Git Hooks 配置

起因

我校社群近期搞了个Missing Semester 组队学习计划

这个计划是补充我们在校大学生在课堂接触少的一些实用开发技能;

其实那个计划我全玩过,但是也是某些深入学习过,一部分就是蜻蜓点水般,更重要的是很开心现在 scnu 在逐步有个大家共同学习的氛围,当然目前也还是很小一部分;

对这个计划感兴趣的人可以通过该链接报名参加一起学习; (过时了)

分割线

过年前重温了下 shell 那部分,其中有个 bash 脚本部分,刚好我去广图借了本《精通Git(第2版)》,其实就是《Pro Git,Second Edition》的中文版,阅读看到 Git Hooks 部分;

简单解释,Git可以在某些重要的事件发生时自动触发自定义脚本;

hook 这个单词意思为 “钩”;

Google 搜了 hooks 一下,搜到一个名词 钩子编程 --- hooking这个解释更到位;

进入正题,我就有个想法了,我可以修改Github仓库中 .git/hooks/pre-commit 文件,当我执行 git commit 该命令的时候,会自动执行 pre-commit 该脚本,终端会回显一些文本信息出来;

实践

添加的脚本内容如下,很简单很普通,就是加点乐趣!

#!/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".

cur_moment=`date '+%H'`

if [ $cur_moment -lt 8 ];
then
    echo "你可真是个夜猫子啊!"
    echo "你这个点还不睡!"
    echo "是准备顺带拿肝玩原神吗?"
    echo "你装模作样提交什么垃圾代码呀!"
    echo "人生在世 健康要紧 懂吗? 年轻人"
    echo " ____  ____  " 
    echo "/ ___|| __ ) "
    echo "\___ \|  _ \ "
    echo " ___) | |_) |"
    echo "|____/|____/ "
    echo "             "
elif [ $cur_moment -lt 12 ];
then
    echo "不错呀! 年轻人"
    echo "美好的一天也要好好码代码呀!"
    echo "记得多喝水!"
    echo "               "
    echo "┬ ┬┌─┐┌┬┐┌─┐┬─┐"
    echo "│││├─┤ │ ├┤ ├┬┘"
    echo "└┴┘┴ ┴ ┴ └─┘┴└─"
    echo "               "
elif [ $cur_moment -lt 14 ];
then 
    echo "该吃饭就给我好好吃饭 好吧"
    echo "                       "
    echo "   _   _   _   _   _   "
    echo "  / \ / \ / \ / \ / \  "
    echo " ( s | o | h | a | i ) "
    echo "  \_/ \_/ \_/ \_/ \_/  "
    echo "                       "
elif [ $cur_moment -lt 18 ];
then 
    echo "码代码的最佳时间段"
    echo "一鼓作气把问题解决吧!"
    echo "                                                                 "
    echo " ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄▄▄▄▄▄▄▄▄▄▄  ▄         ▄  ▄▄▄▄▄▄▄▄▄▄▄ " 
    echo "▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌       ▐░▌▐░░░░░░░░░░░▌"
    echo "▐░█▀▀▀▀▀▀▀▀▀  ▀▀▀▀█░█▀▀▀▀ ▐░█▀▀▀▀▀▀▀▀▀ ▐░▌       ▐░▌ ▀▀▀▀█░█▀▀▀▀ "
    echo "▐░▌               ▐░▌     ▐░▌          ▐░▌       ▐░▌     ▐░▌     "
    echo "▐░█▄▄▄▄▄▄▄▄▄      ▐░▌     ▐░▌ ▄▄▄▄▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌     ▐░▌     "
    echo "▐░░░░░░░░░░░▌     ▐░▌     ▐░▌▐░░░░░░░░▌▐░░░░░░░░░░░▌     ▐░▌     "
    echo "▐░█▀▀▀▀▀▀▀▀▀      ▐░▌     ▐░▌ ▀▀▀▀▀▀█░▌▐░█▀▀▀▀▀▀▀█░▌     ▐░▌     "
    echo "▐░▌               ▐░▌     ▐░▌       ▐░▌▐░▌       ▐░▌     ▐░▌     "
    echo "▐░▌           ▄▄▄▄█░█▄▄▄▄ ▐░█▄▄▄▄▄▄▄█░▌▐░▌       ▐░▌     ▐░▌     "
    echo "▐░▌          ▐░░░░░░░░░░░▌▐░░░░░░░░░░░▌▐░▌       ▐░▌     ▐░▌     "
    echo " ▀            ▀▀▀▀▀▀▀▀▀▀▀  ▀▀▀▀▀▀▀▀▀▀▀  ▀         ▀       ▀      "
    echo "                                                                 "
elif [ $cur_moment -lt 22 ];
then 
    echo "这个点适当学点东西也是不错的"
    echo "注意劳逸结合呀!"
    echo "                      "
    echo "k)             k)     "
    echo "k)             k)     "
    echo "k)  KK  o)OOO  k)  KK "
    echo "k)KK   o)   OO k)KK   "
    echo "k) KK  o)   OO k) KK  "
    echo "k)  KK  o)OOO  k)  KK "
    echo "                      "
elif [ $cur_moment -lt 24 ];
then 
    echo "早点睡啦 早点睡啦"
    echo "最近新闻太多啦!"
    echo "这世道还有比命更值钱的东西吗?"
    echo "                                                                                                         "
    echo "########  ##       ########    ###     ######  ########     ######  ##       ######## ######## ########  "
    echo "##     ## ##       ##         ## ##   ##    ## ##          ##    ## ##       ##       ##       ##     ## "
    echo "##     ## ##       ##        ##   ##  ##       ##          ##       ##       ##       ##       ##     ## "
    echo "########  ##       ######   ##     ##  ######  ######       ######  ##       ######   ######   ########  "
    echo "##        ##       ##       #########       ## ##                ## ##       ##       ##       ##        "
    echo "##        ##       ##       ##     ## ##    ## ##          ##    ## ##       ##       ##       ##        "
    echo "##        ######## ######## ##     ##  ######  ########     ######  ######## ######## ######## ##        "
    echo "                                                                                                         "
fi


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

ascii art

蛮喜欢 ASCII 码绘画这种东西的!

figlet

figlet-npm

VSCode Figlet相关扩展

  • Vscode Figlet
  • Banner comments
  • ASCIIDecorator
  • Banner Comments +

小bug

当我的仓库自动执行 pre-commit 脚本碰到一个 bug

显示一些不能说得上是问题的 warning

eg

src/杂项中的杂项/bash-script/bash-date.sh:2: trailing whitespace.
+date 
src/杂项中的杂项/bash-script/bash-date.sh:8: trailing whitespace.
+date +%A
src/杂项中的杂项/bash-script/bash-date.sh:11: trailing whitespace.
+date +%b
src/杂项中的杂项/bash-script/bash-date.sh:14: trailing whitespace.
...........

这是 git 对代码规范的校验,即代码中不允许以空格结尾,以避免提交无效的代码。

pre-commit 中的这段代码

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

那我直接注释掉就好了,因为我就没打算用这个脚本真的帮助到我什么;

End

注意呀,我只会在自己的一些笔记项目或者无聊瞎搞项目这么玩,不会乱搞别人的项目,虽然 .git 文件夹不会被上传的,但是如果有些项目中有规定了自己的脚本。那就不要这么搞啊!

就是纯粹玩玩而已呀;

Last modification:September 7, 2022
兴趣使然