debug-manager: use g_spawn_sync() instead of fork() and waitpid()
[anjuta.git] / plugins / git / git-log-command.c
blobc93794020f4a6faf41acecc3e10ad706578171d2
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-log-command.h"
27 struct _GitLogCommandPriv
29 GitLogDataCommand *data_command;
30 guint return_code;
32 gchar *branch;
33 gchar *path;
35 /* Filters */
36 gchar *author;
37 gchar *grep;
38 gchar *since_date;
39 gchar *until_date;
40 gchar *since_commit;
41 gchar *until_commit;
44 G_DEFINE_TYPE (GitLogCommand, git_log_command, GIT_TYPE_COMMAND);
46 static void
47 on_data_command_data_arrived (AnjutaCommand *command, GitLogCommand *self)
49 anjuta_command_notify_data_arrived (command);
52 static void
53 on_data_command_finished (AnjutaCommand *command, guint return_code,
54 GitLogCommand *self)
56 ANJUTA_COMMAND_CLASS (git_log_command_parent_class)->notify_complete (ANJUTA_COMMAND (self),
57 self->priv->return_code);
60 static void
61 git_log_command_init (GitLogCommand *self)
63 self->priv = g_new0 (GitLogCommandPriv, 1);
65 self->priv->data_command = git_log_data_command_new ();
67 g_signal_connect (G_OBJECT (self->priv->data_command), "data-arrived",
68 G_CALLBACK (on_data_command_data_arrived),
69 self);
71 g_signal_connect (G_OBJECT (self->priv->data_command), "command-finished",
72 G_CALLBACK (on_data_command_finished),
73 self);
76 static void
77 git_log_command_finalize (GObject *object)
79 GitLogCommand *self;
81 self = GIT_LOG_COMMAND (object);
83 g_object_unref (self->priv->data_command);
84 g_free (self->priv->author);
85 g_free (self->priv->grep);
86 g_free (self->priv->since_date);
87 g_free (self->priv->until_date);
88 g_free (self->priv->since_commit);
89 g_free (self->priv->until_commit);
90 g_free (self->priv);
92 G_OBJECT_CLASS (git_log_command_parent_class)->finalize (object);
95 static guint
96 git_log_command_run (AnjutaCommand *command)
98 GitLogCommand *self;
99 gchar *filter_arg;
100 GString *commit_range;
102 self = GIT_LOG_COMMAND (command);
104 git_command_add_arg (GIT_COMMAND (command), "rev-list");
105 git_command_add_arg (GIT_COMMAND (command), "--topo-order");
106 git_command_add_arg (GIT_COMMAND (command), "--pretty=format:parents %P%n"
107 "author %an%n"
108 "time %at%n"
109 "short log %s%n"
110 "\x0c");
112 if (self->priv->author)
114 filter_arg = g_strdup_printf ("--author=%s", self->priv->author);
115 git_command_add_arg (GIT_COMMAND (command), filter_arg);
116 g_free (filter_arg);
119 if (self->priv->grep)
121 filter_arg = g_strdup_printf ("--grep=%s", self->priv->grep);
122 git_command_add_arg (GIT_COMMAND (command), filter_arg);
123 g_free (filter_arg);
126 if (self->priv->since_date)
128 filter_arg = g_strdup_printf ("--since=%s", self->priv->since_date);
129 git_command_add_arg (GIT_COMMAND (command), filter_arg);
130 g_free (filter_arg);
133 if (self->priv->until_date)
135 filter_arg = g_strdup_printf ("--until=%s", self->priv->until_date);
136 git_command_add_arg (GIT_COMMAND (command), filter_arg);
137 g_free (filter_arg);
140 if (self->priv->since_commit || self->priv->until_commit)
142 commit_range = g_string_new ("");
144 /* Not the most elegant way of doing it... */
145 if (self->priv->since_commit)
146 g_string_append (commit_range, self->priv->since_commit);
148 g_string_append (commit_range, "..");
150 if (self->priv->until_commit)
151 g_string_append (commit_range, self->priv->until_commit);
153 git_command_add_arg (GIT_COMMAND (command), commit_range->str);
155 g_string_free (commit_range, TRUE);
158 if (self->priv->branch)
159 git_command_add_arg (GIT_COMMAND (command), self->priv->branch);
160 else
161 git_command_add_arg (GIT_COMMAND (command), "HEAD");
163 if (self->priv->path)
165 git_command_add_arg (GIT_COMMAND (command), "--");
166 git_command_add_arg (GIT_COMMAND (command), self->priv->path);
169 /* Start the data processing task */
170 anjuta_command_start (ANJUTA_COMMAND (self->priv->data_command));
172 return 0;
175 static void
176 git_log_command_notify_complete (AnjutaCommand *command, guint return_code)
178 GitLogCommand *self;
180 self = GIT_LOG_COMMAND (command);
182 /* Send an empty string to the data processing command so that it knows
183 * to stop when it's done processing data. The command will finish when
184 * the processing thread finishes, and not when git stops executing */
185 git_log_data_command_push_line (self->priv->data_command, "");
187 /* Use the git return code */
188 self->priv->return_code = return_code;
191 static void
192 git_log_command_handle_output (GitCommand *git_command, const gchar *output)
194 GitLogCommand *self;
196 self = GIT_LOG_COMMAND (git_command);
198 git_log_data_command_push_line (self->priv->data_command, output);
201 static void
202 git_log_command_class_init (GitLogCommandClass *klass)
204 GObjectClass* object_class = G_OBJECT_CLASS (klass);
205 GitCommandClass* parent_class = GIT_COMMAND_CLASS (klass);
206 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
208 object_class->finalize = git_log_command_finalize;
209 parent_class->output_handler = git_log_command_handle_output;
210 command_class->run = git_log_command_run;
211 command_class->notify_complete = git_log_command_notify_complete;
215 GitLogCommand *
216 git_log_command_new (const gchar *working_directory,
217 const gchar *branch, const gchar *path,
218 const gchar *author, const gchar *grep,
219 const gchar *since_date, const gchar *until_date,
220 const gchar *since_commit,
221 const gchar *until_commit)
223 GitLogCommand *self;
225 self = g_object_new (GIT_TYPE_LOG_COMMAND,
226 "working-directory", working_directory,
227 "single-line-output", TRUE,
228 NULL);
230 self->priv->author = g_strdup (author);
231 self->priv->path = g_strdup (path);
232 self->priv->branch = g_strdup (branch);
233 self->priv->grep = g_strdup (grep);
234 self->priv->since_date = g_strdup (since_date);
235 self->priv->until_date = g_strdup (until_date);
236 self->priv->since_commit = g_strdup (since_commit);
237 self->priv->until_commit = g_strdup (until_commit);
239 return self;
242 GQueue *
243 git_log_command_get_output_queue (GitLogCommand *self)
245 return git_log_data_command_get_output (self->priv->data_command);