[libgit2-glib] Fix clone example
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [libgit2-glib] Fix clone example
- Date: Sat, 23 Nov 2013 10:45:37 +0000 (UTC)
commit abc19f3574067243230c7b74019726db70b59e7c
Author: Jesse van den Kieboom <jessevdk gmail com>
Date: Sat Nov 23 11:42:50 2013 +0100
Fix clone example
examples/clone.c | 172 ++++++++++++++++++++++++++++++++++++++++-------------
1 files changed, 130 insertions(+), 42 deletions(-)
---
diff --git a/examples/clone.c b/examples/clone.c
index 5bab90b..3ac68e2 100644
--- a/examples/clone.c
+++ b/examples/clone.c
@@ -1,52 +1,136 @@
#include <glib.h>
#include <stdio.h>
+#include <termios.h>
+#include <string.h>
#include "ggit.h"
-static int
-fetch_progress (GgitTransferProgress *stats,
- gpointer useless)
+typedef struct
{
- gint network_percent = (100 * ggit_transfer_progress_get_received_objects (stats)) /
ggit_transfer_progress_get_total_objects (stats);
- gint index_percent = (100 * ggit_transfer_progress_get_indexed_objects (stats)) /
ggit_transfer_progress_get_total_objects (stats);
- gint kbytes = ggit_transfer_progress_get_received_bytes (stats) / 1024;
-
- g_message ("net %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d)",
- network_percent, kbytes,
- ggit_transfer_progress_get_received_objects (stats),
- ggit_transfer_progress_get_total_objects (stats),
- index_percent,
- ggit_transfer_progress_get_indexed_objects (stats),
- ggit_transfer_progress_get_total_objects (stats));
+ GgitRemoteCallbacksClass parent_class;
+} ClonerClass;
- return 0;
+typedef struct
+{
+ GgitRemoteCallbacks parent;
+} Cloner;
+
+static void cloner_class_init (ClonerClass *klass);
+
+G_DEFINE_TYPE (Cloner, cloner, GGIT_TYPE_REMOTE_CALLBACKS)
+
+static void
+cloner_init (Cloner *cloner)
+{
}
-static gint
-cred_acquire (const gchar *url,
- const gchar *username_from_url,
- guint allowed_types,
- GgitCred **cred,
- gpointer user_data)
+static gboolean
+cloner_transfer_progress (GgitRemoteCallbacks *callbacks,
+ GgitTransferProgress *stats,
+ GError **error)
{
- gchar username[128] = "";
- gchar password[128] = "";
- GError *error = NULL;
+ guint recvobjs;
+ guint totobjs;
+ guint indexobjs;
- g_message ("Username: ");
- scanf ("%s", username);
+ recvobjs = ggit_transfer_progress_get_received_objects (stats);
+ totobjs = ggit_transfer_progress_get_total_objects (stats);
+ indexobjs = ggit_transfer_progress_get_indexed_objects (stats);
- /* Yup. Right there on your terminal. Careful where you copy/paste output. */
- g_message ("Password: ");
- scanf ("%s", password);
+ guint network_percent = recvobjs * 100 / totobjs;
+ guint index_percent = indexobjs * 100 / totobjs;
+ guint kbytes = ggit_transfer_progress_get_received_bytes (stats) / 1024;
- *cred = GGIT_CRED (ggit_cred_plaintext_new (username, password, &error));
+ g_printf ("\rnet %3d%% (%4d kb, %5d/%5d) / idx %3d%% (%5d/%5d)",
+ network_percent, kbytes,
+ recvobjs, totobjs,
+ index_percent,
+ indexobjs, totobjs);
- if (error != NULL)
+ if (recvobjs == totobjs && indexobjs == totobjs)
{
- return -1;
+ g_printf("\n");
}
- return 0;
+ return TRUE;
+}
+
+static void
+chomp (gchar *s)
+{
+ int l;
+
+ l = strlen (s);
+
+ if (s[l - 1] == '\n')
+ {
+ s[l - 1] = '\0';
+ }
+}
+
+static gboolean
+cloner_credentials (GgitRemoteCallbacks *callbacks,
+ const gchar *url,
+ const gchar *username_from_url,
+ guint allowed_types,
+ GgitCred **cred,
+ GError **error)
+{
+ gchar username[128];
+ gchar password[128];
+ struct termios old, new;
+
+ g_printf ("Username: ");
+
+ if (fgets (username, sizeof (username) - 1, stdin) == NULL)
+ {
+ return FALSE;
+ }
+
+ g_printf ("Password: ");
+
+
+ if (tcgetattr (STDIN_FILENO, &old) != 0)
+ {
+ return FALSE;
+ }
+
+ new = old;
+ new.c_lflag &= ~ECHO;
+
+ if (tcsetattr (STDIN_FILENO, TCSAFLUSH, &new) != 0)
+ {
+ return FALSE;
+ }
+
+ if (fgets (password, sizeof (password) - 1, stdin) == NULL)
+ {
+ tcsetattr (STDIN_FILENO, TCSAFLUSH, &old);
+
+ return FALSE;
+ }
+
+ tcsetattr (STDIN_FILENO, TCSAFLUSH, &old);
+
+ chomp (username);
+ chomp (password);
+
+ *cred = GGIT_CRED (ggit_cred_plaintext_new (username, password, error));
+
+ if (*error != NULL)
+ {
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+static void
+cloner_class_init (ClonerClass *klass)
+{
+ GgitRemoteCallbacksClass *cb_klass = GGIT_REMOTE_CALLBACKS_CLASS (klass);
+
+ cb_klass->transfer_progress = cloner_transfer_progress;
+ cb_klass->credentials = cloner_credentials;
}
int main(int argc, char **argv)
@@ -57,10 +141,11 @@ int main(int argc, char **argv)
const char *path;
GError *error = NULL;
GFile *location;
+ Cloner *cloner;
// Validate args
if (argc < 3) {
- g_warning ("USAGE: %s <url> <path>\n", argv[0]);
+ g_printf ("USAGE: %s <url> <path>\n", argv[0]);
return -1;
}
@@ -72,16 +157,18 @@ int main(int argc, char **argv)
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);
+ cloner = g_object_new (cloner_get_type(), NULL);
+
+ ggit_clone_options_set_remote_callbacks (options,
+ GGIT_REMOTE_CALLBACKS (cloner));
+
+ g_object_unref (cloner);
// Do the clone
- cloned_repo = ggit_repository_clone(url, location, options, &error);
- if (error != NULL) {
+ cloned_repo = ggit_repository_clone (url, location, options, &error);
+
+ if (error != NULL)
+ {
g_warning ("%s", error->message);
g_error_free (error);
}
@@ -90,5 +177,6 @@ int main(int argc, char **argv)
g_object_unref (cloned_repo);
}
+ ggit_clone_options_free (options);
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]