我正在尝试使用LibGit2,我被困在提交中。我尝试了这一个,它只适用于“初始”提交。对于下一个提交,它失败了-15“当前提示不是第一个父”。
两个问题:
谢谢。
发布于 2014-12-28 02:13:37
我怎么知道我是否需要初次提交?
您可以检查未出生(),以查看头部是否指向引用。
如何执行非初始提交?
您需要提供要提交的索引。这在索引()中是最简单的。但是,索引必须以树的形式提供。名称混乱的树()将为您将索引写到树中。
要获取父文件,可以使用从()从散列获取对象id,或者使用id()从引用名称(如"HEAD“)获取对象id。然后使用查找()从to获取提交对象。
然后将它们传递给创建()或v()。使用git_commit_create_v()可以避免生成git_commit指针的列表,只需将提交作为像printf()这样的参数传递。
有一个ex/general.c中的好例子,它比我更好地解释了所有步骤。我还喜欢使用围绕libgit2的Perl包装器Git::Raw作为示例。看看.xs文件 (基本上是C)。
下面是一个未经测试的将索引提交给HEAD的示例。我想你已经有了作者,提交人和回购。
git_oid tree_id, parent_id, commit_id;
git_tree *tree;
git_commit *parent;
git_index *index;
/* Get the index and write it to a tree */
git_repository_index(&index, repo);
git_index_write_tree(tree, index);
/* Get HEAD as a commit object to use as the parent of the commit */
git_reference_name_to_id(parent_id, repo, "HEAD");
git_commit_lookup(&parent, repo, parent_id);
/* Do the commit */
git_commit_create_v(
commit_id,
repo,
"HEAD", /* The commit will update the position of HEAD */
author,
committer,
NULL, /* UTF-8 encoding */
message,
tree, /* The tree from the index */
1, /* Only one parent */
parent /* No need to make a list with create_v */
);https://stackoverflow.com/questions/27672722
复制相似问题