Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将提交推送到GitHub_C_Libgit2 - Fatal编程技术网

C 将提交推送到GitHub

C 将提交推送到GitHub,c,libgit2,C,Libgit2,我尝试将使用libgit2创建的提交推送到github已经有一段时间了,我已经设法克服了我所面临的很多问题,但“最终”是实际推送提交,因为目前它没有错误,但实际上似乎也不起作用。因此,我的代码类似于(为了简洁起见,我省略了所有错误检查): 。。。 git_commit_create(…);//创建提交 ... git_remote_lookup(&remote,_repo,“origin”); git_remote_init_回调(cbs,git_remote_回调版本); cbs.creden

我尝试将使用libgit2创建的提交推送到github已经有一段时间了,我已经设法克服了我所面临的很多问题,但“最终”是实际推送提交,因为目前它没有错误,但实际上似乎也不起作用。因此,我的代码类似于(为了简洁起见,我省略了所有错误检查):

。。。
git_commit_create(…);//创建提交
...
git_remote_lookup(&remote,_repo,“origin”);
git_remote_init_回调(cbs,git_remote_回调版本);
cbs.credentials=my\u git\u cred\u cb;
git_remote_connect(远程、git_方向、推送和cbs、NULL、NULL);
git_remote_add_push(_repo,“origin”,“refs/heads/master:refs/heads/master”);
git\u push\u options\u init(&options,git\u push\u options\u版本);
options.callbacks=cbs;
git_远程上传(远程、空和选项);
git_远程_断开(远程);
git_远程免费(远程);

所有这些函数都通过了,并且没有返回任何错误,但是提交没有被推送。我的猜测是我的refspec(
refs/heads/master:refs/heads/master
)是不正确的,我试过很多其他人,但我只是在兜圈子。我是否在这里做了一些完全错误的事情,是否有其他人使用libgit2将提交完全推送到github存储库?如有任何帮助/建议,将不胜感激

多亏了Jason Haslam的评论,我终于找到了答案,因为libgit2的完整示例看起来有点单薄,所以我发布了我自己的解决方案作为答案,它可能会帮助其他人。为了简洁起见,我省略了错误和明显的变量创建

//打开reop
git_存储库_open_(&repo,“路径/to/repo”);
//添加提交
git_立即签名(&sig,“姓名”,“电子邮件”);
git_存储库_索引(&index,repo);
git_index_add_bypath(索引,“some_test_file.txt”);
git_索引_写入_树(&tree_oid,index);
git_tree_查找(&tree、repo和tree_oid);
git_参考_名称_至_id(&parent_oid,repo,“HEAD”);
git\u提交\u查找(&父项、回购和父项\u oid);
git_commit_create(&oid_commit,repo,“HEAD”,sig,sig,null,“commit message”,tree,1,&parent);
// ... 释放所有创建的对象
//推送到github
git_远程查找(&remote,repo,“origin”);
git_remote_init_回调(cbs,git_remote_回调版本);
cbs.credentials=&my\u git\u cred\u cb;
git\u push\u options\u init(&options,git\u push\u options\u版本);
options.callbacks=cbs;
char*refspec_字符串[]={
“主管:主管/主管/主管”,
};
git_strarray参考规范数组={
参考规范\u字符串,
1.
};
git_remote_push(远程、&refspec_阵列和选项);
git_远程上传(远程、空和选项);
git_存储库_状态_清理(repo);//不确定是否需要这样做
char*pathspec_字符串[]={
“头”,
};
git_strarray路径规范_数组={
pathspec_字符串,
1.
};
git_reset_默认值(repo、null和pathspec_数组);//需要重置以停止显示任何内容的“git状态”-不确定为什么会这样
git_远程_断开(远程);
// ... 释放所有创建的对象
creditials回调非常简单:

int my\u git\u cred\u cb(git\u cred**cred,const char*url,const char*username\u from\u url,未签名的int-allowed\u类型,void*payload){
if((允许的\u类型和GIT\u CREDTYPE\u USERPASS\u明文)!=0){
返回git_cred_userpass_明文_new(cred,“gh_user”,“gh_pass”);
}否则{
/*请求了某种其他类型的身份验证*/
返回-1;
}
返回1;/*无法提供身份验证数据*/
}

Github是否有您的提交?你能根据它的SHA来拉它吗?GitHub根本没有任何提交,就好像它们根本没有被推送(但没有给出错误)。Git状态显示:尝试查看一些使用libgit2库的工作代码示例,看看您是否可以从中获得一些见解。您是否有理由不使用
Git\u remote\u push
而不是连接、上载等。?你的回调创建了什么样的凭证?我当然没有理由使用push,除了我可能认为它是“正确”的路径,并沿着这条路径走。我想(假设)我必须在远程设备上创建一个推送,然后上传那组推送?老实说,我根本不确定正确的流程应该是什么。回调只返回我的GH user/pass,非常确定该部分没有问题,因为没有401。