1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
6 * anjuta is free software.
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)
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
36 GRegex
*active_branch_regex
;
37 GRegex
*regular_branch_regex
;
41 G_DEFINE_TYPE (GitBranchListCommand
, git_branch_list_command
, GIT_TYPE_COMMAND
);
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,
49 self
->priv
->regular_branch_regex
= g_regex_new (REGULAR_BRANCH_REGEX
, 0, 0,
51 self
->priv
->output
= g_queue_new ();
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
);
75 G_OBJECT_CLASS (git_branch_list_command_parent_class
)->finalize (object
);
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");
92 case GIT_BRANCH_TYPE_ALL
:
93 git_command_add_arg (GIT_COMMAND (command
), "-a");
103 git_branch_list_command_handle_output (GitCommand
*git_command
,
106 GitBranchListCommand
*self
;
107 GMatchInfo
*match_info
;
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);
124 else if (g_regex_match (self
->priv
->regular_branch_regex
, output
, 0,
127 branch_name
= g_match_info_fetch (match_info
, 1);
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
));
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
,
158 GitBranchListCommand
*self
;
160 self
= g_object_new (GIT_TYPE_BRANCH_LIST_COMMAND
,
161 "working-directory", working_directory
,
162 "single-line-output", TRUE
,
165 self
->priv
->type
= type
;
171 git_branch_list_command_get_output (GitBranchListCommand
*self
)
173 return self
->priv
->output
;