-
Notifications
You must be signed in to change notification settings - Fork 323
How to get the latest commit that changed a particular file? #729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
there isn't a built-in API to achieve this currently :( I opened a bug against libgit2 so that we could create one, with a pathway towards making it fast: libgit2/libgit2#5758 one thing that you could potentially do is instead of diffing the trees (since it does a lot of additional work since it has to diff every single file), is to try to grab the |
Thanks so much @lhchavez. Diffing trees was the issue and replacing that with the For any one looking for a way to implement a similar solution, below is the code // allCommits holds all the commits from the repo collected by iterating through all the parents of the HEAD commit
// The same can be achieved using RevWalker (repo.Walk) as well
//
// The approach works for both files and directories
for _, commit := range allCommits {
if commit != nil {
numParents := commit.ParentCount()
if numParents > 0 {
parent := commit.Parent(0)
commitTree, _ := commit.Tree()
parentTree, _ := parent.Tree()
if commitTree != nil && parentTree != nil {
cId, _ := commitTree.EntryByPath(*file)
pId, _ := parentTree.EntryByPath(*file)
if cId != nil && pId != nil {
if cId.Id.String() != pId.Id.String() {
fileEntry = *file
commitMsg = commit.Message()
break
}
}
}
}
}
} |
- based on suggestion in (libgit2/git2go#729)
Hi, I am looking for a way to get the latest commit that affected a particular file / directory. It is something similar to the following git command
I have tried my own way by iterating through all the commits in the repo and comparing it with its immediate ancestor to find the files that were modified in a commit. The sample code is as follows,
I have a function which compares the target file name with the entries stored in the
commitEntries
and this logic works as expected for me. The issue arises when the repo size is large.I tested it with my own repo with around 360 commits and 1100 files, and I get the result in matter of milliseconds. When I tested the same with a larger repo with 20,000 commits and over 5000 objects in the tree, the function takes 3 minutes to return the results.
Is there a built-in API to achieve the expected result or is there any way to optimize my logic to get the expected results faster even for large repos ? Any feedback would be appreciated.
The text was updated successfully, but these errors were encountered: