source: trunk/lib/SVN.h @ 140

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

Addresses #56. Fixed freeing of resources on svn error.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.7 KB
Line 
1// $Id: SVN.h 140 2006-08-04 00:12:11Z 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#ifndef _theplu_svnstat_svn_
25#define _theplu_svnstat_svn_
26
27#include <stdexcept>
28#include <string>
29#include <vector>
30
31// Jari, most of these can be replaced with forward declarations.
32#include <apr_allocator.h>
33#include <svn_client.h>
34#include <svn_ra.h>
35#include <svn_wc.h>
36
37namespace theplu {
38namespace svnstat {
39
40  ///
41  /// If something goes wrong in the use of the different SVN classes,
42  /// an SVNException is thrown.
43  ///
44  struct SVNException : public std::runtime_error
45  { inline SVNException(const std::string& msg) : runtime_error(msg) {} };
46
47  ///
48  /// The SVN class is a front end to the subversion API.
49  ///
50  /// SVN provides one single global access point to the underlying
51  /// subversion API and makes sure that there is only one point of
52  /// access for the binary.
53  ///
54  /// @see Design Patterns (the singleton pattern). Subversion API
55  /// documents.
56  ///
57  class SVN {
58  public:
59
60    enum vc_status {
61      unversioned=0,
62      uptodate,
63      unresolved
64    };
65
66    ///
67    /// @brief The destructor.
68    ///
69    ~SVN(void);
70
71    ///
72    /// @brief Call the underlying svn_client_blame3 for \a path with
73    /// \a receiver and \a baton.
74    ///
75    /// This function is called from SVNblame to do 'svn blame' on an
76    /// item. The \a receiver and \a baton is defined in SVNblame and
77    /// the \a receiver is called by the underlying subversion API for
78    /// every line in \a path provided it the item is under subversion
79    /// control. The \a baton is used to communicate anonymous
80    /// information through the API to the \a receiver. If \a path is
81    /// a binary object an error is returned, all other errors will
82    /// generate an SVNException.
83    ///
84    /// @return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the
85    /// latter can be used to trigger on binary files. Note that
86    /// errors return from underlying subversion API must be cleared
87    /// by the receiver.
88    ///
89    /// @see Subversion API (svn_error_clear).
90    ///
91    svn_error_t * client_blame(const std::string& path,
92                               svn_client_blame_receiver_t receiver,
93                               void *baton);
94
95    ///
96    /// @brief Call the underlying svn_client_info for \a path with \a
97    /// receiver and \a baton.
98    ///
99    /// This function is called from SVNinfo to do 'svn info' on an
100    /// item. The \a receiver and \a baton is defined in SVNinfo and
101    /// the \a receiver is called by the underlying subversion API if
102    /// \a path is under subversion control. The \a baton is used to
103    /// communicate anonymous information through the API to the
104    /// \a receiver.
105    ///
106    /// @see Subversion API documentation, SVNinfo
107    ///
108    void client_info(const std::string& path, svn_info_receiver_t receiver,
109                     void *baton);
110
111    ///
112    /// @brief Get revision dates.
113    ///
114    /// Get dates for all commits to the repository. \a root can be a
115    /// repository path or a path within a working copy. In the latter
116    /// case the corresponding repository will be used to retrieve
117    /// commit dates.
118    ///
119    /// @return Revision dates in a vector where revision is
120    /// implicitly defined by vector index, i.e., element 56 in the
121    /// resulting vector implies revision 56.
122    ///
123    /// @note Currently revision 0 (repositoy creation) is not
124    /// supported.
125    ///
126    /// @throw Various error messages generated from the subversion
127    /// API.
128    ///
129    std::vector<std::string> commit_dates(const std::string& path);
130
131    ///
132    /// @brief Get an instance of SVN.
133    ///
134    /// @throw Throws an SVNException if initialization fails in the
135    /// underlying subversion API calls.
136    ///
137    static SVN* instance(void)
138    { if (!instance_) instance_=new SVN; return instance_; }
139
140    ///
141    /// @throws SVNException if session setup fails.
142    ///
143    void setup_ra_session(const std::string& path);
144
145    ///
146    /// @throws SVNException if access setup fails.
147    ///
148    void setup_wc_adm_access(const std::string& path);
149
150    ///
151    /// @brief Check if entry \a path is under version control
152    ///
153    /// @return True if \a path is under version control, false
154    /// otherwise.
155    ///
156    vc_status version_controlled(const std::string& path);
157
158
159  private:
160    ///
161    /// @brief Constructor
162    ///
163    /// The only way to create a object of SVN type is by calling
164    /// SVN::instance.
165    ///
166    SVN(void);
167
168    ///
169    /// @brief Copy Constructor, not implemented.
170    ///
171    SVN(const SVN&);
172
173    ///
174    /// Free resources when failing to reach end of constructor.
175    ///
176    void cleanup_failed_initialization(svn_error_t *err);
177
178    static SVN* instance_;
179
180    // Subversion API stuff
181
182    // Log message receiver
183    struct log_receiver_baton {
184      std::vector<std::string> commit_dates;
185    };
186    static svn_error_t *
187    log_message_receiver(void *baton, apr_hash_t *changed_paths,
188                         svn_revnum_t rev, const char *author, const char *date,
189                         const char *msg, apr_pool_t *pool);
190
191    svn_wc_adm_access_t* adm_access_;
192    apr_allocator_t* allocator_;
193    svn_client_ctx_t* context_;
194    apr_pool_t* pool_;
195    svn_ra_session_t* ra_session_;
196  };
197
198}} // end of namespace svnstat and namespace theplu
199
200#endif
Note: See TracBrowser for help on using the repository browser.