[libgit2-glib] Add clone example



commit 836b606da859a0efd5fc135ba9ecb4c8ba72897c
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Wed Mar 20 17:09:05 2013 +0100

    Add clone example

 examples/Makefile.am |    4 ++
 examples/clone.c     |   91 ++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 95 insertions(+), 0 deletions(-)
---
diff --git a/examples/Makefile.am b/examples/Makefile.am
index 5c8b2c9..23a01d6 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -11,4 +11,8 @@ EXAMPLE_PROGS         += walk
 walk_SOURCES           = walk.c
 walk_LDADD             = $(progs_ldadd)
 
+EXAMPLE_PROGS          += clone
+clone_SOURCES          = clone.c
+clone_LDADD            = $(progs_ldadd)
+
 # ex:set ts=8 noet:
diff --git a/examples/clone.c b/examples/clone.c
new file mode 100644
index 0000000..fe65a1f
--- /dev/null
+++ b/examples/clone.c
@@ -0,0 +1,91 @@
+#include <glib.h>
+#include <stdio.h>
+#include "ggit.h"
+
+static int
+fetch_progress (const GgitTransferProgress *stats,
+                gpointer                    useless)
+{
+       gint network_percent = (100 * stats->received_objects) / stats->total_objects;
+       gint index_percent = (100 * stats->indexed_objects) / stats->total_objects;
+       gint kbytes = stats->received_bytes / 1024;
+
+       g_message ("net %3d%% (%4d kb, %5d/%5d)  /  idx %3d%% (%5d/%5d)",
+                  network_percent, kbytes,
+                  stats->received_objects, stats->total_objects,
+                  index_percent, stats->indexed_objects, stats->total_objects);
+
+       return 0;
+}
+
+static gint
+cred_acquire (const gchar *url,
+              const gchar *username_from_url,
+              guint        allowed_types,
+              GgitCred   **cred,
+              gpointer     user_data)
+{
+       gchar username[128] = "";
+       gchar password[128] = "";
+       GError *error = NULL;
+
+       g_message ("Username: ");
+       scanf ("%s", username);
+
+       /* Yup. Right there on your terminal. Careful where you copy/paste output. */
+       g_message ("Password: ");
+       scanf ("%s", password);
+
+       *cred = GGIT_CRED (ggit_cred_plaintext_new (username, password, &error));
+
+       if (error != NULL)
+       {
+               return -1;
+       }
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       GgitRepository *cloned_repo = NULL;
+       GgitCloneOptions *options = NULL;
+       const char *url;
+       const char *path;
+       GError *error = NULL;
+       GFile *location;
+
+       // Validate args
+       if (argc < 3) {
+               g_warning ("USAGE: %s <url> <path>\n", argv[0]);
+               return -1;
+       }
+
+       ggit_init ();
+
+       url = argv[1];
+       path = argv[2];
+
+       location = g_file_new_for_commandline_arg (path);
+
+       options = ggit_clone_options_new ();
+       ggit_clone_options_set_fetch_progress_callback (options,
+                                                       fetch_progress,
+                                                       NULL);
+       ggit_clone_options_set_cred_acquire_callback (options,
+                                                     cred_acquire,
+                                                     NULL);
+
+       // Do the clone
+       cloned_repo = ggit_repository_clone(url, location, options, &error);
+       if (error != NULL) {
+               g_warning ("%s", error->message);
+               g_error_free (error);
+       }
+       else
+       {
+               g_object_unref (cloned_repo);
+       }
+
+       return 0;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]