1 | #ifndef _theplu_svndigest_svn_ |
---|
2 | #define _theplu_svndigest_svn_ |
---|
3 | |
---|
4 | // $Id: SVN.h 408 2007-06-29 10:08:19Z 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://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 | |
---|
36 | namespace theplu { |
---|
37 | namespace 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 Call the underlying svn_client_info for \a path with \a |
---|
106 | receiver and \a baton. |
---|
107 | |
---|
108 | This function is called from SVNinfo to do 'svn info' on an |
---|
109 | item. The \a receiver and \a baton is defined in SVNinfo and |
---|
110 | the \a receiver is called by the underlying subversion API if |
---|
111 | \a path is under subversion control. The \a baton is used to |
---|
112 | communicate anonymous information through the API to the \a |
---|
113 | receiver. |
---|
114 | |
---|
115 | \a path can be either a URL or an WC target. |
---|
116 | |
---|
117 | \see Subversion API documentation, SVNinfo |
---|
118 | */ |
---|
119 | void client_info(const std::string& path, svn_info_receiver_t receiver, |
---|
120 | void *baton); |
---|
121 | |
---|
122 | /** |
---|
123 | \a path can be either a URL or an WC target. |
---|
124 | |
---|
125 | \todo doc |
---|
126 | */ |
---|
127 | void client_log(const std::string& path, svn_log_message_receiver_t receiver, |
---|
128 | void *baton); |
---|
129 | |
---|
130 | /** |
---|
131 | \brief Get the subversion properties for \a path. |
---|
132 | |
---|
133 | The retrieved properties are stored in \a properties. To check |
---|
134 | whether \a is a binary item use SVNproperty::binary(void). |
---|
135 | |
---|
136 | \a path can be either a URL or an WC target. |
---|
137 | */ |
---|
138 | void client_proplist(const std::string& path, |
---|
139 | std::map<std::string, std::string>& properties); |
---|
140 | |
---|
141 | /** |
---|
142 | \brief Get an instance of SVN. |
---|
143 | |
---|
144 | The singleton SVN object should be initialized with |
---|
145 | SVN::instancs(const std::string&) before usage of this |
---|
146 | function. Best practice is to initilize the singleton object |
---|
147 | early in the main program. The logic behind this requirement is |
---|
148 | that subverison related classes and calls may expect that |
---|
149 | repository and WC access is properly set up at initialization. |
---|
150 | |
---|
151 | \throw An SVNException if the singleton SVN onject is not |
---|
152 | already initilized. |
---|
153 | |
---|
154 | \see SVN::instancs(const std::string&) |
---|
155 | */ |
---|
156 | static SVN* instance(void); |
---|
157 | |
---|
158 | /** |
---|
159 | \brief Get an instance of SVN setup against repository pointed |
---|
160 | to by \a path. |
---|
161 | |
---|
162 | The singleton SVN object should be initialized with this |
---|
163 | instance call before any subversion related classes or calls |
---|
164 | are made. Best practice is to initilize the singleton object |
---|
165 | early in the main program. The logic behind this requirement is |
---|
166 | that subverison related classes and calls may expect that |
---|
167 | repository and WC access is properly set up at initialization. |
---|
168 | |
---|
169 | \throw Throws an SVNException if initialization fails in the |
---|
170 | underlying subversion API calls, or if \a path is a URL. |
---|
171 | */ |
---|
172 | static SVN* instance(const std::string& path); |
---|
173 | |
---|
174 | /** |
---|
175 | \brief Set up a repository access session. |
---|
176 | |
---|
177 | \throws SVNException if session setup fails, or if a session is |
---|
178 | already set up (i.e., repository cannot be changed during |
---|
179 | program lifetime). |
---|
180 | */ |
---|
181 | // void setup_ra_session(const std::string& path); |
---|
182 | |
---|
183 | /// |
---|
184 | /// @brief Check if entry \a path is under version control |
---|
185 | /// |
---|
186 | /// @return True if \a path is under version control, false |
---|
187 | /// otherwise. |
---|
188 | /// |
---|
189 | vc_status version_controlled(const std::string& path); |
---|
190 | |
---|
191 | private: |
---|
192 | /** |
---|
193 | \brief Constructor |
---|
194 | |
---|
195 | The only way to create an object of SVN type is by calling |
---|
196 | SVN::instance(const std::string&). \a path must be a WC path, |
---|
197 | i.e., not a URL. |
---|
198 | */ |
---|
199 | SVN(const std::string& path); |
---|
200 | |
---|
201 | /// |
---|
202 | /// @brief Copy Constructor, not implemented. |
---|
203 | /// |
---|
204 | SVN(const SVN&); |
---|
205 | |
---|
206 | /// |
---|
207 | /// @brief The destructor. |
---|
208 | /// |
---|
209 | virtual ~SVN(void); |
---|
210 | |
---|
211 | /** |
---|
212 | @brief Free resources when svn API calls fail. |
---|
213 | |
---|
214 | This function will write an error message to stdout, free \a |
---|
215 | err and \a pool resources. If \a err or \a pool are a NULL |
---|
216 | pointers the function will do nothing with these resources. |
---|
217 | |
---|
218 | cleanup will throw a SVNException if \a message has |
---|
219 | length>0. The default bahaviour is to free resources and return |
---|
220 | normally. |
---|
221 | |
---|
222 | @see SVNException |
---|
223 | */ |
---|
224 | void cleanup(svn_error_t *err, apr_pool_t *pool, |
---|
225 | const std::string& message=std::string()); |
---|
226 | |
---|
227 | /** |
---|
228 | @brief Free resources when failing to reach end of |
---|
229 | constructor. |
---|
230 | |
---|
231 | cleanup_failed_init will free all resource acquired in the |
---|
232 | constructor and throw an SVNException with \a message as the |
---|
233 | message. |
---|
234 | |
---|
235 | @see SVNException |
---|
236 | */ |
---|
237 | void cleanup_failed_init(svn_error_t *err, const std::string& message); |
---|
238 | |
---|
239 | static SVN* instance_; |
---|
240 | |
---|
241 | // Subversion API stuff |
---|
242 | |
---|
243 | /** |
---|
244 | the url is fech with svn info. The ursl is stored in a |
---|
245 | url_receiver_baton. The struct is filled in the url_receiver |
---|
246 | function. |
---|
247 | */ |
---|
248 | struct root_url_receiver_baton { |
---|
249 | std::string path; |
---|
250 | }; |
---|
251 | |
---|
252 | /** |
---|
253 | url_receiver is the function passed to the underlying |
---|
254 | subversion API call svn_client_info. This function is called by |
---|
255 | the subversion API for every item matched by the conditions of |
---|
256 | the API call. |
---|
257 | |
---|
258 | \see Subversion API documentation |
---|
259 | */ |
---|
260 | static svn_error_t* |
---|
261 | root_url_receiver(void *baton, const char *path, const svn_info_t *info, |
---|
262 | apr_pool_t *pool); |
---|
263 | |
---|
264 | svn_wc_adm_access_t* adm_access_; |
---|
265 | apr_allocator_t* allocator_; |
---|
266 | svn_client_ctx_t* context_; |
---|
267 | apr_pool_t* pool_; |
---|
268 | svn_ra_session_t* ra_session_; |
---|
269 | }; |
---|
270 | |
---|
271 | }} // end of namespace svndigest and namespace theplu |
---|
272 | |
---|
273 | #endif |
---|