我发现自己正在与一个具有繁琐的变更日志格式的小组合作。我已经决定根据提交消息详细信息自动创建更改日志条目。过程是您提供格式化的提交消息,然后我随时调整更改日志文件,并尝试将其添加到当前提交。
我有一个commit-msg钩子:
#!/bin/bash
GIT_BRANCH=`git branch`
# Only run this hook on the master branch
if [ "$GIT_BRANCH" = '* master' ]
then
# set some variables we'll need
SAMPLE_COMMIT_MESSAGE="minor: #ws-666 Brings the devil back to life"
COMMIT_MESSAGE_FILE=$1
FULL_COMMIT_MESSAGE=$(cat $COMMIT_MESSAGE_FILE)
CURRENT_DATE=$(date +"%Y-%m-%d")
# let's do some pattern matching on the commit to set more variables
[[ $FULL_COMMIT_MESSAGE =~ (ws-[0-9]+)[:space:]*(.*) ]]
JIRA_ISSUE=${BASH_REMATCH[1]^^}
COMMIT_MESSAGE=${BASH_REMATCH[2]}
[[ $FULL_COMMIT_MESSAGE =~ ^(M) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_Major_"
fi
[[ $FULL_COMMIT_MESSAGE =~ ^(m) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_minor_"
fi
[[ $FULL_COMMIT_MESSAGE =~ ^([pP]) ]]
if [ -n "${BASH_REMATCH[1]}" ]
then
CHANGE_IMPACT="_patch_"
fi
#echo Full Commit Message "$FULL_COMMIT_MESSAGE"
#echo Jira issue $JIRA_ISSUE
#echo Change Type $CHANGE_IMPACT
#echo Commit Message "$COMMIT_MESSAGE"
if [ -z $JIRA_ISSUE ]
then
echo "You failed to specify a jira issue, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
if [ -z $CHANGE_IMPACT ]
then
echo "You failed to specify a change impact, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
if [ -z $COMMIT_MESSAGE ]
then
echo "You failed to specify an actual commit message, your commit must look like:"
echo "$SAMPLE_COMMIT_MESSAGE"
exit 1
fi
sed -i -e ":a;N;s/\(# Change log.*\)\n/\1\n\n\* next ($CURRENT_DATE)\n $JIRA_ISSUE\n \* $CHANGE_IMPACT: $COMMIT_MESSAGE/" change-log.md
`git add change-log.md`
echo Running $BASH_SOURCE
set | egrep GIT
echo PWD is $PWD
fi
# For testing, always exit
echo "Exiting, no commit made"
exit 1
然后我运行以下命令:
git checkout -- ../change-log.md ; git commit ../fake -m"Major #ws-123 WeeeeeeEE" ; git status
在git状态中,我看到:
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: ../change-log.md
git是否可以在提交过程中的此阶段添加文件?关于如何最好地做到这一点的其他想法?我想我可以检查特定的更改日志条目是否存在,如果不存在,添加它,然后退出,要求添加该文件。
-j
答案 0 :(得分:0)
在调用git add之前编辑文件后添加sleep 5m
,然后删除到shell并输入git add change-log.md
给我一个错误fatal: Unable to create '/home/jjshoe/test/.git/index.lock': File exists.
这完全有道理。
答案 1 :(得分:0)
你可以做的一件事(有点hacky)是使用以下单行添加一个post-commit钩子:
git commit --amend -C HEAD --no-verify change-log.md
如果已经更改,这将为每个提交添加change-log.md,如果没有则不执行任何操作。