我想在 libgit2 中使用 checkout 来模仿“git checkout”,但它失败了:
- 我已经阅读了api文档并使用了两种方法来设置第二个参数,
- 我阅读了 test_checkout 的示例,将第三个参数设置为
GIT_CHECKOUT_SAFE_CREATE
但它还没有工作。 - .git 文件夹中已经有一个
HEAD
文件。
这个怎么解决???
这是代码的结果:
2013-01-12 18:48:27.789 gitlib3[8661:907] git_repository_open ret:0
2013-01-12 18:48:27.794 gitlib3[8661:907] get repository succeed.
2013-01-12 18:48:27.799 gitlib3[8661:907] count ret:1
2013-01-12 18:48:27.807 gitlib3[8661:907] git_checkout_index. ret:-9
2013-01-12 18:48:27.809 gitlib3[8661:907] err:Reference 'refs/heads/master' not found
和代码本身:
(IBAction)Checkout:(id)sender {
int ret = -1;
git_repository *repo = NULL;
NSArray *str = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [str objectAtIndex:0];
NSString *localPath = [docPath stringByAppendingPathComponent:@"efg/.git"];
ret = git_repository_open(&repo, [localPath UTF8String]);
NSLog(@"git_repository_open ret:%d",ret);
if(repo!=NULL)
{
NSLog(@"get repository succeed.");
}
git_checkout_opts opts = GIT_CHECKOUT_OPTS_INIT;
opts.checkout_strategy = GIT_CHECKOUT_SAFE_CREATE;
/*git_index *index = NULL;
git_repository_index(&index, repo);
if((ret = git_index_entrycount(index)) > 0)
{
NSLog(@"count ret:%d", ret);
}
ret = git_checkout_index(repo, index, &opts);*/
ret = git_checkout_index(repo, NULL, &opts);
NSLog(@"git_checkout_index. ret:%d",ret);
const git_error *err = giterr_last();
if(err == NULL)
{
NSLog(@"NULL");
}
else
{
NSLog(@"err:%s", err->message);
}
}