Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get when the file was last updated from a Github repository

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?

like image 460
Nicke Manarin Avatar asked May 05 '18 21:05

Nicke Manarin


People also ask

How can I tell when a git file was last updated?

If we use git log -p -- b , we can see all the changes on b without knowing the commit hash. If you want to see only the last change, you can use git log -p -1 -- b .

How do I track a file on GitHub?

When you start a new repository, you typically want to add all existing files so that your changes will all be tracked from that point forward. So, the first command you'll typically type is "git add ." (the "." means, this directory. So, it will add everything in this directory.) I'll type "git add ." and press Enter.

Can GitHub track downloads?

In Github, is there a way I can see the number of downloads for a repo? Clone counts are available to authorized users by scraping with a Github username/password as are counts of downloads of asset files within releases.


3 Answers

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 & jq :

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 & jq :

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'
like image 151
Bertrand Martel Avatar answered Oct 15 '22 05:10

Bertrand Martel


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

like image 26
Paul Menzies Avatar answered Oct 15 '22 04:10

Paul Menzies


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.

like image 31
VonC Avatar answered Oct 15 '22 05:10

VonC