Add progress handling to GitCommand
[anjuta-git-plugin.git] / plugins / subversion / svn-diff-command.c
blob788c45b25135d699c7f821bad746200d1439b812
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
6 * Portions based on the original Subversion plugin
7 * Copyright (C) Johannes Schmid 2005
8 *
9 * anjuta is free software.
11 * You may redistribute it and/or modify it under the terms of the
12 * GNU General Public License, as published by the Free Software
13 * Foundation; either version 2 of the License, or (at your option)
14 * any later version.
16 * anjuta is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with anjuta. If not, write to:
23 * The Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor
25 * Boston, MA 02110-1301, USA.
28 #include "svn-diff-command.h"
30 struct _SvnDiffCommandPriv
32 GQueue *output;
33 gchar *path;
34 glong revision1;
35 glong revision2;
36 gboolean recursive;
39 G_DEFINE_TYPE (SvnDiffCommand, svn_diff_command, SVN_TYPE_COMMAND);
41 static void
42 svn_diff_command_init (SvnDiffCommand *self)
44 self->priv = g_new0 (SvnDiffCommandPriv, 1);
45 self->priv->output = g_queue_new ();
48 static void
49 svn_diff_command_finalize (GObject *object)
51 SvnDiffCommand *self;
52 GList *current_line;
54 self = SVN_DIFF_COMMAND (object);
56 current_line = self->priv->output->head;
58 while (current_line)
60 g_free (current_line->data);
61 current_line = g_list_next (current_line);
64 g_queue_free (self->priv->output);
65 g_free (self->priv->path);
66 g_free (self->priv);
68 G_OBJECT_CLASS (svn_diff_command_parent_class)->finalize (object);
71 static guint
72 svn_diff_command_run (AnjutaCommand *command)
74 SvnDiffCommand *self;
75 SvnCommand *svn_command;
76 svn_opt_revision_t revision1;
77 svn_opt_revision_t revision2;
78 apr_array_header_t *options;
79 apr_file_t *diff_file;
80 gchar file_template[] = "anjuta-svn-diffXXXXXX";
81 apr_size_t read_size;
82 gchar *line;
83 svn_error_t *error;
84 apr_status_t apr_error;
85 apr_off_t offset;
87 self = SVN_DIFF_COMMAND (command);
88 svn_command = SVN_COMMAND (self);
90 switch (self->priv->revision1)
92 case SVN_DIFF_REVISION_NONE:
93 /* Treat this as a diff between working copy and base
94 * (show only mods made to the revision of this working copy.) */
95 revision1.kind = svn_opt_revision_base;
96 revision2.kind = svn_opt_revision_working;
97 break;
98 case SVN_DIFF_REVISION_PREVIOUS:
99 /* Diff between selected revision and the one before it.
100 * This is relative to revision2. */
101 revision1.kind = svn_opt_revision_number;
102 revision1.value.number = self->priv->revision2 - 1;
103 revision2.kind = svn_opt_revision_number;
104 revision2.value.number = self->priv->revision2;
105 break;
106 default:
107 /* Diff between two distinct revisions */
108 revision1.kind = svn_opt_revision_number;
109 revision1.value.number = self->priv->revision1;
110 revision2.kind = svn_opt_revision_number;
111 revision2.value.number = self->priv->revision2;
112 break;
115 options = apr_array_make(svn_command_get_pool (SVN_COMMAND (command)),
116 0, sizeof (char *));
118 apr_file_mktemp (&diff_file, file_template, 0,
119 svn_command_get_pool (SVN_COMMAND (command)));
121 error = svn_client_diff3 (options,
122 self->priv->path,
123 &revision1,
124 self->priv->path,
125 &revision2,
126 self->priv->recursive,
127 FALSE,
128 FALSE,
129 FALSE,
130 SVN_APR_LOCALE_CHARSET,
131 diff_file,
132 NULL,
133 svn_command_get_client_context (svn_command),
134 svn_command_get_pool (svn_command));
136 if (error)
138 svn_command_set_error (svn_command, error);
139 return 1;
142 offset = 0;
143 apr_file_seek (diff_file, APR_SET, &offset);
145 while (apr_file_eof (diff_file) != APR_EOF)
147 read_size = 80;
148 line = g_new0 (gchar, (read_size + 1));
150 apr_error = apr_file_read (diff_file, line, &read_size);
152 if (strlen (line))
154 anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
156 /* Make sure that we only output UTF-8. We could have done this by
157 * passing in "UTF-8" for header encoding to the diff API, but there
158 * is the possiblity that an external diff program could be used, in
159 * which case that arguement wouldn't do anything. As a workaround,
160 * have the internal diff system return things in the system
161 * charset, and make the (hopefully safe) assumption that any
162 * external diff program also outputs in the current locale. */
163 g_queue_push_tail (self->priv->output,
164 g_locale_to_utf8 (line, read_size, NULL, NULL,
165 NULL));
166 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
168 g_free (line);
170 anjuta_command_notify_data_arrived (command);
174 apr_file_close (diff_file);
176 return 0;
180 static void
181 svn_diff_command_class_init (SvnDiffCommandClass *klass)
183 GObjectClass* object_class = G_OBJECT_CLASS (klass);
184 AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
186 object_class->finalize = svn_diff_command_finalize;
187 command_class->run = svn_diff_command_run;
190 SvnDiffCommand *
191 svn_diff_command_new (gchar *path, glong revision1, glong revision2,
192 gboolean recursive)
194 SvnDiffCommand *self;
196 self = g_object_new (SVN_TYPE_DIFF_COMMAND, NULL);
197 self->priv->path = svn_command_make_canonical_path (SVN_COMMAND (self),
198 path);
199 self->priv->revision1 = revision1;
200 self->priv->revision2 = revision2;
201 self->priv->recursive = recursive;
203 return self;
206 void
207 svn_diff_command_destroy (SvnDiffCommand *self)
209 g_object_unref (self);
212 GQueue *
213 svn_diff_command_get_output (SvnDiffCommand *self)
215 return self->priv->output;