| 1 | #!/bin/sh |
| 2 | |
| 3 | set -eu |
| 4 | |
| 5 | # if being run directly, add a dash of red |
| 6 | if [ -t 1 ]; then |
| 7 | trap 'printf "\e[0m"' EXIT |
| 8 | printf "\e[1;31m" |
| 9 | fi |
| 10 | |
| 11 | # resolve to a sha, ensure validity |
| 12 | readonly commit="$(git rev-parse --verify "${1:-HEAD}^{commit}")" |
| 13 | |
| 14 | tmpdir="tmp/pre-push/${commit}" |
| 15 | rm -rf "tmp/pre-push/${commit}" |
| 16 | |
| 17 | # unpack all of the changed files, plus the test/common/static-code from that commit |
| 18 | changed_files="$(git diff-tree --no-commit-id --no-renames --diff-filter=d --name-only -r "${commit}" --)" |
| 19 | required_files="$(git cat-file -p "${commit}":test/common/static-code | sed -n 's/^# requires: //p') test/common/static-code" |
| 20 | git archive --prefix="${tmpdir}/" "${commit}" -- ${changed_files} ${required_files} | tar x |
| 21 | |
| 22 | # check that node_modules always gets updated with package.json |
| 23 | if [ -e "${tmpdir}/package.json" -a ! -e "${tmpdir}/node_modules" ]; then |
| 24 | echo 'not ok 0 /node-modules/update' |
| 25 | echo '# package.json changed, but node_modules not updated' |
| 26 | fi |
| 27 | |
| 28 | # if any of the following commands fail, the function |
| 29 | # will abort, leaving the temp directory in place |
| 30 | ( |
| 31 | unset -- $(env | sed -n '/^GIT_/{s/=.*//p}') |
| 32 | cd "${tmpdir}" |
| 33 | git init -qb main . |
| 34 | git add . |
| 35 | WITH_PARTIAL_TREE=1 test/common/static-code |
| 36 | ) |
| 37 | |
| 38 | # paranoia: make sure we delete what we think we will |
| 39 | rm -rf "tmp/pre-push/${commit}" |
| 40 | rmdir --ignore-fail-on-non-empty tmp/pre-push tmp |