本文仅写了 macOS 下的实践, 其他系统可参照修改。
通过 homebrew 安装 imagesnap:
brew install imagesnap
在用户文件夹下新建 gitshot 文件夹:
mkdir ~/gitshots
创建 Git Template 文件夹:
mkdir -p ~/.git-template/hooks
创建 pre-commit git hook 文件: ~/.git-template/hooks/pre-commit
#!/usr/bin/env ruby
file="~/gitshots/#{Time.now.to_i}.jpg"
unless File.directory?(File.expand_path("../../rebase-merge", __FILE__))
puts "😃 -> #{file}!"
system "imagesnap -q -w 1 #{file} &"
end
exit 0
给 pre-commit 文件添加可执行权限:
chmod +x ~/.git-template/hooks/pre-commit
设置为 git 的初始化模板:
git config --global init.templatedir '~/.git-template'
Done!
现在, 新创建和新克隆的 repo 里, 每次执行 git commit 都会用前置摄像头自拍一张,以时间戳为文件名,放入 ~/gitshots
文件夹下。
已经存在的 Repo,需要再次执行下 git init
即可。
大家还可以修改 pre-commit 文件,增加更多好玩脚本,比如添加上 git 的所在仓库名称,和 commit message 之类。
Happy Coding!