#!/bin/sh # Installed by `oc-harness install`. Remove with: rm .git/hooks/pre-commit # # It runs the PROJECT's own checks, and only if the project has any. # # The previous version ran `npm run test:all`, which is a script of THIS KIT, # not of the project the hook was installed into. So in every project that # installed the kit, every commit failed with: # # npm error Missing script: "test:all" # # until the author reached for --no-verify. A gate that blocks every commit # is not a gate. # # The kit's own repository is the exception, and it is handled by the script # list below: `test:all` is there, so the kit gates itself. # # The live-install half is deliberately left out: measure and attribute read # ~/.local/share/opencode, which is slow, machine-specific, and irrelevant to # whether a commit broke anything. set -e repo_root=$(git rev-parse --show-toplevel) cd "$repo_root" if [ -n "$SKIP_SPACE_HARNESS_HOOK" ]; then echo "pre-commit: skipped (SKIP_SPACE_HARNESS_HOOK set)" exit 0 fi # No package.json, no JavaScript project, no gate. if [ ! -f package.json ]; then exit 0 fi # The gate is the project's own test script, if it has one. The kit's own # repository has `test:all`, which is the offline half of its gate. gate="" if node -e "process.exit(require('./package.json').scripts?.['test:all'] ? 0 : 1)" 2>/dev/null; then gate="test:all" elif node -e "process.exit(require('./package.json').scripts?.test ? 0 : 1)" 2>/dev/null; then gate="test" fi if [ -z "$gate" ]; then # Nothing to run. A hook that blocks every commit because the project has # no test script is worse than no hook. exit 0 fi # Git exports GIT_DIR and GIT_INDEX_FILE to the hook. A test that creates its # own repository inherits them and reads or writes this repository instead. unset GIT_DIR GIT_WORK_TREE GIT_INDEX_FILE GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES GIT_COMMON_DIR GIT_PREFIX log="${TMPDIR:-/tmp}/space-harness-hook.log" echo "pre-commit: npm run $gate" if ! npm run "$gate" >"$log" 2>&1; then echo "pre-commit: FAILED. Last lines:" tail -30 "$log" echo "" echo "Fix it, or commit with --no-verify if you are certain it is not yours." exit 1 fi exit 0