更新时间:2022-11-30 23:33:03
由于libgit2不会在其 https://github.com/libgit2/libgit2/issuesrel =nofollow>问题列表,其中一个关键是在原始git项目的源代码中:
最后一个脚本将指导您完成 git clone
的所有阶段。
How to clone a repo (with libgit2)
I want to do exactly what git clone
does but with libgit2
. What I may be asking is what git clone
really does in depth.
This is what I'm doing so far:
git_remote
git checkout
in some way.And now I have no idea what to do. My only guess would be to load the .idx
to a git_index
and use git_repository_set_index
, but that didn't show any files either.
Edit
I tested running git checkout master
on the half-cloned repo, and that did the job. Now I just need to find out how to do it with libgit2, and it seems like there is some useful information in the issue tracker.
Edit 2
I will now add my current code, in hope that someone someday will find it useful, in hope to be that quick-start code I never found. Note: I'm using Obj-C
and Objective-Git
here, but it's mainly plain c.
+ (GTRepository *)cloneFromRemoteURL:(NSURL *)remoteURL toLocalURL:(NSURL *)localURL
{
// Let's 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's hash (which should become it's 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's important… It does make the branches appear in .git/refs/remotes/origin
}
//Somehow do git checkout master here
return [GTRepository repositoryWithURL:localURL error:nil];
}
Since the libgit2 doesn't explicitly mention git clone in its issue list, one lead to follow is in the sources of the original git project with:
That last script will guide you through all the stages of a git clone
.