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

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

updating copyright statements

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