source: branches/0.6-stable/lib/SVN.cc @ 573

Last change on this file since 573 was 573, checked in by Peter Johansson, 13 years ago

fixes #281

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