如何使用libgit2承诺一个Git仓库? [英] How to commit to a git repository using libgit2?
问题描述
由于是创建一个承诺,而使用磁盘上的文件与 libgit2 <无复制粘贴示例HREF =https://www.google.ro/search?q=commit%20to%20a%20git%20repository%20using%20libgit2相对=nofollow>据我可以告诉我以为我应该添加一个。
不要忘了libgit2是全面发展在这个时候(2013年3月),所以来看看官方的文档和源$ C $ C,新功能每天都会添加:
- libgit2 API
- 头被很好的注释 - 在这里是一个例如
- 广泛的测试可能是灵感的源泉
- 有一些官方范例,的 general.c 是启动<一个好地方/ li>
这里有<一个 - - 灵感可能在 LibGit2Sharp 找到href=\"https://github.com/libgit2/libgit2sharp/blob/vNext/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs\"相对=nofollow>一些测试
布尔addGitCommit(
git_repository *回购,git_signature *标志,
为const char *的内容,诠释content_sz,
为const char *消息)
{
INT RC; / *返回code为git_功能* /
git_oid oid_blob; / *在树中的SHA1我们斑点* /
git_oid oid_tree; / *在提交的SHA1我们的树* /
git_oid oid_commit; / *对我们最初的SHA1提交* /
git_blob *斑点; / *我们在树中的BLOB * /
git_tree * tree_cmt; / *我们在提交树* /
git_treebuilder * tree_bld; / *树构建* /
布尔B = FALSE; / *从我们的缓冲区*创建BLOB /
RC = git_blob_create_frombuffer(
&安培; oid_blob,
回购,
内容,
content_sz);
如果(RC == 0){/ *一滴创建的* /
RC = git_blob_lookup(安培; BLOB,回购,和放大器; oid_blob);
如果(RC == 0){/ *斑点创建并发现* /
RC = git_treebuilder_create(安培; tree_bld,NULL);
如果(RC == 0){/ *新树构建器创建的* /
RC = git_treebuilder_insert(
空值,
tree_bld,
名称的最file.txt的
&安培; oid_blob,
GIT_FILEMODE_BLOB);
如果(RC == 0){/ *一滴插在树* /
RC = git_treebuilder_write(
&安培; oid_tree,
回购,
tree_bld);
如果(RC == 0){/ *树被写入到数据库* /
RC = git_tree_lookup(
&安培; tree_cmt,回购,和放大器; oid_tree);
如果(RC == 0){/ *我们已经得到了树的指针* /
RC = git_commit_create(
&安培; oid_commit,回购,HEAD,
标志,标志,/ *同一作者和commiter * /
NULL,/ *默认的UTF-8编码* /
信息,
tree_cmt,0,NULL);
如果(RC == 0){
B =真实的;
}
git_tree_free(tree_cmt);
}
}
}
git_treebuilder_free(tree_bld);
}
git_blob_free(BLOB);
}
}
返回b;
}
存储库来源于 git_repository_init()
或 git_repository_open()
。
签名来自 git_signature_now()
或 git_signature_new()
。
功能更新为HEAD当前分支。
如果你做了 git的状态函数执行
之后,你会发现文件名称的最file.txt的
显示为被删除。这是因为该功能不会创建一个实际的文件,只在GIT数据库的条目。
另外,还要注意 git_commit_create的最后一个参数()
。 0和NULL表示这是第一次(根)提交。对于所有其他应至少提交父母指定的,也许用得到 git_commit_lookup()
。
我只是学习这些东西。如果你知道更好的改善,请这个答案。
Since there is no copy-paste example for creating a commit without using files on disk with libgit2 as far as I can tell I thought I should add one.
Don't forget that libgit2 is in full development at this time (March 2013) so have a look at official documentation and source code, as new features are added daily:
- libgit2 API
- headers are very well commented - here's an example
- extensive tests may be a source of inspiration
- there are some official examples, general.c is a good place to start
- inspiration may be found in LibGit2Sharp - here are some tests
bool addGitCommit (
git_repository * repo, git_signature * sign,
const char * content, int content_sz,
const char * message )
{
int rc; /* return code for git_ functions */
git_oid oid_blob; /* the SHA1 for our blob in the tree */
git_oid oid_tree; /* the SHA1 for our tree in the commit */
git_oid oid_commit; /* the SHA1 for our initial commit */
git_blob * blob; /* our blob in the tree */
git_tree * tree_cmt; /* our tree in the commit */
git_treebuilder * tree_bld; /* tree builder */
bool b = false;
/* create a blob from our buffer */
rc = git_blob_create_frombuffer(
&oid_blob,
repo,
content,
content_sz );
if ( rc == 0 ) { /* blob created */
rc = git_blob_lookup( &blob, repo, &oid_blob );
if ( rc == 0 ) { /* blob created and found */
rc = git_treebuilder_create( &tree_bld, NULL );
if ( rc == 0 ) { /* a new tree builder created */
rc = git_treebuilder_insert(
NULL,
tree_bld,
"name-of-the-file.txt",
&oid_blob,
GIT_FILEMODE_BLOB );
if ( rc == 0 ) { /* blob inserted in tree */
rc = git_treebuilder_write(
&oid_tree,
repo,
tree_bld );
if ( rc == 0 ) { /* the tree was written to the database */
rc = git_tree_lookup(
&tree_cmt, repo, &oid_tree );
if ( rc == 0 ) { /*we've got the tree pointer */
rc = git_commit_create(
&oid_commit, repo, "HEAD",
sign, sign, /* same author and commiter */
NULL, /* default UTF-8 encoding */
message,
tree_cmt, 0, NULL );
if ( rc == 0 ) {
b = true;
}
git_tree_free( tree_cmt );
}
}
}
git_treebuilder_free( tree_bld );
}
git_blob_free( blob );
}
}
return b;
}
The repository comes from git_repository_init()
or git_repository_open()
.
The signature comes from git_signature_now()
or git_signature_new()
.
The function updates the HEAD for current branch.
If you do a git status
after the function executes you will notice that the file name-of-the-file.txt
appears as being deleted. That is because the function does not create an actual file, only an entry in the git database.
Also, note the last arguments of git_commit_create()
. 0 and NULL means that this is the first (root) commit. For all other there should be at least a parent commit specified, maybe obtained using git_commit_lookup()
.
I'm just learning these things. Please improve this answer if you know better.
这篇关于如何使用libgit2承诺一个Git仓库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!