[gitg] Moved format-patch to branch_actions



commit d5f432509e49de7a4a97cec31f47a3618b005459
Author: Jesse van den Kieboom <jessevdk gnome org>
Date:   Sun Jan 17 15:33:44 2010 +0100

    Moved format-patch to branch_actions

 gitg/gitg-branch-actions.c |  116 ++++++++++++++++++++++++++++++++++++++++++++
 gitg/gitg-branch-actions.h |    2 +
 gitg/gitg-dnd.c            |   36 +++-----------
 gitg/gitg-window.c         |   49 ++++++++++--------
 gitg/gitg-window.h         |    2 +
 5 files changed, 155 insertions(+), 50 deletions(-)
---
diff --git a/gitg/gitg-branch-actions.c b/gitg/gitg-branch-actions.c
index 81e8888..62ec4f5 100644
--- a/gitg/gitg-branch-actions.c
+++ b/gitg/gitg-branch-actions.c
@@ -1775,3 +1775,119 @@ gitg_branch_actions_cherry_pick (GitgWindow   *window,
 
 	return ret;
 }
+
+typedef struct
+{
+	GitgRevision *revision;
+	gchar *destination;
+	GOutputStream *stream;
+} FormatPatchInfo;
+
+static FormatPatchInfo *
+format_patch_info_new (GitgRevision *revision, gchar const *destination, GOutputStream *stream)
+{
+	FormatPatchInfo *ret = g_slice_new0 (FormatPatchInfo);
+
+	ret->revision = gitg_revision_ref (revision);
+	ret->destination = g_strdup (destination);
+	ret->stream = stream;
+
+	return ret;
+}
+
+static void
+format_patch_info_free (FormatPatchInfo *info)
+{
+	gitg_revision_unref (info->revision);
+	g_free (info->destination);
+
+	g_object_unref (info->stream);
+
+	g_slice_free (FormatPatchInfo, info);
+}
+
+static void
+on_format_patch_result (GitgWindow   *window,
+                        GitgProgress  progress,
+                        gpointer      data)
+{
+	FormatPatchInfo *info = (FormatPatchInfo *)data;
+
+	if (progress == GITG_PROGRESS_ERROR)
+	{
+		gchar const *message;
+
+		message_dialog (window,
+			            GTK_MESSAGE_ERROR,
+			            _("Failed to generate format-patch"),
+			            NULL,
+			            NULL,
+	                    NULL);
+	}
+
+	format_patch_info_free (info);
+}
+
+static void
+on_format_patch_update (GitgRunner       *runner,
+                        gchar           **lines,
+                        FormatPatchInfo  *info)
+{
+	while (lines && *lines)
+	{
+		g_output_stream_write_all (info->stream, *lines, strlen (*lines), NULL, NULL, NULL);
+		g_output_stream_write_all (info->stream, "\n", 1, NULL, NULL, NULL);
+		++lines;
+	}
+}
+
+GitgRunner *
+gitg_branch_actions_format_patch (GitgWindow     *window,
+                                  GitgRevision   *revision,
+                                  gchar const    *destination)
+{
+	g_return_val_if_fail (GITG_IS_WINDOW (window), NULL);
+	g_return_val_if_fail (revision != NULL, NULL);
+	g_return_val_if_fail (destination != NULL, NULL);
+
+	GitgRunner *ret;
+	GitgRepository *repository = gitg_window_get_repository (window);
+
+	GFile *file = g_file_new_for_uri (destination);
+	GFileOutputStream *stream = g_file_create (file, 0, NULL, NULL);
+	g_object_unref (file);
+
+	if (!stream)
+	{
+		return NULL;
+	}
+
+	gchar *sha1 = gitg_revision_get_sha1 (revision);
+	gchar *message;
+
+	message = g_strdup_printf (_("Generating format-patch for <%s>"),
+	                           gitg_revision_get_subject (revision));
+
+	FormatPatchInfo *info = format_patch_info_new (revision, destination, G_OUTPUT_STREAM (stream));
+
+	ret = run_progress (window,
+	                    _("Format patch"),
+	                    message,
+	                    on_format_patch_result,
+	                    info,
+	                    "format-patch",
+	                    "-1",
+	                    "--stdout",
+	                    sha1,
+	                    NULL);
+
+	if (ret)
+	{
+		g_signal_connect (ret, "update", G_CALLBACK (on_format_patch_update), info);
+	}
+
+	g_free (sha1);
+	g_free (message);
+
+	return ret;
+}
diff --git a/gitg/gitg-branch-actions.h b/gitg/gitg-branch-actions.h
index b7b5859..1cd0f61 100644
--- a/gitg/gitg-branch-actions.h
+++ b/gitg/gitg-branch-actions.h
@@ -44,6 +44,8 @@ gboolean gitg_branch_actions_tag (GitgWindow *window, gchar const *sha1, gchar c
 
 GitgRunner *gitg_branch_actions_cherry_pick (GitgWindow *window, GitgRevision *revision, GitgRef *dest);
 
+GitgRunner *gitg_branch_actions_format_patch (GitgWindow *window, GitgRevision *revision, gchar const *destination);
+
 G_END_DECLS
 
 #endif /* __GITG_BRANCH_ACTIONS_H__ */
diff --git a/gitg/gitg-dnd.c b/gitg/gitg-dnd.c
index 40f58c0..9643416 100644
--- a/gitg/gitg-dnd.c
+++ b/gitg/gitg-dnd.c
@@ -3,6 +3,8 @@
 #include "gitg-cell-renderer-path.h"
 #include "gitg-utils.h"
 #include <string.h>
+#include "gitg-window.h"
+#include "gitg-branch-actions.h"
 
 enum
 {
@@ -905,32 +907,6 @@ gitg_drag_source_data_get_cb (GtkWidget        *widget,
 }
 
 static void
-format_patch (GtkTreeView *tree_view,
-              GitgDndData *data)
-{
-	GitgRepository *repository;
-
-	repository = GITG_REPOSITORY (gtk_tree_view_get_model (tree_view));
-	gchar *sha1 = gitg_revision_get_sha1 (data->revision);
-
-	/* FIXME: this is all sync and bad... */
-	gchar **ret = gitg_repository_command_with_outputv (repository, NULL,
-	                                                    "format-patch",
-	                                                    "-1",
-	                                                    "--stdout",
-	                                                    sha1,
-	                                                    NULL);
-
-	gchar *content = g_strjoinv ("\n", ret);
-	g_strfreev (ret);
-
-	g_file_set_contents (data->xds_destination, content, -1, NULL);
-
-	g_free (sha1);
-	g_free (content);
-}
-
-static void
 gitg_drag_source_end_cb (GtkTreeView    *tree_view,
                          GdkDragContext *context,
                          GitgDndData    *data)
@@ -942,8 +918,12 @@ gitg_drag_source_end_cb (GtkTreeView    *tree_view,
 		if (data->xds_destination != NULL)
 		{
 			/* Do extract it there then */
-			format_patch (tree_view, data);
-			
+			GitgWindow *window = GITG_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (data->tree_view)));
+			gitg_window_add_branch_action (window,
+			                               gitg_branch_actions_format_patch (window,
+			                                                                 data->revision,
+			                                                                 data->xds_destination));
+
 			g_free (data->xds_destination);
 			data->xds_destination = NULL;
 		}
diff --git a/gitg/gitg-window.c b/gitg/gitg-window.c
index 6537207..35219c1 100644
--- a/gitg/gitg-window.c
+++ b/gitg/gitg-window.c
@@ -123,8 +123,8 @@ on_branch_action_runner_end (GitgRunner *runner, gboolean cancelled, GitgWindow
 	g_object_unref (runner);
 }
 
-static gboolean
-add_branch_action (GitgWindow *window, GitgRunner *runner)
+gboolean
+gitg_window_add_branch_action (GitgWindow *window, GitgRunner *runner)
 {
 	if (runner != NULL && gitg_runner_running (runner))
 	{
@@ -550,7 +550,7 @@ on_refs_dnd (GitgRef *source, GitgRef *dest, gboolean dropped, GitgWindow *windo
 	if (source_type == GITG_REF_TYPE_BRANCH &&
 	    dest_type == GITG_REF_TYPE_REMOTE)
 	{
-		ret = add_branch_action (window, gitg_branch_actions_push (window, source, dest));
+		ret = gitg_window_add_branch_action (window, gitg_branch_actions_push (window, source, dest));
 	}
 	else if (source_type == GITG_REF_TYPE_STASH)
 	{
@@ -608,8 +608,8 @@ on_revision_dnd (GitgRevision *source,
 		return FALSE;
 	}
 
-	return add_branch_action (window,
-	                          gitg_branch_actions_cherry_pick (window, source, dest));
+	return gitg_window_add_branch_action (window,
+	                                      gitg_branch_actions_cherry_pick (window, source, dest));
 }
 
 static void
@@ -1769,8 +1769,8 @@ on_push_activated (GtkAction *action, GitgWindow *window)
 	gchar const *branch = g_object_get_data (G_OBJECT (action),
 	                                         DYNAMIC_ACTION_DATA_BRANCH_KEY);
 
-	add_branch_action (window,
-	                   gitg_branch_actions_push_remote (window, window->priv->popup_refs[0], remote, branch));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_push_remote (window, window->priv->popup_refs[0], remote, branch));
 }
 
 static void
@@ -1779,9 +1779,10 @@ on_rebase_activated (GtkAction *action, GitgWindow *window)
 	GitgRef *dest = g_object_get_data (G_OBJECT (action),
 	                                   DYNAMIC_ACTION_DATA_KEY);
 
-	add_branch_action (window, gitg_branch_actions_rebase (window,
-	                                                       window->priv->popup_refs[0],
-	                                                       dest));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_rebase (window,
+	                                                           window->priv->popup_refs[0],
+	                                                           dest));
 }
 
 static void
@@ -1790,9 +1791,10 @@ on_merge_activated (GtkAction *action, GitgWindow *window)
 	GitgRef *dest = g_object_get_data (G_OBJECT (action),
 	                                   DYNAMIC_ACTION_DATA_KEY);
 
-	add_branch_action (window, gitg_branch_actions_merge (window,
-	                                                      dest,
-	                                                      window->priv->popup_refs[0]));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_merge (window,
+	                                                          dest,
+	                                                          window->priv->popup_refs[0]));
 }
 
 static void
@@ -2271,9 +2273,10 @@ on_cherry_pick_activated (GtkAction *action, GitgWindow *window)
 	GitgRef *ref = g_object_get_data (G_OBJECT (action),
 	                                  DYNAMIC_ACTION_DATA_KEY);
 
-	add_branch_action (window, gitg_branch_actions_cherry_pick (window,
-	                                                            rev,
-	                                                            ref));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_cherry_pick (window,
+	                                                               rev,
+	                                                               ref));
 
 	gitg_revision_unref (rev);
 
@@ -2468,17 +2471,19 @@ on_rebase_branch_action_activate (GtkAction *action, GitgWindow *window)
 		source = 0;
 	}
 
-	add_branch_action (window, gitg_branch_actions_rebase (window,
-	                                                       window->priv->popup_refs[source],
-	                                                       window->priv->popup_refs[!source]));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_rebase (window,
+	                                                           window->priv->popup_refs[source],
+	                                                           window->priv->popup_refs[!source]));
 }
 
 void
 on_merge_branch_action_activate (GtkAction *action, GitgWindow *window)
 {
-	add_branch_action (window, gitg_branch_actions_merge (window,
-	                                                       window->priv->popup_refs[0],
-	                                                       window->priv->popup_refs[1]));
+	gitg_window_add_branch_action (window,
+	                               gitg_branch_actions_merge (window,
+	                                                          window->priv->popup_refs[0],
+	                                                          window->priv->popup_refs[1]));
 }
 
 typedef struct
diff --git a/gitg/gitg-window.h b/gitg/gitg-window.h
index 35f28a7..1d2b988 100644
--- a/gitg/gitg-window.h
+++ b/gitg/gitg-window.h
@@ -58,6 +58,8 @@ void gitg_window_show_commit(GitgWindow *window);
 GitgRepository *gitg_window_get_repository(GitgWindow *window);
 void gitg_window_set_select_on_load (GitgWindow *window, gchar const *selection);
 
+gboolean gitg_window_add_branch_action (GitgWindow *window, GitgRunner *runner);
+
 G_END_DECLS
 
 #endif /* __GITG_WINDOW_H__ */



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