Why does git_treebuilder_insert fail for invalid objects?

依然范特西╮ 提交于 2021-01-28 07:56:55

问题


The documention for git_treebuilder_insert seems to imply that it doesn't care whether the object being inserted is valid:

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.

However, when actually using the library to create tree objects, if I try to write an entry with an invalid oid, this function returns failure. As reference, here's the code:

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);

What is the intended behavior, the code or the documentation?


回答1:


The documentation is outdated; the code behaves as expected. The change to validate object pointers was made in order to:

  • Allow library users to make assumptions about the safety of their projects. It is generally an error to create something that dangles and points to an object that does not exist.

  • Improve consistency between creating references and creating objects. Now they both validate that the thing(s) they're pointing to exist, by default.

If you do not want this behavior, you can disable it, by calling:

git_libgit2_opts(GIT_OPT_ENABLE_STRICT_OBJECT_CREATION, 0);


来源:https://stackoverflow.com/questions/48037132/why-does-git-treebuilder-insert-fail-for-invalid-objects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!