【问题标题】:Get when the file was last updated from a Github repository获取文件上次从 Github 存储库更新的时间
【发布时间】:2018-10-16 01:57:17
【问题描述】:

我可以通过using the GitHub v3 API. 获取文件内容(如果是文件夹,我可以获取文件列表) 示例:

https://api.github.com/repos/[Owner]/[Repository]/contents/[Folder]

但是我怎么知道文件上次更新的时间?有相应的 API 吗?

【问题讨论】:

    标签: github github-api


    【解决方案1】:

    使用 Python

    点安装 PyGithub

    from github import Github
    g = Github()
    repo = g.get_repo("datasets/population")
    print(repo.name)
    commits = repo.get_commits(path='data/population.csv')
    print(commits.totalCount)
    if commits.totalCount:
        print(commits[0].commit.committer.date)
    

    输出:

    population
    5
    2020-04-14 15:09:26
    

    https://github.com/PyGithub/PyGithub

    【讨论】:

      【解决方案2】:

      您实际上可以使用您特别提到的请求来确定您想要什么。

      请注意,以下所有日期/时间均在 GMT 下(当然)。

      复制并粘贴以下命令以查找用户 YenForYang 在存储库 ForStackExchange 中的文件夹/文件的最后修改日期和时间:

      \curl -sIA. --ignore-content-length  \
           -H"If-Modified-Since: Sun May 01 00:00:00 9999" \
           "https://api.github.com/repos/YenForYang/ForStackExchange/contents/Folder/File?ref=branch" \
      | \grep -m1 -oP "(?<=Last-Modified: )[ADFJMNOSTWa-eghilnoprtuvy0-9:, ]{25}" \
      

      (如果 Perl 正则表达式不可用,您可以... | grep -F -m1 "Last-Modified:"


      上述命令应返回 (GMT):Thu, 27 Dec 2018 11:01:26 (或者以后,如果我出于某种原因更新文件)


      请注意,如果未指定ref 参数,则为ref=master


      如果您不能复制和粘贴,并且不关心 API 速率限制,您可能会选择较短的:

      \curl -sIL "api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch" | \grep "^Las"
      

      如果你在 Windows 上没有 grep,只需使用 find "Last-Modified: " 代替(双引号是必要的)。

      如果您在 Windows 上没有 curl(下载它...或)使用 Powershell

      (iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
      

      【讨论】:

        【解决方案3】:

        考虑到 git 没有 store file timestamps (and other metadata like permissions and ownership),这将是令人惊讶的,原因是 I detailed here

        因此,远程存储库端(此处为 GitHub)上也不存在该信息。

        【讨论】:

        • 是的,但是 GitHub 知道文件上次更新的时间(例如,如果您打开文件,它会显示上次提交)。
        • 这就是为什么我发现时间戳丢失很奇怪的原因。 :)
        • 提交日期与最后修改时间戳无关
        • 哦,那我错了。但我不明白为什么,你能详细说明一下吗?
        • 我在回答中提到的链接确实很详细。另一个答案只是在复制完成后检索
        【解决方案4】:

        如果您知道确切的文件路径,则可以使用list commits on repository API 指定仅包含具有此特定文件路径的提交的path,然后提取最近的提交(最近的是第一个):

        使用 Rest API v3

        https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1

        使用 &

        curl -s "https://api.github.com/repos/bertrandmartel/speed-test-lib/commits?path=jspeedtest%2Fbuild.gradle&page=1&per_page=1" | \
             jq -r '.[0].commit.committer.date'
        

        使用 GraphqQL API v4

        {
          repository(owner: "bertrandmartel", name: "speed-test-lib") {
            ref(qualifiedName: "refs/heads/master") {
              target {
                ... on Commit {
                  history(first: 1, path: "jspeedtest/build.gradle") {
                    edges {
                      node {
                        committedDate
                      }
                    }
                  }
                }
              }
            }
          }
        }
        

        Try it in the explorer

        使用 &

        curl -s -H "Authorization: Bearer YOUR_TOKEN" \
             -H  "Content-Type:application/json" \
             -d '{ 
                  "query": "{ repository(owner: \"bertrandmartel\", name: \"speed-test-lib\") { ref(qualifiedName: \"refs/heads/master\") { target { ... on Commit { history(first: 1, path: \"jspeedtest/build.gradle\") { edges { node { committedDate } } } } } } } }"
                 }' https://api.github.com/graphql | \
             jq -r '.data.repository.ref.target.history.edges[0].node.committedDate'
        

        【讨论】:

        • 我只是写这个,当然,不太详细:)
        • 如果使用 REST API v3,我们可以使用 &amp;page=1&amp;per_page=1 来避免加载所有提交。
        • 也许只有我一个人,但我觉得 GraphQL 很不直观
        猜你喜欢
        • 2021-08-17
        • 2014-04-19
        • 2021-01-28
        • 2015-05-20
        • 2013-04-18
        • 1970-01-01
        • 1970-01-01
        • 2013-10-21
        • 2014-09-21
        相关资源
        最近更新 更多