如何解决我可以替换提交消息git中的消息吗?
我在 git 中使用了 commit-msg 钩子来检查我的提交是否正确,并希望自动将 BugID 替换为 Bug 地址。但它似乎只能在提交消息的底部再添加一行,而不是替换原始行。有什么办法可以让它工作吗? 我的提交模板是
Branch:
BugId:
Description:
这是我的代码:
with open(sys.argv[1],r+) as fp:
lines=fp.readlines()
for line in lines:
if line[0]=="#":
continue
line=line.strip()
for l in l.split('/n'):
if not l.split(":")[-1]:
print(l,"is empty")
sys.exit(1)
if l.startswith("Bug"):
fp.write(l.replace(l.split(":")[-1],"http:bugs.xxxxxxxxx")
sys.exit(0)
解决方法
这是 Gerrit 的一个示例,如果它不存在,则向每个提交添加“Change-Id: ....”行
当然,它不是 Python,但我认为你可以从那里借用一个想法
#!/bin/sh
# From Gerrit Code Review 2.16.2
#
# Part of Gerrit Code Review (https://www.gerritcodereview.com/)
#
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License,Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,software
# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# avoid [[ which is not POSIX sh.
if test "$#" != 1 ; then
echo "$0 requires an argument."
exit 1
fi
if test ! -f "$1" ; then
echo "file does not exist: $1"
exit 1
fi
if test ! -s "$1" ; then
echo "file is empty: $1"
exit 1
fi
# $RANDOM will be undefined if not using bash,so don't use set -u
random=$( (whoami ; hostname ; date; cat $1 ; echo $RANDOM) | git hash-object --stdin)
dest="$1.tmp.${random}"
# Avoid the --in-place option which only appeared in Git 2.8
# Avoid the --if-exists option which only appeared in Git 2.15
cat "$1" \
| git -c trailer.ifexists=doNothing interpret-trailers --trailer "Change-Id: I${random}" > "${dest}" \
&& mv "${dest}" "$1"
注意脚本的最后几行:它创建一个 dest
,然后打印消息并添加“Change-Id”(如果它不存在)。然后它将临时目标移动到实际成为消息。工作完成。
我对 git hooks 不是很精通,但这个应该在名为 .git/hooks/commit-msg
的文件中,所以我相信这就是你所需要的
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。