Skip to content
CodeTeasePublic

About

A small headless Git server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

SGit

SGit is a fast, secure, and lightweight Git Smart HTTP Protocol server written in Rust, leveraging the performance and safety of Axum, Tokio, and libgit2 (git2-rs). It acts as a self-hosted Git HTTP backend, offering complete support for Git Smart Protocol v2, bi-directional streaming, fine-grained access control, automatic maintenance, and robust security constraints out of the box.

A CodeTease project.

SGit is not ready for production yet.


Features

  • High-Performance Smart HTTP Backend: Exposes the Git Smart Protocol (v2-ready) endpoints (info/refs, git-upload-pack, and git-receive-pack) via asynchronous, bi-directional streaming.
  • Auto-Initialization: Automatically initializes bare Git repositories on the first git push, eliminating the need for manual pre-creation of repositories on the server.
  • Deep Namespace Support: Out-of-the-box support for arbitrarily nested directories/namespaces (e.g., git clone http://localhost:3000/org/team/project.git).
  • Secure by Design:
    • Path Traversal Protection: Multi-layered path sanitization checks block malicious path names containing .., leading/trailing slashes, redundant slashes (//), backslashes (\), or empty names.
    • HTTP Basic Authentication: Enforces credential checks on all write/push actions while allowing read actions (such as cloning or fetching) to bypass authentication if desired.
    • Secure Password Hashing: Avoids plaintext storage by validating user passwords against salted Argon2id hashes (legacy SHA-256 hashes are also supported) defined in a simple TOML configuration file.
  • Self-Healing and Automatic Maintenance:
    • HEAD Resolution: Automatically updates the repository's HEAD to point to a valid active branch (e.g., main or master) if it becomes unborn or invalid after a push.
    • Asynchronous Garbage Collection: Spawns non-blocking background tasks to execute git gc --auto upon successful pushes, keeping disk usage and packfiles optimized.
    • Graceful Shutdown: Listens for system interruption signals to clean up ongoing processes and close client connections safely.
    • Request Timeouts: Safely terminates stalled or runaway git operations using configurable request timeouts.
    • Dynamic Configuration Reloading: Listens for the SIGHUP signal on Unix platforms to dynamically reload user credentials from the TOML configuration file in-place, without restarting the server or interrupting active Git streams.

Configuration

SGit is highly configurable using process-wide environment variables:

Environment Variable Description Default Value
SGIT_HOST IP address / Host to bind the server to 0.0.0.0
SGIT_PORT Port on which the SGit server will run 3000
SGIT_DATA_DIR Base directory where the Git repositories are stored /var/lib/sgit
SGIT_USERS_FILE Path to the TOML configuration file containing user credentials users.toml
SGIT_TIMEOUT Execution timeout in seconds for Git streaming operations 60
SGIT_MAX_REQUEST_SIZE_MB Maximum request body size in MB to prevent disk exhaustion from pushing massive repositories (0 disables the limit) 500
SGIT_MAX_CONCURRENT_REQS Maximum number of concurrent git processes to prevent CPU/RAM usage spikes (0 disables the limit) 20
SGIT_RATE_LIMIT_PER_IP Maximum number of requests per IP address per minute to mitigate spam/DoS 30
SGIT_READ_TIMEOUT Execution timeout in seconds for small metadata read requests like info/refs 15
SGIT_STREAM_TIMEOUT Idle timeout in seconds for data streaming during push/pull operations; resets whenever data moves and 0 disables it (falls back to SGIT_TIMEOUT) 60
SGIT_MAX_REPO_SIZE_MB Maximum allowed repository storage size in MB (enforces disk quota per repository) None (unlimited)

User Authentication Setup

To enable HTTP Basic Authentication for write operations, create a users.toml file (or specify another location via SGIT_USERS_FILE).

If the configuration file is absent, SGit operates in public-write mode (authentication checks are bypassed entirely).

Format of users.toml

Passwords can be stored as salted Argon2id PHC hashes (recommended) or as legacy unsalted SHA-256 hexadecimal hashes. Argon2id hashes are verified in constant time and include their algorithm parameters and a unique salt, so they should be preferred for new deployments.

[users]
# Password for alice is "supersecret"
alice = "f75778f7425be4db0369d09af37a6c2b9a83dea0e53e7bd57412e4b060e607f7"

# Password for bob is "pass123"
bob = "9b8769a4a742959a2d0298c36fb70623f2dfacda8436237df08d8dfd5b37374c"

To generate a legacy SHA-256 hash for a password, you can run:

echo -n "your_password" | sha256sum

To generate a salted Argon2id hash, use the argon2 CLI (or any Argon2 library) and store the complete PHC string it prints:

echo -n "your_password" | argon2 "$(openssl rand -base64 12)" -id -e

Building and Running

Prerequisites

  • Rust (Edition 2024, Rust 1.75+ recommended)
  • Git (CLI must be available on the host PATH for streaming operations)
  • OpenSSL development libraries (required by git2 / openssl-sys unless fully vendored)

1. Build and Run Locally

# Clone the repository
git clone https://github.com/codetease/sgit
cd sgit

# Build the project in release mode
cargo build --release

# Run SGit with custom variables
SGIT_PORT=3000 SGIT_USERS_FILE=users.toml cargo run --release

2. Run with Docker

SGit includes a multi-stage Dockerfile for streamlined containerized deployment.

# Build the Docker image
docker build -t sgit:latest .

# Run the container with a mounted volume for persistent repository storage
docker run -d \
  -p 3000:3000 \
  -v /var/lib/sgit:/var/lib/sgit \
  -e SGIT_DATA_DIR=/var/lib/sgit \
  --name sgit-server \
  sgit:latest

Or pull from GHCR:

docker pull ghcr.io/codetease/sgit:latest

Testing

SGit features a robust suite of integration and unit tests covering health checks, security path sanitization, concurrent request safety, and authentication.

To run the test suite:

cargo test

Note: Since integration tests modify or mock environment variables, they are safely run sequentially using internal test locks to prevent race conditions during execution.


📖 Usage Examples

Once SGit is running at http://localhost:3000, you can interact with it using standard git commands.

Push to Create/Update a Repository

Pushing to SGit will automatically create the repository if it does not exist (assuming you are authenticated if a users.toml is present).

# Create a local repository and add files
git init my-awesome-project
cd my-awesome-project
echo "# Awesome Project" > README.md
git add README.md
git commit -m "Initial commit"

# Add SGit as remote (replace 'alice' and 'localhost:3000' with your settings)
git remote add origin http://alice@localhost:3000/org/projects/my-awesome-project.git

# Push changes
git push -u origin master

Clone a Repository

Reading is permitted publicly (bypassing authentication), or managed via custom setups.

git clone http://localhost:3000/org/projects/my-awesome-project.git

License

This project is under the MIT License.

About

A small headless Git server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Packages

Contributors

Languages