我正在为 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/