使用libgit2从git存储库中删除文件
Deleting a file from git repository using libgit2
假设你在git存储库中有一个文件:
- a.txt
应该使用什么api来创建一个删除该文件的提交?例如,在这个问题中,提交文件时没有在磁盘上创建它。现在是否可以在不使用索引(阶段区域)的情况下删除该文件?
我期待一个类似的流程,也许为树构建器创建一个git_tree_entry
,但情况似乎并非如此。git_reference_list()
没有列出文件,所以这是一个死胡同。同样,在源代码中搜索delete和remove也没有成功。
删除文件与添加文件类似——先删除索引项来执行删除操作,然后再从索引创建提交。
您可能想要使用git_index_remove_bypath
,它将从索引和中删除文件,并解决文件的任何冲突。
下面是一个复制粘贴的例子;getLastCommit()
见本题。
bool commitStage (
git_repository * repo, git_signature * sign,
const char * message )
{
bool b = false;
int rc;
git_index * repo_idx; /* the stage area for our repository */
git_oid oid_idx_tree; /* the SHA1 for the tree generated from index */
git_oid oid_commit; /* the SHA1 for our commit */
git_tree * tree_cmt; /* tree generated from index */
git_commit * parent_commit;/* most recent commit in the head */
parent_commit = getLastCommit( repo );
if ( parent_commit != NULL )
{ /* we have the parent commit */
rc = git_repository_index( &repo_idx, repo );
if ( rc == 0 )
{ /* we now have the index (stage area) structure in repo_idx */
git_index_read(repo_idx);
/* the stage area may be altered here with functions like
git_index_add_bypath();
git_index_remove_bypath();
*/
/* by writing the index we get the SHA1 of the tree */
rc = git_index_write_tree( &oid_idx_tree, repo_idx );
if ( rc == 0 )
{
rc = git_tree_lookup(
&tree_cmt, repo, &oid_idx_tree );
if ( rc == 0 )
{ /* the actual tree structure; create a commit from it */
rc = git_commit_create(
&oid_commit, repo, "HEAD",
sign, sign, NULL,
message,
tree_cmt, 1, (const git_commit**)&parent_commit );
if ( rc == 0 )
{
b = true;
}
}
}
git_index_free( repo_idx );
}
git_commit_free( parent_commit );
}
return b;
}
相关文章:
- 如何在输入新内容 c++ 之前删除文件中的所有内容
- 文件模式标志"ios::app"是否用于删除文件(如果文件已存在)?
- 查找并删除文件 C++ 中的整行
- 如何删除文件中的数字,通过 c++ fstream 的
- 以编程方式防止重命名或删除文件,但仍使其可写
- C++ 如何使用_rmdir删除文件?
- 关于从驱动器中删除文件的问题
- Linux 从永远在线的应用程序中创建和删除文件
- 用户使用C++中的删除功能删除文件
- 如何在C++中删除文件中的一部分文本
- 我可以删除 *.o文件(代码:: blocks)
- 为什么std ::删除文件始终返回-1
- C++ 使用数字删除文件 io 行
- 如何使用 c++ 删除文件夹及其内容
- 如何在 C++ 中的文件处理中删除文件
- 用数字删除文件
- Cygwin:打开句柄时删除文件
- 接收文件并删除文件中的重复项
- 删除文件 C++ 中字符串中最后一个出现的字符
- 在循环内删除文件时使提升recursive_directory_iterator无效