git 如何使用 GitLab API 访问源代码文件和列出目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17063630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:37:02  来源:igfitidea点击:

How to access source code files and list directories using GitLab API?

gitgitlab

提问by Arif Nadeem

I am using GitLab and I completed setup as described in Gitlab installation instructions, I am now trying to access its RestFul API to perform tasks like create/delete Projects or Users(I am able to do this successfully).

我正在使用 GitLab 并按照Gitlab 安装说明中的说明完成设置,我现在尝试访问其 RestFul API 以执行创建/删除项目或用户等任务(我能够成功完成此操作)。

I now have to list out all the directories within the master branch (or any other branch the project has) and want to access source code files listed in those directories(e.g. myproject->master->api, src->somefile.java etc), is it possible to do something like that using the existing Gitlab's API?

我现在必须列出 master 分支(或项目拥有的任何其他分支)中的所有目录,并希望访问这些目录中列出的源代码文件(例如 myproject->master->api、src->somefile.java 等),是否可以使用现有的 Gitlab 的 API 来做类似的事情?

The whole list of API's that can be called are mentioned hereand I was looking into Project Snippets APIand used this API GET /projects/:id/snippetsto list all snippets but it returns an empty array, I do have files in my project but the result is empty.

这里提到可以调用的整个 API 列表,我正在研究Project Snippets API并使用此 APIGET /projects/:id/snippets列出所有片段,但它返回一个空数组,我的项目中确实有文件但结果为空。

I am assuming that project snippets refer to source code files or any other files within the project, please correct me if I am wrong, I am confused here as the authors of the Gitlab API failed to explain the terminology behind the usage of the word 'snippets'.

我假设项目片段是指项目中的源代码文件或任何其他文件,如果我错了,请纠正我,我在这里感到困惑,因为 Gitlab API 的作者未能解释“”一词的用法背后的术语片段'。

How can I access full directory structure and get files I need using the API?

如何使用 API 访问完整的目录结构并获取我需要的文件?

回答by VonC

I am assuming that project snippets refer to source code files or any other files within the project

我假设项目片段是指项目中的源代码文件或任何其他文件

No, you can see snippets in the GitLab demo project: diaspora snippets.
They are in a separate tabs than the "Files" tab.
They would be the equivalent of GitHub gist.

不,您可以在 GitLab 演示项目中看到片段:diaspora snippets
它们位于与“文件”选项卡不同的选项卡中。
它们相当于GitHub gist

Have a look at the repository API, like:

查看存储库 API,例如:

List repository tree

列出存储库树

Get a list of repository files and directories in a project.

获取项目中存储库文件和目录的列表。

GET /projects/:id/repository/tree

Raw blob content

原始 blob 内容

Get the raw file contents for a file.

获取文件的原始文件内容。

GET /projects/:id/repository/commits/:sha/blob

astrattomentionsthat the new API (6.x) requires that a filepath is appended:

astratto提到新的 API (6.x) 要求附加文件路径:

GET /projects/:id/repository/blobs/:sha?filepath=:filepath