Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

General Usages #501

Closed
filharvey opened this Issue Nov 29, 2011 · 3 comments

Comments

Projects
None yet
2 participants

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

Owner

carlosmn commented Nov 29, 2011

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.

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

Owner

carlosmn commented Nov 29, 2011

On Tue, Nov 29, 2011 at 03:21:31PM -0800, Phil Harvey wrote:

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.

What next set of data? You've downloaded the packfile, renamed it and
updated the remote tips. That's all there is. The example code is all
you need to run 'git fetch'.

@carlosmn carlosmn closed this Dec 2, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment