如何检索所有对象 ID?
我正在尝试使用 libgit2 获取 git 存储库中所有对象 ID 的列表。我似乎找不到任何方法。 libgit2 是否有获取所有对象 ID(或迭代它们)的方法,或者我需要手动读取它们?
I am trying to get a list of all object IDs in a git repository, using libgit2. I can't seem to find any method for this. Does libgit2 have a method to get all object IDs (or iterate through them), or do I need to read them manually?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在寻找
修订行走
API。编辑: A libgit2 邮件中的线程 list 专门处理这个问题。
Vicent Marti(libgit2 维护者)更准确的答案是
您所要做的就是将每个分支和标记 oid 推送到修订遍历器中,以递归地遍历提交历史记录。请注意,这不会检索悬空提交(未由分支或标签引用的提交或提交链)。
编辑2:此行为(类似于
git log --all
)已成功在 libgit2sharp(libgit2 .Net 绑定)中实现。编辑 3:最近合并了一项新功能,该功能允许枚举存储在对象数据库中的所有对象(提交、树、blob...):<代码>git_odb_foreach()。
这将更符合@MatrixFrog 正在讨论的 git fsck 场景。
git_odb_foreach()
文档What you may be looking for is the
revision walking
API.Edit: A thread in the libgit2 mailing list specifically deals with this.
A more precise answer from Vicent Marti (libgit2 maintainer) is
All you have to do is to push every branch and tag oids into the revision walker to recursively walk the commit history. Please note this won't retrieve dangling commits (commits or chain of commits that are not referenced by a branch nor a tag).
Edit 2: This behavior (similar to
git log --all
) has been successfully implemented in libgit2sharp (libgit2 .Net bindings).Edit 3: A new feature has recently been merged which would allow to enumerate all the objects (commits, trees, blobs, ...) stored in the object database:
git_odb_foreach()
.This would be more in line with the
git fsck
scenario @MatrixFrog was talking about.git_odb_foreach()
documentation