source: trunk/lib/SVN.h @ 519

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

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.6 KB
Line 
1#ifndef _theplu_svndigest_svn_
2#define _theplu_svndigest_svn_
3
4// $Id: SVN.h 519 2007-12-23 20:14:50Z jari $
5
6/*
7  Copyright (C) 2006 Jari Häkkinen
8  Copyright (C) 2007 Jari Häkkinen, Peter Johansson
9
10  This file is part of svndigest, http://trac.thep.lu.se/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     \brief 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     The singleton SVN object should be initialized with
56     SVN::instancs(const std::string& path), rather than
57     SVN::instance(void), before using any other subversion related
58     classes or calls. Best practice is to initilize the singleton
59     object early in the main program. The logic behind this
60     requirement is that all subverison related classes and calls
61     expect that repository and WC access is properly set up at
62     initialization. However, most functionality is available
63     irrespectively which instance call is made.
64
65     \see Design Patterns (the singleton pattern). Subversion API
66     documents, SVN::instancs(void), SVN::instancs(const
67     std::string&).
68  */
69  class SVN {
70  public:
71
72    enum vc_status {
73      unversioned=0,
74      uptodate,
75      unresolved
76    };
77
78    /**
79       \brief Call the underlying svn_client_blame3 for \a path with
80       \a receiver and \a baton.
81
82       This function is called from SVNblame to do 'svn blame' on an
83       item. The \a receiver and \a baton is defined in SVNblame and
84       the \a receiver is called by the underlying subversion API for
85       every line in \a path provided it the item is under subversion
86       control. The \a baton is used to communicate anonymous
87       information through the API to the \a receiver. If \a path is a
88       binary object an error is returned, all other errors will
89       generate an SVNException.
90
91       \a path can be either a URL or an WC target.
92
93       \return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the
94       latter can be used to trigger on binary files. Note that errors
95       return from underlying subversion API must be cleared by the
96       receiver.
97
98       \see Subversion API (svn_error_clear).
99    */
100    svn_error_t * client_blame(const std::string& path,
101                               svn_client_blame_receiver_t receiver,
102                               void *baton);
103
104    /**
105       \brief Same as function above with the extension that revision
106       \a rev can be set.
107     */
108    svn_error_t * client_blame(const std::string& path,
109                               svn_client_blame_receiver_t receiver,
110                               void *baton, svn_revnum_t rev);
111
112    /**
113       \brief Call the underlying svn_client_info for \a path with \a
114       receiver and \a baton.
115
116       This function is called from SVNinfo to do 'svn info' on an
117       item. The \a receiver and \a baton is defined in SVNinfo and
118       the \a receiver is called by the underlying subversion API if
119       \a path is under subversion control. The \a baton is used to
120       communicate anonymous information through the API to the \a
121       receiver.
122
123       \a path can be either a URL or an WC target.
124
125       \see Subversion API documentation, SVNinfo
126    */
127    void client_info(const std::string& path, svn_info_receiver_t receiver,
128                     void *baton);
129
130    /**
131       \a path can be either a URL or an WC target.
132
133       \todo doc
134    */
135    void client_log(const std::string& path, svn_log_message_receiver_t receiver,
136                    void *baton);
137
138    /**
139       \brief Get the subversion properties for \a path.
140
141       The retrieved properties are stored in \a properties. To check
142       whether \a is a binary item use SVNproperty::binary(void).
143
144       \a path can be either a URL or an WC target.
145    */
146    void client_proplist(const std::string& path,
147                         std::map<std::string, std::string>& properties);
148
149    /**
150       \brief Get an instance of SVN.
151
152       The singleton SVN object should be initialized with
153       SVN::instancs(const std::string&) before usage of this
154       function. Best practice is to initilize the singleton object
155       early in the main program. The logic behind this requirement is
156       that subverison related classes and calls may expect that
157       repository and WC access is properly set up at initialization.
158
159       \throw An SVNException if the singleton SVN onject is not
160       already initilized.
161
162       \see SVN::instancs(const std::string&)
163    */
164    static SVN* instance(void);
165
166    /**
167       \brief Get an instance of SVN setup against repository pointed
168       to by \a path.
169
170       The singleton SVN object should be initialized with this
171       instance call before any subversion related classes or calls
172       are made. Best practice is to initilize the singleton object
173       early in the main program. The logic behind this requirement is
174       that subverison related classes and calls may expect that
175       repository and WC access is properly set up at initialization.
176
177       \throw Throws an SVNException if initialization fails in the
178       underlying subversion API calls, or if \a path is a URL.
179    */
180    static SVN* instance(const std::string& path);
181
182    /**
183       \brief Set up a repository access session.
184
185       \throws SVNException if session setup fails, or if a session is
186       already set up (i.e., repository cannot be changed during
187       program lifetime).
188    */
189    //    void setup_ra_session(const std::string& path);
190
191    ///
192    /// @brief Check if entry \a path is under version control
193    ///
194    /// @return True if \a path is under version control, false
195    /// otherwise.
196    ///
197    vc_status version_controlled(const std::string& path);
198
199  private:
200    /**
201       \brief Constructor
202
203       The only way to create an object of SVN type is by calling
204       SVN::instance(const std::string&). \a path must be a WC path,
205       i.e., not a URL.
206    */
207    SVN(const std::string& path);
208
209    ///
210    /// @brief Copy Constructor, not implemented.
211    ///
212    SVN(const SVN&);
213
214    ///
215    /// @brief The destructor.
216    ///
217    virtual ~SVN(void);
218
219    /**
220       @brief Free resources when svn API calls fail.
221
222       This function will write an error message to stdout, free \a
223       err and \a pool resources. If \a err or \a pool are a NULL
224       pointers the function will do nothing with these resources.
225
226       cleanup will throw a SVNException if \a message has
227       length>0. The default bahaviour is to free resources and return
228       normally.
229
230       @see SVNException
231    */
232    void cleanup(svn_error_t *err, apr_pool_t *pool,
233                 const std::string& message=std::string());
234
235    /**
236       @brief Free resources when failing to reach end of
237       constructor.
238
239       cleanup_failed_init will free all resource acquired in the
240       constructor and throw an SVNException with \a message as the
241       message.
242
243       @see SVNException
244    */
245    void cleanup_failed_init(svn_error_t *err, const std::string& message);
246
247    static SVN* instance_;
248
249    // Subversion API stuff
250
251    /**
252       the url is fech with svn info. The ursl is stored in a
253       url_receiver_baton. The struct is filled in the url_receiver
254       function.
255    */
256    struct root_url_receiver_baton {
257      std::string path;
258    };
259
260    /**
261       url_receiver is the function passed to the underlying
262       subversion API call svn_client_info. This function is called by
263       the subversion API for every item matched by the conditions of
264       the API call.
265
266       \see Subversion API documentation
267    */
268    static svn_error_t*
269    root_url_receiver(void *baton, const char *path, const svn_info_t *info,
270                      apr_pool_t *pool);
271
272    svn_error_t* client_blame_call(const std::string& path,
273                                   svn_client_blame_receiver_t receiver,
274                                   void *baton, svn_opt_revision_t& head);
275
276    svn_wc_adm_access_t* adm_access_;
277    apr_allocator_t* allocator_;
278    svn_client_ctx_t* context_;
279    apr_pool_t* pool_;
280    svn_ra_session_t* ra_session_;
281  };
282
283}} // end of namespace svndigest and namespace theplu
284
285#endif
Note: See TracBrowser for help on using the repository browser.