Git预接收钩子拒绝符号链接中断的推送

Git预接收钩子拒绝符号链接中断的推送,git,symlink,Git,Symlink,我正在为git remote编写一个预接收钩子。如果推送中的任何更改对象断开(即悬挂)符号链接,此预接收挂钩应拒绝推送 就是 #!/bin/bash # hooks/pre-receive while read old_sha1 new_sha1 name ; do files=$(git diff --name-only $old_sha1..$new_sha1) # If a file in $files is a symbolic link pointing at nothing,

我正在为git remote编写一个预接收钩子。如果推送中的任何更改对象断开(即悬挂)符号链接,此预接收挂钩应拒绝推送

就是

#!/bin/bash
# hooks/pre-receive
while read old_sha1 new_sha1 name ; do
  files=$(git diff --name-only $old_sha1..$new_sha1)
  # If a file in $files is a symbolic link pointing at nothing, non-zero exit
done

如何执行此操作?

您可以迭代文件并检查其中是否有损坏的符号链接。为此,您可以使用
test-e
检查是否存在:


对于f,在$files中;做
如果[!-e“$f”];然后
#问题是如果你到了这里。。。
出口1
fi
完成


请注意,我上面的脚本对文件名中的空格不太可靠。使用
git diff--names only-z
并在
'\0'
上正确分割结果将解决此问题。

预接收挂钩在远程git存储库上运行,该存储库是一个“裸”存储库。裸存储库没有存储库的工作副本,也没有常规目录布局中的任何工作副本文件。因此,上述方法将不起作用。