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>
5  *
6  * Portions based on the original Subversion plugin
7  * Copyright (C) Johannes Schmid 2005
8  *
9  * anjuta is free software.
10  *
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.
15  *
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.
20  *
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.
26  */
27 
28 #include "svn-diff-command.h"
29 
30 struct _SvnDiffCommandPriv
31 {
32 	GQueue *output;
33 	gchar *path;
34 	gchar *root_dir;
35 	glong revision1;
36 	glong revision2;
37 	svn_depth_t depth;
38 };
39 
40 G_DEFINE_TYPE (SvnDiffCommand, svn_diff_command, SVN_TYPE_COMMAND);
41 
42 static void
svn_diff_command_init(SvnDiffCommand * self)43 svn_diff_command_init (SvnDiffCommand *self)
44 {
45 	self->priv = g_new0 (SvnDiffCommandPriv, 1);
46 	self->priv->output = g_queue_new ();
47 }
48 
49 static void
svn_diff_command_finalize(GObject * object)50 svn_diff_command_finalize (GObject *object)
51 {
52 	SvnDiffCommand *self;
53 	GList *current_line;
54 
55 	self = SVN_DIFF_COMMAND (object);
56 
57 	current_line = self->priv->output->head;
58 
59 	while (current_line)
60 	{
61 		g_free (current_line->data);
62 		current_line = g_list_next (current_line);
63 	}
64 
65 	g_queue_free (self->priv->output);
66 	g_free (self->priv->path);
67 	g_free (self->priv->root_dir);
68 	g_free (self->priv);
69 
70 	G_OBJECT_CLASS (svn_diff_command_parent_class)->finalize (object);
71 }
72 
73 static gboolean
get_full_str(apr_file_t * diff_file,gchar ** line)74 get_full_str (apr_file_t *diff_file, gchar **line)
75 {
76 	apr_size_t read_size = 1;
77 	gint buf_size = 2;
78 	gint cur_size = 0;
79 	gchar buf;
80 
81 	*line = g_new (gchar, buf_size);
82 	while (apr_file_read (diff_file, &buf, &read_size) != APR_EOF)
83 	{
84 		(*line)[cur_size] = buf;
85   		cur_size++;
86 
87 		if (cur_size >= buf_size)
88 		{
89 			gchar *new_line = g_renew (gchar, *line, buf_size * 2);
90 			buf_size *= 2;
91 			*line = new_line;
92 		}
93 
94 		if (buf == '\n')
95 		{
96 			(*line)[cur_size] = 0;
97 			return TRUE;
98 		}
99 	}
100 	return FALSE;
101 }
102 
103 static guint
svn_diff_command_run(AnjutaCommand * command)104 svn_diff_command_run (AnjutaCommand *command)
105 {
106 	SvnDiffCommand *self;
107 	SvnCommand *svn_command;
108 	svn_opt_revision_t revision1;
109 	svn_opt_revision_t revision2;
110 	apr_array_header_t *options;
111 	apr_file_t *diff_file;
112 	gchar file_template[] = "anjuta-svn-diffXXXXXX";
113 	gchar *line;
114 	svn_error_t *error;
115 	apr_off_t offset;
116 
117 	self = SVN_DIFF_COMMAND (command);
118 	svn_command = SVN_COMMAND (self);
119 
120 	switch (self->priv->revision1)
121 	{
122 		case SVN_DIFF_REVISION_NONE:
123 			/* Treat this as a diff between working copy and base
124 			 * (show only mods made to the revision of this working copy.) */
125 			revision1.kind = svn_opt_revision_base;
126 			revision2.kind = svn_opt_revision_working;
127 			break;
128 		case SVN_DIFF_REVISION_PREVIOUS:
129 			/* Diff between selected revision and the one before it.
130 			 * This is relative to revision2. */
131 			revision1.kind = svn_opt_revision_number;
132 			revision1.value.number = self->priv->revision2 - 1;
133 			revision2.kind = svn_opt_revision_number;
134 			revision2.value.number = self->priv->revision2;
135 			break;
136 		default:
137 			/* Diff between two distinct revisions */
138 			revision1.kind = svn_opt_revision_number;
139 			revision1.value.number = self->priv->revision1;
140 			revision2.kind = svn_opt_revision_number;
141 			revision2.value.number = self->priv->revision2;
142 			break;
143 	}
144 
145 	options = apr_array_make(svn_command_get_pool (SVN_COMMAND (command)),
146 							 0, sizeof (char *));
147 
148 	apr_file_mktemp (&diff_file, file_template, 0,
149 					 svn_command_get_pool (SVN_COMMAND (command)));
150 
151 	error = svn_client_diff4 (options,
152 							  self->priv->path,
153 							  &revision1,
154 							  self->priv->path,
155 							  &revision2,
156 							  self->priv->root_dir,
157 							  self->priv->depth,
158 							  FALSE,
159 							  FALSE,
160 							  FALSE,
161 							  SVN_APR_LOCALE_CHARSET,
162 							  diff_file,
163 							  NULL,
164 							  NULL,
165 							  svn_command_get_client_context (svn_command),
166 							  svn_command_get_pool (svn_command));
167 
168 	if (error)
169 	{
170 		svn_command_set_error (svn_command, error);
171 		return 1;
172 	}
173 
174 	offset = 0;
175 	apr_file_seek (diff_file, APR_SET, &offset);
176 
177 	while (get_full_str (diff_file, &line))
178 	{
179 		anjuta_async_command_lock (ANJUTA_ASYNC_COMMAND (command));
180 
181 		/* Make sure that we only output UTF-8. We could have done this by
182 		 * passing in "UTF-8" for header encoding to the diff API, but there
183 		 * is the possiblity that an external diff program could be used, in
184 		 * which case that arguement wouldn't do anything. As a workaround,
185 		 * have the internal diff system return things in the system
186 		 * charset, and make the (hopefully safe) assumption that any
187 		 * external diff program also outputs in the current locale. */
188 		g_queue_push_tail (self->priv->output,
189 						   g_locale_to_utf8 (line, -1, NULL, NULL,
190 											 NULL));
191 		anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command));
192 
193 		g_free (line);
194 
195 		anjuta_command_notify_data_arrived (command);
196 	}
197 	apr_file_close (diff_file);
198 
199 	return 0;
200 
201 }
202 
203 static void
svn_diff_command_class_init(SvnDiffCommandClass * klass)204 svn_diff_command_class_init (SvnDiffCommandClass *klass)
205 {
206 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
207 	AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
208 
209 	object_class->finalize = svn_diff_command_finalize;
210 	command_class->run = svn_diff_command_run;
211 }
212 
213 SvnDiffCommand *
svn_diff_command_new(const gchar * path,glong revision1,glong revision2,const gchar * root_dir,gboolean recursive)214 svn_diff_command_new (const gchar *path, glong revision1, glong revision2,
215 					  const gchar *root_dir, gboolean recursive)
216 {
217 	SvnDiffCommand *self;
218 
219 	self = g_object_new (SVN_TYPE_DIFF_COMMAND, NULL);
220 	self->priv->path = svn_command_make_canonical_path (SVN_COMMAND (self),
221 														path);
222 	self->priv->root_dir = svn_command_make_canonical_path (SVN_COMMAND (self),
223 															root_dir);
224 	self->priv->revision1 = revision1;
225 	self->priv->revision2 = revision2;
226 	self->priv->depth = recursive ? svn_depth_infinity : svn_depth_empty;
227 
228 	return self;
229 }
230 
231 void
svn_diff_command_destroy(SvnDiffCommand * self)232 svn_diff_command_destroy (SvnDiffCommand *self)
233 {
234 	g_object_unref (self);
235 }
236 
237 GQueue *
svn_diff_command_get_output(SvnDiffCommand * self)238 svn_diff_command_get_output (SvnDiffCommand *self)
239 {
240 	return self->priv->output;
241 }
242