debug-manager: use g_spawn_sync() instead of fork() and waitpid()
[anjuta.git] / plugins / git / git-stash-list-command.c
blobf5ec0265fad4529f255f1fa6a912db3f5742b4ac
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-stash-list-command.h"
27 /* Splits the stash ID from the stash message */
28 #define STASH_REGEX "(stash@\\{(\\d+)\\})(?:\\:) (.*)"
30 struct _GitStashListCommandPriv
32 GRegex *stash_regex;
33 GQueue *output;
34 GFileMonitor *file_monitor;
37 G_DEFINE_TYPE (GitStashListCommand, git_stash_list_command, GIT_TYPE_COMMAND);
39 static void
40 git_stash_list_command_init (GitStashListCommand *self)
42 self->priv = g_new0 (GitStashListCommandPriv, 1);
43 self->priv->stash_regex = g_regex_new (STASH_REGEX, 0, 0, NULL);
45 self->priv->output = g_queue_new ();
48 static void
49 git_stash_list_command_finalize (GObject *object)
51 GitStashListCommand *self;
52 GList *current_stash;
54 self = GIT_STASH_LIST_COMMAND (object);
55 current_stash = self->priv->output->head;
57 g_regex_unref (self->priv->stash_regex);
59 while (current_stash)
61 g_object_unref (current_stash->data);
62 current_stash = g_list_next (current_stash);
65 g_queue_free (self->priv->output);
66 anjuta_command_stop_automatic_monitor (ANJUTA_COMMAND (object));
67 g_free (self->priv);
69 G_OBJECT_CLASS (git_stash_list_command_parent_class)->finalize (object);
72 static guint
73 git_stash_list_command_run (AnjutaCommand *command)
75 GitStashListCommand *self;
77 self = GIT_STASH_LIST_COMMAND (command);
79 git_command_add_arg (GIT_COMMAND (command), "stash");
80 git_command_add_arg (GIT_COMMAND (command), "list");
81 return 0;
84 static void
85 git_stash_list_command_handle_output (GitCommand *git_command,
86 const gchar *output)
88 GitStashListCommand *self;
89 GMatchInfo *match_info;
90 gchar *stash_id;
91 gchar *stash_number;
92 gchar *stash_message;
93 GitStash *stash;
95 self = GIT_STASH_LIST_COMMAND (git_command);
97 match_info = NULL;
98 stash_id = NULL;
99 stash_message = NULL;
100 stash = NULL;
102 if (g_regex_match (self->priv->stash_regex, output, 0, &match_info))
104 stash_id = g_match_info_fetch (match_info, 1);
105 stash_number = g_match_info_fetch (match_info, 2);
106 stash_message = g_match_info_fetch (match_info, 3);
108 stash = git_stash_new (stash_id, stash_message, atoi (stash_number));
110 g_free (stash_id);
111 g_free (stash_number);
112 g_free (stash_message);
114 g_queue_push_head (self->priv->output, stash);
115 anjuta_command_notify_data_arrived (ANJUTA_COMMAND (git_command));
118 if (match_info)
119 g_match_info_free (match_info);
122 static void
123 on_file_monitor_changed (GFileMonitor *file_monitor, GFile *file,
124 GFile *other_file, GFileMonitorEvent event,
125 AnjutaCommand *command)
127 if (event == G_FILE_MONITOR_EVENT_CREATED ||
128 event == G_FILE_MONITOR_EVENT_DELETED)
130 anjuta_command_start (command);
134 static gboolean
135 git_stash_list_command_start_automatic_monitor (AnjutaCommand *command)
137 GitStashListCommand *self;
138 gchar *working_directory;
139 gchar *git_stash_path;
140 GFile *git_stash_file;
142 self = GIT_STASH_LIST_COMMAND (command);
144 g_object_get (G_OBJECT (self), "working-directory", &working_directory,
145 NULL);
147 git_stash_path = g_strjoin (G_DIR_SEPARATOR_S,
148 working_directory,
149 ".git",
150 "refs",
151 "stash",
152 NULL);
154 git_stash_file = g_file_new_for_path (git_stash_path);
156 self->priv->file_monitor = g_file_monitor_file (git_stash_file, 0, NULL,
157 NULL);
159 g_signal_connect (G_OBJECT (self->priv->file_monitor), "changed",
160 G_CALLBACK (on_file_monitor_changed),
161 command);
163 g_free (working_directory);
164 g_free (git_stash_path);
165 g_object_unref (git_stash_file);
167 return TRUE;
170 static void
171 git_stash_list_command_stop_automatic_monitor (AnjutaCommand *command)
173 GitStashListCommand *self;
175 self = GIT_STASH_LIST_COMMAND (command);
177 if (self->priv->file_monitor)
179 g_file_monitor_cancel (self->priv->file_monitor);
180 g_object_unref (self->priv->file_monitor);
181 self->priv->file_monitor = NULL;
185 static void
186 git_stash_list_command_class_init (GitStashListCommandClass *klass)
188 GObjectClass* object_class = G_OBJECT_CLASS (klass);
189 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
190 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
192 object_class->finalize = git_stash_list_command_finalize;
193 parent_class->output_handler = git_stash_list_command_handle_output;
194 command_class->run = git_stash_list_command_run;
195 command_class->start_automatic_monitor = git_stash_list_command_start_automatic_monitor;
196 command_class->stop_automatic_monitor = git_stash_list_command_stop_automatic_monitor;
200 GitStashListCommand *
201 git_stash_list_command_new (const gchar *working_directory)
203 GitStashListCommand *self;
205 self = g_object_new (GIT_TYPE_STASH_LIST_COMMAND,
206 "working-directory", working_directory,
207 "single-line-output", TRUE,
208 NULL);
210 return self;
213 GQueue *
214 git_stash_list_command_get_output (GitStashListCommand *self)
216 return self->priv->output;