How to push git repository through ssh using libgit2

佐手、 提交于 2019-12-21 20:24:58

问题


Since, the ssh support is already implemented, could anybody show me how to write a push() function? I had some success pushing through the local file system but when I try the ssh I only get a segfault.

int cred_acquire_cb(git_cred** cred,const char* url,unsigned int allowed_types, void* payload){
    check_error(git_cred_ssh_keyfile_passphrase_new(cred,"./bitbucket.pub","./bitbucket",NULL),"keyfile      passphrase");
}

int main(int argc, const char *argv[])
{
    char* repo_path="./hello/.git";
    char* remote_name="origin";

    git_repository* repo;
    git_remote* remote;
    git_push* push;
    bool cred_acquire_called;
    int error;

    check_error(git_repository_open(&repo, repo_path),"open repo");
    check_error(git_remote_load(&remote,repo,remote_name),"load a remote");
    git_remote_set_cred_acquire_cb(remote, (git_cred_acquire_cb)cred_acquire_cb, &cred_acquire_called);
    check_error(git_remote_connect(remote, GIT_DIRECTION_PUSH),"connect remote");
    check_error(git_push_new(&push, remote),"new push object");
    check_error(git_push_add_refspec(push,"refs/heads/master:refs/heads/master"),"add push refspec");

   check_error(git_push_finish(push),"finish pushing");
   check_error(git_push_unpack_ok(push),"unpacked OK? ");


   git_push_free(push);
   git_remote_free(remote);
   git_repository_free(repo);

   return 0;
}

I believe I'm not doing it right but I couldn't find any example to see how it's done.

UPDATE: Ok I figured that I need some authentication to connect, so I ended up with this. No segfault here. I still get an Error on git_remote_connect though : Early EOF. What am I doing wrong ?


回答1:


Your cred_acquire_cb function is not returning anything. That might be causing a problem. It should return 0 on success, or -1 on error.



来源:https://stackoverflow.com/questions/17139196/how-to-push-git-repository-through-ssh-using-libgit2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!