后期優化了一版更加清晰, 可以直接參考 git commit 提交規范(優化)
不規范?
Git
每次提交代碼,都要寫 Commit message
(提交說明),否則就不允許提交。但是,一般來說,commit message
應該清晰明了,說明本次提交的目的。當出現問題或者查看提交記錄的時候也能快速的定位
到該次提交, 不正當的提交,讓人一頭霧水,??再??
- 1.測試提交?
- .中英混合?
- .no no no 是什么?
??.jpg
??.jpg
再看下前端框架Angular.js 采用的規范提交記錄
591642591847_.pic_hd.jpg
會不會清晰很多??, (好像英文看起來沒中文的簡潔??, 當然這只是語言問題, 我們自己開發項目提交記錄還是用中文)
配置
規范帶來的好處就不再多說, 直接上干貨, 文章末尾會附上腳本
, 可根據自身需求更改模板
1.下載腳本到本地
下載腳本, 或者創建一個文件復制文末腳本到文件, 修改文件名后綴以.sh
結尾即可, 下載或者創建目錄沒有限制, 任意位置即可
圖片.png
2. 到需要配置git 提交規范的項目根目錄
圖片.png
3. 執行腳本
直接把腳本拖入終端,回車執行即可, 因為是第一次執行腳本,可能沒有權限,會報錯
圖片.png
直接輸入密碼進行授權即可
sudo chmod u+x 腳本
圖片.png
授權完成, 執行腳本
圖片.png
這里會再次讓輸入密碼確認
圖片.png
最后執行成功, 提交規范配置成功??????
4.重啟Sourcetree 提交改動
先看下不規范的提交
圖片.png
直接提交失敗, 可根據彈窗中提示進行修改提交信息
圖片.png
圖片.png
修改/ 刪除 git提交規范
如果配置模板或者規范不滿足自身需求, 可自行修改
如果不想繼續使用規范,可以進行卸載, 在腳本后面增加參數 uninstall
即可卸載
圖片.png
#!/bin/bash
## 到項目跟目錄執行該腳本
ST_COMMIT_MSG=".stCommitMsg"
COMMIT_MSG="commit-msg"
GIT_HOOKS=".git/hooks"
GIT_COMMIT_MSG="$GIT_HOOKS/$COMMIT_MSG"
FIRST_DO="0"
installGitRules() {
whiteFile() {
cat >>$ST_COMMIT_MSG <<EOF
<type>(<scope>) : <subject>
<body>
<footer>
EOF
}
pushd ~/
if [ ! -f $ST_COMMIT_MSG ]; then
echo "$ST_COMMIT_MSG file not exist"
touch .stCommitMsg
whiteFile
else
COPY_FILE="${ST_COMMIT_MSG}_backup"
cp -P ~/$ST_COMMIT_MSG ~/$COPY_FILE
: >$ST_COMMIT_MSG
whiteFile
fi
popd
writeCommitMsg() {
cat >>.git/hooks/commit-msg <<EOF
#!/bin/bash
TIP_MESSAGE='
<type>(<scope>) : <subject>\n
<body>\n
<footer>\n
\n
#type 本次修改功能類型\n
.feat :新功能\n
.fix :修復bug\n
.opt :優化(optimize) 圖片壓縮, 文件刪除等\n
.ci : 版本號升級、branchConfig修改、scrip/podinfo.rb等發布相關修改\n
.test :增加測試\n
.refactor :某個已有功能重構\n
.docs :文檔改變\n
.style :代碼格式改變\n
.revert :撤銷上一次的 commit (revert 命令自動生成)\n
\n
#scope :用來說明此次修改的影響范圍\n
.all :表示影響面大 ,如修改了網絡框架 會對真個程序產生影響\n
.loation :表示影響小,某個小小的功能\n
.module :表示會影響某個模塊 如登錄模塊、首頁模塊 、用戶管理模塊等等\n
\n
#subject: 用來簡要信息描述本次改動\n
\n
#body :具體的修改信息 應該盡量詳細\n
\n
#footer :備注: 文檔鏈接、bug id、設計文檔\n
'
MSG=\$(awk '{printf("%s",\$0)}' \$1)
if [[ \$MSG =~ ^(feat|fix|opt|ci|test|refactor|docs|style|revert)\(.*\):.*$ ]]; then
echo -e " commit success!"
else
echo -e \MSG
echo -e " Error: the commit message is irregular "
echo -e " Error: type must be one of feat|fix|opt|ci|test|refactor|docs|style|revert"
echo -e ' eg: feat(租房): 詳情頁增加無盡流'
echo '詳細文檔??????'
echo -e \$TIP_MESSAGE
exit 1
fi
EOF
}
if [ ! -d $GIT_HOOKS ]; then
pushd .git
mkdir hooks
mkfile -n 0b hooks/commit-msg
popd
writeCommitMsg
else
if [ ! -f $GIT_COMMIT_MSG ]; then
mkfile -n 0b $GIT_COMMIT_MSG
writeCommitMsg
else
COPY_FILE="${COMMIT_MSG}_backup"
cp -P $GIT_COMMIT_MSG $GIT_HOOKS/$COPY_FILE
: >$GIT_COMMIT_MSG
writeCommitMsg
fi
fi
sudo chmod 777 $GIT_COMMIT_MSG
if [ $FIRST_DO != '0' ]; then
echo 'Configuration is successful! ?????? '
echo 'Restart Sourcetree then submit your changes!'
fi
}
uninstallGitRules() {
if [ ! -f $GIT_COMMIT_MSG ]; then
echo "Don't have git commit message rules to remove!"
else
rm $GIT_COMMIT_MSG
echo "remove git commit message rules success!"
fi
pushd ~/
if [ ! -f $ST_COMMIT_MSG ]; then
echo "$ST_COMMIT_MSG file not exist, Don't have git commit rules template to remove!"
else
rm $ST_COMMIT_MSG
echo "remove git commit message rules template success!"
fi
popd
}
install() {
installGitRules
FIRST_DO="1"
installGitRules
}
if [ $# == 0 ]; then
install
exit
fi
for i in "$*"; do
if [ $i == "install" ]; then
install
elif [ $i == "uninstall" ]; then
uninstallGitRules
else
install
fi
done