1 | // $Id: SVN.cc 185 2006-09-06 02:39:18Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen |
---|
5 | |
---|
6 | This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 2 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "SVN.h" |
---|
25 | |
---|
26 | #include <map> |
---|
27 | #include <string> |
---|
28 | #include <vector> |
---|
29 | |
---|
30 | #include <apr_allocator.h> |
---|
31 | #include <apr_hash.h> |
---|
32 | #include <subversion-1/svn_client.h> |
---|
33 | #include <subversion-1/svn_cmdline.h> |
---|
34 | #include <subversion-1/svn_path.h> |
---|
35 | #include <subversion-1/svn_pools.h> |
---|
36 | #include <subversion-1/svn_wc.h> |
---|
37 | #include <subversion-1/svn_subst.h> |
---|
38 | |
---|
39 | namespace theplu { |
---|
40 | namespace svndigest { |
---|
41 | |
---|
42 | |
---|
43 | SVN* SVN::instance_=NULL; |
---|
44 | |
---|
45 | |
---|
46 | SVN::SVN(void) |
---|
47 | : adm_access_(NULL), allocator_(NULL), context_(NULL), pool_(NULL) |
---|
48 | { |
---|
49 | svn_error_t* err=NULL; |
---|
50 | |
---|
51 | // initialize something (APR subsystem and more). The APR |
---|
52 | // subsystem is automatically destroyed at program exit. In case |
---|
53 | // of several calls to svn_cmdline_init (ie. several exceptions |
---|
54 | // thrown and caught with subsequent reinitializatios is safe |
---|
55 | // memorywise but what about APR internal counters?) |
---|
56 | if (svn_cmdline_init("svndigest",stderr) != EXIT_SUCCESS) |
---|
57 | throw SVNException("SVN(void): svn_cmdline_init failed"); |
---|
58 | |
---|
59 | /// create top-level pool |
---|
60 | if (apr_allocator_create(&allocator_)) |
---|
61 | throw SVNException("SVN(void): apr_allocator_create failed"); |
---|
62 | apr_allocator_max_free_set(allocator_,SVN_ALLOCATOR_RECOMMENDED_MAX_FREE); |
---|
63 | pool_ = svn_pool_create_ex(NULL, allocator_); |
---|
64 | apr_allocator_owner_set(allocator_, pool_); |
---|
65 | |
---|
66 | // initialize the repository access library |
---|
67 | if ((err=svn_ra_initialize(pool_))) { |
---|
68 | cleanup_failed_initialization(err); |
---|
69 | throw SVNException("SVN(void): svn_ra_initialize failed"); |
---|
70 | } |
---|
71 | |
---|
72 | // Check that users .subversion exist. Should this really be done? |
---|
73 | // If this check is to be done, we might be forced to support a |
---|
74 | // command line option to change the location of the .subversion |
---|
75 | // stuff (compare with the svn binary). |
---|
76 | if ((err=svn_config_ensure(NULL, pool_))) { |
---|
77 | cleanup_failed_initialization(err); |
---|
78 | throw SVNException("SVN(void): svn_config_ensure failed"); |
---|
79 | } |
---|
80 | |
---|
81 | // create a client context object |
---|
82 | if ((err=svn_client_create_context(&context_, pool_))) { |
---|
83 | cleanup_failed_initialization(err); |
---|
84 | throw SVNException("SVN(void): svn_client_create_context failed"); |
---|
85 | } |
---|
86 | |
---|
87 | if ((err=svn_config_get_config(&(context_->config), NULL, pool_))) { |
---|
88 | cleanup_failed_initialization(err); |
---|
89 | throw SVNException("SVN(void): svn_config_get_config failed"); |
---|
90 | } |
---|
91 | |
---|
92 | // set up authentication stuff |
---|
93 | if ((err=svn_cmdline_setup_auth_baton(&(context_->auth_baton), false, NULL, |
---|
94 | NULL, NULL, false, |
---|
95 | static_cast<svn_config_t*>(apr_hash_get(context_->config, |
---|
96 | SVN_CONFIG_CATEGORY_CONFIG, |
---|
97 | APR_HASH_KEY_STRING)), |
---|
98 | context_->cancel_func, |
---|
99 | context_->cancel_baton, pool_))) { |
---|
100 | cleanup_failed_initialization(err); |
---|
101 | throw SVNException("SVN(void): svn_cmdline_setup_auth_baton failed"); |
---|
102 | } |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | SVN::~SVN(void) |
---|
107 | { |
---|
108 | if (adm_access_) |
---|
109 | svn_error_clear(svn_wc_adm_close(adm_access_)); |
---|
110 | svn_pool_destroy(pool_); |
---|
111 | apr_allocator_destroy(allocator_); |
---|
112 | // other apr resources acquired in svn_cmdline_init are destroyed |
---|
113 | // at program exit, ok since SVN is a singleton |
---|
114 | delete instance_; |
---|
115 | } |
---|
116 | |
---|
117 | |
---|
118 | svn_error_t* SVN::client_blame(const std::string& path, |
---|
119 | svn_client_blame_receiver_t receiver, |
---|
120 | void *baton) |
---|
121 | { |
---|
122 | // Setup to use all revisions |
---|
123 | svn_opt_revision_t peg, start, head; |
---|
124 | peg.kind=svn_opt_revision_unspecified; |
---|
125 | start.kind=svn_opt_revision_number; |
---|
126 | start.value.number=0; |
---|
127 | head.kind=svn_opt_revision_head; |
---|
128 | apr_pool_t *subpool = svn_pool_create(pool_); |
---|
129 | svn_error_t* err=svn_client_blame3(path.c_str(), &peg, &start, &head, |
---|
130 | svn_diff_file_options_create(subpool), |
---|
131 | false, receiver, baton, context_, |
---|
132 | subpool); |
---|
133 | if (err && err->apr_err!=SVN_ERR_CLIENT_IS_BINARY_FILE) { |
---|
134 | cleanup(err,subpool); |
---|
135 | throw SVNException("SVN::client_blame: svn_client_blame3 failed"); |
---|
136 | } |
---|
137 | svn_pool_destroy(subpool); |
---|
138 | return err; |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | void SVN::client_info(const std::string& path, svn_info_receiver_t receiver, |
---|
143 | void *baton) |
---|
144 | { |
---|
145 | apr_pool_t *subpool = svn_pool_create(pool_); |
---|
146 | if (svn_error_t *err=svn_client_info(path.c_str(), NULL, NULL, receiver, |
---|
147 | baton, false, context_, subpool)) { |
---|
148 | cleanup(err,subpool); |
---|
149 | throw SVNException("repository: svn_client_info failed"); |
---|
150 | } |
---|
151 | svn_pool_destroy(subpool); |
---|
152 | } |
---|
153 | |
---|
154 | |
---|
155 | void SVN::client_proplist(const std::string& path, |
---|
156 | std::map<std::string, std::string>& property) |
---|
157 | { |
---|
158 | svn_opt_revision_t peg, revision; |
---|
159 | peg.kind=svn_opt_revision_unspecified; |
---|
160 | revision.kind=svn_opt_revision_head; |
---|
161 | apr_pool_t *subpool = svn_pool_create(pool_); |
---|
162 | apr_array_header_t * properties; |
---|
163 | svn_error_t *err=svn_client_proplist2(&properties, path.c_str(), &peg, |
---|
164 | &revision, false, context_, subpool); |
---|
165 | if (err) { |
---|
166 | cleanup(err,subpool); |
---|
167 | throw SVNException("repository: svn_client_proplist2 failed"); |
---|
168 | } |
---|
169 | for (int j = 0; j < properties->nelts; ++j) { |
---|
170 | svn_client_proplist_item_t *item = |
---|
171 | ((svn_client_proplist_item_t **)properties->elts)[j]; |
---|
172 | for (apr_hash_index_t *hi = apr_hash_first(subpool, item->prop_hash); hi; |
---|
173 | hi = apr_hash_next (hi)) { |
---|
174 | const void *key; |
---|
175 | void *val; |
---|
176 | apr_hash_this (hi, &key, NULL, &val); |
---|
177 | svn_string_t *value; |
---|
178 | err=svn_subst_detranslate_string(&value, |
---|
179 | static_cast<const svn_string_t*>(val), |
---|
180 | false, subpool); |
---|
181 | if (err) { |
---|
182 | cleanup(err,subpool); |
---|
183 | throw SVNException("property: svn_subst_detranslate_string failed"); |
---|
184 | } |
---|
185 | property[static_cast<const char*>(key)]=value->data; |
---|
186 | } |
---|
187 | } |
---|
188 | svn_pool_destroy(subpool); |
---|
189 | } |
---|
190 | |
---|
191 | |
---|
192 | std::vector<std::string> SVN::commit_dates(const std::string& path) |
---|
193 | { |
---|
194 | // Allocate space in subpool to pool_ for apr_path (here a string). |
---|
195 | apr_pool_t *subpool = svn_pool_create(pool_); |
---|
196 | apr_array_header_t* apr_path=apr_array_make(subpool,1,4); |
---|
197 | // Copy path to apr_path. |
---|
198 | (*((const char **) apr_array_push(apr_path))) = |
---|
199 | apr_pstrdup(subpool, svn_path_internal_style(path.c_str(),subpool)); |
---|
200 | |
---|
201 | // Setup to retrieve all commit logs. |
---|
202 | svn_opt_revision_t peg, start, head; |
---|
203 | peg.kind=svn_opt_revision_unspecified; |
---|
204 | start.kind=svn_opt_revision_number; |
---|
205 | start.value.number=0; |
---|
206 | head.kind=svn_opt_revision_head; |
---|
207 | svn_error_t* err=NULL; |
---|
208 | // Retrieving the last revision is only needed for the reserve |
---|
209 | // call below, not needed for the functionality here. |
---|
210 | if ((err=svn_ra_get_latest_revnum(ra_session_, &(head.value.number), |
---|
211 | subpool))) { |
---|
212 | cleanup(err,subpool); |
---|
213 | throw SVNException("commit_dates: svn_ra_get_latest_revnum failed"); |
---|
214 | } |
---|
215 | // The struct we want to pass through to all log_message_receiver |
---|
216 | // calls, here we only want to push all commit dates into a |
---|
217 | // std::vector<std::string>. |
---|
218 | struct log_receiver_baton lb; |
---|
219 | lb.commit_dates.reserve(head.value.number+1); // revision 0 is also stored. |
---|
220 | if ((err=svn_client_log3(apr_path, &peg, &start, &head, 0, false, true, |
---|
221 | log_message_receiver, static_cast<void*>(&lb), |
---|
222 | context_, subpool))) { |
---|
223 | cleanup(err,subpool); |
---|
224 | throw SVNException("commit_dates: svn_client_log3 failed"); |
---|
225 | } |
---|
226 | svn_pool_destroy(subpool); |
---|
227 | return lb.commit_dates; |
---|
228 | } |
---|
229 | |
---|
230 | |
---|
231 | void SVN::cleanup(svn_error_t *err,apr_pool_t *pool) |
---|
232 | { |
---|
233 | svn_handle_error2(err,stderr,false,"svndigest:"); |
---|
234 | svn_error_clear(err); |
---|
235 | svn_pool_destroy(pool); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | void SVN::cleanup_failed_initialization(svn_error_t *err) |
---|
240 | { |
---|
241 | cleanup(err,pool_); |
---|
242 | apr_allocator_destroy(allocator_); |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | svn_error_t * |
---|
247 | SVN::log_message_receiver(void *baton, apr_hash_t *changed_paths, |
---|
248 | svn_revnum_t rev, const char *author, |
---|
249 | const char *date, const char *msg, apr_pool_t *pool) |
---|
250 | { |
---|
251 | struct log_receiver_baton *lb=static_cast<struct log_receiver_baton*>(baton); |
---|
252 | if (date && date[0]) |
---|
253 | lb->commit_dates.push_back(date); |
---|
254 | else |
---|
255 | throw SVNException("No date defined for revision: " + rev); |
---|
256 | return SVN_NO_ERROR; |
---|
257 | } |
---|
258 | |
---|
259 | |
---|
260 | void SVN::setup_ra_session(const std::string& path) { |
---|
261 | // get a session to the repository |
---|
262 | if (svn_error_t *err=svn_client_open_ra_session(&ra_session_, path.c_str(), |
---|
263 | context_,pool_)) { |
---|
264 | // cleanup could be called if a null pool can be passed to |
---|
265 | // svn_pool_destroy (which is just a #define to apr_pool_destroy |
---|
266 | svn_handle_error2(err,stderr,false,"svndigest:"); |
---|
267 | svn_error_clear(err); |
---|
268 | throw SVNException("setup_ra_session: svn_client_open_ra_session failed"); |
---|
269 | } |
---|
270 | } |
---|
271 | |
---|
272 | |
---|
273 | void SVN::setup_wc_adm_access(const std::string& path) |
---|
274 | { |
---|
275 | // Set up svn administration area access. The whole structure is |
---|
276 | // setup, maybe this is unnecessary? |
---|
277 | const char* canonical_path=svn_path_internal_style(path.c_str(), pool_); |
---|
278 | if (svn_error_t *err=svn_wc_adm_open3(&adm_access_, NULL, canonical_path, |
---|
279 | false, -1, context_->cancel_func, |
---|
280 | context_->cancel_baton, pool_)) { |
---|
281 | // cleanup could be called if a null pool can be passed to |
---|
282 | // svn_pool_destroy (which is just a #define to apr_pool_destroy |
---|
283 | svn_handle_error2(err,stderr,false,"svndigest:"); |
---|
284 | svn_error_clear(err); |
---|
285 | throw SVNException("setup_wc_adm_access: svn_wc_adm_open3 failed"); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | SVN::vc_status SVN::version_controlled(const std::string& path) |
---|
291 | { |
---|
292 | svn_wc_status2_t* status=NULL; |
---|
293 | apr_pool_t *subpool = svn_pool_create(pool_); |
---|
294 | if (svn_error_t *err= |
---|
295 | svn_wc_status2(&status,svn_path_internal_style(path.c_str(), subpool), |
---|
296 | adm_access_, subpool)) { |
---|
297 | cleanup(err,subpool); |
---|
298 | throw SVNException("version_controlled(): svn_config_get_config failed"); |
---|
299 | } |
---|
300 | svn_pool_destroy(subpool); |
---|
301 | |
---|
302 | if ((status->text_status==svn_wc_status_none) || |
---|
303 | (status->text_status==svn_wc_status_unversioned)) |
---|
304 | return unversioned; |
---|
305 | else if (status->text_status==svn_wc_status_normal) |
---|
306 | return uptodate; |
---|
307 | |
---|
308 | return unresolved; |
---|
309 | } |
---|
310 | |
---|
311 | |
---|
312 | }} // end of namespace svndigest and namespace theplu |
---|