source: trunk/lib/SVN.h @ 271

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

Fixing singleton implementation issues.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1#ifndef _theplu_svndigest_svn_
2#define _theplu_svndigest_svn_
3
4// $Id: SVN.h 271 2007-05-02 07:53:47Z jari $
5
6/*
7  Copyright (C) 2006 Jari Häkkinen
8  Copyright (C) 2007 Peter Johansson
9
10  This file is part of svndigest, http://lev.thep.lu.se/trac/svndigest
11
12  svndigest is free software; you can redistribute it and/or modify it
13  under the terms of the GNU General Public License as published by
14  the Free Software Foundation; either version 2 of the License, or
15  (at your option) any later version.
16
17  svndigest is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  General Public License for more details.
21
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25  02111-1307, USA.
26*/
27
28#include <map>
29#include <stdexcept>
30#include <string>
31#include <vector>
32
33#include <subversion-1/svn_client.h>
34#include <subversion-1/svn_types.h>
35
36namespace theplu {
37namespace svndigest {
38
39  struct log_receiver_baton;
40
41  ///
42  /// If something goes wrong in the use of the different SVN classes,
43  /// an SVNException is thrown.
44  ///
45  struct SVNException : public std::runtime_error
46  { inline SVNException(const std::string& msg) : runtime_error(msg) {} };
47
48  ///
49  /// The SVN class is a front end to the subversion API.
50  ///
51  /// SVN provides one single global access point to the underlying
52  /// subversion API and makes sure that there is only one point of
53  /// access for the binary.
54  ///
55  /// @see Design Patterns (the singleton pattern). Subversion API
56  /// documents.
57  ///
58  class SVN {
59  public:
60
61    enum vc_status {
62      unversioned=0,
63      uptodate,
64      unresolved
65    };
66
67    ///
68    /// @brief Call the underlying svn_client_blame3 for \a path with
69    /// \a receiver and \a baton.
70    ///
71    /// This function is called from SVNblame to do 'svn blame' on an
72    /// item. The \a receiver and \a baton is defined in SVNblame and
73    /// the \a receiver is called by the underlying subversion API for
74    /// every line in \a path provided it the item is under subversion
75    /// control. The \a baton is used to communicate anonymous
76    /// information through the API to the \a receiver. If \a path is
77    /// a binary object an error is returned, all other errors will
78    /// generate an SVNException.
79    ///
80    /// @return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the
81    /// latter can be used to trigger on binary files. Note that
82    /// errors return from underlying subversion API must be cleared
83    /// by the receiver.
84    ///
85    /// @see Subversion API (svn_error_clear).
86    ///
87    svn_error_t * client_blame(const std::string& path,
88                               svn_client_blame_receiver_t receiver,
89                               void *baton);
90
91    ///
92    /// @brief Call the underlying svn_client_info for \a path with \a
93    /// receiver and \a baton.
94    ///
95    /// This function is called from SVNinfo to do 'svn info' on an
96    /// item. The \a receiver and \a baton is defined in SVNinfo and
97    /// the \a receiver is called by the underlying subversion API if
98    /// \a path is under subversion control. The \a baton is used to
99    /// communicate anonymous information through the API to the
100    /// \a receiver.
101    ///
102    /// @see Subversion API documentation, SVNinfo
103    ///
104    void client_info(const std::string& path, svn_info_receiver_t receiver,
105                     void *baton);
106
107    /**
108       \todo doc
109    */
110    void client_log(std::string path, log_receiver_baton* lb);
111
112
113    /**
114       @brief Get the properties for \a path.
115
116       The retrieved properties are stored in \a properties. To check
117       whether \a is a binary item use SVNproperty::binary(void).
118    */
119    void client_proplist(const std::string& path,
120                         std::map<std::string, std::string>& properties);
121
122    ///
123    /// @brief Get revision dates.
124    ///
125    /// Get dates for all commits to the repository. \a root can be a
126    /// repository path or a path within a working copy. In the latter
127    /// case the corresponding repository will be used to retrieve
128    /// commit dates.
129    ///
130    /// @return Revision dates in a vector where revision is
131    /// implicitly defined by vector index, i.e., element 56 in the
132    /// resulting vector implies revision 56.
133    ///
134    /// @note Currently revision 0 (repositoy creation) is not
135    /// supported.
136    ///
137    /// @throw Various error messages generated from the subversion
138    /// API.
139    ///
140    //std::vector<std::string> commit_dates(const std::string& path);
141
142    ///
143    /// @brief Get an instance of SVN.
144    ///
145    /// @throw Throws an SVNException if initialization fails in the
146    /// underlying subversion API calls.
147    ///
148    static SVN* instance(void)
149    { if (!instance_) instance_=new SVN; return instance_; }
150
151    ///
152    /// @throws SVNException if session setup fails.
153    ///
154    void setup_ra_session(const std::string& path);
155
156    ///
157    /// @throws SVNException if access setup fails.
158    ///
159    void setup_wc_adm_access(const std::string& path);
160
161    ///
162    /// @brief Check if entry \a path is under version control
163    ///
164    /// @return True if \a path is under version control, false
165    /// otherwise.
166    ///
167    vc_status version_controlled(const std::string& path);
168
169  private:
170    ///
171    /// @brief Constructor
172    ///
173    /// The only way to create a object of SVN type is by calling
174    /// SVN::instance.
175    ///
176    SVN(void);
177
178    ///
179    /// @brief Copy Constructor, not implemented.
180    ///
181    SVN(const SVN&);
182
183    ///
184    /// @brief The destructor.
185    ///
186    virtual ~SVN(void);
187
188    /**
189       @brief Free resources when svn API calls fail.
190
191       This function will write an error message to stdout, free \a
192       err and \a pool resources. If \a err or \a pool are a NULL
193       pointers the function will do nothing with these resources.
194
195       cleanup will throw a SVNException if \a message has
196       length>0. The default bahaviour is to free resources and return
197       normally.
198
199       @see SVNException
200    */
201    void cleanup(svn_error_t *err, apr_pool_t *pool,
202                 const std::string& message=std::string());
203
204    /**
205       @brief Free resources when failing to reach end of
206       constructor.
207
208       cleanup_failed_init will free all resource acquired in the
209       constructor and throw an SVNException with \a message as the
210       message.
211
212       @see SVNException
213    */
214    void cleanup_failed_init(svn_error_t *err, const std::string& message);
215
216    static SVN* instance_;
217
218    // Subversion API stuff
219
220    static svn_error_t *
221    log_message_receiver(void *baton, apr_hash_t *changed_paths,
222                         svn_revnum_t rev, const char *author, const char *date,
223                         const char *msg, apr_pool_t *pool);
224
225    svn_wc_adm_access_t* adm_access_;
226    apr_allocator_t* allocator_;
227    svn_client_ctx_t* context_;
228    apr_pool_t* pool_;
229    svn_ra_session_t* ra_session_;
230  };
231
232    // Log message receiver
233    struct log_receiver_baton {
234      std::vector<std::string> authors;
235      std::vector<std::string> commit_dates;
236      std::vector<std::string> msg;
237      std::vector<size_t> rev;
238    };
239
240}} // end of namespace svndigest and namespace theplu
241
242#endif
Note: See TracBrowser for help on using the repository browser.