24

I'm able to get the file contents (and if it's a folder, I'm able to get the list of files) by using the GitHub v3 API. Example:

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

But how can I know when the file was last updated? Is there an API for that?

Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79

5 Answers5

34

If you know the exact file path, you can use list commits on repository API specifying a path which only includes commits with this specific file path and then extract the most recent commit (the most recent is the first one) :

Using Rest API v3

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

Using & :

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'

Using 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

Using & :

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'
Bertrand Martel
  • 42,756
  • 16
  • 135
  • 159
11

Using Python

pip install 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)

Output:

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

https://github.com/PyGithub/PyGithub

Community
  • 1
  • 1
Paul Menzies
  • 161
  • 1
  • 4
  • There also appears to be a `last_modified` field on the contents objects, eg, `contents = repo.get_contents('data/population.csv')` `print(contents.last_modified)` And though it appears the same date, it is also a string :-( – AntonOfTheWoods Mar 11 '22 at 02:34
3

That would be surprising, considering git does not store file timestamps (and other metadata like permissions and ownership), for reasons I detailed here.

So that information is not present on the remote repository side (here GitHub) either.

Sergei Rodionov
  • 4,079
  • 6
  • 27
  • 44
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

You can actually determine what you want using the request you specifically mentioned.

Note that all dates/times below are under GMT (of course).

Copy & paste the following command to find the last modified date and time of Folder/File in repository ForStackExchange by user YenForYang:

\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}" \

(If Perl regex isn't available, you can ... | grep -F -m1 "Last-Modified:")


The above command should return (GMT): Thu, 27 Dec 2018 11:01:26 (or later, if I update the file for some reason)


Note that if the ref parameter is unspecified, ref=master.


And if you can't copy and paste, and don't care about the API rate limits, you might opt for the shorter:

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

And if ya don't have grep on Windows just use find "Last-Modified: " instead (doubles quotes are necessary).

And if you don't have curl on Windows (download it...or) use Powershell

(iwr -me HEAD -usebasic "https://api.github.com/repos/yenforyang/forstackexchange/contents/Folder/File?ref=branch").Headers."Last-Modified"
YenForYang
  • 2,998
  • 25
  • 22
  • This is a great advice. One caveat though: this request results in time of latest commit for ENTIRE folder / repo, not for individual file specified. I failed to find any docs explaining reasons for that. – wass rubleff Aug 17 '22 at 23:01
-1

When you run a public repository with GitHub Pages and want to let your visitors know the history of a page, you can simply put the hyperlink to the https://github.com/<user>/<user>.github.io/commits/main/<path-to-file> for that page on that page.

apio
  • 154
  • 11