summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/network/clone.c30
1 files changed, 28 insertions, 2 deletions
diff --git a/examples/network/clone.c b/examples/network/clone.c
index 7596523c2..fd82f449b 100644
--- a/examples/network/clone.c
+++ b/examples/network/clone.c
@@ -47,9 +47,25 @@ 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;
+ git_remote *origin = NULL;
git_repository *cloned_repo = NULL;
git_checkout_opts checkout_opts = GIT_CHECKOUT_OPTS_INIT;
const char *url = argv[1];
@@ -65,14 +81,24 @@ int do_clone(git_repository *repo, int argc, char **argv)
}
// Set up options
- memset(&checkout_opts, 0, sizeof(checkout_opts));
checkout_opts.checkout_strategy = GIT_CHECKOUT_SAFE;
checkout_opts.progress_cb = checkout_progress;
memset(&pd, 0, sizeof(pd));
checkout_opts.progress_payload = &pd;
+ // 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();
+ if (err) printf("ERROR %d: %s\n", err->klass, err->message);
+ 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, url, path, &checkout_opts, &fetch_progress, &pd);
+ error = git_clone(&cloned_repo, origin, path, &checkout_opts, &fetch_progress, &pd);
+ git_remote_free(origin);
printf("\n");
if (error != 0) {
const git_error *err = giterr_last();