git - 如何获取在预接收 Hook 中提交的所有哈希值?

标签 git hash githooks

我正在为 Git 编写预接​​收 Hook 。如果推送多个提交,其中任何一个提交失败,那么整个推送都会失败。这就是我想要的。

我的问题是,并非所有提交的所有哈希值都被传入。例如,只有最近的提交哈希值是

2 次提交被推送到一个仓库:

Commit 1 - 4b5w<br>
Commit 2 - 6gh7 -------------> passed in to pre-receive hook, 
                               but I want the previous hash too.

我不能使用为每个 ref 调用的更新 Hook ,因为我不希望任何提交在其中任何一个失败时通过,例如提交 1 通过而提交 2 失败是 Not Acceptable ,因为当提交 2 失败时我将不得不以某种方式回滚提交 1。

如何从传递给预接收 Hook 的所有提交中获取哈希值?

最佳答案

您可以使用预接收 Hook 并仍然列出所有推送的提交。
参见 this answer其中包括:

chomp(my @commits = `git rev-list $old..$new`);
if ($?) {
  warn "git rev-list $old..$new failed\n";
  ++$errors, next;
}

foreach my $sha1 (@commits) {
  // validate some policy
}

正如 torek 所说,这仅适用于 master 分支。

您可以 deal with multiple branches :

#!/bin/bash
while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "master" == "$branch" ]; then
        # Do something
    fi
done

关于git - 如何获取在预接收 Hook 中提交的所有哈希值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53071421/

相关文章:

perl - Perl 中确定字符串变量是否与列表中的字符串匹配的惯用方法是什么?

Git hook 脚本失败,但脚本在终端中运行良好

Git:如何通过更新 githook 将提交 merge 到分支中

git - 公共(public) GIT-SVN 创建的存储库的公共(public)和私有(private)分支的推荐工作流

git - 有没有类似bzr qannotate的git blame gui?

ruby - 更改散列内的散列不会更新第一个吗?

git - 如果分支提交了具有特定条件的消息,则防止 merge 到 master

git push --receive-pack 选项和安全性 : how to prevent from hacking?

git - 如何找到谁从 GitHub 上的团队项目中删除了功能分支?

perl - 使用 Perl 从文本文件中提取和打印键值对