source: trunk/lib/SVN.h @ 225

Last change on this file since 225 was 225, checked in by Peter Johansson, 15 years ago

copyright based on log is printed to cout refs #36

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1#ifndef _theplu_svndigest_svn_
2#define _theplu_svndigest_svn_
3
4// $Id: SVN.h 225 2007-03-11 11:41:51Z peter $
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  ///
40  /// If something goes wrong in the use of the different SVN classes,
41  /// an SVNException is thrown.
42  ///
43  struct SVNException : public std::runtime_error
44  { inline SVNException(const std::string& msg) : runtime_error(msg) {} };
45
46  ///
47  /// The SVN class is a front end to the subversion API.
48  ///
49  /// SVN provides one single global access point to the underlying
50  /// subversion API and makes sure that there is only one point of
51  /// access for the binary.
52  ///
53  /// @see Design Patterns (the singleton pattern). Subversion API
54  /// documents.
55  ///
56  class SVN {
57  public:
58
59    enum vc_status {
60      unversioned=0,
61      uptodate,
62      unresolved
63    };
64
65    ///
66    /// @brief The destructor.
67    ///
68    ~SVN(void);
69
70    ///
71    /// @brief Call the underlying svn_client_blame3 for \a path with
72    /// \a receiver and \a baton.
73    ///
74    /// This function is called from SVNblame to do 'svn blame' on an
75    /// item. The \a receiver and \a baton is defined in SVNblame and
76    /// the \a receiver is called by the underlying subversion API for
77    /// every line in \a path provided it the item is under subversion
78    /// control. The \a baton is used to communicate anonymous
79    /// information through the API to the \a receiver. If \a path is
80    /// a binary object an error is returned, all other errors will
81    /// generate an SVNException.
82    ///
83    /// @return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the
84    /// latter can be used to trigger on binary files. Note that
85    /// errors return from underlying subversion API must be cleared
86    /// by the receiver.
87    ///
88    /// @see Subversion API (svn_error_clear).
89    ///
90    svn_error_t * client_blame(const std::string& path,
91                               svn_client_blame_receiver_t receiver,
92                               void *baton);
93
94    ///
95    /// @brief Call the underlying svn_client_info for \a path with \a
96    /// receiver and \a baton.
97    ///
98    /// This function is called from SVNinfo to do 'svn info' on an
99    /// item. The \a receiver and \a baton is defined in SVNinfo and
100    /// the \a receiver is called by the underlying subversion API if
101    /// \a path is under subversion control. The \a baton is used to
102    /// communicate anonymous information through the API to the
103    /// \a receiver.
104    ///
105    /// @see Subversion API documentation, SVNinfo
106    ///
107    void client_info(const std::string& path, svn_info_receiver_t receiver,
108                     void *baton);
109
110    /**
111       @brief Get the properties for \a path.
112
113       The retrieved properties are stored in \a properties. To check
114       whether \a is a binary item use SVNproperty::binary(void).
115    */
116    void client_proplist(const std::string& path,
117                         std::map<std::string, std::string>& properties);
118
119    ///
120    /// @brief Get revision dates.
121    ///
122    /// Get dates for all commits to the repository. \a root can be a
123    /// repository path or a path within a working copy. In the latter
124    /// case the corresponding repository will be used to retrieve
125    /// commit dates.
126    ///
127    /// @return Revision dates in a vector where revision is
128    /// implicitly defined by vector index, i.e., element 56 in the
129    /// resulting vector implies revision 56.
130    ///
131    /// @note Currently revision 0 (repositoy creation) is not
132    /// supported.
133    ///
134    /// @throw Various error messages generated from the subversion
135    /// API.
136    ///
137    std::vector<std::string> commit_dates(const std::string& path);
138
139    ///
140    /// similar to commit_dates
141    ///
142    std::vector<std::string> authors(const std::string& path);
143    std::vector<size_t> revisions(const std::string& path);
144
145    ///
146    /// @brief Get an instance of SVN.
147    ///
148    /// @throw Throws an SVNException if initialization fails in the
149    /// underlying subversion API calls.
150    ///
151    static SVN* instance(void)
152    { if (!instance_) instance_=new SVN; return instance_; }
153
154    ///
155    /// @throws SVNException if session setup fails.
156    ///
157    void setup_ra_session(const std::string& path);
158
159    ///
160    /// @throws SVNException if access setup fails.
161    ///
162    void setup_wc_adm_access(const std::string& path);
163
164    ///
165    /// @brief Check if entry \a path is under version control
166    ///
167    /// @return True if \a path is under version control, false
168    /// otherwise.
169    ///
170    vc_status version_controlled(const std::string& path);
171
172
173  private:
174    ///
175    /// @brief Constructor
176    ///
177    /// The only way to create a object of SVN type is by calling
178    /// SVN::instance.
179    ///
180    SVN(void);
181
182    ///
183    /// @brief Copy Constructor, not implemented.
184    ///
185    SVN(const SVN&);
186
187    /**
188       @brief Free resources when svn API calls fail.
189
190       This function will write an error message to stdout, free \a
191       err and \a pool resources. If \a err or \a pool are a NULL
192       pointers the function will do nothing with these resources.
193
194       cleanup will throw a SVNException if \a message has
195       length>0. The default bahaviour is to free resources and return
196       normally.
197
198       @see SVNException
199    */
200    void cleanup(svn_error_t *err, apr_pool_t *pool,
201                 const std::string& message=std::string());
202
203    /**
204       @brief Free resources when failing to reach end of
205       constructor.
206
207       cleanup_failed_init will free all resource acquired in the
208       constructor and throw an SVNException with \a message as the
209       message.
210
211       @see SVNException
212    */
213    void cleanup_failed_init(svn_error_t *err, const std::string& message);
214
215    static SVN* instance_;
216
217    // Subversion API stuff
218
219    // Log message receiver
220    struct log_receiver_baton {
221      std::vector<std::string> authors;
222      std::vector<std::string> commit_dates;
223      std::vector<std::string> msg;
224      std::vector<size_t> rev;
225    };
226    static svn_error_t *
227    log_message_receiver(void *baton, apr_hash_t *changed_paths,
228                         svn_revnum_t rev, const char *author, const char *date,
229                         const char *msg, apr_pool_t *pool);
230
231    svn_wc_adm_access_t* adm_access_;
232    apr_allocator_t* allocator_;
233    svn_client_ctx_t* context_;
234    apr_pool_t* pool_;
235    svn_ra_session_t* ra_session_;
236  };
237
238}} // end of namespace svndigest and namespace theplu
239
240#endif
Note: See TracBrowser for help on using the repository browser.