Skip to content

Commit

Permalink
Check if SHA1 exists before calling git diff
Browse files Browse the repository at this point in the history
In the case of a force push, the commit might not exist anymore, and the
`git diff` in `has_changes` will fail, but the failure would be silently
ignored (since the error message would be passed to `wc`).

`wc -l` would return 1, so effectively the same image would be reused
over and over.
  • Loading branch information
Lorenzo Manacorda authored and asymmetric committed Oct 18, 2016
1 parent 1c3000d commit bdffbda
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions rebuild-image
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,26 @@ commit_timestamp() {
git show -s --format=%ct "$rev"
}

# is the SHA1 actually present in the repo?
# it could be it isn't, e.g. after a force push
commit_valid() {
local rev=$1
git rev-parse --quiet --verify "$rev^{commit}" > /dev/null
}

cached_revision=$(cached_image_rev)
if [ -z "$cached_revision" ]; then
echo ">>> No cached image found; rebuilding"
rebuild
exit 0
fi

if ! commit_valid "$cached_revision"; then
echo ">>> Git commit of cached image not found in repo; rebuilding"
rebuild
exit 0
fi

echo ">>> Found cached image rev $cached_revision"
if has_changes "$cached_revision" "$CIRCLE_SHA1" ; then
echo ">>> Found changes, rebuilding"
Expand Down

0 comments on commit bdffbda

Please sign in to comment.