# pgit

A Git-like CLI that imports git repositories into PostgreSQL, making the entire history queryable with SQL. Built on pg-xpatch, a custom PostgreSQL Table Access Method that stores data with delta compression.

pgit doesn't replace git for day-to-day version control. Its value is analytical: once a repo is in Postgres, you can run arbitrary SQL against commits, file versions, paths, and blobs. File coupling queries, contributor analysis, churn detection, full-text search across commit messages — things that require fragile scripting over `git log` become straightforward joins and aggregates.

## How it works

pgit walks git's object graph and writes commits, blobs, file references, and paths into PostgreSQL tables. Text and binary content go through xpatch (a Rust delta compression library with bindings for multiple languages) to keep storage compact. The `multiEntry`-style decomposition means each file version at each commit is a row in the file references table, which is the largest component.

For the Linux kernel (1.4M commits, 24.4M file versions), import takes ~2 hours and the data occupies 2.7 GB — a 53.5x compression ratio over raw objects. See [[linux-kernel-pgit]] for the full analysis.

## Usage

```bash
go install github.com/imgajeed76/pgit/v4/cmd/pgit@latest

# Import a repository
pgit import /path/to/repo

# Query with psql
psql -d pgit -c "SELECT author, COUNT(*) FROM commits GROUP BY author ORDER BY 2 DESC LIMIT 10"
```

## Related components

- **pg-xpatch** — the PostgreSQL Table Access Method providing delta-compressed storage
- **xpatch** — the underlying delta compression library (Rust, with Python/Node.js/C/WASM bindings)

## Limitations

Requires a PostgreSQL instance with the pg-xpatch extension installed. Import is CPU and memory intensive — the Linux kernel import used a 24-core/512 GB RAM machine. Smaller repos would need less, but this isn't a laptop-friendly tool for large repositories.
