只有在远程存储库(libgit2)中创建了分支时,push才有效

时间:2017-05-02 07:29:54

标签: c++ libgit2

我在libgit2中执行了“git push”等效...当我在某个分支上尝试它时,实际上也是在远程存储库中创建的,它运行良好。但是如果我有一些只在本地创建的分支,则不执行推送。

当我在cmd中执行“git push origin test_branch”时,会打印

  • [new branch] test_branch - > test_branch

我希望我的程序在远程存储库中创建这个分支,就像git一样。

有些想法如何做到这一点?

    string path="path/to/repo";
    int result=0;

    git_libgit2_init();
    const char * REPO_PATH = path.c_str();
    git_repository * repo = nullptr;
    git_repository_open(&repo, REPO_PATH);
    git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;

    // get the remote.
    git_remote* remote = NULL;
    git_remote_lookup(&remote, repo, "origin");

    // connect to remote
    callbacks.credentials = cred_acquire_cb;
    git_remote_connect(remote, GIT_DIRECTION_PUSH,&callbacks,NULL,NULL);
    if (1 != git_remote_connected(remote)) cerr << "problem connecting to remote" << endl;

    // add a push refspec
    if (0 != git_remote_add_push(repo,"origin", "refs/heads/test_branch:refs/heads/test_branch")) cerr << "git_remote_add_push() returned error value!" << endl;

    // configure options
    git_push_options options;
    if (0!=git_push_init_options(&options, GIT_PUSH_OPTIONS_VERSION)) cerr << "git_push_init_options() returned error value!" << endl;

    // do the push
    result=git_remote_upload(remote, NULL, &options);

    if (result != 0) cerr << "git_remote_upload() failed" << endl;

    git_remote_disconnect(remote);
    git_remote_free(remote);
    git_repository_free(repo);
    git_libgit2_shutdown();

0 个答案:

没有答案