I have a post-receive git hook:
#!/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
How can I check the status of a git command and stop the script from continuing if it fails?
Something like the following:
#!/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(orset -e==set -o errexit) and the shell will do it automatically for you whenever an unchecked command fails.-eis generally discouraged because of its unintuitive semantics. See Why does set -e not work inside () ||.set -e, but I still think simple shell scripts should beset -eby default. Too bad one can't rely on it in libraryish shell functions, exactly because of the behavior you mention. :(syslogto capture problems.