tech_human
tech_human

Reputation: 7144

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?

Upvotes: 12

Views: 9574

Answers (1)

Chris
Chris

Reputation: 137182

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:

  • %cD: committer date, RFC2822 style
  • %cr: committer date, relative
  • %ct: committer date, UNIX timestamp
  • %ci: committer date, ISO 8601 format
  • %ad: author date (format respects --date= option)
  • %aD: author date, RFC2822 style
  • %ar: author date, relative
  • %at: author date, UNIX timestamp
  • %ai: author date, ISO 8601 format

Upvotes: 17

Related Questions