1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2007 <jrliggett@cox.net>
6 * Portions based on the original Subversion plugin
7 * Copyright (C) Johannes Schmid 2005
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)
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
39 G_DEFINE_TYPE (SvnDiffCommand
, svn_diff_command
, SVN_TYPE_COMMAND
);
42 svn_diff_command_init (SvnDiffCommand
*self
)
44 self
->priv
= g_new0 (SvnDiffCommandPriv
, 1);
45 self
->priv
->output
= g_queue_new ();
49 svn_diff_command_finalize (GObject
*object
)
54 self
= SVN_DIFF_COMMAND (object
);
56 current_line
= self
->priv
->output
->head
;
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
);
68 G_OBJECT_CLASS (svn_diff_command_parent_class
)->finalize (object
);
72 svn_diff_command_run (AnjutaCommand
*command
)
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";
84 apr_status_t apr_error
;
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
;
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
;
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
;
115 options
= apr_array_make(svn_command_get_pool (SVN_COMMAND (command
)),
118 apr_file_mktemp (&diff_file
, file_template
, 0,
119 svn_command_get_pool (SVN_COMMAND (command
)));
121 error
= svn_client_diff3 (options
,
126 self
->priv
->recursive
,
130 SVN_APR_LOCALE_CHARSET
,
133 svn_command_get_client_context (svn_command
),
134 svn_command_get_pool (svn_command
));
138 svn_command_set_error (svn_command
, error
);
143 apr_file_seek (diff_file
, APR_SET
, &offset
);
145 while (apr_file_eof (diff_file
) != APR_EOF
)
148 line
= g_new0 (gchar
, (read_size
+ 1));
150 apr_error
= apr_file_read (diff_file
, line
, &read_size
);
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
,
166 anjuta_async_command_unlock (ANJUTA_ASYNC_COMMAND (command
));
170 anjuta_command_notify_data_arrived (command
);
174 apr_file_close (diff_file
);
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
;
191 svn_diff_command_new (gchar
*path
, glong revision1
, glong revision2
,
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
),
199 self
->priv
->revision1
= revision1
;
200 self
->priv
->revision2
= revision2
;
201 self
->priv
->recursive
= recursive
;
207 svn_diff_command_destroy (SvnDiffCommand
*self
)
209 g_object_unref (self
);
213 svn_diff_command_get_output (SvnDiffCommand
*self
)
215 return self
->priv
->output
;