[giggle] Add support for to show number of commits of each contributor



commit 038c4bd9340456212304796fac1f7959d3e357dd
Author: João Pedro Forjaz de Lacerda <jpl codethink co uk>
Date:   Sun May 1 02:38:26 2011 +0100

    Add support for to show number of commits of each contributor
    
    Fixes https://bugzilla.gnome.org/show_bug.cgi?id=648086

 libgiggle-git/giggle-git-authors.c |   43 +++++++++++++++++++++++++---
 libgiggle/giggle-author.c          |   53 +++++++++++++++++++++++++++++++++--
 libgiggle/giggle-author.h          |    4 +++
 3 files changed, 92 insertions(+), 8 deletions(-)
---
diff --git a/libgiggle-git/giggle-git-authors.c b/libgiggle-git/giggle-git-authors.c
index 985abc6..81fff8b 100644
--- a/libgiggle-git/giggle-git-authors.c
+++ b/libgiggle-git/giggle-git-authors.c
@@ -40,6 +40,7 @@ typedef struct {
 typedef struct {
 	GHashTable* known_names;
 	GHashTable* known_emails;
+	guint       commits;
 } GiggleFlexibleAuthor;
 
 static void
@@ -71,6 +72,12 @@ giggle_flexible_author_add_email (GiggleFlexibleAuthor* self,
 }
 
 static void
+giggle_flexible_author_add_commit (GiggleFlexibleAuthor* self)
+{
+	self->commits += 1;
+}
+
+static void
 find_popular (gchar const* key,
 	      GiggleVote* vote,
 	      GiggleVote**best)
@@ -116,6 +123,7 @@ giggle_flexible_author_new (gchar const* name,
 						    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 */
@@ -176,7 +184,7 @@ git_authors_finalize (GObject *object)
 	GiggleGitAuthorsPriv *priv;
 
 	priv = GET_PRIV (object);
-	
+
 	g_list_foreach (priv->authors, (GFunc) g_object_unref, NULL);
 	g_list_free (priv->authors);
 	priv->authors = NULL;
@@ -224,6 +232,7 @@ add_author (gchar const          *key,
 	    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;
@@ -238,10 +247,30 @@ add_author (gchar const          *key,
 	} else {
 		string = g_strdup (popular_name);
 	}
-	priv->authors = g_list_prepend (priv->authors, giggle_author_new_from_string (string));
+
+	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,
@@ -283,6 +312,7 @@ authors_handle_output (GiggleJob   *job,
 						     (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));
@@ -292,6 +322,7 @@ authors_handle_output (GiggleJob   *job,
 						     (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));
@@ -301,6 +332,7 @@ authors_handle_output (GiggleJob   *job,
 						     (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));
@@ -314,11 +346,13 @@ authors_handle_output (GiggleJob   *job,
 		}
 	}
 
-	g_list_foreach (priv->authors, (GFunc)g_object_unref, NULL);
+	g_list_foreach (priv->authors, (GFunc) g_object_unref, NULL);
 	g_list_free (priv->authors);
 
 	priv->authors = NULL;
-	g_hash_table_foreach (authors_by_name, (GHFunc)add_author, priv);
+	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);
 }
@@ -340,4 +374,3 @@ giggle_git_authors_get_list (GiggleGitAuthors *authors)
 
 	return priv->authors;
 }
-
diff --git a/libgiggle/giggle-author.c b/libgiggle/giggle-author.c
index 4bfde32..e70f9e4 100644
--- a/libgiggle/giggle-author.c
+++ b/libgiggle/giggle-author.c
@@ -29,6 +29,7 @@ enum {
 	PROP_0,
 	PROP_NAME,
 	PROP_EMAIL,
+	PROP_COMMITS,
 	PROP_STRING,
 };
 
@@ -36,6 +37,7 @@ typedef struct {
 	char* string;
 	char* email;
 	char* name;
+	guint commits;
 } GiggleAuthorPriv;
 
 G_DEFINE_TYPE (GiggleAuthor, giggle_author, G_TYPE_OBJECT)
@@ -63,6 +65,16 @@ author_set_email (GiggleAuthorPriv *priv,
 }
 
 static void
+author_set_commits (GiggleAuthorPriv *priv,
+		    guint             commits)
+{
+	g_free (priv->string);
+
+	priv->commits = commits;
+	priv->string = NULL;
+}
+
+static void
 author_set_string (GiggleAuthorPriv *priv,
 		   const char       *string)
 {
@@ -77,6 +89,9 @@ author_set_string (GiggleAuthorPriv *priv,
 	priv->string = g_strdup (string);
 	priv->email = priv->name = NULL;
 
+       /* if needed, change regex to deal with commit numbers (example below):
+	* regex = g_regex_new ("^\\s*([^<]+?)?\\s*(?:<([^>]+)>)?\\s*(?:\\((\\d+)\\))?\\s*$");
+	* can be accessed via: g_match_info_fetch (match, 3) */
 	if (G_UNLIKELY (!regex)) {
 		regex = g_regex_new ("^\\s*([^<]+?)?\\s*(?:<([^>]+)>)?\\s*$",
 				     G_REGEX_OPTIMIZE | G_REGEX_RAW, 0, &error);
@@ -113,6 +128,10 @@ author_set_property (GObject      *object,
 		author_set_email (priv, g_value_get_string (value));
 		break;
 
+	case PROP_COMMITS:
+		author_set_commits (priv, g_value_get_uint (value));
+		break;
+
 	case PROP_STRING:
 		author_set_string (priv, g_value_get_string (value));
 		break;
@@ -140,6 +159,10 @@ author_get_property (GObject    *object,
 		g_value_set_string (value, priv->email);
 		break;
 
+	case PROP_COMMITS:
+		g_value_set_uint (value, priv->commits);
+		break;
+
 	case PROP_STRING:
 		g_value_set_string (value, priv->string);
 		break;
@@ -183,6 +206,11 @@ giggle_author_class_init (GiggleAuthorClass *class)
 							      "The email address of the author",
 							      NULL, G_PARAM_READWRITE));
 
+	g_object_class_install_property (object_class, PROP_COMMITS,
+					 g_param_spec_uint ("commits", "Commits",
+							    "The number of commits made by the author",
+							    0, G_MAXUINT, 0, G_PARAM_READWRITE));
+
 	g_object_class_install_property (object_class, PROP_STRING,
 					 g_param_spec_string ("string", "Author String",
 							      "The string describing the author",
@@ -246,6 +274,21 @@ giggle_author_get_name (GiggleAuthor* author)
 }
 
 void
+giggle_author_set_commits (GiggleAuthor *author,
+			   guint         commits)
+{
+	g_return_if_fail (GIGGLE_IS_AUTHOR (author));
+	g_object_set (author, "commits", commits, NULL);
+}
+
+guint
+giggle_author_get_commits (GiggleAuthor *author)
+{
+	g_return_val_if_fail (GIGGLE_IS_AUTHOR (author), 0);
+	return GET_PRIV (author)->commits;
+}
+
+void
 giggle_author_set_string (GiggleAuthor *author,
 			  char const   *string)
 {
@@ -271,9 +314,13 @@ giggle_author_get_string (GiggleAuthor* author)
 			if (string->len > 0)
 				g_string_append_c (string, ' ');
 
-			g_string_append_c (string, '<');
-			g_string_append   (string, priv->email);
-			g_string_append_c (string, '>');
+			g_string_append_printf (string, "<%s> ", priv->email);
+		}
+		if (priv->commits) {
+			if (string->len > 0)
+				g_string_append_c (string, ' ');
+
+			g_string_append_printf (string, "(%d)", priv->commits);
 		}
 
 		if (string->len) {
diff --git a/libgiggle/giggle-author.h b/libgiggle/giggle-author.h
index 944ed0f..8e6f930 100644
--- a/libgiggle/giggle-author.h
+++ b/libgiggle/giggle-author.h
@@ -49,6 +49,10 @@ GiggleAuthor * giggle_author_new_from_name   (const char   *name,
 					      const char   *email);
 GiggleAuthor * giggle_author_new_from_string (const char   *string);
 
+void           giggle_author_set_commits     (GiggleAuthor *author,
+                                              guint         commits);
+guint          giggle_author_get_commits     (GiggleAuthor *author);
+
 void           giggle_author_set_email       (GiggleAuthor *author,
 					      char const   *email);
 char const *   giggle_author_get_email       (GiggleAuthor *author);



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