2

libgit2 ディスク上のファイルを使用せずにコミットを作成するためのコピー アンド ペーストの例がないので、追加する必要があると思いました。

libgit2 は現時点 (2013 年 3 月) で完全に開発中であることを忘れないでください。新しい機能が毎日追加されているため、公式ドキュメントとソース コードを参照してください。

4

1 に答える 1

2
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()


私はこれらのことを学んでいるだけです。よく知っている場合は、この回答を改善してください。

于 2013-03-29T21:09:08.377 に答える