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  * 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.
12  *
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.
17  *
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.
23  */
24 
25 #include "svn-switch-command.h"
26 
27 struct _SvnSwitchCommandPriv
28 {
29 	gchar *working_copy_path;
30 	gchar *branch_url;
31 	glong revision;
32 	gboolean recursive;
33 };
34 
35 G_DEFINE_TYPE (SvnSwitchCommand, svn_switch_command, SVN_TYPE_COMMAND);
36 
37 static void
svn_switch_command_init(SvnSwitchCommand * self)38 svn_switch_command_init (SvnSwitchCommand *self)
39 {
40 	self->priv = g_new0 (SvnSwitchCommandPriv, 1);
41 }
42 
43 static void
svn_switch_command_finalize(GObject * object)44 svn_switch_command_finalize (GObject *object)
45 {
46 	SvnSwitchCommand *self;
47 
48 	self = SVN_SWITCH_COMMAND (object);
49 
50 	g_free (self->priv->working_copy_path);
51 	g_free (self->priv->branch_url);
52 	g_free (self->priv);
53 
54 	G_OBJECT_CLASS (svn_switch_command_parent_class)->finalize (object);
55 }
56 
57 static guint
svn_switch_command_run(AnjutaCommand * command)58 svn_switch_command_run (AnjutaCommand *command)
59 {
60 	SvnSwitchCommand *self;
61 	SvnCommand *svn_command;
62 	svn_opt_revision_t revision;
63 	svn_revnum_t switched_revision;
64 	gchar *revision_message;
65 	svn_error_t *error;
66 
67 	self = SVN_SWITCH_COMMAND (command);
68 	svn_command = SVN_COMMAND (command);
69 
70 	if (self->priv->revision == SVN_SWITCH_REVISION_HEAD)
71 		revision.kind = svn_opt_revision_head;
72 	else
73 	{
74 		revision.kind = svn_opt_revision_number;
75 		revision.value.number = self->priv->revision;
76 	}
77 
78 	error = svn_client_switch (&switched_revision,
79 							   self->priv->working_copy_path,
80 							   self->priv->branch_url,
81 							   &revision,
82 							   self->priv->recursive,
83 							   svn_command_get_client_context (svn_command),
84 							   svn_command_get_pool (svn_command));
85 
86 	if (error)
87 	{
88 		svn_command_set_error (svn_command, error);
89 		return 1;
90 	}
91 
92 	revision_message = g_strdup_printf ("Switched to revision %ld.",
93 										switched_revision);
94 
95 	svn_command_push_info (svn_command, revision_message);
96 	g_free (revision_message);
97 
98 	return 0;
99 }
100 
101 static void
svn_switch_command_class_init(SvnSwitchCommandClass * klass)102 svn_switch_command_class_init (SvnSwitchCommandClass *klass)
103 {
104 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
105 	AnjutaCommandClass* command_class = ANJUTA_COMMAND_CLASS (klass);
106 
107 	object_class->finalize = svn_switch_command_finalize;
108 	command_class->run = svn_switch_command_run;
109 }
110 
111 SvnSwitchCommand *
svn_switch_command_new(const gchar * working_copy_path,const gchar * branch_url,glong revision,gboolean recursive)112 svn_switch_command_new (const gchar *working_copy_path, const gchar *branch_url,
113 						glong revision, gboolean recursive)
114 {
115 	SvnSwitchCommand *self;
116 
117 	self = g_object_new (SVN_TYPE_SWITCH_COMMAND, NULL);
118 	self->priv->working_copy_path = svn_command_make_canonical_path (SVN_COMMAND (self),
119 																	 working_copy_path);
120 	self->priv->branch_url = svn_command_make_canonical_path (SVN_COMMAND (self),
121 															  branch_url);
122 	self->priv->revision = revision;
123 	self->priv->recursive = recursive;
124 
125 	return self;
126 }
127 
128 void
svn_switch_command_destroy(SvnSwitchCommand * self)129 svn_switch_command_destroy (SvnSwitchCommand *self)
130 {
131 	g_object_unref (self);
132 }
133