source: trunk/lib/SVN.h @ 138

Last change on this file since 138 was 138, checked in by Jari Häkkinen, 15 years ago

Fixes #23. No system calls to svn are done anymore.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1// $Id: SVN.h 138 2006-08-03 21:40:15Z jari $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen
5
6  This file is part of svnstat, http://lev.thep.lu.se/trac/svnstat
7
8  svnstat is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  svnstat is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#ifndef _theplu_svnstat_svn_
25#define _theplu_svnstat_svn_
26
27#include <stdexcept>
28#include <string>
29#include <vector>
30
31// Jari, most of these can be replaced with forward declarations.
32#include <apr_allocator.h>
33#include <svn_client.h>
34#include <svn_ra.h>
35#include <svn_wc.h>
36
37namespace theplu {
38namespace svnstat {
39
40  ///
41  /// If something goes wrong in the use of the SVN class, a
42  /// SVNException is thrown.
43  ///
44  struct SVNException : public std::runtime_error
45  { inline SVNException(const std::string& msg) : runtime_error(msg) {} };
46
47  ///
48  /// The SVN class is a front end to the subversion API.
49  ///
50  /// SVN provides one single global access point to the underlying
51  /// subversion API and makes sure that there is only one point of
52  /// access for the binary.
53  ///
54  /// @see Design Patterns (the singleton pattern). Subversion API
55  /// documents.
56  ///
57  class SVN {
58  public:
59
60    enum vc_status {
61      unversioned=0,
62      uptodate,
63      unresolved
64    };
65
66    ///
67    /// @brief The destructor.
68    ///
69    ~SVN(void);
70
71    ///
72    /// @brief Call the underlying svn_client_blame3 for \a path with
73    /// \a receiver and \a baton.
74    ///
75    /// This function is called from SVNblame to do 'svn blame' on an
76    /// item. The \a receiver and \a baton is defined in SVNblame and
77    /// the \a receiver is called by the underlying subversion API for
78    /// every line in \a path provided it the item is under subversion
79    /// control. The \a baton is used to communicate anonymous
80    /// information through the API to the \a receiver. If \a path is
81    /// a binary object an error is returned, all other errors will
82    /// generate an SVNException.
83    ///
84    /// @return SVN_NO_ERROR or SVN_ERR_CLIENT_IS_BINARY_FILE, the
85    /// latter can be used to trigger on binary files. Note that
86    /// errors return from underlying subversion API must be cleared
87    /// by the receiver.
88    ///
89    /// @see Subversion API (svn_error_clear).
90    ///
91    svn_error_t * client_blame(const std::string& path,
92                               svn_client_blame_receiver_t receiver,
93                               void *baton);
94
95    ///
96    /// @brief Call the underlying svn_client_info for \a path with \a
97    /// receiver and \a baton.
98    ///
99    /// This function is called from SVNinfo to do 'svn info' on an
100    /// item. The \a receiver and \a baton is defined in SVNinfo and
101    /// the \a receiver is called by the underlying subversion API if
102    /// \a path is under subversion control. The \a baton is used to
103    /// communicate anonymous information through the API to the
104    /// \a receiver.
105    ///
106    /// @see Subversion API documentation, SVNinfo
107    ///
108    inline void client_info(const std::string& path,
109                            svn_info_receiver_t receiver, void *baton)
110    {
111      if (svn_client_info(path.c_str(), NULL, NULL, receiver, baton, false,
112                          context_, pool_))
113        throw SVNException("repository: svn_client_info failed");
114    }
115
116    ///
117    /// @brief Get revision dates.
118    ///
119    /// Get dates for all commits to the repository. \a root can be a
120    /// repository path or a path within a working copy. In the latter
121    /// case the corresponding repository will be used to retrieve
122    /// commit dates.
123    ///
124    /// @return Revision dates in a vector where revision is
125    /// implicitly defined by vector index, i.e., element 56 in the
126    /// resulting vector implies revision 56.
127    ///
128    /// @note Currently revision 0 (repositoy creation) is not
129    /// supported.
130    ///
131    /// @throw Various error messages generated from the subversion
132    /// API.
133    ///
134    std::vector<std::string> commit_dates(const std::string& path);
135
136    ///
137    /// @brief Get an instance of SVN.
138    ///
139    /// @throw Throws an SVNException if initialization fails in the
140    /// underlying subversion API calls.
141    ///
142    /// @todo Make sure that the underlying constructor performs
143    /// proper cleanup before throwing an exception.
144    ///
145    static SVN* instance(void)
146    { if (!instance_) instance_=new SVN; return instance_; }
147
148    ///
149    /// @throws SVNException if session setup fails.
150    ///
151    void setup_ra_session(const std::string& path);
152
153    ///
154    /// @throws SVNException if access setup fails.
155    ///
156    void setup_wc_adm_access(const std::string& path);
157
158    ///
159    /// @brief Check if entry \a path is under version control
160    ///
161    /// @return True if \a path is under version control, false
162    /// otherwise.
163    ///
164    vc_status version_controlled(const std::string& path);
165
166
167  private:
168    ///
169    /// @brief Constructor
170    ///
171    /// The only way to create a object of SVN type is by calling
172    /// SVN::instance.
173    ///
174    /// @todo Make sure that proper cleanup is performed before
175    /// throwing an exception.
176    ///
177    SVN(void);
178
179    ///
180    /// @brief Copy Constructor, not implemented.
181    ///
182    SVN(const SVN&);
183
184    static SVN* instance_;
185
186    // Subversion API stuff
187
188    // Log message receiver
189    struct log_receiver_baton {
190      std::vector<std::string> commit_dates;
191    };
192    static svn_error_t *
193    log_message_receiver(void *baton, apr_hash_t *changed_paths,
194                         svn_revnum_t rev, const char *author, const char *date,
195                         const char *msg, apr_pool_t *pool);
196
197    svn_wc_adm_access_t* adm_access_;
198    apr_allocator_t* allocator_;
199    svn_client_ctx_t* context_;
200    apr_pool_t* pool_;
201    svn_ra_session_t* ra_session_;
202  };
203
204}} // end of namespace svnstat and namespace theplu
205
206#endif
Note: See TracBrowser for help on using the repository browser.