1 | #ifndef _theplu_svndigest_svn_ |
---|
2 | #define _theplu_svndigest_svn_ |
---|
3 | |
---|
4 | // $Id: SVN.h 575 2008-03-18 13:27:27Z peter $ |
---|
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://trac.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software |
---|
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
26 | 02111-1307, USA. |
---|
27 | */ |
---|
28 | |
---|
29 | #include <map> |
---|
30 | #include <stdexcept> |
---|
31 | #include <string> |
---|
32 | #include <vector> |
---|
33 | |
---|
34 | #include <subversion-1/svn_client.h> |
---|
35 | #include <subversion-1/svn_types.h> |
---|
36 | |
---|
37 | namespace theplu { |
---|
38 | namespace svndigest { |
---|
39 | |
---|
40 | struct log_receiver_baton; |
---|
41 | |
---|
42 | /// |
---|
43 | /// If something goes wrong in the use of the different SVN classes, |
---|
44 | /// an SVNException is thrown. |
---|
45 | /// |
---|
46 | struct SVNException : public std::runtime_error |
---|
47 | { inline SVNException(const std::string& msg) : runtime_error(msg) {} }; |
---|
48 | |
---|
49 | /** |
---|
50 | \brief The SVN class is a front end to the subversion API. |
---|
51 | |
---|
52 | SVN provides one single global access point to the underlying |
---|
53 | subversion API and makes sure that there is only one point of |
---|
54 | access for the binary. |
---|
55 | |
---|
56 | The singleton SVN object should be initialized with |
---|
57 | SVN::instancs(const std::string& path), rather than |
---|
58 | SVN::instance(void), before using any other subversion related |
---|
59 | classes or calls. Best practice is to initilize the singleton |
---|
60 | object early in the main program. The logic behind this |
---|
61 | requirement is that all subverison related classes and calls |
---|
62 | expect that repository and WC access is properly set up at |
---|
63 | initialization. However, most functionality is available |
---|
64 | irrespectively which instance call is made. |
---|
65 | |
---|
66 | \see Design Patterns (the singleton pattern). Subversion API |
---|
67 | documents, SVN::instancs(void), SVN::instancs(const |
---|
68 | std::string&). |
---|
69 | */ |
---|
70 | class SVN { |
---|
71 | public: |
---|
72 | |
---|
73 | enum vc_status { |
---|
74 | unversioned=0, |
---|
75 | uptodate, |
---|
76 | unresolved |
---|
77 | }; |
---|
78 | |
---|
79 | /** |
---|
80 | \brief Call the underlying svn_client_blame3 for \a path with |
---|
81 | \a receiver and \a baton. |
---|
82 | |
---|
83 | This function is called from SVNblame to do 'svn blame' on an |
---|
84 | item. The \a receiver and \a baton is defined in SVNblame and |
---|
85 | the \a receiver is called by the underlying subversion API for |
---|
86 | every line in \a path provided it the item is under subversion |
---|
87 | control. The \a baton is used to communicate anonymous |
---|
88 | information through the API to the \a receiver. If \a path is a |
---|
89 | binary object an error is returned, all other errors will |
---|
90 | generate an SVNException. |
---|
91 | |
---|
92 | \a path can be either a URL or an WC target. |
---|
93 | |
---|
94 | \return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the |
---|
95 | latter can be used to trigger on binary files. Note that errors |
---|
96 | return from underlying subversion API must be cleared by the |
---|
97 | receiver. |
---|
98 | |
---|
99 | \see Subversion API (svn_error_clear). |
---|
100 | */ |
---|
101 | svn_error_t * client_blame(const std::string& path, |
---|
102 | svn_client_blame_receiver_t receiver, |
---|
103 | void *baton); |
---|
104 | |
---|
105 | /** |
---|
106 | \brief Call the underlying svn_client_info for \a path with \a |
---|
107 | receiver and \a baton. |
---|
108 | |
---|
109 | This function is called from SVNinfo to do 'svn info' on an |
---|
110 | item. The \a receiver and \a baton is defined in SVNinfo and |
---|
111 | the \a receiver is called by the underlying subversion API if |
---|
112 | \a path is under subversion control. The \a baton is used to |
---|
113 | communicate anonymous information through the API to the \a |
---|
114 | receiver. |
---|
115 | |
---|
116 | \a path can be either a URL or an WC target. |
---|
117 | |
---|
118 | \see Subversion API documentation, SVNinfo |
---|
119 | */ |
---|
120 | void client_info(const std::string& path, svn_info_receiver_t receiver, |
---|
121 | void *baton); |
---|
122 | |
---|
123 | /** |
---|
124 | \a path can be either a URL or an WC target. |
---|
125 | |
---|
126 | \todo doc |
---|
127 | */ |
---|
128 | void client_log(const std::string& path, svn_log_message_receiver_t receiver, |
---|
129 | void *baton); |
---|
130 | |
---|
131 | /** |
---|
132 | \brief Get the subversion properties for \a path. |
---|
133 | |
---|
134 | The retrieved properties are stored in \a properties. To check |
---|
135 | whether \a is a binary item use SVNproperty::binary(void). |
---|
136 | |
---|
137 | \a path can be either a URL or an WC target. |
---|
138 | */ |
---|
139 | void client_proplist(const std::string& path, |
---|
140 | std::map<std::string, std::string>& properties); |
---|
141 | |
---|
142 | /** |
---|
143 | \brief Get an instance of SVN. |
---|
144 | |
---|
145 | The singleton SVN object should be initialized with |
---|
146 | SVN::instancs(const std::string&) before usage of this |
---|
147 | function. Best practice is to initilize the singleton object |
---|
148 | early in the main program. The logic behind this requirement is |
---|
149 | that subverison related classes and calls may expect that |
---|
150 | repository and WC access is properly set up at initialization. |
---|
151 | |
---|
152 | \throw An SVNException if the singleton SVN onject is not |
---|
153 | already initilized. |
---|
154 | |
---|
155 | \see SVN::instancs(const std::string&) |
---|
156 | */ |
---|
157 | static SVN* instance(void); |
---|
158 | |
---|
159 | /** |
---|
160 | \brief Get an instance of SVN setup against repository pointed |
---|
161 | to by \a path. |
---|
162 | |
---|
163 | The singleton SVN object should be initialized with this |
---|
164 | instance call before any subversion related classes or calls |
---|
165 | are made. Best practice is to initilize the singleton object |
---|
166 | early in the main program. The logic behind this requirement is |
---|
167 | that subverison related classes and calls may expect that |
---|
168 | repository and WC access is properly set up at initialization. |
---|
169 | |
---|
170 | \throw Throws an SVNException if initialization fails in the |
---|
171 | underlying subversion API calls, or if \a path is a URL. |
---|
172 | */ |
---|
173 | static SVN* instance(const std::string& path); |
---|
174 | |
---|
175 | /** |
---|
176 | \brief Set up a repository access session. |
---|
177 | |
---|
178 | \throws SVNException if session setup fails, or if a session is |
---|
179 | already set up (i.e., repository cannot be changed during |
---|
180 | program lifetime). |
---|
181 | */ |
---|
182 | // void setup_ra_session(const std::string& path); |
---|
183 | |
---|
184 | /// |
---|
185 | /// @brief Check if entry \a path is under version control |
---|
186 | /// |
---|
187 | /// @return True if \a path is under version control, false |
---|
188 | /// otherwise. |
---|
189 | /// |
---|
190 | vc_status version_controlled(const std::string& path); |
---|
191 | |
---|
192 | private: |
---|
193 | /** |
---|
194 | \brief Constructor |
---|
195 | |
---|
196 | The only way to create an object of SVN type is by calling |
---|
197 | SVN::instance(const std::string&). \a path must be a WC path, |
---|
198 | i.e., not a URL. |
---|
199 | */ |
---|
200 | SVN(const std::string& path); |
---|
201 | |
---|
202 | /// |
---|
203 | /// @brief Copy Constructor, not implemented. |
---|
204 | /// |
---|
205 | SVN(const SVN&); |
---|
206 | SVN& operator=(const SVN&); |
---|
207 | |
---|
208 | /// |
---|
209 | /// @brief The destructor. |
---|
210 | /// |
---|
211 | virtual ~SVN(void); |
---|
212 | |
---|
213 | /** |
---|
214 | @brief Free resources when svn API calls fail. |
---|
215 | |
---|
216 | This function will write an error message to stdout, free \a |
---|
217 | err and \a pool resources. If \a err or \a pool are a NULL |
---|
218 | pointers the function will do nothing with these resources. If |
---|
219 | \a mute is true, no error message is written. |
---|
220 | |
---|
221 | cleanup will throw a SVNException if \a message has |
---|
222 | length>0. The default bahaviour is to free resources and return |
---|
223 | normally. |
---|
224 | |
---|
225 | @see SVNException |
---|
226 | */ |
---|
227 | void cleanup(svn_error_t *err, apr_pool_t *pool, |
---|
228 | std::string message=std::string(), bool mute=false); |
---|
229 | |
---|
230 | /** |
---|
231 | @brief Free resources when failing to reach end of |
---|
232 | constructor. |
---|
233 | |
---|
234 | cleanup_failed_init will free all resource acquired in the |
---|
235 | constructor and throw an SVNException with \a message as the |
---|
236 | message. |
---|
237 | |
---|
238 | @see SVNException |
---|
239 | */ |
---|
240 | void cleanup_failed_init(svn_error_t *err, const std::string& message, |
---|
241 | bool mute=false); |
---|
242 | |
---|
243 | static SVN* instance_; |
---|
244 | |
---|
245 | // Subversion API stuff |
---|
246 | |
---|
247 | /** |
---|
248 | the url is fech with svn info. The ursl is stored in a |
---|
249 | url_receiver_baton. The struct is filled in the url_receiver |
---|
250 | function. |
---|
251 | */ |
---|
252 | struct root_url_receiver_baton { |
---|
253 | std::string path; |
---|
254 | }; |
---|
255 | |
---|
256 | /** |
---|
257 | url_receiver is the function passed to the underlying |
---|
258 | subversion API call svn_client_info. This function is called by |
---|
259 | the subversion API for every item matched by the conditions of |
---|
260 | the API call. |
---|
261 | |
---|
262 | \see Subversion API documentation |
---|
263 | */ |
---|
264 | static svn_error_t* |
---|
265 | root_url_receiver(void *baton, const char *path, const svn_info_t *info, |
---|
266 | apr_pool_t *pool); |
---|
267 | |
---|
268 | svn_wc_adm_access_t* adm_access_; |
---|
269 | apr_allocator_t* allocator_; |
---|
270 | svn_client_ctx_t* context_; |
---|
271 | apr_pool_t* pool_; |
---|
272 | svn_ra_session_t* ra_session_; |
---|
273 | }; |
---|
274 | |
---|
275 | }} // end of namespace svndigest and namespace theplu |
---|
276 | |
---|
277 | #endif |
---|