在GitHub API - 什么是最好的方式来获取与每个文件相关联的最后一个提交消息? [英] On GitHub API - what is the best way to get the last commit message associated with each file?

查看:341
本文介绍了在GitHub API - 什么是最好的方式来获取与每个文件相关联的最后一个提交消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,邮件与提交相关联。但是当你看一下GitHub上的一个仓库,它有助于列出每个文件的消息,以及最后更改的时间。

So far as I understand it, messages are associated with commits. But when you look at a repo on GitHub it helpfully lists the message by each file, for when it was last changed.

我想在网络中复制视图的回购我有。看看GitHub api它看起来我唯一的方法来获得这个信息是下载所有提交(可以分页),并从最近的工作分配提交消息到文件你的本地缓存,进一步和进一步回到,直到你有每个文件的消息,潜在的第一次提交,如果任何文件从最初提交后没有更改

I'd like to replicate that in a web view of a repo I have. Looking at the GitHub api it looks to me the only way to get that info is to download all the commits (which can be paged), and work from the most recent ones assigning commit messages to the files in your local cache, going further and further back until you've got the message for every file, potentially to the very first commit, if any of the files have not been changed since the initial commit

问题是,是正确的方法吗?

Question is, is that the right way to do it? Is that not going to kill even the 5000/hr quota?

推荐答案

好的,在弄清楚你需要的是最新的每个文件的提交消息,这里是你可以做的。

Ok, after figuring out that what you need is the latest commit message for each file, here's what you can do.

首先,获取存储库中的文件列表。为此,您需要:

First, get the list of files in your repository. To do this, you need to:

1) fetch要列出文件的分支的引用对象

GET https://api.github.com/repos/:owner/:repo/git/refs/heads/:branch

您可能需要主分支,因此这是您要创建的请求的示例:

You probably want the master branch, so this is an example of the request you will make:

https://api.github.com/repos/izuzak/pmrpc/git/refs/heads/master

回应您将得到如下:

{
  "ref": "refs/heads/master",
  "url": "https://api.github.com/repos/izuzak/pmrpc/git/refs/heads/master",
  "object": {
    "sha": "fd6973f430a3367ad718ff049f1b075843913d6f",
    "type": "commit",
    "url": "https://api.github.com/repos/izuzak/pmrpc/git/commits/fd6973f430a3367ad718ff049f1b075843913d6f"
  }
}

2)使用 object.url 属性获取引用指向的提交对象您在上一步中收到的回复:

2) fetch the commit object that the reference points to, using the object.url property of the response you received in the previous step:

GET https://api.github.com/repos/izuzak/pmrpc/git/commits/fd6973f430a3367ad718ff049f1b075843913d6f

您将获得的响应将如下所示:

The response you will get will look like this:

{
  "sha": "fd6973f430a3367ad718ff049f1b075843913d6f",
  "url": "https://api.github.com/repos/izuzak/pmrpc/git/commits/fd6973f430a3367ad718ff049f1b075843913d6f",
  "html_url": "https://github.com/izuzak/pmrpc/commits/fd6973f430a3367ad718ff049f1b075843913d6f",
  "author": {
    "name": "Ivan Zuzak",
    "email": "izuzak@gmail.com",
    "date": "2013-04-09T08:55:45Z"
  },
  "committer": {
    "name": "Ivan Zuzak",
    "email": "izuzak@gmail.com",
    "date": "2013-04-09T08:55:45Z"
  },
  "tree": {
    "sha": "f5f5de80f67dd794ffbd4abb855fb7d1a573660e",
    "url": "https://api.github.com/repos/izuzak/pmrpc/git/trees/f5f5de80f67dd794ffbd4abb855fb7d1a573660e"
  },
  "message": "fix typos",
  "parents": [
    {
      "sha": "d3617ae56dda793131e743b2ff394984bbab6ca3",
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/commits/d3617ae56dda793131e743b2ff394984bbab6ca3",
      "html_url": "https://github.com/izuzak/pmrpc/commits/d3617ae56dda793131e743b2ff394984bbab6ca3"
    }
  ]
}

3)获取树对象在上一步中获取的提交对象。您将按照上一步骤响应中提供的 tree.url 链接执行此操作:

3) fetch the tree object of the commit object fetched in the previous step. You will do this by following the tree.url link provided in the response of the previous step:

GET https://api.github.com/repos/izuzak/pmrpc/git/trees/f5f5de80f67dd794ffbd4abb855fb7d1a573660e

响应如下:

{
  "sha": "f5f5de80f67dd794ffbd4abb855fb7d1a573660e",
  "url": "https://api.github.com/repos/izuzak/pmrpc/git/trees/f5f5de80f67dd794ffbd4abb855fb7d1a573660e",
  "tree": [
    {
      "mode": "100644",
      "type": "blob",
      "sha": "726f21a4adec8c24c2fab6cf5b455d094a8b21bf",
      "path": "LICENSE.markdown",
      "size": 568,
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/blobs/726f21a4adec8c24c2fab6cf5b455d094a8b21bf"
    },
    {
      "mode": "100644",
      "type": "blob",
      "sha": "eb94760b81441b34a73d1b085d9f153ae48b0e63",
      "path": "README.markdown",
      "size": 5772,
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/blobs/eb94760b81441b34a73d1b085d9f153ae48b0e63"
    },
    {
      "mode": "040000",
      "type": "tree",
      "sha": "2e72b217b8644ce6874cda03387a7ab2d8eee55e",
      "path": "examples",
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/trees/2e72b217b8644ce6874cda03387a7ab2d8eee55e"
    },
    {
      "mode": "100644",
      "type": "blob",
      "sha": "64b0dbe4981759c0f9640c8e882c97c7324fc798",
      "path": "pmrpc.js",
      "size": 24546,
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/blobs/64b0dbe4981759c0f9640c8e882c97c7324fc798"
    }
  ]
}

这些都是存储库中的所有文件和文件夹。请注意,对于文件夹,您将需要递归地获取文件夹树对象,以获取文件夹中的文件列表。在上面的响应中, examples 是一个文件夹,您可以通过type属性的树值查看。所以,您会在文件夹提供的网址上发送另一个GET请求:

These are all the files and folders in the repository. Notice however that for folders you will need to recursively fetch the folder tree object to get the list of files in the folder. In the response above, the examples is a folder which you can see by the tree value of the type property. So, you would to another GET request on the url provided with the folder:

  GET https://api.github.com/repos/izuzak/pmrpc/git/trees/2e72b217b8644ce6874cda03387a7ab2d8eee55e

另一种方法是获取列表 (所有文件夹中的)所有文件(仅在一个请求中使用 recursive = 1 developer.github.com/v3/git/trees/#get-a-tree-recursively\">在这里。我建议你使用这种方法,因为它只需要一个HTTP请求。

An alternative approach is to get the list of all files (in all folders) with just one request, using the recursive=1 parameter, as described here. I suggest you use this approach since it requires just a single HTTP request.

接下来,现在你有repo文件和文件夹的列表,你将获取更改每个文件/文件夹的最后一次提交。要这样做,请提出此请求

Next, now that you have the list of files and folders in the repo, you will get the last commit that changed each of the files/folders. To do that, make this request

GET https://api.github.com/repos/:user/:repo/commits?path=FILE_OR_FOLDER_PATH

例如,这是一个请求, code> examples 上述文件夹:

So, for example, this is a request to fetch the commits for the examples folder mentioned above:

GET https://api.github.com/repos/izuzak/pmrpc/commits?path=examples

的提交对象,你应该看看该列表中的第一个对象(因为你对文件的最后一次提交感兴趣),并检索 commit.message 属性获取您需要的邮件:

The response you will get is a list of commit object, and you should just look at the first object in that list (since you are interested in the last commit for the file) and retrieve the commit.message property to get the message you need:

[
  {
    "sha": "3437f015257683a86e3b973b3279754df9ac2b24",
    "commit": {
      "author": { ... },
      "committer": { ... },
      "message": "change mode",
      "tree": { ... },
      "url": "https://api.github.com/repos/izuzak/pmrpc/git/commits/3437f015257683a86e3b973b3279754df9ac2b24",
      "comment_count": 0
    },
    ...
  },
  {
    ...
  }
]

在这种情况下,更改文件夹的最新提交的消息 examples 是更改模式。

In this case, the message for the latest commit that changed the folder examples is "change mode."

因此,基本上,您需要发出3个HTTP请求以获取文件列表,然后为每个文件和文件夹。坏消息是,如果你有很多文件 - 你会做很多HTTP请求。好消息是,您可以缓存响应,以便在没有更改的情况下不需要发出请求(请参见此处 a>更多信息)。此外,您不会一次抓取所有提交消息,您将在用户浏览文件夹时抓取它们(就像在GitHub上点击文件夹一样)。因此,您应该能够轻松地保持在5000个请求的限制之内。

So, basically, you need to make 3 HTTP requests to fetch the list of files, and then 1 HTTP request for each file and folder. The bad news is that if you have lots of files -- you will be making lots of HTTP requests. The good news is that you can cache responses so that you don't need to make requests if nothing changed (see here for more info). Also, you will not be fetching all the commit messages at once, you will fetch them as the user navigates through the folders (just as on GitHub as you click on folders). Thus you should be able to stay within limits of 5000 requests easily.

希望这有助于!让我知道,如果你找到一个更简单的方法来做这个:)。我不知道如果这是一个方法来实现这只有1-2请求,这可能是你期望的。

Hope this helps! And let me know if you find an easier way to do this :). I don't know if theres a way to achieve this with just 1-2 requests, which is probably what you expected.

这篇关于在GitHub API - 什么是最好的方式来获取与每个文件相关联的最后一个提交消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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