Skip to content

Instantly share code, notes, and snippets.

@negokaz
Last active August 23, 2018 03:51
Show Gist options
  • Save negokaz/27a39274061a728ebd09f847ea4e93b4 to your computer and use it in GitHub Desktop.
Save negokaz/27a39274061a728ebd09f847ea4e93b4 to your computer and use it in GitHub Desktop.
git prepare-commit-msg hook for emojified commit message prefix
#!/bin/bash
# git prepare-commit-msg hook for emojified commit message prefix
# inspired by:
# - Angular Commit Message Conventions
# https://github.com/angular/angular.js/blob/v1.7.2/DEVELOPERS.md#type
# - Git Commit message Emoji · GitHub
# https://gist.github.com/parmentf/035de27d6ed1dce0b36a
#
# Install
# - Run following command in your git working directory
# bash:
# curl https://gist.githubusercontent.com/negokaz/27a39274061a728ebd09f847ea4e93b4/raw/prepare-commit-msg -o .git/hooks/prepare-commit-msg && chmod +x .git/hooks/prepare-commit-msg
# fish:
# curl https://gist.githubusercontent.com/negokaz/27a39274061a728ebd09f847ea4e93b4/raw/prepare-commit-msg -o .git/hooks/prepare-commit-msg; and chmod +x .git/hooks/prepare-commit-msg
readonly COMMIT_MSG_FILE=$1
readonly COMMIT_SOURCE=$2
readonly SHA1=$3
to_emoji () {
echo -n $1 | perl -CO -pE 's/\\u(\p{Hex}+)/chr(hex($1))/xieg'
}
readonly emoji_feat="$(to_emoji '\U2728')"
readonly emoji_fix="$(to_emoji '\U1f41e')"
readonly emoji_doc="$(to_emoji '\U1f4da')"
readonly emoji_style="$(to_emoji '\U1f484')"
readonly emoji_refactor="$(to_emoji '\U1f528')"
readonly emoji_pref="$(to_emoji '\U1f680')"
readonly emoji_test="$(to_emoji '\U1f6a8')"
readonly emoji_chore="$(to_emoji '\U1f477')"
readonly emoji_merge="$(to_emoji '\U1f500')"
create_full_template() {
local msg_temp_file="$(mktemp)"
{
echo "# Overview (Uncomment one of the following templates)"
echo "#${emoji_feat}feat: "
echo "# └ A new feature"
echo "#${emoji_fix}fix: "
echo "# └ A bug fix"
echo "#${emoji_doc}doc: "
echo "# └ Documentation only changes"
echo "#${emoji_style}style: "
echo "# └ Changes that do not affect the meaning of the code"
echo "# (white-space, formatting, missing semi-colons, etc)"
echo "#${emoji_refactor}refactor: "
echo "# └ A code change that neither fixes a bug nor adds a feature"
echo "#${emoji_pref}perf: "
echo "# └ A code change that improves performance"
echo "#${emoji_test}test: "
echo "# └ Adding missing or correcting existing tests"
echo "#${emoji_chore}chore: "
echo "# └ Changes to the build process or auxiliary tools and libraries "
echo "# such as documentation generation"
echo ""
echo "# Details"
echo ""
cat "${COMMIT_MSG_FILE}"
} > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
add_emoji_by_message_prefix() {
local msg_temp_file="$(mktemp)"
{
if grep --silent '^\s*feat:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_feat}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*fix:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_fix}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*doc:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_doc}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*style:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_style}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*refactor:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_refactor}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*perf:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_pref}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*test:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_test}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*chore:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_chore}$(cat ${COMMIT_MSG_FILE})"
else
cat "${COMMIT_MSG_FILE}"
fi
} > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
add_merge_emoji() {
local msg_temp_file="$(mktemp)"
echo "${emoji_merge}$(cat ${COMMIT_MSG_FILE})" > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
case "${COMMIT_SOURCE}" in
message) # use -m/-F
add_emoji_by_message_prefix
;;
template) # use template (ex: -t option)
: # do nothing
;;
merge) # merge commit
add_merge_emoji
;;
squash) # squash commits in a branch with --squash
: # do nothing
;;
commit) # use -c/-C/--amend
: # do nothing
;;
*) # no option
create_full_template
;;
esac
#!/bin/bash
# git prepare-commit-msg hook for emojified commit message prefix
# inspired by:
# - Angular Commit Message Conventions
# https://github.com/angular/angular.js/blob/v1.7.2/DEVELOPERS.md#type
# - Git Commit message Emoji · GitHub
# https://gist.github.com/parmentf/035de27d6ed1dce0b36a
#
# Install
# - Run following command in your git working directory
# bash:
# curl https://gist.githubusercontent.com/negokaz/27a39274061a728ebd09f847ea4e93b4/raw/prepare-commit-msg.ja -o .git/hooks/prepare-commit-msg && chmod +x .git/hooks/prepare-commit-msg
# fish:
# curl https://gist.githubusercontent.com/negokaz/27a39274061a728ebd09f847ea4e93b4/raw/prepare-commit-msg.ja -o .git/hooks/prepare-commit-msg; and chmod +x .git/hooks/prepare-commit-msg
readonly COMMIT_MSG_FILE=$1
readonly COMMIT_SOURCE=$2
readonly SHA1=$3
to_emoji () {
echo -n $1 | perl -CO -pE 's/\\u(\p{Hex}+)/chr(hex($1))/xieg'
}
readonly emoji_feat="$(to_emoji '\U2728')"
readonly emoji_fix="$(to_emoji '\U1f41e')"
readonly emoji_doc="$(to_emoji '\U1f4da')"
readonly emoji_style="$(to_emoji '\U1f484')"
readonly emoji_refactor="$(to_emoji '\U1f528')"
readonly emoji_pref="$(to_emoji '\U1f680')"
readonly emoji_test="$(to_emoji '\U1f6a8')"
readonly emoji_chore="$(to_emoji '\U1f477')"
readonly emoji_merge="$(to_emoji '\U1f500')"
create_full_template() {
local msg_temp_file="$(mktemp)"
{
echo "# 概要 (下記テンプレートを1つ選択してコメントを外してください)"
echo "#${emoji_feat}feat: "
echo "# └ 新機能追加"
echo "#${emoji_fix}fix: "
echo "# └ バグ修正"
echo "#${emoji_doc}doc: "
echo "# └ ドキュメントのみの変更"
echo "#${emoji_style}style: "
echo "# └ プログラムの動きに影響を与えない変更"
echo "# (インデントの調整やフォーマッタにかけた場合など)"
echo "#${emoji_refactor}refactor: "
echo "# └ バグ修正や新機能追加以外のコード修正"
echo "#${emoji_pref}perf: "
echo "# └ パフォーマンスを改善するためのコード修正"
echo "#${emoji_test}test: "
echo "# └ テストの追加や既に存在するテストの修正"
echo "#${emoji_chore}chore: "
echo "# └ ビルドプロセスやドキュメント生成のような補助ツールやライブラリの変更"
echo ""
echo "# 詳細"
echo ""
cat "${COMMIT_MSG_FILE}"
} > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
add_emoji_by_message_prefix() {
local msg_temp_file="$(mktemp)"
{
if grep --silent '^\s*feat:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_feat}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*fix:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_fix}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*doc:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_doc}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*style:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_style}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*refactor:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_refactor}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*perf:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_pref}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*test:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_test}$(cat ${COMMIT_MSG_FILE})"
elif grep --silent '^\s*chore:' "${COMMIT_MSG_FILE}"; then
echo "${emoji_chore}$(cat ${COMMIT_MSG_FILE})"
else
cat "${COMMIT_MSG_FILE}"
fi
} > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
add_merge_emoji() {
local msg_temp_file="$(mktemp)"
echo "${emoji_merge}$(cat ${COMMIT_MSG_FILE})" > "${msg_temp_file}"
cat "${msg_temp_file}" > "${COMMIT_MSG_FILE}"
rm "${msg_temp_file}"
}
case "${COMMIT_SOURCE}" in
message) # use -m/-F
add_emoji_by_message_prefix
;;
template) # use template (ex: -t option)
: # do nothing
;;
merge) # merge commit
add_merge_emoji
;;
squash) # squash commits in a branch with --squash
: # do nothing
;;
commit) # use -c/-C/--amend
: # do nothing
;;
*) # no option
create_full_template
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment