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-update-command.h"
29 
30 struct _SvnUpdateCommandPriv
31 {
32 	gchar *path;
33 	gchar *revision;
34 	gboolean recursive;
35 };
36 
37 G_DEFINE_TYPE (SvnUpdateCommand, svn_update_command, SVN_TYPE_COMMAND);
38 
39 static void
svn_update_command_init(SvnUpdateCommand * self)40 svn_update_command_init (SvnUpdateCommand *self)
41 {
42 	self->priv = g_new0 (SvnUpdateCommandPriv, 1);
43 }
44 
45 static void
svn_update_command_finalize(GObject * object)46 svn_update_command_finalize (GObject *object)
47 {
48 	SvnUpdateCommand *self;
49 
50 	self = SVN_UPDATE_COMMAND (object);
51 
52 	g_free (self->priv->path);
53 	g_free (self->priv->revision);
54 	g_free (self->priv);
55 
56 	G_OBJECT_CLASS (svn_update_command_parent_class)->finalize (object);
57 }
58 
59 static guint
svn_update_command_run(AnjutaCommand * command)60 svn_update_command_run (AnjutaCommand *command)
61 {
62 	SvnUpdateCommand *self;
63 	SvnCommand *svn_command;
64 	svn_opt_revision_t *revision;
65 	apr_array_header_t *update_paths;
66 	apr_array_header_t *update_revisions;
67 	svn_error_t *error;
68 	svn_revnum_t revision_number;
69 	gchar *revision_message;
70 
71 	self = SVN_UPDATE_COMMAND (command);
72 	svn_command = SVN_COMMAND (command);
73 	revision = svn_command_get_revision (self->priv->revision);
74 
75 	/* I just copied this so don't blame me... */
76 	update_paths = apr_array_make (svn_command_get_pool (svn_command), 1,
77 								   sizeof (char *));
78 	(*((const char **) apr_array_push (update_paths))) = self->priv->path;
79 
80 	error = svn_client_update2 (&update_revisions,
81 								update_paths,
82 								revision,
83 								self->priv->recursive,
84 								FALSE,
85 								svn_command_get_client_context (svn_command),
86 								svn_command_get_pool (svn_command));
87 
88 	if (error)
89 	{
90 		svn_command_set_error (svn_command, error);
91 		return 1;
92 	}
93 
94 	revision_number = (*(svn_revnum_t *) apr_array_pop (update_revisions));
95 
96 	revision_message = g_strdup_printf ("Updated to revision %ld.",
97 										(glong) revision_number);
98 	svn_command_push_info (SVN_COMMAND (command), revision_message);
99 	g_free (revision_message);
100 
101 	return 0;
102 }
103 
104 static void
svn_update_command_class_init(SvnUpdateCommandClass * klass)105 svn_update_command_class_init (SvnUpdateCommandClass *klass)
106 {
107 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
108 	AnjutaCommandClass *command_class = ANJUTA_COMMAND_CLASS (klass);
109 
110 	object_class->finalize = svn_update_command_finalize;
111 	command_class->run = svn_update_command_run;
112 }
113 
114 
115 SvnUpdateCommand *
svn_update_command_new(const gchar * path,const gchar * revision,gboolean recursive)116 svn_update_command_new (const gchar *path, const gchar *revision, gboolean recursive)
117 {
118 	SvnUpdateCommand *self;
119 
120 	self = g_object_new (SVN_TYPE_UPDATE_COMMAND, NULL);
121 
122 	self->priv->path = svn_command_make_canonical_path (SVN_COMMAND (self),
123 														path);
124 	self->priv->revision = g_strdup (revision);
125 	self->priv->recursive = recursive;
126 
127 	return self;
128 }
129 
130 void
svn_update_command_destroy(SvnUpdateCommand * self)131 svn_update_command_destroy (SvnUpdateCommand *self)
132 {
133 	g_object_unref (self);
134 }
135