source: trunk/lib/SVN.h @ 233

Last change on this file since 233 was 233, checked in by Peter Johansson, 14 years ago

Refs #117. Initial ci of SVNlog class. Class should be used and functions in SVN could be removed.

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