Enable authenticated clones in network sample
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
diff --git a/examples/network/clone.c b/examples/network/clone.c
index f8a5ec4..fd82f44 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -47,6 +47,21 @@ static void checkout_progress(const char *path, size_t cur, size_t tot, void *pa
print_progress(pd);
}
+static int cred_acquire(git_cred **out, const char *url, unsigned int allowed_types, void *payload)
+{
+ char username[128] = {0};
+ char password[128] = {0};
+
+ printf("Username: ");
+ scanf("%s", username);
+
+ /* Yup. Right there on your terminal. Careful where you copy/paste output. */
+ printf("Password: ");
+ scanf("%s", password);
+
+ return git_cred_userpass_plaintext_new(out, username, password);
+}
+
int do_clone(git_repository *repo, int argc, char **argv)
{
progress_data pd;
@@ -71,7 +86,7 @@ int do_clone(git_repository *repo, int argc, char **argv)
memset(&pd, 0, sizeof(pd));
checkout_opts.progress_payload = &pd;
- // Create the origin remote
+ // Create the origin remote, and set up for auth
error = git_remote_new(&origin, NULL, "origin", url, GIT_REMOTE_DEFAULT_FETCH);
if (error != 0) {
const git_error *err = giterr_last();
@@ -79,6 +94,7 @@ int do_clone(git_repository *repo, int argc, char **argv)
else printf("ERROR %d: no detailed info\n", error);
return error;
}
+ git_remote_set_cred_acquire_cb(origin, cred_acquire, NULL);
// Do the clone
error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd);