Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

General Usages #501

Closed
filharvey opened this Issue · 3 comments

2 participants

@filharvey

Is there any basic examples on how to connect to a repo, and do a pull from it using the library.

Actually I have the connecting to the repo working, I'm now trying to figure out how to pull from the repo and create all the files locally.

Sorry for being such a newbie here.

Thanks

phil

@carlosmn
Owner

You can't pull right now, because merging isn't implemented yet (and a 'pull' is a porcelain command, so we probably won't support it directly). You can fetch from a remote and call whatever else to merge.

@filharvey

Thanks, I have it now connecting, downloading the first pack file. How once I have the basic /refs/remotes/ downloads and a FETCH_HEAD file. How do I fetch the next set of data.

My code is below

// open repo
error = git_repository_open (&repo, "../demo/.git");

// if no repo, create config and initial empty repo
if (error != GIT_SUCCESS)
{
git_repository_init (&repo, "../demo", 0);
git_repository_config(&cfg, repo, NULL, NULL);

    git_config_set_int32 (cfg, "core.repositoryformatversion", 0);
    git_config_set_bool (cfg, "core.filemode", 1);
    git_config_set_bool (cfg, "core.bare", 0);
    git_config_set_bool (cfg, "core.logallrefupdates", 1);
    git_config_set_bool (cfg, "core.ignorecase", 1);

    git_config_set_string (cfg, "remote.origin.url", "git://github.com/libgit2/libgit2");
    git_config_set_string (cfg, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*");

    error = git_remote_new(&remote, repo, "git://github.com/libgit2/libgit2", "origin");
    if (error < GIT_SUCCESS)
        goto cleanup;

    git_repository_free(repo);
    Sleep (1000);

    git_repository_open (&repo, "../demo/.git");
}

git_repository_config(&cfg, repo, NULL, NULL);

// connect to repo
error = git_remote_get(&remote, cfg, "origin");
if (error < GIT_SUCCESS)
goto cleanup;

error = git_remote_connect(remote, GIT_DIR_FETCH);
if (error < GIT_SUCCESS)
    goto cleanup;

// get pack file
char *packname = NULL;
error = git_remote_download(&packname, remote);
if (error < GIT_SUCCESS)
return error;

git_indexer *idx = NULL;
git_indexer_stats stats;

// load in / unpack pack file
if (packname != NULL)
{
// Create a new instance indexer
error = git_indexer_new(&idx, packname);
if (error < GIT_SUCCESS)
return error;

    error = git_indexer_run(idx, &stats);
    if (error < GIT_SUCCESS)
        return error;

    error = git_indexer_write(idx);
    if (error < GIT_SUCCESS)
        return error;

    const git_oid   *hash = git_indexer_hash(idx);
    char oid[GIT_OID_HEXSZ + 1];
    memset(oid, 0x0, sizeof(oid));
    git_oid_fmt(oid, hash);
    git_indexer_free(idx);

    error = rename_packfile(packname, oid);
    if (error < GIT_SUCCESS)
        return error;
}

free(packname);

error = git_remote_update_tips(remote);
if (error < GIT_SUCCESS)
    return error;

Again, sorry for if any of these are simple questions.

Thanks

@carlosmn
Owner
@carlosmn carlosmn closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.