2

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

  • err:Unexpected HTTP status code: 401

连接到遥控器时在哪里设置凭据?代码有什么问题??谢谢。

(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
4

1 回答 1

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;
}
于 2013-01-14T07:56:35.267 回答