Intelligent Git Pre-Commit and Pre-Push Workflow
This document details the architecture, configuration, operation, and troubleshooting of the Git hook workflow for the GitHub Backup Automation System monorepo. The work is split so commits stay fast:
.githooks/pre-commitruns fast checks only: secret scan, formatting and lint, static analysis, and the unit tests of the areas that changed..githooks/pre-pushre-runs those checks for the commits being pushed and adds the slow builds: static Go binaries, the Next.js production build, and Docker images when Docker files change.
1. Architecture Overview
The repository is a polyglot monorepo containing three codebases:
- Go Backend, Worker CLI & MCP Server: Go 1.26 module (
github.com/MishraShardendu22/github-backup) powering the Fiber REST/WebSocket API, the backup worker and the MCP server. - AI Observatory Service: FastAPI + LangChain Tool-Calling RAG application managed with
uvundersrc/services/github-backup-observatory/. - Dashboard Application: Next.js 16 (App Router, Turbopack, React 19, Biome, Tailwind CSS v4, the Observatory design system packages) under
src/apps/github-backup-dashboard/.
flowchart TD
A["git commit"] --> B["pre-commit: detect staged files"]
B --> C{"Affected subsystems"}
C -->|"Go files"| D1["gofmt, go vet, go test\n(changed services only)"]
C -->|"Python files"| D2["Pyright, Observatory test suites"]
C -->|"Frontend files"| D3["Biome lint, tsc --noEmit"]
C -->|"Docs / assets / app.db only"| D4["Fast path: secret scan only"]
C -->|"Hooks, scripts, tests, Makefile"| D5["bash -n, the changed script's own test"]
C -->|"Workflows / skills"| D6["actionlint / validate-skills.py"]
C -->|"Any other file"| D7["Secret scan; CI checks the rest"]
P["git push"] --> Q["pre-push: the commits new to the remote"]
Q --> R["Re-run pre-commit checks for those commits"]
R --> S{"Changed areas"}
S -->|"Go code"| T1["CGO_ENABLED=0 go build ./..."]
S -->|"Frontend"| T2["pnpm run build"]
S -->|"Dockerfile / .dockerignore / compose"| T3["docker buildx build (cache only)"]
2. Why Native Tracked Git Hooks (.githooks/)?
Rather than imposing heavyweight JavaScript wrappers (Husky) or third-party binary managers (Lefthook) on a polyglot codebase, the repository uses tracked native Git hooks located in .githooks/:
- Zero External Dependencies: Reuses the repository's native toolchains (
go,uv,pnpm,docker,bash). - Cross-Platform Compatibility: Portable bash that also runs on the bash 3.2 macOS ships (no associative arrays,
mapfileor${var,,}), as well as Linux, Windows Git Bash and WSL.tests/githooks_test.shexercises both hooks with stub tools. - Version Controlled: The hooks are tracked in Git, so all contributors share identical validation rules.
- Fail-Fast & Dependency-Ordered: Validations run in order of speed (Formatting, Static Analysis, Tests, then Builds in pre-push).
3. Validation Matrix
Pre-commit (fast checks)
| Subsystem | Stage | Validation Command | Remediation Command |
|---|---|---|---|
| All | Secret scan | Staged .env, key files and token patterns | git restore --staged <file> |
| Workflows | Lint | actionlint (from PATH, else its image) for staged .github/ files | Fix what actionlint reports |
| Hooks, scripts, tests, Makefile | Syntax, own tests | bash -n on the staged scripts; the test of each changed script (.githooks/* → tests/githooks_test.sh, scripts/setup.sh → tests/setup_script_test.sh, ...); a changed tests/*_test.sh runs itself; tests/test_*.py for Python scripts; make -n for the Makefile | Fix what the test reports |
| Skills | Schema | python3 scripts/validate-skills.py for staged .agents/skills/ files | Fix what it reports |
| Go | Formatting | gofmt -l <staged_files> | make format or gofmt -w <file> |
| Go | Static Analysis | go vet <changed service packages> | Fix the reported issues |
| Go | Unit Tests | go test <changed service packages> | make test-go |
| Python | Static Typecheck | cd src/services/github-backup-observatory && uv run --with pyright pyright | make typecheck |
| Python | Test Suites | cd src/services/github-backup-observatory && uv run pytest | make test-py / make test-agents |
| Frontend | Lint & Format | cd src/apps/github-backup-dashboard && pnpm run lint (biome check) | make format / pnpm run format |
| Frontend | Type Check | cd src/apps/github-backup-dashboard && pnpm exec tsc --noEmit | Fix the reported type errors |
| Frontend | Unit Tests | cd src/apps/github-backup-dashboard && pnpm test (Vitest) | make test-dashboard |
Go checks are scoped to the service that changed (./src/backend/github-backup-api/..., ./src/backend/github-backup-mcp/..., or ./src/services/github-backup-worker/...). Changes to go.mod, go.sum or shared Go code check ./....
Pre-push (builds)
| Trigger | Build Command |
|---|---|
Go code, go.mod or go.sum changed | CGO_ENABLED=0 go build ./... |
| Dashboard files changed | cd src/apps/github-backup-dashboard && pnpm run build |
A Dockerfile changed | docker buildx build --output type=cacheonly -f <Dockerfile> <context> for that image (the API's Dockerfile.service as the identity service, deploy/shared/db-backup with PG_MAJOR=16) |
.dockerignore or docker-compose.yml changed | The same, for every image |
The builds run side by side, and each one's output is printed when it finishes (PREPUSH_SERIAL=1 runs them one after another with their output as it comes). Docker builds are skipped with a warning when Docker or buildx is not available; CI builds and smoke-tests every image (make smoke).
4. Installation & Activation
To activate the hooks in your local repository clone:
# Recommended: Using Makefile
make hooks-install
# Or directly via installer script:
./scripts/install-hooks.sh
# Or manually configure Git:
git config core.hooksPath .githooks
chmod +x .githooks/*
With the relative path, git runs the hooks of the checkout you commit from, and a branch that has no .githooks (one from before the hooks, such as Frozen-UI-Do-Not-Touch) runs none. To keep the hooks on such branches and in their worktrees, set an absolute path to a checkout of main instead: git config core.hooksPath "$PWD/.githooks". pre-push runs the pre-commit beside it, so both come from that checkout.
5. Selective vs Full Validation
- Targeted Subsystem Execution (pre-commit):
- Staging only Go files runs gofmt, go vet and go test for the affected services.
- Staging only Observatory files (
src/services/github-backup-observatory/,pyrightconfig.json) runs Pyright and the Observatory test suites; a Python script or test elsewhere runs the script tests instead. - Staging only Frontend files runs Biome and
tsc --noEmit; the Next.js build waits for pre-push.
- Documentation Fast Path:
- Staging only documentation files (
*.md,*.txt,*.png,.gitignore,*.env.example) or only*.dbsnapshots skips every check except the secret scan, which always runs.
- Staging only documentation files (
- Everything else runs its own check, or none:
- A hook, a script, a test or the
Makefilegetsbash -nand the test that covers it (the table above); a workflow under.github/is linted with actionlint; a skill is validated. None of them runs the Go, Python or frontend suites: what they run is CI's to run. - A file no area claims (
.gitleaks.toml,docker-compose.yml, a config at the root) gets the secret scan only, and the hook says so: CI runs what the file affects.make pre-commit(PRECOMMIT_ALL=1) still runs every fast check.
- A hook, a script, a test or the
- New-Commit Validation (pre-push):
- The hook reads the refs git is pushing and works out the commits the push adds to the remote: those reachable from the pushed tip but from no branch the remote has (
PRECOMMIT_COMMITS="<sha> ..."). A stacked branch forks from the layer below it, and a branch that mergedmaincarriesmain's commits: the remote has both already, so neither is checked again; a merge commit counts only the files it changed against every parent, its conflict resolutions. The pre-commit checks run once for those commits, then the changed areas are built (added, copied, modified, renamed and deleted files all count). A push that adds nothing new says so and exits. With nothing of the remote known locally, the fork point from localmainstands in; without push information (for examplemake pre-push) it checks and builds everything.
- The hook reads the refs git is pushing and works out the commits the push adds to the remote: those reachable from the pushed tip but from no branch the remote has (
6. Developer CLI Commands
The Makefile provides unified developer shortcuts:
# Run every fast pre-commit check manually
make pre-commit
# Run the full pre-push gate (checks + Go, Next.js and Docker builds) without pushing
make pre-push
# Build and smoke-test the whole Docker Compose stack
make smoke
# Auto-format all Go and Frontend source code
make format
# Run Pyright (Python) and TypeScript compiler checks
make typecheck
# Run all linters (Biome, Go vet, Pyright)
make lint
# Run all unit and AI agent test suites
make test
7. Environment Variables & Bypass Overrides
In specific development scenarios (e.g. drafting intermediate commits or quick emergency docs), the hooks support explicit environment overrides:
- Bypass Test Suites (pre-commit, and the checks pre-push re-runs):
SKIP_TESTS=1 git commit -s -S -m "chore: quick update"
- Bypass Builds (pre-push; pre-commit no longer builds):
SKIP_BUILDS=1 git push
- Bypass Docker Image Builds Only (pre-push):
SKIP_DOCKER=1 git push
- Force Full Validation:
PRECOMMIT_ALL=1 git commit -s -S -m "feat: monorepo change"PREPUSH_ALL=1 git push
- Skip a Hook Entirely:
SKIP_HOOKS=1 git commit -s -S -m "wip: draft checkpoint" # or PRECOMMIT_BYPASS=1PREPUSH_BYPASS=1 git push
- Complete Git Bypass (Native Git):
git commit -s -S --no-verify -m "wip: draft checkpoint"
- Allow a Push To
main: the hook blocks any push whose remote ref isrefs/heads/main(includinggit push origin HEAD:mainfrom a feature branch);ALLOW_MAIN_PUSH=1 git pushoverrides it. (The worker used to pushapp.dbtomain; its state now lives in PostgreSQL and nothing pushes tomainautomatically.)
8. Continuous Integration Alignment
The hooks mirror the jobs in .github/workflows/ci.yml: gofmt, go vet, race-enabled go test, static Go builds, Pyright and the Observatory suites, Biome, tsc and the Next.js build. CI additionally checks go mod tidy, builds every image and runs the compose smoke test, so a commit that passes both hooks locally should pass CI.