From dae841d4bbccc21b07e0810e7c005c425d0f8e70 Mon Sep 17 00:00:00 2001 From: h-yoshikawa44 <43331308+h-yoshikawa44@users.noreply.github.com> Date: Sat, 17 Aug 2024 18:08:44 +0900 Subject: [PATCH] =?UTF-8?q?settings:=20Issue=E7=95=AA=E5=8F=B7=E6=8A=BD?= =?UTF-8?q?=E5=87=BA=E3=81=AEGitHooks=E8=A8=AD=E5=AE=9A(#1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmessage | 4 +-- .../extraction-issue-number | 30 +++++++++++++++++++ lefthook.yml | 9 ++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 .lefthook/prepare-commit-msg/extraction-issue-number diff --git a/.gitmessage b/.gitmessage index c4a421c..99f365f 100644 --- a/.gitmessage +++ b/.gitmessage @@ -1,4 +1,4 @@ -: (#) +: (#Issue) # ==== Type ==== # feat: 機能追加 @@ -26,4 +26,4 @@ # 7. Use the body to explain what and why vs. how # (内容で、何が変わったか、なぜそうしたのか、どうやったのかを説明する) # -# How to Write a Git Commit Message https://chris.beams.io/posts/git-commit/ \ No newline at end of file +# How to Write a Git Commit Message https://chris.beams.io/posts/git-commit/ diff --git a/.lefthook/prepare-commit-msg/extraction-issue-number b/.lefthook/prepare-commit-msg/extraction-issue-number new file mode 100644 index 0000000..48d892d --- /dev/null +++ b/.lefthook/prepare-commit-msg/extraction-issue-number @@ -0,0 +1,30 @@ +#!/bin/sh + +# sed コマンドが GNU か BSD か確認 +GNU_SED=true +sed --version 1>/dev/null 2>/dev/null || GNU_SED=false + +# コミットメッセージ入力前に、ブランチ名から Issue 番号を抽出して置換する +COMMIT_MSG_FILE=$1 +MESSAGE=$(cat "$COMMIT_MSG_FILE") + +ISSUE_NUMBER=$(git rev-parse --abbrev-ref HEAD | grep -Eo "^(feature|bugfix|release)/[0-9]+" | grep -Eo "[0-9]+") +if [ -n "$ISSUE_NUMBER" ]; then + if [ "$GNU_SED" == "true" ]; then + sed -i "s/(#Issue)/(#$ISSUE_NUMBER)/" $COMMIT_MSG_FILE + else + sed -i "" "s/(#Issue)/(#$ISSUE_NUMBER)/" $COMMIT_MSG_FILE + fi + exit 0 +fi + +read -p "Issue 番号がブランチ名にないので置換できませんが、続行しますか? (y/N): " YM < /dev/tty +case "$YM" in + [yY]*) + if [ "$GNU_SED" == "true" ]; then + sed -i "s/(#Issue)//" $COMMIT_MSG_FILE + else + sed -i "" "s/(#Issue)//" $COMMIT_MSG_FILE + fi;; + *) echo "abort." ; exit 1 ;; +esac diff --git a/lefthook.yml b/lefthook.yml index 12279e6..5fe1100 100644 --- a/lefthook.yml +++ b/lefthook.yml @@ -24,3 +24,12 @@ pre-commit: stage_fixed: true skip: - merge + +prepare-commit-msg: + scripts: + 'extraction-issue-number': + # 応答する処理があっても動作するようにする + interactive: true + runner: sh + skip: + - rebase