LibGit2提交示例 [英] LibGit2 Commit example

查看:78
本文介绍了LibGit2提交示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LibGit2,但是我陷入了提交状态.我尝试了这一个,它仅适用于初始"提交.对于下一次提交,它失败并显示-15当前提示不是第一父级".

两个问题:

  1. 我怎么知道我是否需要初始提交?
  2. 如何执行非初始提交?

谢谢.

解决方案

我怎么知道我是否需要初次提交?

您可以检查 git_repository_head_unborn()以查看HEAD是否指向是否参考.

如何执行非初始提交?

您需要提供要提交的索引.使用 git_repository_index()最简单的方法.但是,索引必须以树的形式提供.容易混淆的 git_index_write_tree()会将索引写入树中你.

要获取父母,请使用 git_oid_fromstr()从哈希中获取对象ID或 git_reference_name_to_id()到从诸如"HEAD"之类的引用名称中获取它.然后使用 git_commit_lookup()从以下位置获取提交对象oids.

然后将它们传递到 git_commit_create() git_commit_create_v().使用git_commit_create_v()可以避免创建git_commit指针列表,而只需将提交作为参数传入printf().

ex/general.c中有一个很好的例子比我将做的更好,解释所有步骤.我还喜欢使用Git :: Raw(libgit2周围的Perl包装器)作为示例.查看 .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 */
);

I am trying to use LibGit2, and I 'm stuck in commit. I 've tried this one which works only for the "initial" commit. For next commits, it fails with -15 "current tip is not the first parent".

Two questions:

  1. How do I know If i need an initial commit ?
  2. How do I perform a non-initial commit?

Thx.

解决方案

How do I know If i need an initial commit ?

You can check git_repository_head_unborn() to see if HEAD points at a reference or not.

How do I perform a non-initial commit?

You need to supply the index to be committed. This is easiest done with git_repository_index(). However, the index must be supplied as a tree. The confusingly named git_index_write_tree() will write the index to a tree for you.

To get the parent(s), use git_oid_fromstr() to get an object id from a hash or git_reference_name_to_id() to get it from a reference name such as "HEAD". Then use git_commit_lookup() to get the commit object(s) from the oids.

Then pass them into git_commit_create() or git_commit_create_v(). Using git_commit_create_v() avoids having to make a list of git_commit pointers, just pass the commits in as arguments like printf().

There is a good example in ex/general.c which does a better job explaining all the steps than I will. I also like to use Git::Raw, the Perl wrapper around libgit2, as an example. Look at the .xs files (which are basically C).

Here is an untested example of committing the index to HEAD. I'm assuming you already have the author, committer and repo.

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

这篇关于LibGit2提交示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆