2012-05-17 8 views
8

(libgit2 साथ) एक रेपोएक Git रेपो (गहराई में) क्लोन

मैं वास्तव में क्या git clone करता है लेकिन libgit2 साथ क्या करना चाहते क्लोन करने के लिए कैसे। मैं क्या पूछ सकता हूं git clone वास्तव में गहराई से करता है।

  1. एक रेपो
  2. प्रारंभ जोड़ने के लिए दूरदराज के
  3. एक packfile एक git_remote
  4. बनाएं डाउनलोड
  5. सूचकांक कॉन्फ़िग फ़ाइल समायोजित करें:

    यह मैं अब तक क्या कर रहा है पैकफाइल और इंडेक्स लिखें (हमें एक .idx फ़ाइल देता है)

  6. (संपादित करें) डिस्क पर सभी अलग-अलग शाखाएं लिखें।
  7. (संपादित करें) git checkout किसी भी तरह से करें।

और अब मुझे नहीं पता कि क्या करना है। मेरा एकमात्र अनुमान .idx को git_index पर लोड करना होगा और git_repository_set_index का उपयोग करना होगा, लेकिन इससे कोई भी फाइल नहीं दिखाई देगी।

संपादित

मैं आधा क्लोन रेपो पर git checkout master चल परीक्षण किया, और कहा कि काम किया है। अब मुझे यह पता लगाने की जरूरत है कि libgit2 के साथ इसे कैसे किया जाए, और ऐसा लगता है कि समस्या ट्रैकर में कुछ उपयोगी जानकारी है।

संपादित 2

मैं अब उम्मीद है कि किसी को किसी दिन कि त्वरित-प्रारंभ कोड मैंने कभी नहीं पाया जा करने के लिए यह उपयोगी मिल जाएगा उम्मीद में, में, मेरे वर्तमान कोड जोड़ देगा। नोट: मैं यहां Obj-C और Objective-Git का उपयोग कर रहा हूं, लेकिन यह मुख्य रूप से सादा सी है।

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

} 

// किसी तरह Git चेकआउट मास्टर यहाँ

return [GTRepository repositoryWithURL:localURL error:nil]; 

} 
+0

दिलचस्प प्रतिक्रिया। इसे किसी भी तरह 'gitlib2' lib में एकीकृत किया जाना चाहिए। – VonC

उत्तर

4

कर के बाद से libgit2 अपने issue list, एक नेतृत्व का पालन करने में explicitly mention git clone नहीं है के साथ मूल Git परियोजना के स्रोतों में है:

वह अंतिम स्क्रिप्ट आपको git clone के सभी चरणों के माध्यम से मार्गदर्शन करेगी।

संबंधित मुद्दे