Implement bisecting
[anjuta-git-plugin.git] / plugins / git / git-status-command.c
blobdf0073ed6eca01c0e50927aec87f215ff3ce280e
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-status-command.h"
27 #define STATUS_REGEX "(modified|new file|deleted|unmerged): (.*)"
28 #define SECTION_COMMIT_REGEX "Changes to be committed:"
29 #define SECTION_NOT_UPDATED_REGEX "Changed but not updated:"
31 struct _GitStatusCommandPriv
33 GQueue *status_queue;
34 GHashTable *path_lookup_table;
35 GitStatusSections sections;
36 GitStatusSections current_section;
37 GRegex *status_regex;
38 GRegex *section_commit_regex;
39 GRegex *section_not_updated_regex;
42 G_DEFINE_TYPE (GitStatusCommand, git_status_command, GIT_TYPE_COMMAND);
44 static guint
45 git_status_command_run (AnjutaCommand *command)
47 git_command_add_arg (GIT_COMMAND (command), "status");
49 return 0;
52 static void
53 git_status_command_handle_output (GitCommand *git_command, const gchar *output)
55 GitStatusCommand *self;
56 GMatchInfo *match_info;
57 GitStatus *status_object;
58 gchar *status;
59 gchar *path;
61 self = GIT_STATUS_COMMAND (git_command);
63 /* See if the section has changed */
64 if (g_regex_match (self->priv->section_commit_regex, output, 0, NULL))
66 self->priv->current_section = GIT_STATUS_SECTION_COMMIT;
68 else if (g_regex_match (self->priv->section_not_updated_regex, output, 0,
69 NULL))
71 self->priv->current_section = GIT_STATUS_SECTION_NOT_UPDATED;
74 if (self->priv->sections & self->priv->current_section)
76 if (g_regex_match (self->priv->status_regex, output, 0, &match_info))
78 /* Git sometimes mentions paths twice in status output. This can
79 * happen, for example, where there is a conflict, in which case a
80 * path would show up as both "unmerged" and "modified." */
82 status = g_match_info_fetch (match_info, 1);
83 path = g_match_info_fetch (match_info, 2);
84 g_strchug (path);
86 if (!g_hash_table_lookup_extended (self->priv->path_lookup_table,
87 path, NULL, NULL))
89 status_object = git_status_new (path, status);
90 g_queue_push_tail (self->priv->status_queue, status_object);
91 g_hash_table_insert (self->priv->path_lookup_table,
92 g_strdup (path), NULL);
93 anjuta_command_notify_data_arrived (ANJUTA_COMMAND (git_command));
96 g_free (status);
97 g_free (path);
98 g_match_info_free (match_info);
103 static void
104 git_status_command_init (GitStatusCommand *self)
106 self->priv = g_new0 (GitStatusCommandPriv, 1);
107 self->priv->status_queue = g_queue_new ();
108 self->priv->path_lookup_table = g_hash_table_new_full (g_str_hash,
109 g_str_equal,
110 g_free, NULL);
111 self->priv->status_regex = g_regex_new (STATUS_REGEX, 0, 0, NULL);
112 self->priv->section_commit_regex = g_regex_new (SECTION_COMMIT_REGEX, 0, 0,
113 NULL);
114 self->priv->section_not_updated_regex = g_regex_new (SECTION_NOT_UPDATED_REGEX,
115 0, 0, NULL);
118 static void
119 git_status_command_finalize (GObject *object)
121 GitStatusCommand *self;
122 GList *current_status;
124 self = GIT_STATUS_COMMAND (object);
125 current_status = self->priv->status_queue->head;
127 while (current_status)
129 g_object_unref (current_status->data);
130 current_status = g_list_next (current_status);
133 g_queue_free (self->priv->status_queue);
134 g_hash_table_destroy (self->priv->path_lookup_table);
135 g_regex_unref (self->priv->status_regex);
136 g_regex_unref (self->priv->section_commit_regex);
137 g_regex_unref (self->priv->section_not_updated_regex);
139 g_free (self->priv);
141 G_OBJECT_CLASS (git_status_command_parent_class)->finalize (object);
144 static void
145 git_status_command_class_init (GitStatusCommandClass *klass)
147 GObjectClass* object_class = G_OBJECT_CLASS (klass);
148 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
149 AnjutaCommandClass* command_class = ANJUTA_COMMAND_CLASS (klass);
151 object_class->finalize = git_status_command_finalize;
152 parent_class->output_handler = git_status_command_handle_output;
153 command_class->run = git_status_command_run;
157 GitStatusCommand *
158 git_status_command_new (const gchar *working_directory,
159 GitStatusSections sections)
161 GitStatusCommand *self;
163 self = g_object_new (GIT_TYPE_STATUS_COMMAND,
164 "working-directory", working_directory,
165 "single-line-output", TRUE,
166 NULL);
168 self->priv->sections = sections;
170 return self;
173 GQueue *
174 git_status_command_get_status_queue (GitStatusCommand *self)
176 return self->priv->status_queue;