为什么 git_treebuilder_insert 会因无效对象而失败?

Why does git_treebuilder_insert fail for invalid objects?

documention for git_treebuilder_insert似乎暗示它不关心插入的对象是否有效:

No attempt is being made to ensure that the provided oid points to an existing git object in the object database, nor that the attributes make sense regarding the type of the pointed at object.

然而,当实际使用库创建树对象时,如果我尝试使用无效的 oid 写入条目,此函数 returns 失败。作为参考,这里是代码:

if (filemode != GIT_FILEMODE_COMMIT &&
    !git_object__is_valid(bld->repo, id, otype_from_mode(filemode)))
    return tree_error("Failed to insert entry; invalid object specified", filename);

预期的行为、代码或文档是什么?

文档已过时;代码的行为符合预期。 The change to validate object pointers 是为了:

  • 允许图书馆用户对其项目的安全性做出假设。创建悬挂并指向不存在的对象的东西通常是错误的。

  • 提高创建引用和创建对象之间的一致性。现在他们都默认验证他们指向的事物是否存在。

如果您想要这种行为,您可以通过调用来禁用它:

git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0);