如何获取github上特定文件的最后一个commit哈希值?

时间:2019-06-06 13:09:12

标签: github github-api

我正在尝试检索github上特定文件的最后提交哈希。

我想要此文件的最后提交哈希 https://github.com/publicsuffix/list/blob/master/public_suffix_list.dat

以下链接为我提供了所有已修改文件的列表。 https://api.github.com/repos/publicsuffix/list/commits/master

我可以通过检查files对象来查找提交哈希,以查看所需的文件是否属于上次提交。

 "files": [
    {
      "sha": "6a4c772093e1c7694627463ef05a99be98ab355b",
      "filename": "public_suffix_list.dat",
      "status": "modified",
      "additions": 1,
      "deletions": 0,
      "changes": 1,
      "blob_url": "https://github.com/publicsuffix/list/blob/6f03f42a65d006c8ae657f125f14fb8f9d3337f4/public_suffix_list.dat",
      "raw_url": "https://github.com/publicsuffix/list/raw/6f03f42a65d006c8ae657f125f14fb8f9d3337f4/public_suffix_list.dat",
      "contents_url": "https://api.github.com/repos/publicsuffix/list/contents/public_suffix_list.dat?ref=6f03f42a65d006c8ae657f125f14fb8f9d3337f4",
      "patch": "@@ -11793,6 +11793,7 @@ goip.de\n // Submitted by Eduardo Vela <evn@google.com>\n run.app\n a.run.app\n+web.app\n *.0emm.com\n appspot.com\n blogspot.ae"
    }
  ]

1 个答案:

答案 0 :(得分:0)

git log <project_dir>/<file_location>/<file_name>

由于您需要最新的提交,请使用

git log -n 1 --pretty=format:%H -- <project_dir>/<file_location>/<file_name>

编辑: 因为@phd在此答案的评论部分纠正了我。

请参考Github API doc并创建如下网址

https://api.github.com/repos/<github_handle>/<repo_name>/commits?path=>file_path>

https://api.github.com/repos/shravan40/tiny_url/commits?path=README.md