Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using nodegit, how can I find the date a given file was last updated in a git repo?

Tags:

git

nodegit

I'm trying to use nodegit to treat git as a db for content. As such, I'm writing functions to access content in the repo. I'm able to retrieve a file blob and other info about a given file, but I'm struggling to get timestamp information.

I'd like to get 1) the date the file was created, and 2) the date it was last updated. But so far, I haven't figured out how this is possible.

In order to get the file, I need to follow these steps:

1) Retrieve the latest Commit using getMasterCommit.
2) From the Commit, get the file TreeEntry using getEntry.
3) From the TreeEntry, get various metadata and then get the Blob with getBlob.
4) From the Blob, fetch other metadata plus the raw text of the file.

The trouble is that the only place that I can get a date that I've found is from the Commit, which has a date function. This could help get the last updated date, but it's actually not helpful because it just returns the date of the Commmit (obviously!), and yet it's not clear that the file was updated for that commit.

I'd also like to be able to get the date created for a given file. I can imagine that this might be possible to get by searching back through the commit history of a given TreeEntry, but it's not yet clear to me how this could be done. In fact, being able to search through the commit history of a given file might be the thing that's needed here. But I've been unable to see if that's possible.

Can anyone provide guidance here?

like image 581
fraxture Avatar asked Nov 09 '22 05:11

fraxture


1 Answers

There's a rev walker you can use to walk the history, just as if you're doing git log. Take a look at this example: https://github.com/nodegit/nodegit/blob/master/examples/walk-history-for-file.js

With the fileHistoryWalk, you can filter the files to find the file you're interested in, and then get the earliest ref that has it.

like image 147
yelsayed Avatar answered Nov 15 '22 04:11

yelsayed