如何使用libgit2从git中获取?

时间:2013-01-14 05:37:57

标签: libgit2

我读了fetch.c并尝试更新本地存储库中的内容(就像“git fetch”),但是git_remote_connect返回-1。

  • err:Unexpected HTTP status code: 401

连接到遥控器时设置信用的位置?代码有什么问题? THX。

(IBAction)Fetch:(id)sender {
git_remote *remote = NULL;
const git_error *err = NULL;
int ret = -1;
bool invoked = false;

git_repository *repo = NULL;
NSArray *str = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [str objectAtIndex:0];
NSString *localPath = [docPath stringByAppendingPathComponent:@"abc/.git"];
NSLog(@"localPath:%@", localPath);
ret = git_repository_open(&repo, [localPath UTF8String]);
NSLog(@"git_repository_open ret:%d",ret);
err = giterr_last();
if(err == NULL)
{
    NSLog(@"NULL");
}
else
{
    NSLog(@"err:%s", err->message);
}

ret = git_remote_load(&remote, repo, "origin");
NSLog(@"git_remote_load ret:%d", ret);
err = giterr_last();
if(err == NULL)
{
    NSLog(@"No error");
}
else
{
    NSLog(@"err:%s", err->message);
    return;
}
ret = git_remote_load(&remote, repo, "origin");
NSLog(@"git_remote_load ret:%d", ret);
ret = git_remote_connect(remote, GIT_DIRECTION_FETCH);
NSLog(@"git_remote_connect ret:%d", ret);
err = giterr_last();
if(err == NULL)
{
    NSLog(@"No error");
}
else
{
    NSLog(@"err:%s", err->message);
    return;
}
ret = git_remote_download(remote, &transferProgressCallback, &invoked);
NSLog(@"git_remote_download ret:%d", ret);
err = giterr_last();
if(err == NULL)
{
    NSLog(@"No error");
}
else
{
    NSLog(@"err:%s", err->message);
    return;
}
ret = git_remote_update_tips(remote);
NSLog(@"git_remote_update_tips ret:%d", ret);
err = giterr_last();
if(err == NULL)
{
    NSLog(@"No error");
}
else
{
    NSLog(@"err:%s", err->message);
    return;
}
}

这是远程配置

[remote "origin"]
    url = http://remote_path/git/share.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

1 个答案:

答案 0 :(得分:2)

我找到下面的函数来设置远程获取的信用

bool invoked = false;
git_remote_set_cred_acquire_cb(remote, cred_acquire_cb, &invoked);

cred_acquire_cb的代码如下:

static int cred_acquire_cb(git_cred **cred, const char *url, unsigned int allowed_types, void *payload)
{
    char *_remote_user = "user";
    char *_remote_pass = "pass";

    *((bool*)payload) = true;

    if ((GIT_CREDTYPE_USERPASS_PLAINTEXT & allowed_types) == 0 ||
        git_cred_userpass_plaintext_new(cred, _remote_user, _remote_pass) < 0)
        return -1;

    return 0;
}