Skip to content
go-libfossil Artifact [ce134a33b0]
Login

Artifact [ce134a33b0]

Artifact ce134a33b0e8b1a0017110de2ef0553e052378e048b461102282edd72bea5fb9:


#!/bin/sh
# libfossil pre-commit hook
# Runs tests with both SQLite drivers + DST + OTel sub-module (~45s) before every commit.
#
# Install: git config core.hooksPath .githooks
# Skip:    git commit --no-verify (emergency only)

set -e

run_test() {
	label="$1"
	shift
	if output=$("$@" 2>&1); then
		echo "  $label: OK"
	else
		echo "  $label: FAILED"
		echo ""
		echo "$output"
		echo ""
		exit 1
	fi
}

echo "=== pre-commit: modernc (default) ==="
run_test "unit tests" go test ./... -short -count=1 -timeout=120s

echo "=== pre-commit: ncruces ==="
run_test "unit tests (ncruces)" go test -tags test_ncruces ./... -short -count=1 -timeout=120s

echo "=== pre-commit: vet ==="
run_test "go vet" go vet ./...

echo "=== pre-commit: observer/otel ==="
run_test "otel sub-module" sh -c "cd observer/otel && GOWORK=off go test ./... -short -count=1 -timeout=30s"

echo "=== pre-commit: build libfossil binary ==="
run_test "cmd/libfossil" go build ./cmd/libfossil/

echo "=== pre-commit: docs SDK drift ==="
# Skip gracefully if gomarkdoc cannot be installed (fresh clone, offline, etc.).
# Use the pinned version from Makefile so the check is deterministic across environments.
GOMARKDOC_VERSION=$(awk -F':= *' '/^GOMARKDOC_VERSION/ {print $2; exit}' Makefile 2>/dev/null)
GOMARKDOC_VERSION=${GOMARKDOC_VERSION:-latest}
if ! command -v gomarkdoc >/dev/null 2>&1; then
	if ! go install "github.com/princjef/gomarkdoc/cmd/gomarkdoc@${GOMARKDOC_VERSION}" 2>/dev/null; then
		echo "  gomarkdoc: SKIPPED (install failed; commit allowed)"
	fi
else
	# Reinstall if locally-installed gomarkdoc is the wrong version. Best-effort;
	# tolerate failure so contributors can commit offline. Note: gomarkdoc --version
	# output format is e.g. "gomarkdoc v1.1.0"; we extract the last whitespace-separated token.
	_gm_installed=$(gomarkdoc --version 2>/dev/null | awk '{print $NF}')
	if [ "$_gm_installed" != "$GOMARKDOC_VERSION" ] && [ "$GOMARKDOC_VERSION" != "latest" ]; then
		go install "github.com/princjef/gomarkdoc/cmd/gomarkdoc@${GOMARKDOC_VERSION}" >/dev/null 2>&1 || true
	fi
fi
if command -v gomarkdoc >/dev/null 2>&1; then
	if make -s docs-gen-sdk >/dev/null 2>&1; then
		if git diff --quiet -- docs/site/content/docs/reference/sdk; then
			echo "  SDK pages: in sync"
		else
			echo "  SDK pages: OUT OF DATE"
			echo ""
			echo "  Run 'make docs-gen-sdk' and commit the result."
			echo ""
			git diff --stat -- docs/site/content/docs/reference/sdk
			exit 1
		fi
	else
		echo "  docs-gen-sdk: SKIPPED (target not available)"
	fi
fi

echo "=== pre-commit: all passed ==="