From 24326186db0be2afb04aebf3b1277495eaaec150 Mon Sep 17 00:00:00 2001 From: yexin Date: Fri, 20 Jul 2018 16:48:42 +0800 Subject: Feat: add commit message template and hook Use cmake to install the template and hook. Change-Id: Ie86ad2e1a4d1fe1999941445be969618b192eb32 Signed-off-by: yexin --- CMakeLists.txt | 16 ++++- scripts/git/commit-msg-hook.py | 136 ++++++++++++++++++++++++++++++++++++++++ scripts/git/commit-msg-template | 28 +++++++++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100755 scripts/git/commit-msg-hook.py create mode 100644 scripts/git/commit-msg-template diff --git a/CMakeLists.txt b/CMakeLists.txt index a3c2a83..fbf5f4d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,9 +37,23 @@ MESSAGE(STATUS "Shared library dir: " ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) add_custom_target(clean-cmake-files - COMMAND ${CMAKE_COMMAND} -P clean-all.cmake + COMMAND ${CMAKE_COMMAND} -P clean-all.cmake ) +execute_process(COMMAND git config --local --get commit.template + OUTPUT_VARIABLE commit_template) +if(commit_template STREQUAL "") + message(STATUS "Setting git commit template...") + execute_process(COMMAND git config --local commit.template ${CMAKE_SOURCE_DIR}/scripts/git/commit-msg-template) + message(STATUS "Setting git commit template...done") +endif() + +if(NOT EXISTS "${CMAKE_SOURCE_DIR}/.git/hooks/commit-msg") + message(STATUS "Setting git commit hook...") + execute_process(COMMAND ln -s ${CMAKE_SOURCE_DIR}/scripts/git/commit-msg-hook.py ${CMAKE_SOURCE_DIR}/.git/hooks/commit-msg) + message(STATUS "Setting git commit hook...done") +endif() + option(WITH_SECUREC_LIB "Option description" OFF) option(WITH_HAL_LIB "Option description" OFF) diff --git a/scripts/git/commit-msg-hook.py b/scripts/git/commit-msg-hook.py new file mode 100755 index 0000000..719ff22 --- /dev/null +++ b/scripts/git/commit-msg-hook.py @@ -0,0 +1,136 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import sys + + +# format: \033[type;fg;bgm +# +# fg bg color +# ------------------------------------------- +# 30 40 black +# 31 41 red +# 32 42 green +# 33 43 yellow +# 34 44 blue +# 35 45 purple +# 36 46 cyan +# 37 47 white +# +# type +# ------------------------- +# 0 normal +# 1 bold +# 4 underline +# 5 blink +# 7 invert +# 8 hide +# +# examples: +# \033[1;31;40m +# \033[0m + + +STYLE = { + 'fore': + { + 'black' : 30, + 'red' : 31, + 'green' : 32, + 'yellow' : 33, + 'blue' : 34, + 'purple' : 35, + 'cyan' : 36, + 'white' : 37, + }, + + 'back': + { + 'black' : 40, + 'red' : 41, + 'green' : 42, + 'yellow' : 43, + 'blue' : 44, + 'purple' : 45, + 'cyan' : 46, + 'white' : 47, + }, + + 'mode': + { + 'normal' : 0, + 'bold' : 1, + 'underline' : 4, + 'blink' : 5, + 'invert' : 7, + 'hide' : 8, + }, + + 'default': + { + 'end': 0, + }, +} + + +def style(string, mode='', fore='', back=''): + + mode = '%s' % STYLE['mode'][mode] if STYLE['mode'].has_key(mode) else '' + + fore = '%s' % STYLE['fore'][fore] if STYLE['fore'].has_key(fore) else '' + + back = '%s' % STYLE['back'][back] if STYLE['back'].has_key(back) else '' + + style = ';'.join([s for s in [mode, fore, back] if s]) + + style = '\033[%sm' % style if style else '' + + end = '\033[%sm' % STYLE['default']['end'] if style else '' + + return '%s%s%s' % (style, string, end) + + +def check_subject(subject_line): + types = ['Feat', 'Fix', 'Refactor', 'Style', 'Docs', 'Test', 'Chore'] + + if subject_line.startswith(' '): + print style('Error: Subject line starts with whitespace\n', fore='red') + return 1 + + if len(subject_line) > 50: + print style('Error: Subject line should be limited to 50 chars\n', fore='red') + return 1 + + ll = subject_line.split(':') + if len(ll) < 2: + print style('Error: Subject line should have a type\n', fore='red') + return 1 + + type = ll[0] + if type not in types: + print style('Error: Subject line starts with unknown type\n', fore='red') + return 1 + + return 0 + + +contents = [] +ret = 0 +subject = True + +with open(sys.argv[1], 'r') as commit_msg: + contents = commit_msg.readlines() + +for line in contents: + dup = line.lstrip() + if dup.startswith('#'): + continue + if subject is True: + ret = check_subject(line) + subject = False + else: + if len(line) > 72: + print style('Error: Body line should be limited to 72 chars\n', fore='red') + ret = 1 + +exit(ret) diff --git a/scripts/git/commit-msg-template b/scripts/git/commit-msg-template new file mode 100644 index 0000000..6d72af1 --- /dev/null +++ b/scripts/git/commit-msg-template @@ -0,0 +1,28 @@ +# : (if applied, this commit will...) (Max 50 chars) +# |<---- Using a Maximum Of 50 Characters ---->| + + +# Explain why this change is being made +# |<---- Try To Limit Each Line to a Maximum Of 72 Characters ---->| + +# Provide links or keys to any relevant tickets, articles or other resources +# Example: Github issue #23 + +# --- COMMIT END --- +# Type can be +# Feat (new feature) +# Fix (bug fix) +# Refactor (refactoring production code) +# Style (formatting, missing semi colons, etc; no code change) +# Docs (changes to documentation) +# Test (adding or refactoring tests; no production code change) +# Chore (updating grunt tasks etc; no production code change) +# -------------------- +# Remember to +# Choose one of types above in subject line +# Use the imperative mood in the subject line +# Do not end the subject line with a period +# Separate subject from body with a blank line +# Use the body to explain what and why vs. how +# Can use multiple lines with "-" for bullet points in body +# -------------------- -- cgit 1.2.3-korg