]> git.proxmox.com Git - libgit2.git/commitdiff
Adding credentials callback to ls-remote and fetch too.
authorKrzysztof Adamski <k@japko.eu>
Sun, 1 Sep 2013 16:35:39 +0000 (18:35 +0200)
committerKrzysztof Adamski <k@japko.eu>
Sun, 1 Sep 2013 17:00:16 +0000 (19:00 +0200)
examples/network/Makefile
examples/network/clone.c
examples/network/common.c [new file with mode: 0644]
examples/network/common.h
examples/network/fetch.c
examples/network/ls-remote.c

index 810eb705b89d0202a36d91b9521c0dad23c17bc3..f65c6cb26d6b6d1350ef89200b3fd6b53edbad25 100644 (file)
@@ -11,7 +11,8 @@ OBJECTS = \
   ls-remote.o \
   fetch.o \
   clone.o \
-  index-pack.o
+  index-pack.o \
+  common.o
 
 all: $(OBJECTS)
        $(CC) $(CFLAGS) $(LDFLAGS) -o git2 $(OBJECTS) $(LIBRARIES)
index 00c25c1ae89d21b7cdbe7246db3b97d3fd48d543..a09a947288a6ecd7348e52872d0aafbeb2ace830 100644 (file)
@@ -9,19 +9,6 @@
 # include <unistd.h>
 #endif
 
-/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
- * with permission of the original author, Martin Pool.
- * http://sourcefrog.net/weblog/software/languages/C/unused.html
- */
-#ifdef UNUSED
-#elif defined(__GNUC__)
-# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
-#elif defined(__LCLINT__)
-# define UNUSED(x) /*@unused@*/ x
-#else
-# define UNUSED(x) x
-#endif
-
 typedef struct progress_data {
        git_transfer_progress fetch_progress;
        size_t completed_steps;
@@ -63,24 +50,6 @@ 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 * UNUSED(url),
-               const char * UNUSED(username_from_url),
-               unsigned int UNUSED(allowed_types),
-               void * UNUSED(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)
 {
@@ -107,7 +76,7 @@ int do_clone(git_repository *repo, int argc, char **argv)
        clone_opts.checkout_opts = checkout_opts;
        clone_opts.fetch_progress_cb = &fetch_progress;
        clone_opts.fetch_progress_payload = &pd;
-       clone_opts.cred_acquire_cb = cred_acquire;
+       clone_opts.cred_acquire_cb = cred_acquire_cb;
 
        // Do the clone
        error = git_clone(&cloned_repo, url, path, &clone_opts);
diff --git a/examples/network/common.c b/examples/network/common.c
new file mode 100644 (file)
index 0000000..d123eed
--- /dev/null
@@ -0,0 +1,34 @@
+#include "common.h"
+#include <stdio.h>
+
+/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
+ * with permission of the original author, Martin Pool.
+ * http://sourcefrog.net/weblog/software/languages/C/unused.html
+ */
+#ifdef UNUSED
+#elif defined(__GNUC__)
+# define UNUSED(x) UNUSED_ ## x __attribute__((unused))
+#elif defined(__LCLINT__)
+# define UNUSED(x) /*@unused@*/ x
+#else
+# define UNUSED(x) x
+#endif
+
+int cred_acquire_cb(git_cred **out,
+               const char * UNUSED(url),
+               const char * UNUSED(username_from_url),
+               unsigned int UNUSED(allowed_types),
+               void * UNUSED(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);
+}
index a4cfa1a7ed0d2b76ab6809bb91825d68532f2fd3..1b09caad412a15ca1259eaab7eab996c64d53d0e 100644 (file)
@@ -12,6 +12,12 @@ int fetch(git_repository *repo, int argc, char **argv);
 int index_pack(git_repository *repo, int argc, char **argv);
 int do_clone(git_repository *repo, int argc, char **argv);
 
+int cred_acquire_cb(git_cred **out,
+               const char * url,
+               const char * username_from_url,
+               unsigned int allowed_types,
+               void *payload);
+
 #ifndef PRIuZ
 /* Define the printf format specifer to use for size_t output */
 #if defined(_MSC_VER) || defined(__MINGW32__)
index 6020ec6ecc714db2b32b5f48d73e014d067cded1..ce016ce0b778e1620dfeaec577d3fa405a76efc7 100644 (file)
@@ -92,6 +92,7 @@ int fetch(git_repository *repo, int argc, char **argv)
        callbacks.update_tips = &update_cb;
        callbacks.progress = &progress_cb;
        git_remote_set_callbacks(remote, &callbacks);
+       git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
 
        // Set up the information for the background worker thread
        data.remote = remote;
index e837654a1481bdf425d752db57f5fb8d0c6dfc83..b22ac47a0f52394c361210621a1cd7fa3c0251ed 100644 (file)
@@ -27,6 +27,7 @@ static int use_remote(git_repository *repo, char *name)
                        goto cleanup;
        }
 
+       git_remote_set_cred_acquire_cb(remote, &cred_acquire_cb, NULL);
 
        error = git_remote_connect(remote, GIT_DIRECTION_FETCH);
        if (error < 0)