我有一個混帳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