Clone a git repo (in depth)

How to clone repo (with libgit2 )

I want to do exactly what git clone , but with libgit2 . What I can ask is what git clone really does.

This is what I am doing so far:

  • Initialize repo
  • Adjust the configuration file to add a remote
  • Create git_remote
  • Download batch file
  • Index the batch file and write the index (gives us the .idx file)
  • (edit) Write all the different branches to disk.
  • (edit). Do a git checkout in some way.

And now I have no idea what to do. My only suggestion would be to load .idx into git_index and use git_repository_set_index , but also did not display the files.

Edit

I tested running git checkout master on a semi-inclined repo and it did the job. Now I just need to figure out how to do this with libgit2, and it seems that there is some useful information in the problem tracker.

Edit 2

Now I will add my current code, in the hope that someone will someday find it useful, in the hope of being that fast code that I have never found. Note. I use Obj-C and Objective-Git here, but basically it's just c.

 + (GTRepository *)cloneFromRemoteURL:(NSURL *)remoteURL toLocalURL:(NSURL *)localURL { // Let suppose the URL looks like: https://github.com/libgit2/libgit2.git // Then we need to get a URL like this too: git://github.com/libgit2/libgit2.git // This may be a bit dodgy, but it will do for now. const char *gitURL = [remoteURL.absoluteString stringByReplacingOccurrencesOfString:@"https://github.com/" withString:@"git://github.com/"].UTF8String; //Setup int error; git_repository *repo git_config *cfg; git_remote *remote; NSURL *gitDirURL = [localURL URLByAppendingPathComponent:@".git"]; error = git_repository_open(&repo, gitDirURL.path.UTF8String); if (error != GIT_SUCCESS) { git_repository_init(&repo, gitDirURL.path.UTF8String, 1); //Config git_repository_config(&cfg, repo); 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", gitURL); git_config_set_string (cfg, "remote.origin.fetch", "+refs/heads/*:refs/remotes/origin/*"); git_config_set_string (cfg, "branch.master.remote", "origin"); git_config_set_string (cfg, "branch.master.merge", "refs/heads/master"); git_repository_set_workdir(repo, localURL.path.UTF8String); error = git_remote_new(&remote, repo, "A remote", gitURL, "origin"); git_repository_free(repo); git_repository_open (&repo, localURL.path.UTF8String); } git_repository_config(&cfg, repo); // connect to repo error = git_remote_load(&remote, repo, "origin"); error = git_remote_connect(remote, GIT_DIR_FETCH); // get pack file git_off_t bytes; git_indexer_stats stats; error = git_remote_download(remote, &bytes, &stats); NSURL *packFolderURL = [localURL URLByAppendingPathComponent:@".git/objects/pack"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *array = [fileManager contentsOfDirectoryAtURL:packFolderURL includingPropertiesForKeys:nil options:0 error:nil]; NSLog(@"Dictionary:%@",array); NSString *result; for (NSURL *url in array) { if ([url.path rangeOfString:@".pack"].location != NSNotFound) { result = url.path; } } const char *packname = [result UTF8String]; // unpack pack file if (packname != NULL) { git_indexer *indexer; git_indexer_stats stats2; int error; char hash[GIT_OID_HEXSZ + 1] = {0}; error = git_indexer_new(&indexer, packname); error = git_indexer_run(indexer, &stats2); error = git_indexer_write(indexer); // Get the packfile hash (which should become it filename) git_oid_fmt(hash, git_indexer_hash(indexer)); NSString *hashStr = [NSString stringWithCString:hash encoding:NSUTF8StringEncoding]; hashStr = [NSString stringWithFormat:@"pack-%@.idx",hashStr]; const char *indexPath = [hashStr UTF8String]; puts(hash); git_index *index; git_index_open(&index, indexPath); git_index_read(index); git_repository_set_index(repo, index); git_indexer_free(indexer); git_remote_update_tips(remote, update_cb2); //No idea what it does, but it seems like it important… It does make the branches appear in .git/refs/remotes/origin } 

// Somehow make a git check wizard here

 return [GTRepository repositoryWithURL:localURL error:nil]; } 
+8
git c git-clone objective-c
source share
1 answer

Since libgit2 does not explicitly mention git clone in its list of releases , one of which follows it, is in the sources of the original git project with:

The last script will guide you through all the stages of git clone .

+4
source share

All Articles