gitgithubgithub-api

How to get last commit date inside a folder/directory in github repo?


For example: I want to get a last commit date in here - https://github.com/elasticsearch/elasticsearch/tree/master/dev-tools/pmd

With this I can get into pmd folder - https://api.github.com/repos/elasticsearch/elasticsearch/contents/dev-tools/pmd

But this doesn't have any data about the dates. I tried https://api.github.com/repos/elasticsearch/elasticsearch/contents/dev-tools/pmd/commits and this returns me 'not found' message.

Tried the git url with sha - https://api.github.com/repos/elasticsearch/elasticsearch/git/blobs/58788337ae94dbeaac72a0901d778a629460c992 but even this doesn't return any helpful info.

How to get the date of the commit inside a folder using github-api?


Solution

  • Using the GitHub API

    Request the commits that touched the subdirectory using GET /repos/:owner/:repo/commits, passing in the path argument that specifies the subdirectory in question:

    path: string, Only commits containing this file path will be returned.

    The response will be zero or more commits. Take the latest and look at its commit/author/date or commit/committer/date, depending on which you're looking for:

    [
      {
        ...,
        "commit": {
          "author": {
            ...,
            "date": "2011-04-14T16:00:49Z"
          },
          "committer": {
            ...,
            "date": "2011-04-14T16:00:49Z"
          },
          ...,
        },
      },
    ]
    

    Using a local copy

    Try git log -n 1 --pretty=format:%cd path/to/directory. This will give you the committer date of the most recent commit in that directory.

    You can also use these other date formats: