如何使用libgit2提交到git存储库
How to commit to a git repository using libgit2?
因为没有复制粘贴的例子来创建一个提交而不使用libgit2磁盘上的文件,据我所知,我认为我应该添加一个。
不要忘记libgit2目前(2013年3月)还处于完全开发阶段,所以请查看官方文档和源代码,因为每天都有新功能添加:
- <
- libgit2 API/gh>
- 标题注释得很好——下面是一个例子
- 广泛的测试可能是灵感的来源
- 有一些官方的例子,general.c是一个很好的起点
- 灵感可能来自LibGit2Sharp -这里有一些测试
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;
}
存储库来自git_repository_init()
或git_repository_open()
。签名来自git_signature_now()
或git_signature_new()
函数更新当前分支的HEAD。
如果在函数执行后执行git status
,您将注意到文件name-of-the-file.txt
显示为被删除。这是因为该函数并没有创建一个实际的文件,只是在git数据库中创建了一个条目。
还要注意git_commit_create()
的最后一个参数。0和NULL表示这是第一次(根)提交。对于所有其他的,至少应该指定一个父提交,可以使用git_commit_lookup()
获得。
我只是在学习这些东西。
相关文章:
- 将字符串存储在c++中的稳定内存中
- std::原子加载和存储都需要吗
- 自动完成在git存储库中的Visual Studio项目中不起作用
- git 子模块的替代方案,用于使用多个存储库的项目
- 使用 libgit2 从另一个分支创建新的 git 存储库?
- 我被困在尝试克隆需要 cmake 文件和依赖项的 git 存储库中
- Visual Studio 2017 C++:克隆的 git 存储库中缺少包含的内容
- 使用 Git init、fetch 和 checkout 克隆 Git 存储库
- git 存储库中的多个引用
- 为 git 存储库制作 c 文件
- 使用 iOS 或 c/c++ 访问 SVN 或远程 Git 存储库等
- 如何在多个 GIT 存储库中组织大型项目
- 如何将更改后的行与C代码的git存储库中的函数关联起来
- Git 1.56 boost存储库中缺少boost::posix_time::to_time_t
- 如何在编译时确定git存储库的状态
- Visual Studio 在从 GIT 存储库拉取文件时不会通知我文件更改(使用 samba)
- 如何有效地为一个有很多依赖的c++产品使用git存储库/子模块
- 如何使用libgit2从git存储库中的HEAD获取最后一次提交
- 使用libgit2从git存储库中删除文件
- 如何使用libgit2提交到git存储库