From 4998b8fa71d74503c3be942eebd42767f6a432bf Mon Sep 17 00:00:00 2001 From: James Liggett Date: Sat, 26 Apr 2008 00:29:40 -0700 Subject: [PATCH] Implement branch switching. --- TODO.tasks | 10 +-- plugins/git/Makefile.am | 6 +- plugins/git/anjuta-git.glade | 80 +++++++++++++++++- plugins/git/anjuta-git.ui | 1 + plugins/git/git-branch-checkout-command.c | 99 +++++++++++++++++++++++ plugins/git/git-branch-checkout-command.h | 63 +++++++++++++++ plugins/git/git-switch-dialog.c | 130 ++++++++++++++++++++++++++++++ plugins/git/git-switch-dialog.h | 35 ++++++++ plugins/git/plugin.c | 9 +++ 9 files changed, 426 insertions(+), 7 deletions(-) create mode 100644 plugins/git/git-branch-checkout-command.c create mode 100644 plugins/git/git-branch-checkout-command.h create mode 100644 plugins/git/git-switch-dialog.c create mode 100644 plugins/git/git-switch-dialog.h diff --git a/TODO.tasks b/TODO.tasks index 50977fd1..ab7d852c 100644 --- a/TODO.tasks +++ b/TODO.tasks @@ -658,11 +658,6 @@ Fix c++/gobject class generator to allow adding members, methods, signals, prope - - List branches and switch to selected one - - - Remote branch handling @@ -740,6 +735,11 @@ Fix c++/gobject class generator to allow adding members, methods, signals, prope Merging branches + + + + + List branches and switch to selected one diff --git a/plugins/git/Makefile.am b/plugins/git/Makefile.am index 0d47a50e..0f0b878a 100644 --- a/plugins/git/Makefile.am +++ b/plugins/git/Makefile.am @@ -79,7 +79,11 @@ libanjuta_git_la_SOURCES = \ git-branch-combo-model.c \ git-branch-combo-model.h \ git-merge-dialog.h \ - git-merge-dialog.c + git-merge-dialog.c \ + git-branch-checkout-command.h \ + git-branch-checkout-command.c \ + git-switch-dialog.c \ + git-switch-dialog.h libanjuta_git_la_LDFLAGS = $(ANJUTA_PLUGIN_LDFLAGS) diff --git a/plugins/git/anjuta-git.glade b/plugins/git/anjuta-git.glade index bbedbc15..4e794b05 100644 --- a/plugins/git/anjuta-git.glade +++ b/plugins/git/anjuta-git.glade @@ -1,6 +1,6 @@ - + @@ -914,4 +914,82 @@ + + 5 + Switch to Another Branch + GTK_WIN_POS_CENTER_ON_PARENT + GDK_WINDOW_TYPE_HINT_DIALOG + False + + + True + 2 + + + True + 0 + GTK_SHADOW_NONE + + + True + 12 + + + True + + + + + + + True + <b>Branch to switch to:</b> + True + + + label_item + + + + + False + 1 + + + + + True + GTK_BUTTONBOX_END + + + True + True + True + gtk-cancel + True + -6 + + + + + True + True + True + gtk-ok + True + -5 + + + 1 + + + + + False + GTK_PACK_END + + + + + diff --git a/plugins/git/anjuta-git.ui b/plugins/git/anjuta-git.ui index dc5375be..f54c3958 100644 --- a/plugins/git/anjuta-git.ui +++ b/plugins/git/anjuta-git.ui @@ -10,6 +10,7 @@ + diff --git a/plugins/git/git-branch-checkout-command.c b/plugins/git/git-branch-checkout-command.c new file mode 100644 index 00000000..af32d83d --- /dev/null +++ b/plugins/git/git-branch-checkout-command.c @@ -0,0 +1,99 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ +/* + * anjuta-git + * Copyright (C) James Liggett 2008 + * + * anjuta-git is free software. + * + * You may 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. + * + * anjuta-git 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 anjuta-git. If not, write to: + * The Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301, USA. + */ + +#include "git-branch-checkout-command.h" + +struct _GitBranchCheckoutCommandPriv +{ + gchar *branch_name; + gboolean create; +}; + +G_DEFINE_TYPE (GitBranchCheckoutCommand, git_branch_checkout_command, GIT_TYPE_COMMAND); + +static void +git_branch_checkout_command_init (GitBranchCheckoutCommand *self) +{ + self->priv = g_new0 (GitBranchCheckoutCommandPriv, 1); +} + +static void +git_branch_checkout_command_finalize (GObject *object) +{ + GitBranchCheckoutCommand *self; + + self = GIT_BRANCH_CHECKOUT_COMMAND (object); + + g_free (self->priv->branch_name); + g_free (self->priv); + + G_OBJECT_CLASS (git_branch_checkout_command_parent_class)->finalize (object); +} + +static guint +git_branch_checkout_command_run (AnjutaCommand *command) +{ + GitBranchCheckoutCommand *self; + + self = GIT_BRANCH_CHECKOUT_COMMAND (command); + + git_command_add_arg (GIT_COMMAND (command), "checkout"); + + if (self->priv->create) + git_command_add_arg (GIT_COMMAND (command), "-b"); + + git_command_add_arg (GIT_COMMAND (command), self->priv->branch_name); + + return 0; +} + +static void +git_branch_checkout_command_class_init (GitBranchCheckoutCommandClass *klass) +{ + GObjectClass* object_class = G_OBJECT_CLASS (klass); + GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass); + AnjutaCommandClass* command_class = ANJUTA_COMMAND_CLASS (klass); + + object_class->finalize = git_branch_checkout_command_finalize; + parent_class->output_handler = git_command_send_output_to_info; + command_class->run = git_branch_checkout_command_run; +} + + +GitBranchCheckoutCommand * +git_branch_checkout_command_new (const gchar *working_directory, + const gchar *branch_name, gboolean create) +{ + GitBranchCheckoutCommand *self; + + self = g_object_new (GIT_TYPE_BRANCH_CHECKOUT_COMMAND, + "working-directory", working_directory, + "single-line-output", TRUE, + NULL); + + self->priv->branch_name = g_strdup (branch_name); + self->priv->create = create; + + return self; +} diff --git a/plugins/git/git-branch-checkout-command.h b/plugins/git/git-branch-checkout-command.h new file mode 100644 index 00000000..c46afcba --- /dev/null +++ b/plugins/git/git-branch-checkout-command.h @@ -0,0 +1,63 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ +/* + * anjuta-git + * Copyright (C) James Liggett 2008 + * + * anjuta-git is free software. + * + * You may 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. + * + * anjuta-git 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 anjuta-git. If not, write to: + * The Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301, USA. + */ + +#ifndef _GIT_BRANCH_CHECKOUT_COMMAND_H_ +#define _GIT_BRANCH_CHECKOUT_COMMAND_H_ + +#include +#include "git-command.h" + +G_BEGIN_DECLS + +#define GIT_TYPE_BRANCH_CHECKOUT_COMMAND (git_branch_checkout_command_get_type ()) +#define GIT_BRANCH_CHECKOUT_COMMAND(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIT_TYPE_BRANCH_CHECKOUT_COMMAND, GitBranchCheckoutCommand)) +#define GIT_BRANCH_CHECKOUT_COMMAND_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIT_TYPE_BRANCH_CHECKOUT_COMMAND, GitBranchCheckoutCommandClass)) +#define GIT_IS_BRANCH_CHECKOUT_COMMAND(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIT_TYPE_BRANCH_CHECKOUT_COMMAND)) +#define GIT_IS_BRANCH_CHECKOUT_COMMAND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIT_TYPE_BRANCH_CHECKOUT_COMMAND)) +#define GIT_BRANCH_CHECKOUT_COMMAND_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIT_TYPE_BRANCH_CHECKOUT_COMMAND, GitBranchCheckoutCommandClass)) + +typedef struct _GitBranchCheckoutCommandClass GitBranchCheckoutCommandClass; +typedef struct _GitBranchCheckoutCommand GitBranchCheckoutCommand; +typedef struct _GitBranchCheckoutCommandPriv GitBranchCheckoutCommandPriv; + +struct _GitBranchCheckoutCommandClass +{ + GitCommandClass parent_class; +}; + +struct _GitBranchCheckoutCommand +{ + GitCommand parent_instance; + + GitBranchCheckoutCommandPriv *priv; +}; + +GType git_branch_checkout_command_get_type (void) G_GNUC_CONST; +GitBranchCheckoutCommand *git_branch_checkout_command_new (const gchar *working_directory, + const gchar *branch_name, + gboolean create); + +G_END_DECLS + +#endif /* _GIT_BRANCH_CHECKOUT_COMMAND_H_ */ diff --git a/plugins/git/git-switch-dialog.c b/plugins/git/git-switch-dialog.c new file mode 100644 index 00000000..d5a1d81d --- /dev/null +++ b/plugins/git/git-switch-dialog.c @@ -0,0 +1,130 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ +/* + * anjuta + * Copyright (C) James Liggett 2008 + * + * anjuta is free software. + * + * You may 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. + * + * anjuta 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 anjuta. If not, write to: + * The Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301, USA. + */ + +#include "git-switch-dialog.h" + +static void +on_checkout_command_finished (AnjutaCommand *command, guint return_code, + Git *plugin) +{ + AnjutaStatus *status; + + status = anjuta_shell_get_status (ANJUTA_PLUGIN (plugin)->shell, + NULL); + + anjuta_status (status, _("Git: Branch checkout complete."), 5); + + report_errors (command, return_code); + + g_object_unref (command); +} + + +static void +on_switch_dialog_response (GtkDialog *dialog, gint response_id, + GitBranchComboData *data) +{ + GtkWidget *branch_combo; + gchar *branch; + GtkTreeIter iter; + GitBranchCheckoutCommand *checkout_command; + + if (response_id == GTK_RESPONSE_OK) + { + branch_combo = glade_xml_get_widget (data->gxml, "branch_combo"); + + gtk_combo_box_get_active_iter (GTK_COMBO_BOX (branch_combo), &iter); + branch = git_branch_combo_model_get_branch (data->model, &iter); + + checkout_command = git_branch_checkout_command_new (data->plugin->project_root_directory, + branch, FALSE); + + g_free (branch); + + create_message_view (data->plugin); + + g_signal_connect (G_OBJECT (checkout_command), "command-finished", + G_CALLBACK (on_checkout_command_finished), + data->plugin); + + g_signal_connect (G_OBJECT (checkout_command), "data-arrived", + G_CALLBACK (on_command_info_arrived), + data->plugin); + + anjuta_command_start (ANJUTA_COMMAND (checkout_command)); + } + + gtk_widget_destroy (GTK_WIDGET (dialog)); + git_branch_combo_data_free (data); +} + +static void +switch_dialog (Git *plugin) +{ + GladeXML *gxml; + GtkWidget *dialog; + GtkWidget *branch_combo; + GtkListStore *branch_list_store; + GitBranchComboData *data; + GitBranchListCommand *list_command; + + gxml = glade_xml_new (GLADE_FILE, "switch_dialog", NULL); + + dialog = glade_xml_get_widget (gxml, "switch_dialog"); + branch_combo = glade_xml_get_widget (gxml, "branch_combo"); + branch_list_store = git_branch_combo_model_new (); + + gtk_combo_box_set_model (GTK_COMBO_BOX (branch_combo), + GTK_TREE_MODEL (branch_list_store)); + git_branch_combo_model_setup_widget (branch_combo); + + data = git_branch_combo_data_new (branch_list_store, + GTK_COMBO_BOX (branch_combo), gxml, + plugin); + + list_command = git_branch_list_command_new (plugin->project_root_directory, + GIT_BRANCH_TYPE_LOCAL); + + g_signal_connect (G_OBJECT (list_command), "data-arrived", + G_CALLBACK (on_list_branch_command_data_arrived), + data); + + g_signal_connect (G_OBJECT (list_command), "command-finished", + G_CALLBACK (on_list_branch_command_finished), + data); + + anjuta_command_start (ANJUTA_COMMAND (list_command)); + + g_signal_connect (G_OBJECT (dialog), "response", + G_CALLBACK (on_switch_dialog_response), + data); + + gtk_widget_show_all (dialog); +} + +void +on_menu_git_switch (GtkAction *action, Git *plugin) +{ + switch_dialog (plugin); +} diff --git a/plugins/git/git-switch-dialog.h b/plugins/git/git-switch-dialog.h new file mode 100644 index 00000000..b1101f4c --- /dev/null +++ b/plugins/git/git-switch-dialog.h @@ -0,0 +1,35 @@ +/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ +/* + * anjuta + * Copyright (C) James Liggett 2008 + * + * anjuta is free software. + * + * You may 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. + * + * anjuta 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 anjuta. If not, write to: + * The Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301, USA. + */ + +#ifndef _GIT_SWITCH_DIALOG_H +#define _GIT_SWITCH_DIALOG_H + +#include "git-branch-list-command.h" +#include "git-branch-checkout-command.h" +#include "git-ui-utils.h" +#include "git-branch-combo-model.h" + +void on_menu_git_switch (GtkAction *action, Git *plugin); + +#endif diff --git a/plugins/git/plugin.c b/plugins/git/plugin.c index aeb3f1ea..77592dd6 100644 --- a/plugins/git/plugin.c +++ b/plugins/git/plugin.c @@ -26,6 +26,7 @@ #include "git-remove-dialog.h" #include "git-resolve-dialog.h" #include "git-merge-dialog.h" +#include "git-switch-dialog.h" #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-git.ui" @@ -73,6 +74,14 @@ static GtkActionEntry actions_git[] = { G_CALLBACK (on_menu_git_remove) /* action callback */ }, { + "ActionGitSwitch", /* Action name */ + GTK_STOCK_JUMP_TO, /* Stock icon, if any */ + N_("_Switch to another branch..."), /* Display label */ + NULL, /* short-cut */ + NULL, /* Tooltip */ + G_CALLBACK (on_menu_git_switch) /* action callback */ + }, + { "ActionGitMerge", /* Action name */ GTK_STOCK_CONVERT, /* Stock icon, if any */ N_("_Merge..."), /* Display label */ -- 2.11.4.GIT