# Repositories API

> 原文：[https://docs.gitlab.com/ee/api/repositories.html](https://docs.gitlab.com/ee/api/repositories.html)

*   [List repository tree](#list-repository-tree)
*   [Get a blob from repository](#get-a-blob-from-repository)
*   [Raw blob content](#raw-blob-content)
*   [Get file archive](#get-file-archive)
*   [Compare branches, tags or commits](#compare-branches-tags-or-commits)
*   [Contributors](#contributors)
*   [Merge Base](#merge-base)

# Repositories API[](#repositories-api "Permalink")

## List repository tree[](#list-repository-tree "Permalink")

获取项目中存储库文件和目录的列表. 如果可公开访问该存储库，则无需身份验证即可访问此端点.

此命令提供的功能与`git ls-tree`命令基本相同. 有关更多信息，请参阅[Git internals 文档](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects/#_tree_objects)中的*Tree Objects*部分.

```
GET /projects/:id/repository/tree 
```

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `path` （可选）-存储库中的路径. 用于获取子目录的内容
*   `ref` （可选）-存储库分支或标记的名称，或者如果未提供默认分支的名称
*   `recursive` （可选）-用于获取递归树的布尔值（默认为 false）
*   `per_page` （可选）-每页显示的结果数. 如果未指定，则默认为`20`

```
[  {  "id":  "a1e8f8d745cc87e3a9248358d9352bb7f9a0aeba",  "name":  "html",  "type":  "tree",  "path":  "files/html",  "mode":  "040000"  },  {  "id":  "4535904260b1082e14f867f7a24fd8c21495bde3",  "name":  "images",  "type":  "tree",  "path":  "files/images",  "mode":  "040000"  },  {  "id":  "31405c5ddef582c5a9b7a85230413ff90e2fe720",  "name":  "js",  "type":  "tree",  "path":  "files/js",  "mode":  "040000"  },  {  "id":  "cc71111cfad871212dc99572599a568bfe1e7e00",  "name":  "lfs",  "type":  "tree",  "path":  "files/lfs",  "mode":  "040000"  },  {  "id":  "fd581c619bf59cfdfa9c8282377bb09c2f897520",  "name":  "markdown",  "type":  "tree",  "path":  "files/markdown",  "mode":  "040000"  },  {  "id":  "23ea4d11a4bdd960ee5320c5cb65b5b3fdbc60db",  "name":  "ruby",  "type":  "tree",  "path":  "files/ruby",  "mode":  "040000"  },  {  "id":  "7d70e02340bac451f281cecf0a980907974bd8be",  "name":  "whitespace",  "type":  "blob",  "path":  "files/whitespace",  "mode":  "100644"  }  ] 
```

## Get a blob from repository[](#get-a-blob-from-repository "Permalink")

Allows you to receive information about blob in repository like size and content. Note that blob content is Base64 encoded. This endpoint can be accessed without authentication if the repository is publicly accessible.

```
GET /projects/:id/repository/blobs/:sha 
```

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `sha` （必填）-Blob SHA

## Raw blob content[](#raw-blob-content "Permalink")

通过 blob SHA 获取 blob 的原始文件内容. 如果可公开访问该存储库，则无需身份验证即可访问此端点.

```
GET /projects/:id/repository/blobs/:sha/raw 
```

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `sha` （必填）-Blob SHA

## Get file archive[](#get-file-archive "Permalink")

获取存储库的存档. 如果可公开访问该存储库，则无需身份验证即可访问此端点.

该端点的速率限制阈值为每分钟 5 个请求.

```
GET /projects/:id/repository/archive[.format] 
```

`format`是归档格式的可选后缀. 默认值为`tar.gz` 选项是`tar.gz` ， `tar.bz2` ， `tbz` ， `tbz2` ， `tb2` ， `bz2` ， `tar`和`zip` . 例如，指定`archive.zip`将以 ZIP 格式发送存档.

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `sha` （可选）-提交要下载的 SHA. 可以使用标签，分支引用或 SHA. 如果未指定，则默认为默认分支的尖端. 例如：

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.com/api/v4/projects/<project_id>/repository/archive?sha=<commit_sha>" 
```

## Compare branches, tags or commits[](#compare-branches-tags-or-commits "Permalink")

如果可公开访问该存储库，则无需身份验证即可访问此端点. 请注意，如果达到[差异限制](../development/diffs.html#diff-limits) ，差异可能会有一个空的差异字符串.

```
GET /projects/:id/repository/compare 
```

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `from` （必填）-提交 SHA 或分支名称
*   `to` （必需）-提交 SHA 或分支名称
*   `straight` （可选）-比较方法，对于`from`和`to`之间（ `from` .. `to` ）之间的直接比较，为`true`对于使用合并基数（ `from` ... `to` ）'比较`to` `false` . 默认值为`false` .

```
GET /projects/:id/repository/compare?from=master&to=feature 
```

Response:

```
 {  "commit":  {  "id":  "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",  "short_id":  "12d65c8dd2b",  "title":  "JS fix",  "author_name":  "Example User",  "author_email":  "user@example.com",  "created_at":  "2014-02-27T10:27:00+02:00"  },  "commits":  [{  "id":  "12d65c8dd2b2676fa3ac47d955accc085a37a9c1",  "short_id":  "12d65c8dd2b",  "title":  "JS fix",  "author_name":  "Example User",  "author_email":  "user@example.com",  "created_at":  "2014-02-27T10:27:00+02:00"  }],  "diffs":  [{  "old_path":  "files/js/application.js",  "new_path":  "files/js/application.js",  "a_mode":  null,  "b_mode":  "100644",  "diff":  "--- a/files/js/application.js\n+++ b/files/js/application.js\n@@ -24,8 +24,10 @@\n //= require g.raphael-min\n //= require g.bar-min\n //= require branch-graph\n-//= require highlightjs.min\n-//= require ace/ace\n //= require_tree .\n //= require d3\n //= require underscore\n+\n+function fix() { \n+  alert(\"Fixed\")\n+}",  "new_file":  false,  "renamed_file":  false,  "deleted_file":  false  }],  "compare_timeout":  false,  "compare_same_ref":  false  } 
```

## Contributors[](#contributors "Permalink")

获取存储库贡献者列表. 如果可公开访问该存储库，则无需身份验证即可访问此端点.

```
GET /projects/:id/repository/contributors 
```

Parameters:

*   `id` （必填）-经过身份验证的用户拥有[的项目](README.html#namespaced-path-encoding)的 ID 或[URL 编码路径](README.html#namespaced-path-encoding)
*   `order_by` （可选）-返回按`name` ， `email`或`commits` （按提交日期排序）字段排序的贡献者. 默认为`commits`
*   `sort` （可选）-返回贡献者按`asc`或`desc`顺序排序. 默认为`asc`

Response:

```
[{  "name":  "Example User",  "email":  "example@example.com",  "commits":  117,  "additions":  2097,  "deletions":  517  },  {  "name":  "Sample User",  "email":  "sample@example.com",  "commits":  33,  "additions":  338,  "deletions":  244  }] 
```

## Merge Base[](#merge-base "Permalink")

获取 2 个或更多引用（提交 SHA，分支名称或标签）的公共祖先.

```
GET /projects/:id/repository/merge_base 
```

| Attribute | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | integer/string | yes | 项目的 ID 或[URL 编码的路径](README.html#namespaced-path-encoding) |
| `refs` | array | yes | 裁判找到共同的祖先，可以传递多个裁判 |

```
curl --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/5/repository/merge_base?refs[]=304d257dcb821665ab5110318fc58a007bd104ed&refs[]=0031876facac3f2b2702a0e53a26e89939a42209" 
```

响应示例：

```
{  "id":  "1a0b36b3cdad1d2ee32457c102a8c0b7056fa863",  "short_id":  "1a0b36b3",  "title":  "Initial commit",  "created_at":  "2014-02-27T08:03:18.000Z",  "parent_ids":  [],  "message":  "Initial commit\n",  "author_name":  "Example User",  "author_email":  "user@example.com",  "authored_date":  "2014-02-27T08:03:18.000Z",  "committer_name":  "Example User",  "committer_email":  "user@example.com",  "committed_date":  "2014-02-27T08:03:18.000Z"  } 
```