我有一个混帐post-receive
钩:如果git命令失败,如何退出git钩子脚本?
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ -n "$branch" ] && [ "master" == "$branch" ]; then
working_tree="/path/to/working/dir"
GIT_WORK_TREE=$working_tree git checkout $branch -f
GIT_WORK_TREE=$working_tree git pull
<more instructions>
fi
done
如何检查一个Git命令的状态和持续如果失败停止脚本?
类似以下内容:
#!/bin/bash
while read oldrev newrev refname
do
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ -n "$branch" ] && [ "master" == "$branch" ]; then
working_tree="/path/to/working/dir"
GIT_WORK_TREE=$working_tree git checkout $branch -f
GIT_WORK_TREE=$working_tree git pull
if [ <error conditional> ]
echo "error message"
exit 1
fi
fi
done
用'运行它/ bin/bash -e'(或者'set -e' =='set -o errexit'),并且shell会在未经检查的命令失败时自动为您执行。 – PSkocik
@PSkocik'-e'通常因为其不直观的语义而受到阻碍。请参阅[为什么set -e不能在()||]内工作(https://unix.stackexchange.com/questions/65532/why-does-set-e-not-work-inside)。 – hvd
@ hvd是的,这绝对是'set -e',但我仍然认为简单的shell脚本默认应该是'set -e'。太糟糕了,因为你提到的行为,在图书馆的shell函数中不能依赖它。 :( – PSkocik