如何使用gitlab api获取子文件夹和文件

时间:2013-09-23 06:25:10

标签: gitlab

我正在使用gitlab api获取文件和文件夹并成功,

但我只能获取目录名,而不能获取其子文件夹和文件。

那么,我怎样才能获得我的存储库的完整树。

请告诉我。

提前致谢, Mallikarjuna

3 个答案:

答案 0 :(得分:3)

根据API,我们可以使用

GET /projects/:id/repository/tree

列出项目中的文件和目录。但是我们只能以这种方式获取repo顶层的文件和目录,以及使用param path在顶级目录的子目录。

例如,如果您想获得script/js/components的目录,可以使用

GET /projects/:id/repository/tree?path=script/js/components

答案 1 :(得分:0)

Rest API

您可以使用recursive option通过/ projects /:id / repository / tree?recursive = true

获取完整的树

例如:https://your_gitlab_host/api/v4/projects/:id/repository/tree?recursive=true&per_page=100

GraphQL API

您还可以使用最近发布的Gitlab GraphQL API以递归的方式获取树:

{
  project(fullPath: "project_name_here") {
    repository {
      tree(ref: "master", recursive: true){
        blobs{
          nodes {
            name
            type
            flatPath
          }
        }
      }
    }
  }
}

您可以转到以下URL:https:// $ gitlab_url /-/ graphql-explorer并通过上面的查询

Graphql端点是“ https:// $ gitlab_url / api / graphql”上的POST 使用的示例:

gitlab_url=<your gitlab host>
access_token=<your access token>
project_name=<your project name>
branch=master

curl -s -H "Authorization: Bearer $access_token" \
     -H "Content-Type:application/json" \
     -d '{ 
          "query": "{ project(fullPath: \"'$project_name'\") { repository { tree(ref: \"'$branch'\", recursive: true){ blobs{ nodes { name type flatPath }}}}}}"
      }' "https://$gitlab_url/api/graphql" | jq '.'

答案 2 :(得分:0)

您应该对文件的完整路径进行 url 编码。例如,以免假设您的存储库下的文件路径是:javascript/header.js

然后你可以使用: curl --head --header "PRIVATE-TOKEN: " "https://<>/api/v4/projects//repository/files/javascript%2Fheader%2Ejs"