Skip to main content

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-commit runs fast checks only: secret scan, formatting and lint, static analysis, and the unit tests of the areas that changed.
  • .githooks/pre-push re-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:

  1. 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.
  2. AI Observatory Service: FastAPI + LangChain Tool-Calling RAG application managed with uv under src/services/github-backup-observatory/.
  3. 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, mapfile or ${var,,}), as well as Linux, Windows Git Bash and WSL. tests/githooks_test.sh exercises 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)​

SubsystemStageValidation CommandRemediation Command
AllSecret scanStaged .env, key files and token patternsgit restore --staged <file>
WorkflowsLintactionlint (from PATH, else its image) for staged .github/ filesFix what actionlint reports
Hooks, scripts, tests, MakefileSyntax, own testsbash -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 MakefileFix what the test reports
SkillsSchemapython3 scripts/validate-skills.py for staged .agents/skills/ filesFix what it reports
GoFormattinggofmt -l <staged_files>make format or gofmt -w <file>
GoStatic Analysisgo vet <changed service packages>Fix the reported issues
GoUnit Testsgo test <changed service packages>make test-go
PythonStatic Typecheckcd src/services/github-backup-observatory && uv run --with pyright pyrightmake typecheck
PythonTest Suitescd src/services/github-backup-observatory && uv run pytestmake test-py / make test-agents
FrontendLint & Formatcd src/apps/github-backup-dashboard && pnpm run lint (biome check)make format / pnpm run format
FrontendType Checkcd src/apps/github-backup-dashboard && pnpm exec tsc --noEmitFix the reported type errors
FrontendUnit Testscd 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)​

TriggerBuild Command
Go code, go.mod or go.sum changedCGO_ENABLED=0 go build ./...
Dashboard files changedcd src/apps/github-backup-dashboard && pnpm run build
A Dockerfile changeddocker 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 changedThe 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​

  1. 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.
  2. Documentation Fast Path:
    • Staging only documentation files (*.md, *.txt, *.png, .gitignore, *.env.example) or only *.db snapshots skips every check except the secret scan, which always runs.
  3. Everything else runs its own check, or none:
    • A hook, a script, a test or the Makefile gets bash -n and 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.
  4. 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 merged main carries main'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 local main stands in; without push information (for example make pre-push) it checks and builds everything.

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=1
    PREPUSH_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 is refs/heads/main (including git push origin HEAD:main from a feature branch); ALLOW_MAIN_PUSH=1 git push overrides it. (The worker used to push app.db to main; its state now lives in PostgreSQL and nothing pushes to main automatically.)

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.