我不能代表 Windows,但是:
-
技术上可以在 pre-commit 挂钩中执行此类操作。
- 不要。
修改“您将提交的内容”的预提交挂钩很烦人(如果没有别的,它违反了“最小惊讶规则”,您的版本控制系统只是存储您告诉它存储的版本)。除此之外,存储大的预压缩二进制文件会干扰 git 尝试在包文件中节省空间,并且会导致存储库快速膨胀、性能低下、内存不足等。 ZIP 存档是预压缩的二进制文件,因此会表现不佳。
一般来说,处理发布的更合理的“hook-y”方式是设置一个“发布服务器”,您可以将新发布推送到该“发布服务器”,并让推送触发归档生成。 (有一些方法可以在没有单独的服务器/存储库的情况下做到这一点,您可以采用更拉式的方式来实现,但推式很容易说明。)
[编辑:我最初考虑过git archive,但没有意识到你可以方便地排除文件,所以写在下面。所以,jthill's answer 更好,应该是首选。在某些情况下,由于某种原因,git archive 可能不会这样做。]
例如,这里有一个服务器端post-receive 钩子代码片段,它检查名称与release* 匹配的分支是否已被推送,如果是,则调用具有分支名称的shell 函数(一次对于每个这样的分支):
#! /bin/sh
NULL_SHA1=0000000000000000000000000000000000000000
scan()
{
local oldsha newsha fullref shortref
local optype
while read oldsha newsha fullref; do
case $oldsha,$newsha in
$NULL_SHA1,*) optype=create;;
*,$NULL_SHA1) optype=delete;;
*) optype=update;;
esac
case $fullref in
refs/heads/*)
reftype=branch
shortref=${fullref#refs/heads/}
;;
*)
reftype=other
shortref=fullref
;;
esac
case $optype,$reftype,$shortref in
create,branch,release*|update,branch,release*)
do_release $shortref;;
esac
done
}
scan
(以上大部分内容都是样板文件,我已将其简化为基本内容)。您必须编写 do_release 函数,它可能类似于(完全未经测试):
do_release()
{
local tmpdir=/tmp/build.$$ # or use mktemp -d
# $tmpdir/index is git's index; $tmpdir/t is the work tree
trap "rm -rf $tmpdir; exit 1" 1 2 3 15
rm -rf $tmpdir
mkdir $tmpdir/t
GIT_INDEX_FILE=$tmpdir/index GIT_WORK_TREE=$tmpdir/t git checkout $1
# now clean out grunt files and make zip archive
(cd $workdir/t; rm -rf grunt; zip ../t.zip .)
# put completed zip archive in export location, name it
# based on the branch name
mv $workdir/t.zip /place/where/zip/files/live/$1.zip
# clean up temp dir now, and no longer need to clean up
# on signal related abort
rm -rf $tmpdir
trap - 1 2 3 15
}