如何使用 libgit2 创建空提交?

How can I create an empty commit using libgit2?

我一直在查看 libgit2 C API reference,但我看不出如何模仿 git commit --allow-empty 的行为。 libgit2 是否有创建空提交的内置方法?如果不是,git 如何在后台创建一个空提交,我应该如何使用 libgit2 实现相同的行为?

使用与父提交相同的树调用 git_commit_create。即:

// Get parent somehow.
git_commit *parent = ...;

// Use the same tree as the parent.
git_tree *tree;
git_commit_tree(&tree, parent);

// Create the commit.
git_commit_create(..., tree, 1, parent);