post-commit
post-commitフックはgit hooksの一機能で「すべてのコミット処理が完了」すると実行されます。
git hooksとはカスタムスクリプトを起動するフック機能です。(詳細は公式ドキュメント参照)
実例
以下の記事ではpost-commitのシェルスクリプトを作成するpythonコードが紹介しています。
【Pythonバージョン管理】git hookを使用してコミットをトリガーにpyproject.tomlとgit tagを更新するスクリプトについて
コード
create_post-commit.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | #!/usr/bin/env bash
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
create_post_commit() {
cat > "$1" << EOF
#!/usr/bin/env bash
source "$SCRIPT_DIR/../.venv/bin/activate"
poetry run python "$SCRIPT_DIR/../ci/run_git_tag_base_pyproject.py"
if [ $? -ne 0 ]; then
printf "Error occurred in run_git_tag_base_pyproject.py. Exiting post-commit.\n"
exit 1
fi
git push origin main:main
git push --tags
printf ".git/hooks/post-commit end!!!\n"
EOF
if [ "$2" == "execute" ]; then
chmod +x "$1"
echo "$1 created with execution permission."
else
echo "$1 created."
fi
}
if [ -f "$SCRIPT_DIR/.git/hooks/post-commit" ]; then
# For shellcheck SC2162
read -r -p "$SCRIPT_DIR/../.git/hooks/post-commit already exists. Do you want to create $SCRIPT_DIR/.git/hooks/post-commit.second instead? (y/N): " choice
if [[ $choice == "y" || $choice == "Y" ]]; then
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit.second"
exit 0
else
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
fi
fi
create_post_commit "$SCRIPT_DIR/../.git/hooks/post-commit" "execute"
exit 0
|