Skip to content

HuachunSi/git-server

Repository files navigation

Git Server

一个基于 Rust 的自建 Git 服务器,支持 Git Smart HTTP 协议和 Git LFS。

🌟 特性

  • Git Smart HTTP 协议:完整支持 git clonegit fetchgit 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 等)

安装

  1. 克隆仓库:
git clone https://github.com/yourusername/git-server.git
cd git-server
  1. 编译项目:
cargo build --release
  1. 创建配置文件:
cp config.toml.example config.toml
  1. 编辑 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
  1. 运行服务器:
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 --release

📖 使用说明

Git 操作

创建仓库

curl -X POST http://localhost:8080/username/my-repo

克隆仓库

git clone http://localhost:8080/username/my-repo.git

推送代码

cd my-repo
git add .
git commit -m "Initial commit"
git push origin main

拉取代码

git pull origin main

列出用户的所有仓库

curl http://localhost:8080/username

删除仓库

curl -X DELETE http://localhost:8080/username/my-repo

Git LFS 操作

安装 Git LFS

git lfs install

跟踪大文件

git lfs track "*.psd"
git lfs track "*.zip"
git add .gitattributes

添加大文件

git add large-file.psd
git commit -m "Add large file"
git push origin main

拉取 LFS 文件

git lfs pull

🏗️ 项目结构

git-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           # 项目文档

🔧 API 端点

Git Smart HTTP 协议

方法 路径 描述
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 列出仓库

Git LFS API

方法 路径 描述
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 认证

生成 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

🐳 Docker 部署

创建 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::

📝 配置说明

Server 配置

  • host: 服务器监听地址(默认:0.0.0.0
  • port: 服务器监听端口(默认:8080
  • workers: 工作线程数(默认:4

Git 配置

  • repos_path: Git 仓库存储路径
  • allow_create: 是否允许创建仓库
  • allow_anonymous_read: 是否允许匿名读取
  • allow_anonymous_write: 是否允许匿名写入

S3 配置

  • bucket: S3 存储桶名称
  • region: AWS 区域
  • endpoint: S3 端点(可选,用于 MinIO 等)
  • access_key_id: AWS 访问密钥(可选)
  • secret_access_key: AWS 密钥(可选)
  • lfs_prefix: LFS 对象前缀(默认:lfs

Auth 配置

  • jwt_secret: JWT 密钥
  • token_expiration: 令牌过期时间(秒)
  • enabled: 是否启用认证

🤝 贡献

欢迎贡献!请遵循以下步骤:

  1. Fork 本仓库
  2. 创建特性分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 开启 Pull Request

📄 许可证

本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情

🙏 致谢

📞 联系方式

🗺️ 路线图

  • Git Smart HTTP 协议支持
  • Git LFS 基础支持
  • S3 存储集成
  • JWT 认证
  • 用户管理 API
  • Web UI 管理界面
  • Webhook 支持
  • 仓库镜像功能
  • 多数据库支持
  • 更细粒度的权限控制
  • API 速率限制
  • 统计和监控
  • SSH 协议支持

💡 常见问题

Q: 如何使用 MinIO 代替 AWS S3?

A: 在配置文件中设置 endpoint

[s3]
bucket = "git-lfs"
region = "us-east-1"
endpoint = "http://localhost:9000"
access_key_id = "minioadmin"
secret_access_key = "minioadmin"

Q: 如何禁用认证?

A: 在配置文件中设置:

[auth]
enabled = false

并启用匿名访问:

[git]
allow_anonymous_read = true
allow_anonymous_write = true

Q: 性能如何?

A: 本服务器基于 Rust 和 Tokio 构建,具有出色的性能表现。在典型配置下,可以处理数千个并发连接。

Q: 支持哪些 Git 客户端?

A: 支持所有标准的 Git 客户端,包括命令行 Git、GitHub Desktop、SourceTree 等。


Made with ❤️ using Rust

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors