Skip to content

Instantly share code, notes, and snippets.

@YooWaan
Last active September 16, 2019 12:56
Show Gist options
  • Save YooWaan/18cda96d18e161757951353e66e3d7f4 to your computer and use it in GitHub Desktop.
Save YooWaan/18cda96d18e161757951353e66e3d7f4 to your computer and use it in GitHub Desktop.
git-conventinal-or-semantic-comits
#!/bin/bash
typ="$(head -n 1 "$1" | grep -Eo '^[A-Za-z_\-]+(\([^)]+\))*!?:' | grep -o '^[A-Za-z_\-]*')"
case "$typ" in
"fix" | "feat" | "refactor" | "docs" | "test" | "style" | "perf" | "build" | "release" | "snipet" | "security" )
printf "%b%s%b\\n" "\\033[32;1m" "commit $typ" "\\033[0m"
;;
*)
printf "%b%s%b\\n" "\\033[31;1m" "unsupported commit type [${typ}] !!!" "\\033[0m"
exit 1
;;
esac
msg=$(cat "$1")
printf '' >| "$1"
echo "$msg" | sed -e "s/docs\:/$(printf '\xF0\x9F\x93\x9A') &/" -e "s/docs(/$(printf '\xF0\x9F\x93\x9A') &/" \
-e "s/test\:/$(printf '\xF0\x9F\x92\x8A') &/" -e "s/test(/$(printf '\xF0\x9F\x92\x8A') &/" \
-e "s/feat\:/$(printf '\xE2\x9C\x8A') &/" -e "s/feat(/$(printf '\xE2\x9C\x8A') &/" \
-e "s/style\:/$(printf '\xE2\xAD\x90') &/" -e "s/style(/$(printf '\xE2\xAD\x90') &/" \
-e "s/perf\:/$(printf '\xF0\x9F\x8F\x81') &/" -e "s/perf(/$(printf '\xF0\x9F\x8F\x81') &/" \
-e "s/fix\:/$(printf '\xF0\x9F\x94\xA5') &/" -e "s/fix(/$(printf '\xF0\x9F\x94\xA5') &/" \
-e "s/refactor\:/$(printf '\xF0\x9F\x8C\x88') &/" -e "s/refactor(/$(printf '\xF0\x9F\x8C\x88') &/" \
-e "s/build\:/$(printf '\xF0\x9F\x90\xB3') &/" -e "s/build(/$(printf '\xF0\x9F\x90\xB3') &/" \
-e "s/release\:/$(printf '\xF0\x9F\x9A\x80') &/" -e "s/release(/$(printf '\xF0\x9F\x9A\x80') &/" \
-e "s/snipet\:/$(printf '\xF0\x9F\x8D\x84') &/" -e "s/snipet(/$(printf '\xF0\x9F\x8D\x84') &/" \
-e "s/security\:/$(printf '\xF0\x9F\x94\x90') &/" -e "s/security(/$(printf '\xF0\x9F\x94\x90') &/" >> "$1"
#
# Type
#
# ✊ feat: A new feature
# πŸ”₯ fix: A bug fix
# 🌈 refactor: A code change that neither fixes a bug nor adds a feature
# πŸ“š docs: Documentation only changes
# πŸ’Š test: Adding missing tests or correcting existing tests
# ⭐ style: Changes that do not affect the meaning of the code (css, sass, white-space, formatting, missing semi-colons, etc)
# 🏁 perf: A code change that improves performance
# 🐳 build: Changes that affect the build system or external dependencies or our CI configuration files and scripts
# πŸš€ relase: ship it
# πŸ” security: security works
# πŸ„ snipet: experiment or snipet works
#
#
# Scope (Please write scopes)
#
# auth
# service1
# service2
# infra
#
emoji={'feat':'\\xE2\\x9C\\x8A',
'fix': '\\xF0\\x9F\\x94\\xA5',
'refactor': '\\xF0\\x9F\\x8C\\x88',
'docs': '\\xF0\\x9F\\x93\\x9A',
'test': '\\xF0\\x9F\\x92\\x8A',
'style': '\\xE2\\xAD\\x90',
'perf': '\\xF0\\x9F\\x8F\\x81',
'build': '\\xF0\\x9F\\x90\\xB3',
'release': '\\xF0\\x9F\\x9A\\x80',
'snipet': '\\xF0\\x9F\\x8D\\x84',
'security': '\\xF0\\x9F\\x94\\x90'
}
print(' | '.join(['"{}"'.format(syb) for syb in emoji]))
print('sed ' + ' \\\n'.join(['-e "s/{}\\:/$(printf \'{}\') &/" -e "s/{}(/$(printf \'{}\') &/"'.format(syb, emoji[syb], syb, emoji[syb]) for syb in emoji]) + ' >> "$1"')

conventional or semantic commit for loose

features

  • template
  • replace type symbol
  • check loose format check

install

# download commit-template, commit-msg
chmod +x commit-msg
cp commit-msg .git/hooks
git config commit.template <path to template>/commit-template

commit message

<commit type>: description or <commit type>(scope): description

commit flows

develop -> git add/rm -> git commit -> git push
                        /         A
           .-----------'          '----------------------.
           V                                             |
   [template load] ---------.->  <edit mesage>  ---> [replace symbol to emoji]
       `- commit-template -'      * write msg like conventinal

files explain

  • commit-template commit sentence template
  • commit-msg replace and check commit type
  • gen.py generate commit-msg code lines

how to check commit-msg

$ echo "feat: add new func
fix: fix bug
refactor: change structure
docs: write api docs
test: add test case
style: css update
perf: increase
build: modify env variable
release: ship it ver111
snipet: add simple shell
security: modify holl

feat(mod): add feature with scope
fix(mod): fix bug with scope
test(mod): update test case with scope" > msg.txt

$ bash commit-msg msg.txt
commit feat
✊ feat: add new func
πŸ”₯ fix: fix bug
🌈 refactor: change structure
πŸ“š docs: write api docs
πŸ’Š test: add test case
⭐ style: css update
🏁 perf: increase
🐳 build: modify env variable
πŸš€ release: ship it ver111
πŸ„ snipet: add simple shell
πŸ” security: modify holl

✊ feat(mod): add feature with scope
πŸ”₯ fix(mod): fix bug with scope
πŸ’Š test(mod): update test case with scope

how to update commit-msg

# generate code
$ vi gen.py
emoji={'feat':'\\xE2\\x9C\\x8A',
   # update emoji
}

# update commit-msg
$ python gen.py
$ vi commit-msg

case "$typ" in
     "fix" | "feat" | "refactor" | "docs" | "test" | "style" | "perf" | "build" | "release" | "snipet" | "security" )
     # ↑ update condition

...

echo "$msg" | # copy and paste sed commnad

Links

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment