source: trunk/lib/SVN.cc @ 91

Last change on this file since 91 was 91, checked in by Jari Häkkinen, 15 years ago

Fixes #4, #19, #32, #34: Started to implement subversion API usage.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.1 KB
Line 
1// $Id: SVN.cc 91 2006-03-23 22:56:17Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat 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  svnstat 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 <string>
27#include <apr_hash.h>
28#include <svn_cmdline.h>
29#include <svn_path.h>
30#include <svn_pools.h>
31#include <svn_wc.h>
32
33namespace theplu {
34namespace svnstat {
35
36
37  SVN* SVN::instance_=NULL;
38
39
40  SVN::SVN(void)
41    : adm_access_(NULL), allocator_(NULL), context_(NULL), pool_(NULL)
42  {
43    // To be honest, I (Jari) am not perfectly sure of what I am
44    // doing, most bits here are stolen from subversion source for the
45    // svn command line binary (subverion/svn/main.c)
46
47    // initialize something (APR subsystem and more). The APR
48    // subsystem is automatically destroyed at program exit. In case
49    // of several calls to svn_cmdline_init (ie. several exceptions
50    // thrown and caught with subsequent reinitializatios is safe
51    // memorywise but what about APR internal counters?)
52    if (svn_cmdline_init("svnstat",stderr) != EXIT_SUCCESS)
53      throw SVNException("SVN(void): svn_cmdline_init failed");
54
55    /// create top-level pool
56    if (apr_allocator_create(&allocator_))
57      throw SVNException("SVN(void): apr_allocator_create failed");
58
59    apr_allocator_max_free_set(allocator_,SVN_ALLOCATOR_RECOMMENDED_MAX_FREE);
60    pool_ = svn_pool_create_ex(NULL, allocator_);
61    apr_allocator_owner_set(allocator_, pool_);
62
63    // initialize the repository access library
64    svn_error_t* err = svn_ra_initialize(pool_);
65    if (err) {
66      // destroys pool and clears err
67      svn_cmdline_handle_exit_error(err, pool_, "svnstat: ");
68      apr_allocator_destroy(allocator_);
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    err = svn_config_ensure(NULL, pool_);
77    if (err) {
78      // destroys pool and clears err
79      svn_cmdline_handle_exit_error(err, pool_, "svnstat: ");
80      apr_allocator_destroy(allocator_);
81      throw SVNException("SVN(void): svn_config_ensure failed");
82    }
83
84    // create a client context object
85    err = svn_client_create_context(&context_, pool_);
86    if (err) {
87      // destroys pool and clears err
88      svn_cmdline_handle_exit_error(err, pool_, "svnstat: ");
89      apr_allocator_destroy(allocator_);
90      throw SVNException("SVN(void): svn_client_create_context failed");
91    }
92
93    err = svn_config_get_config(&(context_->config), NULL, pool_);
94    if (err) {
95      // destroys pool and clears err
96      svn_cmdline_handle_exit_error(err, pool_, "svnstat: ");
97      apr_allocator_destroy(allocator_);
98      throw SVNException("SVN(void): svn_config_get_config failed");
99    }
100
101    // set up authentication stuff
102    if ((err=svn_cmdline_setup_auth_baton(&(context_->auth_baton), false, NULL,
103                                          NULL, NULL, false,
104            static_cast<svn_config_t*>(apr_hash_get(context_->config,
105                                                    SVN_CONFIG_CATEGORY_CONFIG,
106                                                    APR_HASH_KEY_STRING)),
107                                          context_->cancel_func,
108                                          context_->cancel_baton, pool_)))
109      svn_handle_error2(err, stderr, TRUE, "svnstat: ");
110  }
111
112
113  SVN::~SVN(void)
114  {
115    svn_pool_destroy(pool_);
116    apr_allocator_destroy(allocator_);
117    // other apr resources acquired in svn_cmdline_init are destroyed
118    // at program exit, ok since SVN is a singleton
119    delete instance_;
120  }
121
122
123  void SVN::setup_wc_adm_access(const std::string& path)
124  {
125    // Set up svn administration area access. The whole structure is
126    // setup, maybe this should not be done?
127    const char* canonical_path=svn_path_internal_style(path.c_str(), pool_);
128    if (svn_wc_adm_open3(&adm_access_, NULL, canonical_path, false, -1,
129                         context_->cancel_func,context_->cancel_baton, pool_))
130      // should the error space be cleared?
131      throw SVNException("setup_wc_adm_access: svn_wc_adm_open3 failed");
132  }
133
134
135  SVN::vc_status SVN::version_controlled(const std::string& path)
136  {
137    svn_wc_status2_t* status=NULL;
138    if (svn_wc_status2(&status,svn_path_internal_style(path.c_str(), pool_),
139                       adm_access_, pool_))
140      throw SVNException("version_controlled(): svn_config_get_config failed");
141
142    if ((status->text_status==svn_wc_status_none) ||
143        (status->text_status==svn_wc_status_unversioned))
144      return unversioned;
145    else if (status->text_status==svn_wc_status_normal)
146      return uptodate;
147
148    return unresolved;
149  }
150
151
152}} // end of namespace svnstat and namespace theplu
Note: See TracBrowser for help on using the repository browser.