source: trunk/lib/SVN.h @ 791

Last change on this file since 791 was 693, checked in by Jari Häkkinen, 13 years ago

Fixes #339. Change to GPLv3.

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