File: clone.c

package info (click to toggle)
libgit2-glib 0.0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,456 kB
  • ctags: 4,162
  • sloc: ansic: 15,757; sh: 11,230; makefile: 308; python: 52
file content (183 lines) | stat: -rw-r--r-- 3,638 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <glib.h>
#include <stdio.h>
#include <termios.h>
#include <string.h>
#include <glib/gprintf.h>
#include "ggit.h"

typedef struct
{
	GgitRemoteCallbacksClass parent_class;
} ClonerClass;

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 gboolean
cloner_transfer_progress (GgitRemoteCallbacks   *callbacks,
                          GgitTransferProgress  *stats,
                          GError               **error)
{
	guint recvobjs;
	guint totobjs;
	guint indexobjs;

	recvobjs = ggit_transfer_progress_get_received_objects (stats);
	totobjs = ggit_transfer_progress_get_total_objects (stats);
	indexobjs = ggit_transfer_progress_get_indexed_objects (stats);

	guint network_percent = recvobjs * 100 / totobjs;
	guint index_percent = indexobjs * 100 / totobjs;
	guint kbytes = ggit_transfer_progress_get_received_bytes (stats) / 1024;

	g_printf ("\rnet %3d%% (%4d kb, %5d/%5d)  /  idx %3d%% (%5d/%5d)",
	          network_percent, kbytes,
	          recvobjs, totobjs,
	          index_percent,
	          indexobjs, totobjs);

	if (recvobjs == totobjs && indexobjs == totobjs)
	{
		g_printf("\n");
	}

	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)
{
	GgitRepository *cloned_repo = NULL;
	GgitCloneOptions *options = NULL;
	const char *url;
	const char *path;
	GError *error = NULL;
	GFile *location;
	Cloner *cloner;

	// Validate args
	if (argc < 3) {
		g_printf ("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 ();
	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)
	{
		g_warning ("%s", error->message);
		g_error_free (error);
	}
	else
	{
		g_object_unref (cloned_repo);
	}

	ggit_clone_options_free (options);
	return 0;
}