如何获取文件Git API

时间:2018-02-13 06:31:00

标签: git api github

我有分支“开发”。在这个分支中,我有文件index.php。

如何使用Git Rest API

  • 在branch Develop中获取index.php文件的行数代码
  • 在branch Develop中获取index.php文件的 SLOC

有任何人知道吗?

enter image description here

1 个答案:

答案 0 :(得分:2)

根据GitHub documentation,,没有直接的方法可以做到这一点。

  

在branch Develop中获取index.php文件的行数代码。

  1. 获取GitHub身份验证令牌here

  2. 使用信息here使用GitHub API获取文件的base64编码内容:

    GET /repos/:owner/:repo/contents/:path
    

    curl示例:

    curl https://api.github.com/repos/google/protobuf/contents/README.md
    
    curl --header 'Authorization: token $GITHUB_AUTHENTICATION_TOKEN' \
     --header 'Accept: application/vnd.github.v3.raw' \
     --remote-name \
     --location <url to the file>
    

    传递分支的ref参数。将?ref=branch添加到特定分支的URL末尾。

  3. 解码响应中base64编码的content字段。如果使用Linux shell,请使用base64 --decode。一些编程语言有一个库来执行此操作。

  4. 计算文件中的行数。如果使用Linux shell,请使用wc -l。一些编程语言有一个库来执行此操作。

  5.   

    在分支开发中获取index.php文件的这个文件的SLOC。

    在上面的步骤 3 之后使用sloccountcloc