5

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?

fraxture
  • 5,113
  • 4
  • 43
  • 83

2 Answers2

2

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.

yelsayed
  • 5,236
  • 3
  • 27
  • 38
  • I've looked over that code. Confusing a bit. Why is `fileHistoryWalk` called on the file multiple times, first on line 49 and then again on line 34? Why isn't it enough to treat the initial results of the `fileHistoryWalk` call as the history of the file? – fraxture Dec 04 '16 at 17:29
  • I believe it's doing it in batches of 500, looks like the API requires a max count. I'd experiment with it a little to see if it gets your what you want. – yelsayed Dec 05 '16 at 02:03
-1

You can iterate through each file and see
commit-hash date-time commit-message user file-name. Run following command in your repo using the terminal.

$ git ls-tree -r --name-only HEAD | while read filename; do
 echo "$(git log -1 --format="%h %ad- %s [%an]" -- $filename) $filename"
done     

Sample Output:

6ea69fa Sun Nov 20 04:29:08 2016 +0600- commit-message-1 [user-1] c.txt
f83fad2 Thu Oct 27 00:14:37 2016 +0600- commit-message-10 [user-4] b.txt
b5356e3 Mon Oct 31 14:55:43 2016 +0600- commit-message-7 [user-2] a.txt
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • 1
    Just seeing it. It's interesting, but I'm trying to solve it using nodegit, not the CLI. Could provide hints, though. – fraxture Dec 04 '16 at 16:51