获取仓库目录Tree¶
GET /api/v5/repos/{owner}/{repo}/git/trees/{sha}
该接口用于获取指定仓库的目录文件树信息,可按分支名、提交或目录树 SHA 定位版本,支持递归获取子目录,并以分页方式查看目录条目。
访问令牌支持说明¶
- AtomGit 用户访问令牌(AtomGit user access tokens)
是否允许匿名访问¶
此接口允许匿名访问
请求参数¶
| 参数名 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
owner |
path | string | 是 | 仓库所属空间地址(组织或个人的地址 path) |
repo |
path | string | 是 | 仓库路径(path) |
sha |
path | string | 是 | 可以是分支名(如 master)、Commit 或者目录 Tree 的 SHA |
page |
query | integer | 否 | 当前的页码 |
per_page |
query | integer | 否 | 每页的数量,最大为 100,默认 20 |
recursive |
query | integer | 否 | 赋值为 1 时递归获取目录 |
file_path |
query | string | 否 | 文件树路径 |
响应参数¶
| 字段 | 类型 | 必返 | 可为空 | 说明 |
|---|---|---|---|---|
tree |
object[] | 是 | 否 | 目录树条目列表 |
tree 对象
| 字段 | 类型 | 必返 | 可为空 | 说明 |
|---|---|---|---|---|
tree.sha |
string | 是 | 否 | 目录树或文件对象的 SHA |
tree.name |
string | 是 | 否 | 文件或目录名称 |
tree.type |
string | 是 | 否 | 对象类型(tree 表示目录,blob 表示文件) |
tree.path |
string | 是 | 否 | 文件或目录路径 |
tree.mode |
string | 是 | 否 | 文件模式(如 040000 表示目录,100644 表示普通文件) |
tree.md5 |
string | 是 | 否 | 文件内容 MD5 值 |
失败响应¶
该接口可能返回以下常见失败状态码,错误响应体的字段名以后端实际返回为准。
400 参数错误
401 未认证
404 资源不存在
HTTP 响应状态码¶
| 状态码 | 说明 |
|---|---|
200 |
成功(OK) |
304 |
未修改(Not modified) |
400 |
参数错误(Bad request) |
401 |
需要认证(Requires authentication) |
404 |
资源不存在(Not found) |
429 |
请求过于频繁,触发限流(Too many requests) |
500 |
系统内部错误(Internal server error) |
请求示例¶
参数说明¶
- 该接口允许匿名访问,示例中未携带认证信息;如需访问私有仓库,可携带 AtomGit 用户访问令牌。
<owner>替换为仓库所属空间地址(组织或个人的地址 path);<repo>替换为仓库路径(path);<sha>替换为分支名、Commit 或目录 Tree 的 SHA。
cURL¶
curl -X GET 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/git/trees/<sha>' \
-H 'Accept: application/json'
递归获取目录时,可追加查询参数 recursive=1:
curl -X GET 'https://api.atomgit.com/api/v5/repos/<owner>/<repo>/git/trees/<sha>?recursive=1' \
-H 'Accept: application/json'
JavaScript¶
// 原生 fetch (Node.js 18+ / 浏览器)
const response = await fetch(
"https://api.atomgit.com/api/v5/repos/<owner>/<repo>/git/trees/<sha>",
{
headers: {
"Accept": "application/json",
},
},
);
if (!response.ok) {
throw new Error(`请求失败: ${response.status}`);
}
const data = await response.json();
console.log(data);
Python¶
import requests
resp = requests.get(
"https://api.atomgit.com/api/v5/repos/<owner>/<repo>/git/trees/<sha>",
headers={"Accept": "application/json"},
timeout=10,
)
resp.raise_for_status()
print(resp.json())
响应示例¶
状态码: 200
{
"tree": [
{
"sha": "5259c4b24f015ffdc3663e81837a730c2a108e1f",
"name": "b",
"type": "tree",
"path": "a/b",
"mode": "040000",
"md5": "a7e86136543b019d72468ceebf71fb8e"
}
]
}
错误码¶
| 错误码 | 说明 |
|---|---|
400 |
参数错误或请求无法解析 |
401 |
未认证或凭证无效(缺少/无效的访问凭证) |
404 |
请求的资源不存在 |
429 |
请求过于频繁,触发限流 |
500 |
系统内部错误 |
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 gitcode-dev/atomgit-api-doc