Skip to content

Instantly share code, notes, and snippets.

@T-Oner

T-Oner/commit-msg

Last active Oct 13, 2020
Embed
What would you like to do?
迫于公司提交代码要求越来越繁琐,而且还是人肉写 txt,经常有错别字,特此写一个 git 自动化脚本,生成 test_focus
#!/bin/sh
#
# An example hook script to check the commit log message.
# Called by "git commit" with one argument, the name of the file
# that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
# To enable this hook, rename this file to "commit-msg".
# Uncomment the below to add a Signed-off-by line to the message.
# Doing this in a hook is a bad idea in general, but the prepare-commit-msg
# hook is more suited to it.
#
# SOB=$(git var GIT_AUTHOR_IDENT | sed -n 's/^\(.*>\).*$/Signed-off-by: \1/p')
# grep -qs "^$SOB" "$1" || echo "$SOB" >> "$1"
# This example catches duplicate Signed-off-by lines.
# test "" = "$(grep '^Signed-off-by: ' "$1" |
# sort | uniq -c | sed -e '/^[ ]*1[ ]/d')" || {
# echo >&2 Duplicate Signed-off-by lines.
# exit 1
# }
commit_msg=$(cat .git/COMMIT_EDITMSG)
first=${commit_msg:0:1}
if [ ! "$first" = "@" ];then
echo 'exit'
exit 0
fi
project_name="Settings";
year=$(date "+%Y");
sub_dir=$(date "+%Y-%m");
file_name=${project_name}_$(date "+%Y%m")$(expr $(expr $(date "+%d") + $(expr 6 - $(date "+%w")))).txt;
dir_path=test_focus/${year}/${sub_dir}
file_path=${dir_path}/${file_name}
create_file() {
tmp_file_header_1="======================%s========================"
tmp_file_header_2="### begin:%s"
tmp_file_header_3="### end:%s"
if [ -f ${file_path} ];then
echo "File exist."
else
echo "Create file"
mkdir -p ${dir_path}
touch ${file_path}
var_begin=$(date "+%Y-%m")-$(expr $(expr $(date "+%d") - $(date "+%w")) + 1)
var_end=$(date "+%Y-%m")-$(expr $(expr $(date "+%d") + $(expr 6 - $(date "+%w"))))
file_header_1=$(printf $tmp_file_header_1 $var_end)
file_header_2=$(printf "$tmp_file_header_2" $var_begin)
file_header_3=$(printf "$tmp_file_header_3" $var_end)
echo ${file_header_1}>>${file_path}
echo ${file_header_2}>>${file_path}
echo ${file_header_3}>>${file_path}
echo "">>${file_path}
echo "">>${file_path}
fi
}
get_jira() {
jira_str=$(echo "$1" | egrep -E '(JIRA#([A-Za-z0-9]+-[0-9]+))' -o)
str_arry=($(echo $jira_str | awk -F# '{print $1,$2}'))
echo ${str_arry[1]}
}
get_module() {
module_str=$(echo "$1" | egrep -E '((\w+):(\w+))' -o)
str_arry=($(echo $module_str | awk -F: '{print $1,$2}'))
echo ${str_arry[1]}
}
add_content() {
jira=$(get_jira $commit_msg)
module=$(get_module $commit_msg)
author=$(git config user.name)
content_1="### author:$author"
content_2="### JIRAID:$jira"
content_3="### 模块:$module"
content_4="### 重点:"
echo "---">>${file_path}
echo "">>${file_path}
echo ${content_1}>>${file_path}
echo ${content_2}>>${file_path}
echo ${content_3}>>${file_path}
echo ${content_4}>>${file_path}
echo "">>${file_path}
}
create_file
add_content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.