一个基于 Rust 的自建 Git 服务器,支持 Git Smart HTTP 协议和 Git LFS。
- Git Smart HTTP 协议:完整支持
git clone、git fetch、git push等操作 - Git LFS 支持:基于 AWS S3 的大文件存储
- 认证与授权:基于 JWT 的用户认证和仓库访问控制
- 高性能:使用 Axum 异步框架和 Tokio 运行时
- 易于部署:支持配置文件和环境变量配置
- S3 兼容:支持 AWS S3、MinIO 等 S3 兼容存储
- Web 框架:Axum - 高性能异步 Web 框架
- Git 操作:git2-rs - libgit2 的 Rust 绑定
- 对象存储:aws-sdk-s3 - AWS SDK for Rust
- 认证:jsonwebtoken - JWT 实现
- 异步运行时:Tokio - Rust 异步运行时
- Rust 1.70 或更高版本
- Git 2.0 或更高版本
- S3 兼容的对象存储(AWS S3、MinIO 等)
- 克隆仓库:
git clone https://github.com/yourusername/git-server.git
cd git-server- 编译项目:
cargo build --release- 创建配置文件:
cp config.toml.example config.toml- 编辑
config.toml配置您的设置:
[server]
host = "0.0.0.0"
port = 8080
[git]
repos_path = "./repos"
allow_create = true
allow_anonymous_read = false
allow_anonymous_write = false
[s3]
bucket = "git-lfs-storage"
region = "us-east-1"
# endpoint = "http://localhost:9000" # 使用 MinIO 时取消注释
[auth]
jwt_secret = "your-secret-key-here"
token_expiration = 604800
enabled = true- 运行服务器:
cargo run --release或者直接运行编译后的二进制文件:
./target/release/git-server也可以使用环境变量配置(优先级高于配置文件):
export GIT_SERVER_SERVER__HOST=0.0.0.0
export GIT_SERVER_SERVER__PORT=8080
export GIT_SERVER_GIT__REPOS_PATH=./repos
export GIT_SERVER_S3__BUCKET=git-lfs-storage
export GIT_SERVER_S3__REGION=us-east-1
export GIT_SERVER_AUTH__JWT_SECRET=your-secret-key
cargo run --releasecurl -X POST http://localhost:8080/username/my-repogit clone http://localhost:8080/username/my-repo.gitcd my-repo
git add .
git commit -m "Initial commit"
git push origin maingit pull origin maincurl http://localhost:8080/usernamecurl -X DELETE http://localhost:8080/username/my-repogit lfs installgit lfs track "*.psd"
git lfs track "*.zip"
git add .gitattributesgit add large-file.psd
git commit -m "Add large file"
git push origin maingit lfs pullgit-server/
├── src/
│ ├── auth/ # 认证和授权模块
│ │ └── mod.rs # JWT、用户认证、权限控制
│ ├── config/ # 配置模块
│ │ └── mod.rs # 配置文件和环境变量处理
│ ├── error/ # 错误处理
│ │ └── mod.rs # 自定义错误类型
│ ├── git/ # Git 操作模块
│ │ ├── mod.rs # 仓库管理
│ │ └── smart_http.rs # Git Smart HTTP 协议实现
│ ├── handlers/ # HTTP 处理器
│ │ ├── git.rs # Git 协议处理器
│ │ ├── lfs.rs # LFS API 处理器
│ │ └── mod.rs
│ ├── lfs/ # Git LFS 模块
│ │ └── mod.rs # LFS 数据结构和 API
│ ├── storage/ # 存储模块
│ │ └── mod.rs # S3 存储服务
│ ├── lib.rs # 库入口
│ └── main.rs # 应用入口
├── config.toml.example # 配置文件示例
├── Cargo.toml # Rust 项目配置
└── README.md # 项目文档
| 方法 | 路径 | 描述 |
|---|---|---|
| GET | /:owner/:repo/info/refs?service=git-upload-pack |
获取引用列表(克隆/拉取) |
| POST | /:owner/:repo/git-upload-pack |
上传包(克隆/拉取) |
| POST | /:owner/:repo/git-receive-pack |
接收包(推送) |
| POST | /:owner/:repo |
创建仓库 |
| DELETE | /:owner/:repo |
删除仓库 |
| GET | /:owner |
列出仓库 |
| 方法 | 路径 | 描述 |
|---|---|---|
| POST | /api/:owner/:repo/info/lfs/objects/batch |
LFS 批量操作 |
| POST | /api/:owner/:repo/info/lfs/objects/verify |
验证上传的对象 |
| GET | /api/:owner/:repo/info/lfs/objects/:oid |
获取对象信息 |
| DELETE | /api/:owner/:repo/info/lfs/objects/:oid |
删除对象 |
- None:无权限
- Read:只读权限(克隆、拉取)
- Write:读写权限(克隆、拉取、推送)
- Admin:管理员权限(所有操作)
- 用户可以读写自己的仓库
- 可以配置匿名读/写权限
- 支持 JWT 令牌认证
生成 JWT 令牌:
// 示例代码
let auth_service = AuthService::new(jwt_secret, expiration);
let token = auth_service.generate_token("username")?;使用令牌:
curl -H "Authorization: Bearer YOUR_JWT_TOKEN" \
http://localhost:8080/username/repo创建 Dockerfile:
FROM rust:1.75 as builder
WORKDIR /app
COPY . .
RUN cargo build --release
FROM debian:bookworm-slim
RUN apt-get update && apt-get install -y git ca-certificates && rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/target/release/git-server /usr/local/bin/
EXPOSE 8080
CMD ["git-server"]构建和运行:
docker build -t git-server .
docker run -p 8080:8080 \
-v ./repos:/repos \
-e GIT_SERVER_GIT__REPOS_PATH=/repos \
-e GIT_SERVER_S3__BUCKET=git-lfs \
-e GIT_SERVER_S3__REGION=us-east-1 \
git-server运行测试:
cargo test运行特定模块的测试:
cargo test git::
cargo test lfs::host: 服务器监听地址(默认:0.0.0.0)port: 服务器监听端口(默认:8080)workers: 工作线程数(默认:4)
repos_path: Git 仓库存储路径allow_create: 是否允许创建仓库allow_anonymous_read: 是否允许匿名读取allow_anonymous_write: 是否允许匿名写入
bucket: S3 存储桶名称region: AWS 区域endpoint: S3 端点(可选,用于 MinIO 等)access_key_id: AWS 访问密钥(可选)secret_access_key: AWS 密钥(可选)lfs_prefix: LFS 对象前缀(默认:lfs)
jwt_secret: JWT 密钥token_expiration: 令牌过期时间(秒)enabled: 是否启用认证
欢迎贡献!请遵循以下步骤:
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送到分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情
- Git - 版本控制系统
- Axum - Web 框架
- git2-rs - Git 库
- AWS SDK for Rust - S3 客户端
- Issues: GitHub Issues
- Email: your.email@example.com
- Git Smart HTTP 协议支持
- Git LFS 基础支持
- S3 存储集成
- JWT 认证
- 用户管理 API
- Web UI 管理界面
- Webhook 支持
- 仓库镜像功能
- 多数据库支持
- 更细粒度的权限控制
- API 速率限制
- 统计和监控
- SSH 协议支持
A: 在配置文件中设置 endpoint:
[s3]
bucket = "git-lfs"
region = "us-east-1"
endpoint = "http://localhost:9000"
access_key_id = "minioadmin"
secret_access_key = "minioadmin"A: 在配置文件中设置:
[auth]
enabled = false并启用匿名访问:
[git]
allow_anonymous_read = true
allow_anonymous_write = trueA: 本服务器基于 Rust 和 Tokio 构建,具有出色的性能表现。在典型配置下,可以处理数千个并发连接。
A: 支持所有标准的 Git 客户端,包括命令行 Git、GitHub Desktop、SourceTree 等。
Made with ❤️ using Rust