How to get the last commit date of a file with ruby/grit?

1.2k views Asked by At

I have a jekyll site, and I want to find the last commit date of a certain post using ruby/grit.

I know that I can do the following using git:

git log -1 --format="%cd" -- <file>

How can I do something equivalent using ruby/grit please?

2

There are 2 answers

3
the Tin Man On

From the File documentation for mtime:

Returns the modification time for the named file as a Time object.

File.mtime("testfile")   #=> Tue Apr 08 12:58:04 CDT 2003

Ruby also supports ctime, which is when the directory information for the file was changed. On Windows ctime is a little different behavior, because Windows supports creation times for files, unlike Linux and Mac OS.

0
Pablo Fernandez heelhook On

You can simply do this:

repo = Grit::Repo.new(...)
repo.log('master', path_of_the_file, max_count: 1)[0].date

Hope that helps!