Skip to content

Commit

Permalink
settings: Issue番号抽出のGitHooks設定(#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
h-yoshikawa44 committed Aug 17, 2024
1 parent 7c5cdb2 commit dae841d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitmessage
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
: (#)
: (#Issue)

# ==== Type ====
# feat: 機能追加
Expand Down Expand Up @@ -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/
# How to Write a Git Commit Message https://chris.beams.io/posts/git-commit/
30 changes: 30 additions & 0 deletions .lefthook/prepare-commit-msg/extraction-issue-number
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,12 @@ pre-commit:
stage_fixed: true
skip:
- merge

prepare-commit-msg:
scripts:
'extraction-issue-number':
# 応答する処理があっても動作するようにする
interactive: true
runner: sh
skip:
- rebase

0 comments on commit dae841d

Please sign in to comment.