3

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
4
  • 1
    Run it with /bin/bash -e (or set -e == set -o errexit) and the shell will do it automatically for you whenever an unchecked command fails. Commented Aug 3, 2017 at 22:05
  • @PSkocik -e is generally discouraged because of its unintuitive semantics. See Why does set -e not work inside () ||. Commented Aug 3, 2017 at 22:11
  • @hvd Yeah, that absolutely sucks about set -e, but I still think simple shell scripts should be set -e by default. Too bad one can't rely on it in libraryish shell functions, exactly because of the behavior you mention. :( Commented Aug 3, 2017 at 22:15
  • It's worth noting here that exiting the post-receive function has no effect on the push, and error messages from post-receive may also be entirely invisible. A post-receive script usually has to use something that smells like syslog to capture problems. Commented Aug 4, 2017 at 0:35

1 Answer 1

3

How can I check the status of a git command and stop the script from continuing if it fails?

The same way you check the status of any shell command: by looking at the return code. You can inspect the value of the shell variable $? after the command exits, as in:

GIT_WORK_TREE=$working_tree git pull
if [ $? -ne 0 ]; then
  exit 1
fi

Or by using the command itself as part of a conditional, as in:

if ! GIT_WORK_TREE=$working_tree git pull; then
  exit 1
fi
Sign up to request clarification or add additional context in comments.

3 Comments

This seems needlessly verbose. || exit 1 is enough.
Not if you want to emit some sort of useful error message (which the OP is doing in the question).
And which you aren't doing in your answer. If you want to emit a useful error message, I'd go with ||, but in combination with a shell function myself: something along the lines of || fail "command failed", with fail() { echo "error: $1" >&2; exit 1; }. This doesn't distract as much when you're reading the script later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.