GitTest: Get cloning over ssh to work

Libgit2 is quite horrible. When passing it credentials, if the
credentials are invalid, it will just ask for the credentials again and
again and again, without giving any feedback that the credentials have
failed. This just sucks.
This commit is contained in:
Vishesh Handa
2019-05-15 12:43:24 +02:00
parent 128e14d3df
commit 36c265a72d

View File

@ -1,5 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <git2.h> #include <git2.h>
@ -151,7 +152,7 @@ int gj_git_commit(char *git_base_path, char *author_name, char *author_email, ch
{ {
// FIXME: Better check for this! // FIXME: Better check for this!
// Probably first commit // Probably first commit
git_error_clear(); // git_error_clear();
err = git_commit_create(&commit_id, repo, "HEAD", sig, sig, NULL, message, tree, 0, NULL); err = git_commit_create(&commit_id, repo, "HEAD", sig, sig, NULL, message, tree, 0, NULL);
if (err < 0) if (err < 0)
@ -216,30 +217,46 @@ int credentials_cb(git_cred **out, const char *url, const char *username_from_ur
unsigned int allowed_types, void *payload) unsigned int allowed_types, void *payload)
{ {
printf("UsernameProvided: %s\n", username_from_url); printf("UsernameProvided: %s\n", username_from_url);
printf("Allowed Types: %d\n", allowed_types);
printf("Payload: %p\n", payload);
if (allowed_types != GIT_CREDTYPE_SSH_KEY) if (!(allowed_types & GIT_CREDTYPE_SSH_KEY))
{ {
printf("Some other auth mechanism is being used"); printf("Some other auth mechanism is being used: %d\n", allowed_types);
return -1; return -1;
} }
int err;
char *publickey = "/Users/vishesh/.ssh/id_rsa.pub"; char *publickey = "/Users/vishesh/.ssh/id_rsa.pub";
char *privatekey = "/Users/vishesh/.ssh/id_rsa.pub"; char *privatekey = "/Users/vishesh/.ssh/id_rsa";
char *passphrase = ""; char *passphrase = "";
git_cred *cred = NULL; int err = git_cred_ssh_key_new(out, username_from_url, publickey, privatekey, passphrase);
err = git_cred_ssh_key_new(&cred, username_from_url, publickey, privatekey, passphrase);
if (err < 0) if (err < 0)
{ {
return handle_error(err); return handle_error(err);
} }
*out = cred;
return 0; return 0;
} }
int certificate_check_cb(git_cert *cert, int valid, const char *host, void *payload)
{
printf("Valid: %d\n", valid);
printf("CertType: %d\n", cert->cert_type);
if (valid == 0)
{
printf("%s: Invalid certificate\n", host);
}
if (cert->cert_type == GIT_CERT_HOSTKEY_LIBSSH2)
{
printf("LibSSH2 Key: %p\n", payload);
return 0;
}
return -1;
}
int gj_git_clone(char *clone_url, char *git_base_path) int gj_git_clone(char *clone_url, char *git_base_path)
{ {
int err; int err;
@ -247,9 +264,9 @@ int gj_git_clone(char *clone_url, char *git_base_path)
git_clone_options options = GIT_CLONE_OPTIONS_INIT; git_clone_options options = GIT_CLONE_OPTIONS_INIT;
options.fetch_opts.callbacks.transfer_progress = fetch_progress; options.fetch_opts.callbacks.transfer_progress = fetch_progress;
options.fetch_opts.callbacks.credentials = credentials_cb; options.fetch_opts.callbacks.credentials = credentials_cb;
//options.fetch_opts.callbacks.certificate_check = certificate_check_cb; options.fetch_opts.callbacks.certificate_check = certificate_check_cb;
git_clone(&repo, clone_url, git_base_path, &options); err = git_clone(&repo, clone_url, git_base_path, &options);
if (err < 0) if (err < 0)
{ {
return handle_error(err); return handle_error(err);
@ -269,7 +286,8 @@ int main(int argc, char *argv[])
{ {
char *git_base_path = "/tmp/journal_test"; char *git_base_path = "/tmp/journal_test";
//char *clone_url = "https://github.com/GitJournal/journal_test.git"; //char *clone_url = "https://github.com/GitJournal/journal_test.git";
char *clone_url = "ssh://git@github.com:GitJournal/journal_test.git"; //char *clone_url = "git@github.com:GitJournal/journal_test.git";
char *clone_url = "root@pi.local:git/test";
char *add_pattern = "."; char *add_pattern = ".";
char *author_name = "TestMan"; char *author_name = "TestMan";
char *author_email = "TestMan@example.com"; char *author_email = "TestMan@example.com";
@ -283,6 +301,16 @@ int main(int argc, char *argv[])
gj_git_clone(clone_url, git_base_path); gj_git_clone(clone_url, git_base_path);
printf("We seem to be done\n"); printf("We seem to be done\n");
git_libgit2_shutdown(); git_libgit2_shutdown();
int features = git_libgit2_features();
bool supports_threading = features & GIT_FEATURE_THREADS;
bool supports_https2 = features & GIT_FEATURE_HTTPS;
bool supports_ssh = features & GIT_FEATURE_SSH;
printf("Threading: %d\n", supports_threading);
printf("Https: %d\n", supports_https2);
printf("SSH: %d\n", supports_ssh);
return 0; return 0;
} }