" git的日志-1完整路径/文件MyFile"与libgit2 [英] "git log -1 fullpath/myfile" with libgit2

查看:181
本文介绍了" git的日志-1完整路径/文件MyFile"与libgit2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施 git的日志-1完整路径/文件MyFile 与libgit2。我是相当新的libgit2。我在正确的轨道上吗?这是我到目前为止有:

  git_repository_head(安培; refToHead,回购);headOID = git_reference_oid(refToHead);
git_commit_lookup(安培; headCommit,回购,headOID);headTreeOID = git_commit_tree_oid(headCommit);
git_tree_lookup(安培;树,回购,headTreeOID);git_tree_entry_byname(树repopath / MYFILE);

不幸的是 git_tree_entry_byname 似乎没有在回购的子目录中的文件。任何想法?

感谢您,
拉尔斯


解决方案

  

不幸的是git_tree_entry_byname似乎没有对文件的工作
  回购的子目录。


git_tree_entry_byname 只能针对其将立即传递树下的条目(树木,斑点,并且支持时,子模块)。

一个简单的解决你的问题将依靠 git_tree_get_subtree (见的测试)来检索包含在树中最深的树,由于其相对路径。这将工作不管条目有多深树结构中的

因此​​,你必须调用 git_tree_git_subtree 来检索你后,再调用 get_tree_entry_byname 传递给它的父树。


  

我想实施的 git的日志-1完整路径/文件MyFile 与libgit2


如果你愿意检索的最后提交更新的文件,你可能想看看这个回答这给文件的历史主题,并告诫一些通用的提示。


  

我找不到任何线索来帮助你在回答链接提交。


您将不得不利用修订步行 API。


  • 的功能的说明可以发现这里

  • 系统测试展示不同的行走策略也可以为你提供一些帮助

基本上,你必须从 HEAD找出 OID 树条目匹配你的文件名。一旦做到这一点,你不得不走递归 HEAD犯的父母,对于每个提交的树,尝试并确定以下两种变化之一。


  • 检测,如果该文件已被重命名 OID 弹出一个新的文件消失) LI>
  • 检测,如果该文件的内容发生了变化(树条目仍与它的名称存在,但它的OID已经改变)。

从循环屁股退出一旦你发现一个,或者当你有没有更多的承诺处理。

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

解决方案

Unfortunately git_tree_entry_byname seems not to work for files in subdirectories of the repo.

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 invoke get_tree_entry_byname passing it the parent tree.

I want to implement git log -1 fullpath/myfile with libgit2

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.

I can't find any clue to get the commit in your linked answer.

You'll have to leverage the revision walking API.

  • Description of the feature can be found here.
  • A test demonstrating different walking strategies may also provide you with some help

Basically, you'd have to find out from the HEAD the oid of the tree entry matching your filename. Once this is done you'd have to recursively walk the parents of the HEAD commit and, for each commit tree, try and identify one of the two following changes.

  • Detect if the file has been renamed (the tree entry has disappeared from its parent tree, a new file with the same oid popped up under the parent tree)
  • Detect if the content of the file has changed (the tree entry still exists with its name but its oid has changed).

Exit from the loop ass soon as you detect one, or when you have no more commit to process.

这篇关于" git的日志-1完整路径/文件MyFile"与libgit2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆