🦔

emoji 付きcommit messageを生成する

2024/09/13に公開

commit messageを絵文字付きで書くのだけど、絵文字毎回考えるのが面倒だったので、生成させた時のメモ。
aicommits いいんだけど、絵文字でわかりやすくさせたり、自分なりのフォーマットに合わせたいんよな..
自分でpromptつくれば、choreとかfeatとかfixをつけるangular formatも同様にできる.

code

~/bin/git-ac
#!/bin/bash

set -ue

BRANCH=$(git rev-parse --abbrev-ref HEAD)

cat <<__DESCRIPTION__ | perl -pne 's|\\|\\\\|g' | perl -pne 's|\n|\\n|g' | perl -pne 's|\t|\\t|g' | perl -pne 's|"|\\"|g' > /tmp/gitac-description.txt
You are commit message generator.
Generate a commit log text in conventional commits format by referencing Rules and user input.

## Rules

Format:
  <emoji> <branch>? <message>

Arguments:
  <branch> git branch name. current branch is "$BRANCH". If current branch is "master" or "main", skip this argument as empty string.
  <emoji> github emoji text to describe commit summary. emoji is picked up by following.
    :seedling: New feature, logic, functions.
    :tada: Release something.
    :bug: Fix bugs.
    :recycle: Refactor, rename and format code.
    :scissors: Delete files or some resources.
    :pencil2: Write comments, docs.
    :up: Update version or some dependencies of application.
    :package: Install resources, modules, packages.
  <message> git commit message for summerizing git diff change.

Examples:
  :seedling: feature/123 Added a new feature for user authentication.
  :bug: fix/123 Fix bug of user email format.
  :pencil2: feature/123 Add usage section in README.
  :package: Install UniRx and UniTask.

__DESCRIPTION__

SYSTEM_PROMPT=$(< /tmp/gitac-description.txt)
USER_PROMPT=$(git diff --cached | perl -pne 's|\\|\\\\|g' | perl -pne 's|\n|\\n|g' | perl -pne 's|\t|\\t|g' | perl -pne 's|"|\\"|g')

cat <<__JSON__ > /tmp/gitac.json
{
  "model": "gpt-4o-mini",
  "messages": [
    {
      "role": "system",
      "content": "$SYSTEM_PROMPT"
    },
    {
      "role": "user",
      "content": "$USER_PROMPT"
    }
  ],
  "temperature": 0.1
}
__JSON__

COMMIT_MESSAGE=$(curl -Ls https://api.openai.com/v1/chat/completions \
	-H "Content-Type: application/json" \
	-H "Authorization: Bearer $OPENAI_APIKEY" \
  --data @/tmp/gitac.json | jq -rMc '.choices[0].message.content')

git commit -m "\"$COMMIT_MESSAGE\""

ポイント

  • temperatureは0.1で低めにする
  • ちゃんと入力はエスケープする (tab, newline, backslash)
  • apikeyはちゃんと作って環境変数に入れておく

Usage

自分は $HOME/binにpath通して雑に使ってる。
chmod +x ~/bin/git-ac で gitのサブコマンドとして利用可能.

実行例:

$ git ac
$ git log -p
commit e6dfff64309a65112a5f8a963e5991af37dde8e4 (HEAD -> master, origin/master, origin/HEAD)
Author: mattak <mattak.me@gmail.com>
Date:   Fri Sep 13 17:54:13 2024 +0900

    :seedling: Added new Zsh configuration files for Dart and Conda, and updated .zshrc to source them.

diff --git a/.zshrc b/.zshrc
index 7be0d39..3a002ef 100644
--- a/.zshrc
+++ b/.zshrc
@@ -90,4 +90,6 @@ precmd () {

 [[ -s $HOME/.profile ]] && source $HOME/.profile
 [[ -s $HOME/.zshrc.local ]] && source $HOME/.zshrc.local
+[[ -s "$HOME/.zshrcs/dart.zshrc" ]] && source "$HOME/.zshrcs/dart.zshrc"
+[[ -s "$HOME/.zshrcs/conda.zshrc" ]] && source "$HOME/.zshrcs/conda.zshrc"

Discussion