9

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?

tech_human
  • 6,592
  • 16
  • 65
  • 107

1 Answers1

13

New answer 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"
      },
      ...,
    },
  },
]

Original answer 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
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • Awesome, I tried it on my example and got the output. But any idea why is it showing 2 different dates i.e. in author it shows 2014-03-12 and in committer it's 2014-03-13? – tech_human Sep 18 '14 at 20:55
  • What is the difference between author and committer? – tech_human Sep 18 '14 at 20:56
  • @tech_human, often there is no difference. But the person who wrote the code is the author, and the person who most recently committed it is the committer. See [this answer](http://stackoverflow.com/a/18754896/354577), which references [this chapter](http://git-scm.com/book/ch2-3.html) in Pro Git, for more detail. I'm pretty sure that these can have different dates but the same author if, e.g., the author rebased the commit. – ChrisGPT was on strike Sep 18 '14 at 20:58
  • Basically, for local, this boils down to using the additional flag `-n 1` and entering in the directory path as a position argument after `git log`. i.e. `git log -n 1 `. – ijoseph Jul 07 '20 at 19:53