Commit 2b10a2b0e8f658af52ee806f1ab7e46eca37772f

Ben Straub 2012-12-13T11:47:14

Enable authenticated clones in network sample

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);