2017-02-23 65 views
1

我試圖做一個非常簡單的git克隆與libgit2在網絡上使用SSH - 獲取過程中的標題中的錯誤。我不知道我在做什麼錯 - 這不是網絡問題,因爲我可以通過命令行克隆回購。鍵也在指定的路徑中。Libgit2 - 無法驗證SSH會話:無法打開公鑰文件

此外,密鑰已經設置,如果我想ssh到機器我想克隆我只需要提供一個密碼,所以不知道爲什麼我需要重新定義它在這裏。

#include <git2.h> 
#include <iostream> 

// Callback function 
int cred_cb(git_cred **out, const char *url, const char *username_from_url, 
    unsigned int allowed_types, void *payload) { 

    // https://libgit2.github.com/libgit2/#HEAD/group/cred/git_cred_ssh_key_new 
    return git_cred_ssh_key_new(out, "username", 
     "~/.ssh/id_rsa.pub", "~/.ssh/id_rsa", "password"); 
} 

int main() { 
    // Start up libgit2 
    git_libgit2_init(); 

    // Test clone setup 
    git_repository *repo = NULL; 
    const char *url = "ssh://[email protected]/home/git_repo_to_clone"; 
    const char *path = "tmp"; 

    // Test clone 
    git_clone_options clone_opts = GIT_CLONE_OPTIONS_INIT; 
    clone_opts.fetch_opts.callbacks.credentials = cred_cb; 
    int error = git_clone(&repo, url, path, &clone_opts); 

    // Prints the last error message 
    std::cout << giterr_last()->message << std::endl; 

    // Clean up 
    git_libgit2_shutdown(); 

    return 0; 
} 

印刷allowed_typescred_cb22,所以如果你試圖返回不同類型的git_cred(例如,git_cred_userpass_plaintext)圖書館抱怨callback returned unsupported credentials type,也沒有指定的回調(通過調用git_cloneNULL作爲第三個參數),那麼它就表示authentication required but no callback set。我可能會漏掉一些明顯的東西,我會很感激任何幫助,謝謝。

編輯

我不是與git_cred_ssh_key_from_agent(out, "username")嘗試,它似乎做了什麼(git的文件夾被克隆),儘管丟失的文件。雖然現在問題是它變成了cred_cbgit_clone之間的一個無限循環(似乎是來回調用)。

回答

1

密鑰文件的路徑應該是沒有〜的完整路徑。 Libgit2不會像shell那樣擴展。

此外,密碼應該是用於加密您的私鑰的密碼,而不是您用來驗證遠程密碼的密碼。