Skip to content

Instantly share code, notes, and snippets.

@mlocati
Created January 23, 2019 09:35
Show Gist options
  • Save mlocati/ba9a9bad0ad5f09cd92c3b2897283b42 to your computer and use it in GitHub Desktop.
Save mlocati/ba9a9bad0ad5f09cd92c3b2897283b42 to your computer and use it in GitHub Desktop.
Check if a go-pear.phar file is an official one
#!/bin/sh
# This script checks if a go-pear.phar is the same as one of the
# ones released on https://github.com/pear/pearweb_phars
#
# MIT License
# Made by Michele Locati <[email protected]> on 2019-01-23
set -o errexit
set -o nounset
IFS='
'
FILETOCHECK="${1:-}"
getFileHash () {
GETFILEHASH_RESULT="$(sha1sum "${1}")"
printf '%s' "${GETFILEHASH_RESULT%% *}"
}
if test -z "$FILETOCHECK"; then
echo 'Please specify the path to the go-pear.phar file to be checked.'
exit 2
fi
if test ! -f "$FILETOCHECK"; then
printf 'Unable to find the file %s\n' "$FILETOCHECK"
exit 2
fi
FILETOCHECK_HASH="$(getFileHash "$FILETOCHECK")"
printf 'Creating temporary directory... '
TMPDIR=$(mktemp -d)
cleanup () {
rm -rf "$TMPDIR" || true
}
trap cleanup EXIT
printf 'done.\n'
printf 'Cloning official git repository https://github.com/pear/pearweb_phars... '
git clone --quiet https://github.com/pear/pearweb_phars.git "$TMPDIR/git"
printf 'done.\n'
printf 'Listing relevant commits... '
COMMIT_LIST=$(git -C "$TMPDIR/git" log --format=tformat:%H go-pear.phar)
printf 'done.\n'
printf 'Checking if %s is in the official repository... ' "$FILETOCHECK"
FOUND_COMMIT=''
for COMMIT in $COMMIT_LIST; do
git -C "$TMPDIR/git" show "$COMMIT:go-pear.phar" > "$TMPDIR/$COMMIT"
HASH="$(getFileHash "$TMPDIR/$COMMIT")"
if test "$FILETOCHECK_HASH" = "$HASH"; then
FOUND_COMMIT="$COMMIT"
break
fi
done
if test -z "$FOUND_COMMIT"; then
printf 'FAILED!\n%s is not one of the released on https://github.com/pear/pearweb_phars\n' "$FILETOCHECK"
exit 1
fi
printf 'found.\n%s is the same as the one officially released on %s\nGOOD!\n' "$FILETOCHECK" "$(git -C "$TMPDIR/git" show --no-patch --format=tformat:%ci "$FOUND_COMMIT")"
exit 0
@green-george
Copy link

Thank you for the script. Do you know where go-pear.phar is usually located in a linux system?

lack of better, you could always do:
sudo find / -name "go-pear.phar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment