5

假设您在 git 存储库中有一个文件:

  • 一个.txt

应该使用哪些 API 来创建删除该文件的提交?例如在这个问题中,文件被提交而不在磁盘上创建它。现在是否可以在不使用索引(舞台区域)的情况下删除该文件?

我期待一个类似的流程,也许为树构建器创建一个git_tree_entry,但似乎并非如此。git_reference_list()不列出文件,所以有死胡同。搜索删除和删除的来源也没有成功。

4

2 回答 2

4

删除文件类似于添加文件 - 您删除索引条目以暂存删除,然后您可以从索引创建提交。

您可能想要使用git_index_remove_bypath,它将从索引中删除文件解决该文件的任何冲突。

于 2013-03-29T22:59:45.790 回答
0

这是一个复制粘贴示例;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;
}
于 2013-03-30T10:46:20.767 回答