Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the history of a file?

Tags:

I got another libgit2 issue and will be very grateful for your help.

I'm trying to retrieve file history, i.e. list of commits where this file was changed. And it seems to be quite unconventional... As far as I can see, there are no function for that.

The only approach I can come up with is to use revision walking API to iterate through revisions, check the tree object attached to commit and search for given file there, if found, add commit to my list, otherwise proceed to next commit.

But it looks none-optimal for me...

May be is there any other approach, for example, look directly into .git folder and get needed information there?

Many thanks in advance!

like image 208
shytikov Avatar asked Nov 23 '11 09:11

shytikov


People also ask

How do I find my file history in Windows 10?

To access it, right-click on the Start button, click Run, and then enter the word "Control" at the run prompt. Once the Control Panel opens, click on System and Security, followed by File History. You can see what the File History screen looks like in Figure 1.

How do I find the history of a folder?

Right-click a file or folder in the project and click Show History. In the Change Explorer view, open a change set, right-click a file or folder in the change set, and select Show History.


2 Answers

But it looks none-optimal for me...

Your approach is the correct one. Beware that you'll have to fight against:

  • Plain renaming (same object Hash, different tree entry name)
  • Renaming and content updation occuring in the same commit (Different hash, different tree entry name. Would require file content analysis and comparison feature which is not available in libgit2)
  • Multiple parents history (two branches which have been merged and into which the same file has been modified in a different way)

May be is there any other approach, for example, look directly into .git folder and get needed information there?

Even though understanding the .git folder layout is always a well-spent time, I'm afraid this won't help you with this specific file history issue.

Note: this question is very close from this libgit2sharp issue: How to get the last commit that affected a given file?

Update

Pull request #963 adds this very feature.

It's available since LibGit2Sharp.0.22.0-pre20150415174523 pre-release NuGet package.

like image 78
nulltoken Avatar answered Oct 27 '22 12:10

nulltoken


This is mainly followed in issues/495 of libgit2.
Even though it is implemented in libgit2sharp (PR 963, for milestone 22), it is still "up for grabs" in libgit2 itself.

The issue is documented in issues/3041: Provide log functionality wrapping the revwalk.
The approach mentioned in the question was used in this libgit2sharp example and can be adapted to C using libgit2. It remains the current workaround, pending the resolution of 3041.

like image 21
VonC Avatar answered Oct 27 '22 11:10

VonC