作为CI构建的一部分,我们有一个脚本,可以部署最新版本的项目,并使用上一次提交中的主题和部署链接对相应的PR发表评论。
但是,如果开发人员同时向PR提交多个提交,有时这些构建包含多个提交的更改。我想修改脚本,以便包含链接的消息还包含来自每个新提交的主题行。
当前实现只获得最近的提交,其代码行类似于:
COMMIT_MESSAGE=$(git --no-pager log --pretty=format:"%s" -1)
我是否可以通过类似的方式与git进行交互,以获取与上次提交的推送相同的所有提交的消息?
答案 0 :(得分:1)
您可能需要在团队git服务器中配置update
git hook。
钩子接收三个命令行参数:被推入的引用的名称,其先前状态的SHA-1,其新状态的SHA-1。因此,钩子中的git log --pretty=format:"%s" $2..$3
将为您提供所需的日志。
您应该专门处理分支创建和删除的情况,在这种情况下相应的SHA-1将归零:
refname="$1"
oldrev="$2"
newrev="$3"
if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
echo "usage: $0 <ref> <oldrev> <newrev>" >&2
exit 1
fi
echo "Updating \"$refname\" from \"$oldrev\" to \"$newrev\"" >&2
if [ "$oldrev" = "0000000000000000000000000000000000000000" ]; then
echo "\"$refname\" was created with rev \"$newrev\"" >&2
# do whatever you need for a new branch or _tag_
# you may do as described in http://stackoverflow.com/a/5720575/3159253
# to list only commits specific for a newly created branch/tag,
# with the only exception: the reference isn't yet created in the target repo,
# so you don't need to filter it out
LOG=$(git log --no-merges --pretty=oneline $newrev --not $(git for-each-ref --format="%(refname)") -- ) --
echo -e "New reference log\n$LOG\n===" >&2
exit 0
fi
if [ "$newrev" = "0000000000000000000000000000000000000000" ]; then
echo "\"$refname\" was deleted (last rev was \"$oldrev\"" >&2
# do whatever you need when a deleted branch or _tag_
exit 0
fi
LOG=$(git log --pretty=oneline $oldrev..$newrev)
echo -e "Changed reference log\n$LOG\n===" >&2
查看Git Hooks docs了解详情。
请记住,钩子是为分支和标签执行的。分支名称
以refs/heads
开头。而标签 - 来自refs/tags
,因此如果需要,您可以通过这些前缀区分它们。
答案 1 :(得分:0)
您需要在拉取请求之前跟踪提交,但是如果您可以获得该提交哈希,则可以使用此更新的git log命令:
git --no-pager log --pretty=format:"%s" <git_hash>..HEAD
例如,这是我的一个回购的输出:
> git --no-pager log --pretty=format:"%s" d8ede574fd33cac4d78bda1bce08049962c7a405..HEAD
Update .coffee file as well
Update for RN 0.25
Uodat license to 2016
2.10.0
Merge pull request #66 from Hilzu/master%
你没有提供足够的上下文来知道你是否可以在PR之前访问git哈希,但是如果你没有,我无法想象没有它就可以实现。
如果您可以绑定到CI服务器上的git挂钩,那将是理想的,但是如果您不能,那么如果您改变流量以进行提取,则可以在提取时看到提交。见this answer。如果你获取,你可以比较origin与你当前的master,存储你当前所在的提交哈希,合并你的pull,然后使用git hash显示两次提交之间的所有消息。