由于没有在不使用libgit2磁盘上文件的情况下创建提交而不使用磁盘上的文件的复制粘贴示例,因此我想我应该添加一个。

不要忘记libgit2目前处于全面开发阶段(2013年3月),因此请查看官方文档和源代码,因为每天都会添加新功能:

  • as far as I can tell
  • header 得到很好的注释-libgit2 API
  • here's an example可能是灵感的来源
  • 有一些extensive testsofficial examples是开始
  • 的好地方
  • 灵感可以在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++ - 如何使用libgit2提交到git仓库?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15711444/

    10-11 16:18