如何在git push上获取传入提交的列表,以获取钩子中的新分支 [英] How to get a list of incoming commits on git push for a new branch in a hook

查看:296
本文介绍了如何在git push上获取传入提交的列表,以获取钩子中的新分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个预接收钩子,以在接受推送请求之前进行一些验证. 它与现有分支正常工作,因为我使用以下git命令来获取列表 传入的提交:

I'm writing a pre-receive hook to do some validation before accepting commits on the push. It works fine with existing branches since I use following git command to get a list of incoming commits:

git rev-list $old_sha1..$new_sha1

但是,如果用户将新分支推送到共享存储库中,上述命令将失败,因为 old_sha1全为零.所以问题是我如何获取列表的传入提交列表 新创建的分支.进行git rev-list $new_sha1不起作用,因为它会给出所有修订版本,直到开始为止.有没有办法指定类似这样的内容:

However, above command fails if user pushing a new branch into a shared repository because old_sha1 is all zeros. So the question is how do I get a list of incoming commits for a newly created branch. Doing git rev-list $new_sha1 does not work since it gives all revisions till the beginning of times. Is there a way to specify something like this:

git rev-list $branching_sha1..$new_sha1

推荐答案

尝试一下:

git rev-list $new_sha1 $(git for-each-ref --format '^%(refname:short)' refs/heads/)

这使用git for-each-ref打印已知的参考文献;在--format中指定将短名称与前面的插入符号一起输出;并使用refs/heads/表示仅选择本地标题.您最终会运行以下内容:

This uses git for-each-ref to print known refs; specifies in --format that the short name be output along with a preceding caret; and says with refs/heads/ to selecting just local heads. You end up running something like this:

git rev-list $new_sha1 ^master ^foo ^bar ^baz

插入符号的意思是排除可从此处到达的提交".因此,这将为您提供从$new_sha1可以访问的所有提交,但从任何现有引用中访问的没有.

The caret means "exclude commits reachable from here". So this will give you all commits that are reachable from $new_sha1, but aren’t from any of the existing refs.

要同时处理new-ref和existing-ref案例,您可以使用bash的数组支持:

To handle the new-ref and existing-ref cases at once, you can use bash’s array support:

if ! [ $old_sha1 = 0000000000000000000000000000000000000000 ] ; then
    excludes=( ^$old_sha1 )
else
    excludes=( $(git for-each-ref --format '^%(refname:short)' refs/heads/) )
fi

git rev-list $new_sha1 "${excludes[@]}"

这篇关于如何在git push上获取传入提交的列表,以获取钩子中的新分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆