我如何获得“沙”参数从GitHub API没有下载整个文件? [英] How do I get the "sha" parameter from GitHub API without downloading the whole file?

查看:84
本文介绍了我如何获得“沙”参数从GitHub API没有下载整个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图通过使用GitHub API来更新GitHub存储库中的文件,同时只有该文件的路径。我的计划是第一次,获取像这里描述的文件内容: https://developer.github.com / v3 / repos / contents / ,然后使用sha字段更新文件。



回答这里如何找到Github文件的SHA blob 。然后,使用GET / repos /:owner /:repo / contents /:path,下载整个文件,作为返回的JSON中的一个字段,效率低下。所以,我的问题是:有没有办法在不下载整个文件的情况下获得sha字段?

可以利用< rev>:<路径> 扩展SHA -1语法 来从其父树中检索有关Blob的元数据。

例如,假设您使用 libgit2 / libgit2sharp 存储库,并且您想要检索文件 Lib的sha /MoQ/Moq.license.txt 来自 master 分支:


  • 利用 GitHub Trees API 检索与 Lib / MoQ 父目录匹配的树

  • 从返回的Json有效内容中检索blob的sha,其路径是 Moq.license.txt

  • 确保网址编码< rev>:<路径> 段,因为它包含正斜杠



简介:


  • 语法: GET / repos /< ; owner> /< repo> / git / trees / url_encode(< branch_name>:< parent_path>)
  • https://api.github.com/repos/libgit2/libgit2sharp/git/trees/master:Lib%2FMoQ\">https://api.github.com/repos/libgit2/libgit2sharp/git/trees/master:Lib %2FMoQ


上面的示例链接将返回以下有效内容

  {
sha:2f2c87728225f9cbb6e2d8c5997b6031e72ddca4,
url:https://api.github.com/repos/libgit2/libgit2sharp/ git / trees / 2f2c87728225f9cbb6e2d8c5997b6031e72ddca4,
tree:[
{
path:Moq.dll,
mode:100644,
type:blob,
sha:bdd4235f215541017a9f37b6155f18e309573838,
size:659968,
url:https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/bdd4235f215541017a9f37b6155f18e309573838
},
{
path: Moq.license.txt,
mode:100644,
type:blob,
sha:c9216ccba318292d76fd308f232e7871bbbe77be,
size :1748,
url:https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/c9216ccba318292d76fd308f232e7871bbbe77be
},
{
路径:Moq.xml,
mode:100644,
type:blob,
sha:160c1b5165fd967f4c79bc6043f0cc2ec28710d8,
大小:314572,
url:https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/160c1b5165fd967f4c79bc6043f0cc2ec28710d8
}
],
truncated:false
}


I have been trying to update a file in a GitHub repository by using GitHub API while having only a path to that file. My plan was first, get the file contents like described here: https://developer.github.com/v3/repos/contents/ , and then use the "sha" field to "update a file".

It worked fine just as described in the answer here How to find a Github file 's SHA blob .

However, using GET /repos/:owner/:repo/contents/:path , downloads the whole file, as a field in the returned JSON, which is inefficient. So, my question is: is there a way to get just the "sha" field without downloading the entire file?

解决方案

You could leverage the <rev>:<path> extended SHA-1 syntax to retrieve some meta data about the Blob from its parent Tree.

For instance, provided you work with the libgit2/libgit2sharp repository, and you'd like to retrieve the sha of the file Lib/MoQ/Moq.license.txt from the master branch:

  • Leverage the GitHub Trees API to retrieve the content of the Tree matching the Lib/MoQ parent directory
  • Retrieve from the returned Json payload the sha of the blob which path is Moq.license.txt
  • Make sure to url encode the the <rev>:<path> segment as it contains forward slashes

In brief:

The example link above will return the following payload

{
  "sha": "2f2c87728225f9cbb6e2d8c5997b6031e72ddca4",
  "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/trees/2f2c87728225f9cbb6e2d8c5997b6031e72ddca4",
  "tree": [
    {
      "path": "Moq.dll",
      "mode": "100644",
      "type": "blob",
      "sha": "bdd4235f215541017a9f37b6155f18e309573838",
      "size": 659968,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/bdd4235f215541017a9f37b6155f18e309573838"
    },
    {
      "path": "Moq.license.txt",
      "mode": "100644",
      "type": "blob",
      "sha": "c9216ccba318292d76fd308f232e7871bbbe77be",
      "size": 1748,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/c9216ccba318292d76fd308f232e7871bbbe77be"
    },
    {
      "path": "Moq.xml",
      "mode": "100644",
      "type": "blob",
      "sha": "160c1b5165fd967f4c79bc6043f0cc2ec28710d8",
      "size": 314572,
      "url": "https://api.github.com/repos/libgit2/libgit2sharp/git/blobs/160c1b5165fd967f4c79bc6043f0cc2ec28710d8"
    }
  ],
  "truncated": false
}

这篇关于我如何获得“沙”参数从GitHub API没有下载整个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆