source: trunk/lib/SVN.h @ 234

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

fixes #87 and #95 and #103 and #104 and #80 and #108, and refs #69

  • 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 234 2007-04-09 12:08:26Z 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    /// @brief Get an instance of SVN.
149    ///
150    /// @throw Throws an SVNException if initialization fails in the
151    /// underlying subversion API calls.
152    ///
153    static SVN* instance(void)
154    { if (!instance_) instance_=new SVN; return instance_; }
155
156    ///
157    /// @throws SVNException if session setup fails.
158    ///
159    void setup_ra_session(const std::string& path);
160
161    ///
162    /// @throws SVNException if access setup fails.
163    ///
164    void setup_wc_adm_access(const std::string& path);
165
166    ///
167    /// @brief Check if entry \a path is under version control
168    ///
169    /// @return True if \a path is under version control, false
170    /// otherwise.
171    ///
172    vc_status version_controlled(const std::string& path);
173
174  private:
175    ///
176    /// @brief Constructor
177    ///
178    /// The only way to create a object of SVN type is by calling
179    /// SVN::instance.
180    ///
181    SVN(void);
182
183    ///
184    /// @brief Copy Constructor, not implemented.
185    ///
186    SVN(const SVN&);
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.