logo
0
0
WeChat Login

RESTful2Git

轻量级 HTTP → Git 适配层:通过统一的 RESTful API 直接操作远端 Git 仓库(HTTPS + BasicAuth 透传),不做本地持久化。

快速开始

PORT=8080 go run ./cmd/server
  • 仅支持 HTTPS 远端:https://{host}/{org}/{repo}.git
  • 请求体大小默认上限 20MB(可在 main.go 修改)

统一响应

除原始文件下载外,所有接口返回:

{ "code": 200, "message": "ok", "data": { ... } }
  • 成功:code=200message=ok
  • 失败:code=HTTP 状态码message=错误说明

请求头

  • X-Git-UsernameX-Git-Password:远端 BasicAuth。公开仓库的读接口可不传;写接口必传。
  • X-Git-AuthorX-Git-Email:写操作的作者信息。未提供时默认:Name=restful2gitEmail=noreply@{host};作者名优先取 X-Git-Author

路由与用法(支持多级仓库路径,如 Bring/Tools/RESTful2Git

  • 列目录:GET /git/{host}/tree/{repo}?ref=&path=
  • 读文件:GET /git/{host}/files/{repo}?ref=&path=&raw=&download=
    • raw=true 时自动识别 MIME,并可通过 download=true 控制 Content-Disposition
    • 若文件是 LFS 指针,自动访问 LFS 下载真实内容
  • 提交列表:GET /git/{host}/commits/{repo}?ref=&limit=
  • 提交详情:GET /git/{host}/commit/{sha}/{repo}?ref=&patch=(可选返回 patch)
  • 比较:GET /git/{host}/compare/{repo}?from=&to=&patch=
  • 单提交差异:GET /git/{host}/diff/{repo}?ref=&path=(自动加深浅克隆以获取父提交)
  • 分支列表:GET /git/{host}/branches/{repo}
  • 标签列表:GET /git/{host}/tags/{repo}

  • 新建/更新文件:PUT /git/{host}/files/{repo}
    • body: path, content(base64|text), encoding(base64|text=默认base64), ref, [message], [author], [baseCommit], [lfs]
    • 未传 message 自动生成:新增 feat: add {path},更新 chore: update {path}
  • 删除文件:DELETE /git/{host}/files/{repo}
    • body: path, ref, [message], [author], [baseCommit]
    • 文件不存在返回 404;提交时自动拾取删除,不再使用索引删除避免 entry not found
  • 移动文件:POST /git/{host}/files:move/{repo}
    • body: from, to, ref, [message], [author], [baseCommit]
    • 未传 message 自动生成:chore: move {from} -> {to}
  • 上传文件(表单):POST /git/{host}/upload/{repo}
    • form: file(binary), path, ref, [message], [author], [lfs]
    • lfs=true 时自动使用 LFS batch 与上传;若对象已存在,写入指针文件(避免 502)

分支/标签

  • 创建分支:POST /git/{host}/branches/{repo}(body: name, from
  • 删除分支:DELETE /git/{host}/branches/{name}/{repo}(直接 remote push 删除)
  • 创建标签:POST /git/{host}/tags/{repo}(body: name, from,存在则 409)
  • 删除标签:DELETE /git/{host}/tags/{name}/{repo}(直接 remote push 删除)

错误码约定

  • 400 Bad Request:参数错误、无法解析、对象解析失败等
  • 401 Unauthorized:缺少或错误的 X-Git-Username/Password
  • 404 Not Found:仓库不存在、文件不存在、分支/标签不存在
  • 409 Conflict:并发冲突(baseCommit 不匹配)、无内容可提交(no changes to commit)、标签已存在
  • 502 Bad Gateway:远端 Git/LFS 返回的错误(非不存在类)

仓库不存在时统一:{ "code": 404, "message": "repository not found" }

项目架构

  • main.go:Gin 入口、路由、限流
  • internal/http/handlers.go:业务处理(树、文件、提交、对比、分支、标签、上传等)
  • internal/http/response.go:统一响应封装
  • internal/lfs/client.go:LFS batch 客户端(upload/download,pointer 处理)

关键特性:

  • 无持久化:每次请求临时浅克隆(Depth=1),完成即清理
  • HTTPS + BasicAuth 直连远端,多主机、多组织/多仓库、多级路径
  • 读接口鉴权可选(公共仓库),写接口鉴权必传
  • MIME 自动识别、下载文件名、LFS 自动指针/内容处理

本地运行与部署

go build ./... PORT=8080 ./restful2git # 或 go run ./cmd/server

容器化与系统化部署可将 PORT、反向代理(TLS/鉴权)置于网关层。

许可协议

本项目采用 MIT License。

About

No description, topics, or website provided.
Language
Go96.2%
Dockerfile2%
HTML0.9%
Shell0.8%
Others0.1%