[gitg] Implement renaming of a local branch



commit fabca85e26b5ba6a237223f93fb8c01d47728627
Author: Paolo Borelli <pborelli gnome org>
Date:   Mon Aug 17 18:56:12 2009 +0200

    Implement renaming of a local branch

 gitg/gitg-branch-actions.c |  116 ++++++++++++++++++++++++++++++++++++++++++++
 gitg/gitg-branch-actions.h |    1 +
 gitg/gitg-menus.xml        |    7 +++
 gitg/gitg-window.c         |   12 +++++
 4 files changed, 136 insertions(+), 0 deletions(-)
---
diff --git a/gitg/gitg-branch-actions.c b/gitg/gitg-branch-actions.c
index 1638b02..af32656 100644
--- a/gitg/gitg-branch-actions.c
+++ b/gitg/gitg-branch-actions.c
@@ -522,6 +522,122 @@ gitg_branch_actions_remove (GitgWindow *window,
 	return ret;
 }
 
+static GitgRunner *
+rename_branch (GitgWindow  *window,
+               GitgRef     *ref,
+               const gchar *newname)
+{
+	gchar const *oldname = gitg_ref_get_shortname (ref);
+	GitgRepository *repository = gitg_window_get_repository (window);
+
+	if (!gitg_repository_commandv (repository, NULL, "branch", "-m", oldname, newname, NULL))
+	{
+		gint ret = message_dialog (window,
+		                           GTK_MESSAGE_ERROR,
+		                           _("Branch <%s> could not be renamed to <%s>"),
+		                           _("This usually means that a branch with that name already exists. Do you want to overwrite the branch?"),
+		                           _("Force rename"),
+		                           oldname, newname);
+
+		if (ret == GTK_RESPONSE_ACCEPT)
+		{
+			if (!gitg_repository_commandv (repository, NULL, "branch", "-M", oldname, newname, NULL))
+			{
+				message_dialog (window, 
+				                GTK_MESSAGE_ERROR,
+				                _("Branch <%s> could not be forcefully renamed"),
+				                NULL,
+				                NULL,
+				                oldname);
+
+				return NULL;
+			}
+			else
+			{
+				gitg_repository_reload (repository);
+				return NULL;
+			}
+		}
+	}
+	else
+	{
+		gitg_repository_reload (repository);
+
+		return NULL;
+	}
+	
+	return NULL;
+}
+
+static gchar *
+rename_dialog (GitgWindow *window, const gchar *oldname)
+{
+	GtkWidget *dlg;
+	
+	GtkDialogFlags flags = GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT;
+	dlg = gtk_dialog_new_with_buttons ("gitg",
+                                           GTK_WINDOW (window),
+                                           flags,
+                                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                                           "_Rename", GTK_RESPONSE_OK,
+                                           NULL);
+	gtk_dialog_set_has_separator (GTK_DIALOG (dlg), FALSE);
+	gtk_dialog_set_default_response (GTK_DIALOG (dlg), GTK_RESPONSE_OK);
+
+	GtkWidget *box = gtk_hbox_new (FALSE, 6);
+	GtkWidget *label = gtk_label_new (_("Name:"));
+	GtkWidget *entry = gtk_entry_new ();
+	gtk_entry_set_text (GTK_ENTRY (entry), oldname);
+	gtk_entry_set_width_chars (GTK_ENTRY (entry), 25);
+	gtk_entry_set_activates_default (GTK_ENTRY (entry), TRUE);
+	
+	gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
+	gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 0);
+	gtk_widget_show_all (box);
+	gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), box, TRUE, TRUE, 12);
+	
+	gint ret = gtk_dialog_run (GTK_DIALOG (dlg));
+
+	gchar *newname = NULL;
+	if (ret == GTK_RESPONSE_OK)
+	{
+		const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
+		if (*text != '\0' && strcmp (text, oldname))
+		{
+			newname = g_strdup (text);
+		}
+	}
+
+	gtk_widget_destroy (dlg);
+	
+	return newname;
+}
+
+GitgRunner * 
+gitg_branch_actions_rename (GitgWindow *window,
+                            GitgRef    *ref)
+{
+	g_return_val_if_fail (GITG_IS_WINDOW (window), NULL);
+	g_return_val_if_fail (ref != NULL, NULL);
+	
+	if (gitg_ref_get_ref_type (ref) == GITG_REF_TYPE_BRANCH)
+	{
+		gchar *newname = rename_dialog (window, gitg_ref_get_shortname (ref));
+
+		if (newname)
+		{
+			GitgRef *cp = gitg_ref_copy (ref);
+			GitgRunner *ret = NULL;
+			ret = rename_branch (window, cp, newname);
+			gitg_ref_free (cp);
+			g_free (newname);
+			return ret;
+		}
+	}
+
+	return NULL;
+}
+
 static void
 reset_buffer (GitgRunner *runner, GString *buffer)
 {
diff --git a/gitg/gitg-branch-actions.h b/gitg/gitg-branch-actions.h
index 224f007..991ba28 100644
--- a/gitg/gitg-branch-actions.h
+++ b/gitg/gitg-branch-actions.h
@@ -29,6 +29,7 @@
 G_BEGIN_DECLS
 
 GitgRunner *gitg_branch_actions_remove (GitgWindow *window, GitgRef *ref);
+GitgRunner *gitg_branch_actions_rename (GitgWindow *window, GitgRef *ref);
 gboolean gitg_branch_actions_checkout (GitgWindow *window, GitgRef *ref);
 
 GitgRunner *gitg_branch_actions_merge (GitgWindow *window, GitgRef *source, GitgRef *dest);
diff --git a/gitg/gitg-menus.xml b/gitg/gitg-menus.xml
index bd7b5ba..6855c76 100644
--- a/gitg/gitg-menus.xml
+++ b/gitg/gitg-menus.xml
@@ -49,6 +49,12 @@
           </object>
         </child>
         <child>
+          <object class="GtkAction" id="RenameAction">
+            <property name="label" translatable="yes">Rename branch</property>
+            <signal name="activate" handler="on_rename_branch_action_activate"/>
+          </object>
+        </child>
+        <child>
           <object class="GtkAction" id="RebaseAction">
             <property name="label" translatable="yes">Rebase branch onto...</property>
           </object>
@@ -112,6 +118,7 @@
       <popup name="ref_popup">
         <menuitem action="CheckoutAction"/>
         <menuitem action="RemoveAction"/>
+        <menuitem action="RenameAction"/>
         <separator/>
         <menu name="Rebase" action="RebaseAction">
           <placeholder name="Placeholder"/>
diff --git a/gitg/gitg-window.c b/gitg/gitg-window.c
index 5f63e66..383c006 100644
--- a/gitg/gitg-window.c
+++ b/gitg/gitg-window.c
@@ -1765,6 +1765,7 @@ popup_ref (GitgWindow *window, GdkEventButton *event)
 
 	GtkAction *checkout = gtk_ui_manager_get_action (window->priv->menus_ui_manager, "/ui/ref_popup/CheckoutAction");
 	GtkAction *remove = gtk_ui_manager_get_action (window->priv->menus_ui_manager, "/ui/ref_popup/RemoveAction");
+	GtkAction *rename = gtk_ui_manager_get_action (window->priv->menus_ui_manager, "/ui/ref_popup/RenameAction");
 
 	if (gitg_ref_get_ref_type (ref) == GITG_REF_TYPE_REMOTE)
 	{
@@ -1785,11 +1786,14 @@ popup_ref (GitgWindow *window, GdkEventButton *event)
 		
 		g_free (local);
 		gtk_action_set_label (remove, _("Remove remote branch"));
+		gtk_action_set_visible (rename, FALSE);
 	}
 	else if (gitg_ref_get_ref_type (ref) == GITG_REF_TYPE_BRANCH)
 	{
 		gtk_action_set_label (checkout, _("Checkout working copy"));
 		gtk_action_set_label (remove, _("Remove local branch"));
+		gtk_action_set_visible (rename, TRUE);
+		gtk_action_set_label (rename, _("Rename local branch"));
 		
 		GitgRef *working = gitg_repository_get_current_working_ref (window->priv->repository);
 
@@ -1798,11 +1802,13 @@ popup_ref (GitgWindow *window, GdkEventButton *event)
 	else if (gitg_ref_get_ref_type (ref) == GITG_REF_TYPE_STASH)
 	{
 		gtk_action_set_label (remove, _("Remove stash"));
+		gtk_action_set_visible (rename, FALSE);
 		gtk_action_set_visible (checkout, FALSE);
 	}
 	else if (gitg_ref_get_ref_type (ref) == GITG_REF_TYPE_TAG)
 	{
 		gtk_action_set_label (remove, _("Remove tag"));
+		gtk_action_set_visible (rename, FALSE);
 		
 		if (!has_local_ref (window, gitg_ref_get_shortname (ref)))
 		{
@@ -1906,6 +1912,12 @@ on_remove_branch_action_activate (GtkAction *action, GitgWindow *window)
 }
 
 void
+on_rename_branch_action_activate (GtkAction *action, GitgWindow *window)
+{
+	gitg_branch_actions_rename (window, window->priv->popup_refs[0]);
+}
+
+void
 on_rebase_branch_action_activate (GtkAction *action, GitgWindow *window)
 {
 	gint source;



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