I want to implement git log -1 fullpath/myfile
with libgit2. I am fairly new to libgit2. Am I on the right track? This is what I have so far:
git_repository_head(&refToHead, repo);
headOID = git_reference_oid(refToHead);
git_commit_lookup(&headCommit, repo, headOID);
headTreeOID = git_commit_tree_oid(headCommit);
git_tree_lookup(&tree, repo, headTreeOID);
git_tree_entry_byname(tree, "repopath/myfile");
Unfortunately git_tree_entry_byname
seems not to work for files in subdirectories of the repo. Any idea?
Thank you, Lars
git_tree_entry_byname
only works against the entries (trees, blobs and, when supported, submodules) which are immediately under the passed tree.One simple solution to your question would be to rely on git_tree_get_subtree (see tests) to retrieve the deepest subtree contained in a tree, given its relative path. This would work no matter how deep the entry is in the tree structure.
Thus, you'd have to call
git_tree_git_subtree
to retrieve the parent tree of the entry you're after, then invokeget_tree_entry_byname
passing it the parent tree.If you're willing to retrieve which commit lastly updated a file, you might want to take a look at this answer which gives some general purpose hints on the file history topic and caveats.
You'll have to leverage the
revision walking
API.Basically, you'd have to find out from the
HEAD
theoid
of the tree entry matching yourfilename
. Once this is done you'd have to recursively walk the parents of theHEAD
commit and, for each commit tree, try and identify one of the two following changes.oid
popped up under the parent tree)Exit from the loop ass soon as you detect one, or when you have no more commit to process.