File: giggle-git-authors.c

package info (click to toggle)
giggle 0.7-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,084 kB
  • sloc: ansic: 23,243; sh: 11,642; makefile: 442; xml: 81
file content (372 lines) | stat: -rw-r--r-- 10,535 bytes parent folder | download | duplicates (4)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2007 Imendio AB
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "giggle-git-authors.h"

#include <libgiggle/giggle-author.h>

#include <string.h>

typedef struct GiggleGitAuthorsPriv GiggleGitAuthorsPriv;

struct GiggleGitAuthorsPriv {
	GList *authors;
};

/* START: GiggleFlexibleAuthor API */
typedef struct {
	guint  votes;
	gchar* ballot;
} GiggleVote;

typedef struct {
	GHashTable* known_names;
	GHashTable* known_emails;
	guint       commits;
} GiggleFlexibleAuthor;

static void
_giggle_flexible_author_add_ballot (GHashTable * hash_table,
				    gchar const* ballot)
{
	GiggleVote* vote = g_hash_table_lookup (hash_table, ballot);

	if (!vote) {
		vote = g_new0 (GiggleVote, 1);
		vote->ballot = g_strdup (ballot);
		g_hash_table_insert (hash_table, vote->ballot, vote);
	}
	vote->votes++;
}

static void
giggle_flexible_author_add_name (GiggleFlexibleAuthor* self,
				 gchar const * name)
{
	_giggle_flexible_author_add_ballot (self->known_names, name);
}

static void
giggle_flexible_author_add_email (GiggleFlexibleAuthor* self,
				  gchar const * email)
{
	_giggle_flexible_author_add_ballot (self->known_emails, email);
}

static void
giggle_flexible_author_add_commit (GiggleFlexibleAuthor* self)
{
	self->commits += 1;
}

static void
find_popular (gchar const* key,
	      GiggleVote* vote,
	      GiggleVote**best)
{
	if (G_UNLIKELY (!*best)) {
		*best = vote;
	} else {
		if (vote->votes > (*best)->votes) {
			*best = vote;
		}
	}
}

static gchar const *
_giggle_flexible_author_get_voted (GHashTable* hash_table)
{
	GiggleVote* vote = NULL;
	g_hash_table_foreach (hash_table, (GHFunc)find_popular, &vote);
	g_return_val_if_fail (vote, "");
	return vote->ballot;
}

static gchar const *
giggle_flexible_author_get_voted_name (GiggleFlexibleAuthor *self)
{
	return _giggle_flexible_author_get_voted (self->known_names);
}

static gchar const *
giggle_flexible_author_get_voted_email (GiggleFlexibleAuthor *self)
{
	return _giggle_flexible_author_get_voted (self->known_emails);
}

static GiggleFlexibleAuthor*
giggle_flexible_author_new (gchar const* name,
			    gchar const* email)
{
	GiggleFlexibleAuthor* self = g_slice_new0 (GiggleFlexibleAuthor);
	self->known_names = g_hash_table_new_full  (g_str_hash, g_str_equal,
						    g_free, g_free);
	self->known_emails = g_hash_table_new_full (g_str_hash, g_str_equal,
						    g_free, g_free);
	giggle_flexible_author_add_name  (self, name);
	giggle_flexible_author_add_email (self, email);
	self->commits = 1;
	return self;
}
/* END: GiggleFlexibleAuthor API */

static void     git_authors_dispose       (GObject           *object);
static void     git_authors_get_property  (GObject           *object,
					   guint              param_id,
					   GValue            *value,
					   GParamSpec        *pspec);
static void     git_authors_set_property  (GObject           *object,
					   guint              param_id,
					   const GValue      *value,
					   GParamSpec        *pspec);
static gboolean authors_get_command_line  (GiggleJob         *job,
					   gchar            **command_line);
static void     authors_handle_output     (GiggleJob         *job,
					   const gchar       *output,
					   gsize              length);

G_DEFINE_TYPE (GiggleGitAuthors, giggle_git_authors, GIGGLE_TYPE_JOB)

#define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GIGGLE_TYPE_GIT_AUTHORS, GiggleGitAuthorsPriv))

static void
giggle_git_authors_class_init (GiggleGitAuthorsClass *class)
{
	GObjectClass *object_class = G_OBJECT_CLASS (class);
	GiggleJobClass *job_class  = GIGGLE_JOB_CLASS (class);

	object_class->dispose      = git_authors_dispose;
	object_class->get_property = git_authors_get_property;
	object_class->set_property = git_authors_set_property;

	job_class->get_command_line = authors_get_command_line;
	job_class->handle_output    = authors_handle_output;

#if 0
	g_object_class_install_property (object_class,
					 PROP_MY_PROP,
					 g_param_spec_string ("my-prop",
							      "My Prop",
							      "Describe the property",
							      NULL,
							      G_PARAM_READABLE));
#endif

	g_type_class_add_private (object_class, sizeof (GiggleGitAuthorsPriv));
}

static void
giggle_git_authors_init (GiggleGitAuthors *git_authors)
{
}

static void
git_authors_dispose (GObject *object)
{
	GiggleGitAuthorsPriv *priv;

	priv = GET_PRIV (object);

	g_list_free_full (priv->authors, g_object_unref);
	priv->authors = NULL;

	G_OBJECT_CLASS (giggle_git_authors_parent_class)->dispose (object);
}

static void
git_authors_get_property (GObject    *object,
		    guint       param_id,
		    GValue     *value,
		    GParamSpec *pspec)
{
	switch (param_id) {
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static void
git_authors_set_property (GObject      *object,
		    guint         param_id,
		    const GValue *value,
		    GParamSpec   *pspec)
{
	switch (param_id) {
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
		break;
	}
}

static gboolean
authors_get_command_line (GiggleJob *job,
			  gchar    **command_line)
{
	*command_line = g_strdup (GIT_COMMAND " log --format=format:'%an <%ae>'");
	return TRUE;
}

static void
add_author (gchar const          *key,
	    GiggleFlexibleAuthor *value,
	    GiggleGitAuthorsPriv *priv)
{
	/* FIXME: giggle_author_new (name, email) */
	GiggleAuthor *author_to_add;
	gchar const* popular_name  = giggle_flexible_author_get_voted_name (value);
	gchar const* popular_email = giggle_flexible_author_get_voted_email (value);
	gchar* string = NULL;

	if (strcmp (popular_name, key)) {
		/* this is not the most popular one */
		return;
	}

	if (popular_email && *popular_email) {
		string = g_strdup_printf ("%s <%s>", popular_name, popular_email);
	} else {
		string = g_strdup (popular_name);
	}

	author_to_add = giggle_author_new_from_string (string);
	giggle_author_set_commits (author_to_add, value->commits);
	priv->authors = g_list_prepend (priv->authors, author_to_add);
	g_free (string);
}

static gint
authors_compare_commits (gconstpointer a,
			 gconstpointer b)
{
	guint commits_a = giggle_author_get_commits (GIGGLE_AUTHOR (a));
	guint commits_b = giggle_author_get_commits (GIGGLE_AUTHOR (b));
	gint result = commits_a - commits_b;

	if (result < 0) {
		return 1;
	} else if (result > 0) {
		return -1;
	} else {
		return 0;
	}
}

static void
authors_handle_output (GiggleJob   *job,
		       const gchar *output,
		       gsize        length)
{
	GiggleGitAuthorsPriv *priv;
	GHashTable           *authors_by_name;
	GHashTable           *authors_by_email;
	gchar               **lines;
	gchar               **line;

	priv = GET_PRIV (job);

	lines = g_strsplit (output, "\n", -1);

	authors_by_name  = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
	authors_by_email = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

	for (line = lines; line && *line; line++) {
		if (**line != '\0') {
			GiggleAuthor* author = giggle_author_new_from_string (*line);
			gchar const * email = giggle_author_get_email (author);
			gchar const * name  = giggle_author_get_name  (author);

			/* try to find this author */
			GiggleFlexibleAuthor* by_name;
			GiggleFlexibleAuthor* by_email;
			by_name  = name  ? g_hash_table_lookup (authors_by_name,  name)  : NULL;
			by_email = email ? g_hash_table_lookup (authors_by_email, email) : NULL;

			if (!by_name && !by_email) {
				/* create a new flexible author */
				by_name = giggle_flexible_author_new (giggle_author_get_name (author),
								      giggle_author_get_email (author));
				g_hash_table_insert (authors_by_name,
						     (gpointer) g_strdup (giggle_author_get_name (author)),
						     by_name);
				g_hash_table_insert (authors_by_email,
						     (gpointer) g_strdup (giggle_author_get_email (author)),
						     by_name);
			} else if (!by_name) {
				giggle_flexible_author_add_commit (by_email);
				/* add the name to by_email */
				giggle_flexible_author_add_name (by_email,
								 giggle_author_get_name (author));
				giggle_flexible_author_add_email (by_email,
								  giggle_author_get_email (author));
				g_hash_table_insert (authors_by_name,
						     (gpointer) g_strdup (giggle_author_get_name (author)),
						     by_email);
			} else if (!by_email) {
				giggle_flexible_author_add_commit (by_name);
				/* add the email to by_name */
				giggle_flexible_author_add_email (by_name,
								  giggle_author_get_email (author));
				giggle_flexible_author_add_name (by_name,
								 giggle_author_get_name (author));
				g_hash_table_insert (authors_by_email,
						     (gpointer) g_strdup (giggle_author_get_email (author)),
						     by_name);
			} else if (by_name == by_email) {
				giggle_flexible_author_add_commit (by_name);
				/* just increase the counters */
				giggle_flexible_author_add_email (by_name,
								  giggle_author_get_email (author));
				giggle_flexible_author_add_name (by_name,
								 giggle_author_get_name (author));
			} else {
				/* both are different and real flexible authors, merge them */
				g_warning ("FIXME: implement merging; ask sven@imendio.com for an implementation and give him your git repository as a test case");
			}
			g_object_unref (author);
		}
	}

	g_list_free_full (priv->authors, g_object_unref);
	priv->authors = NULL;

	g_hash_table_foreach (authors_by_name, (GHFunc) add_author, priv);

	priv->authors = g_list_sort (priv->authors, (GCompareFunc) authors_compare_commits);

	g_strfreev (lines);
}

GiggleJob *
giggle_git_authors_new (void)
{
	return g_object_new (GIGGLE_TYPE_GIT_AUTHORS, NULL);
}

GList *
giggle_git_authors_get_list (GiggleGitAuthors *authors)
{
	GiggleGitAuthorsPriv *priv;

	g_return_val_if_fail (GIGGLE_IS_GIT_AUTHORS (authors), NULL);

	priv = GET_PRIV (authors);

	return priv->authors;
}