Copyright header fixes...
[anjuta-git-plugin.git] / plugins / git / git-branch-list-command.c
blobc2a0f58624bf26ead48c8862201eee6d97c68f2b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5 *
6 * anjuta is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * anjuta is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with anjuta. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include "git-branch-list-command.h"
27 /* Detects the currently active branch in the output */
28 #define ACTIVE_BRANCH_REGEX "^\\* (.*)"
30 /* Other branches. Just strips off the space and newlines for us :) */
31 #define REGULAR_BRANCH_REGEX "^(?:\\s) (.*)"
33 struct _GitBranchListCommandPriv
35 GitBranchType type;
36 GRegex *active_branch_regex;
37 GRegex *regular_branch_regex;
38 GQueue *output;
41 G_DEFINE_TYPE (GitBranchListCommand, git_branch_list_command, GIT_TYPE_COMMAND);
43 static void
44 git_branch_list_command_init (GitBranchListCommand *self)
46 self->priv = g_new0 (GitBranchListCommandPriv, 1);
47 self->priv->active_branch_regex = g_regex_new (ACTIVE_BRANCH_REGEX, 0, 0,
48 NULL);
49 self->priv->regular_branch_regex = g_regex_new (REGULAR_BRANCH_REGEX, 0, 0,
50 NULL);
51 self->priv->output = g_queue_new ();
54 static void
55 git_branch_list_command_finalize (GObject *object)
57 GitBranchListCommand *self;
58 GList *current_branch;
60 self = GIT_BRANCH_LIST_COMMAND (object);
61 current_branch = self->priv->output->head;
63 g_regex_unref (self->priv->active_branch_regex);
64 g_regex_unref (self->priv->regular_branch_regex);
66 while (current_branch)
68 g_object_unref (current_branch->data);
69 current_branch = g_list_next (current_branch);
72 g_queue_free (self->priv->output);
73 g_free (self->priv);
75 G_OBJECT_CLASS (git_branch_list_command_parent_class)->finalize (object);
78 static guint
79 git_branch_list_command_run (AnjutaCommand *command)
81 GitBranchListCommand *self;
83 self = GIT_BRANCH_LIST_COMMAND (command);
85 git_command_add_arg (GIT_COMMAND (command), "branch");
87 switch (self->priv->type)
89 case GIT_BRANCH_TYPE_REMOTE:
90 git_command_add_arg (GIT_COMMAND (command), "-r");
91 break;
92 case GIT_BRANCH_TYPE_ALL:
93 git_command_add_arg (GIT_COMMAND (command), "-a");
94 break;
95 default:
96 break;
99 return 0;
102 static void
103 git_branch_list_command_handle_output (GitCommand *git_command,
104 const gchar *output)
106 GitBranchListCommand *self;
107 GMatchInfo *match_info;
108 gchar *branch_name;
109 GitBranch *branch;
110 gboolean active;
112 match_info = NULL;
113 branch_name = NULL;
114 branch = NULL;
115 active = FALSE;
117 self = GIT_BRANCH_LIST_COMMAND (git_command);
119 if (g_regex_match (self->priv->active_branch_regex, output, 0, &match_info))
121 branch_name = g_match_info_fetch (match_info, 1);
122 active = TRUE;
124 else if (g_regex_match (self->priv->regular_branch_regex, output, 0,
125 &match_info))
127 branch_name = g_match_info_fetch (match_info, 1);
130 if (branch_name)
131 branch = git_branch_new (branch_name, active);
133 g_free (branch_name);
134 g_match_info_free (match_info);
136 g_queue_push_head (self->priv->output, branch);
137 anjuta_command_notify_data_arrived (ANJUTA_COMMAND (git_command));
141 static void
142 git_branch_list_command_class_init (GitBranchListCommandClass *klass)
144 GObjectClass* object_class = G_OBJECT_CLASS (klass);
145 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
146 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
148 object_class->finalize = git_branch_list_command_finalize;
149 parent_class->output_handler = git_branch_list_command_handle_output;
150 command_class->run = git_branch_list_command_run;
154 GitBranchListCommand *
155 git_branch_list_command_new (const gchar *working_directory,
156 GitBranchType type)
158 GitBranchListCommand *self;
160 self = g_object_new (GIT_TYPE_BRANCH_LIST_COMMAND,
161 "working-directory", working_directory,
162 "single-line-output", TRUE,
163 NULL);
165 self->priv->type = type;
167 return self;
170 GQueue *
171 git_branch_list_command_get_output (GitBranchListCommand *self)
173 return self->priv->output;