Add wc.Context.locked.
[jelmer/subvertpy.git] / subvertpy / util.h
1 /*
2  * Copyright © 2008 Jelmer Vernooij <jelmer@jelmer.uk>
3  * -*- coding: utf-8 -*-
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #ifndef _SUBVERTPY_UTIL_H_
21 #define _SUBVERTPY_UTIL_H_
22
23 #include <svn_version.h>
24 #include <svn_io.h>  /* for svn_stream_t */
25
26 #if SVN_VER_MAJOR != 1
27 #error "only svn 1.x is supported"
28 #endif
29
30 #ifdef SUBVERTPY_OVERRIDE_SVN_VER_MINOR
31 #define ONLY_SINCE_SVN(maj, min) (SUBVERTPY_OVERRIDE_SVN_VER_MINOR >= (min))
32 #else
33 #define ONLY_SINCE_SVN(maj, min) (SVN_VER_MINOR >= (min))
34 #endif
35
36 #define ONLY_BEFORE_SVN(maj, min) (!(ONLY_SINCE_SVN(maj, min)))
37
38 #ifdef __GNUC__
39 #pragma GCC visibility push(hidden)
40 #endif
41
42 svn_error_t *py_cancel_check(void *cancel_baton);
43 __attribute__((warn_unused_result)) apr_pool_t *Pool(apr_pool_t *parent);
44 void handle_svn_error(svn_error_t *error);
45 bool string_list_to_apr_array(apr_pool_t *pool, PyObject *l, apr_array_header_t **);
46 bool relpath_list_to_apr_array(apr_pool_t *pool, PyObject *l, apr_array_header_t **);
47 PyObject *prop_hash_to_dict(apr_hash_t *props);
48 apr_hash_t *prop_dict_to_hash(apr_pool_t *pool, PyObject *py_props);
49 svn_error_t *py_svn_log_wrapper(
50     void *baton, apr_hash_t *changed_paths, long revision, const char *author,
51     const char *date, const char *message, apr_pool_t *pool);
52 __attribute__((warn_unused_result)) svn_error_t *py_svn_error(void);
53 void PyErr_SetSubversionException(svn_error_t *error);
54 PyTypeObject *PyErr_GetSubversionExceptionTypeObject(void);
55
56 #define RUN_SVN(cmd) { \
57     svn_error_t *err; \
58     PyThreadState *_save; \
59     _save = PyEval_SaveThread(); \
60     err = (cmd); \
61     PyEval_RestoreThread(_save); \
62     if (err != NULL) { \
63         handle_svn_error(err); \
64         svn_error_clear(err); \
65         return NULL; \
66     } \
67 }
68
69 #define RUN_SVN_WITH_POOL(pool, cmd) { \
70     svn_error_t *err; \
71     PyThreadState *_save; \
72     _save = PyEval_SaveThread(); \
73     err = (cmd); \
74     PyEval_RestoreThread(_save); \
75     if (err != NULL) { \
76         handle_svn_error(err); \
77         svn_error_clear(err); \
78         apr_pool_destroy(pool); \
79         return NULL; \
80     } \
81 }
82
83 PyObject *wrap_lock(svn_lock_t *lock);
84 apr_array_header_t *revnum_list_to_apr_array(apr_pool_t *pool, PyObject *l);
85 svn_stream_t *new_py_stream(apr_pool_t *pool, PyObject *py);
86 PyObject *PyErr_NewSubversionException(svn_error_t *error);
87 apr_hash_t *config_hash_from_object(PyObject *config, apr_pool_t *pool);
88 void PyErr_SetAprStatus(apr_status_t status);
89 PyObject *py_dirent(const svn_dirent_t *dirent, int dirent_fields);
90 PyObject *PyOS_tmpfile(void);
91 PyObject *pyify_changed_paths(apr_hash_t *changed_paths, bool node_kind, apr_pool_t *pool);
92 bool pyify_log_message(
93     apr_hash_t *changed_paths, const char *author,
94     const char *date, const char *message, bool node_kind,
95     apr_pool_t *pool, PyObject **py_changed_paths, PyObject **revprops);
96 #if ONLY_SINCE_SVN(1, 6)
97 PyObject *pyify_changed_paths2(apr_hash_t *changed_paths2, apr_pool_t *pool);
98 #endif
99 apr_file_t *apr_file_from_object(PyObject *object, apr_pool_t *pool);
100
101 #if ONLY_SINCE_SVN(1, 5)
102 svn_error_t *py_svn_log_entry_receiver(void *baton, svn_log_entry_t *log_entry, apr_pool_t *pool);
103 #endif
104
105 #ifdef __GNUC__
106 #pragma GCC visibility pop
107 #endif
108
109 #define CB_CHECK_PYRETVAL(ret) \
110     if (ret == NULL) { \
111         PyGILState_Release(state); \
112         return py_svn_error(); \
113     }
114
115 #if SVN_VER_MINOR < 5
116 typedef enum svn_depth_t {
117     svn_depth_unknown = -2,
118     svn_depth_exclude = -1,
119     svn_depth_empty = 0,
120     svn_depth_files = 1,
121     svn_depth_immediates = 2,
122     svn_depth_infinity = 3
123 } svn_depth_t;
124 #endif
125
126 typedef struct {
127     PyObject_HEAD
128     apr_hash_t *config;
129     apr_pool_t *pool;
130 } ConfigObject;
131
132 typedef struct {
133     PyObject_HEAD
134     svn_stream_t *stream;
135     apr_pool_t *pool;
136     bool closed;
137 } StreamObject;
138
139 extern PyTypeObject Stream_Type;
140
141 #if ONLY_BEFORE_SVN(1, 7)
142 const char *
143 svn_uri_canonicalize(const char *uri,
144                      apr_pool_t *result_pool);
145 const char *
146 svn_relpath_canonicalize(const char *relpath,
147                          apr_pool_t *result_pool);
148
149 const char *
150 svn_dirent_canonicalize(const char *dirent,
151                         apr_pool_t *result_pool);
152 #endif
153
154 const char *py_object_to_svn_uri(PyObject *obj, apr_pool_t *pool);
155 const char *py_object_to_svn_dirent(PyObject *obj, apr_pool_t *pool);
156 const char *py_object_to_svn_relpath(PyObject *obj, apr_pool_t *pool);
157 const char *py_object_to_svn_path_or_url(PyObject *obj, apr_pool_t *pool);
158 char *py_object_to_svn_string(PyObject *obj, apr_pool_t *pool);
159 const char *py_object_to_svn_abspath(PyObject *obj, apr_pool_t *pool);
160 #define py_object_from_svn_abspath PyBytes_FromString
161
162 #if PY_MAJOR_VERSION >= 3
163 #define PyRepr_FromFormat PyUnicode_FromFormat
164 #else
165 #define PyRepr_FromFormat PyString_FromFormat
166 #endif
167
168 #if PY_MAJOR_VERSION >= 3
169 #define py_from_svn_revnum PyLong_FromLong
170 #define py_to_svn_revnum PyLong_AsLong
171 #else
172 #define py_from_svn_revnum PyInt_FromLong
173 #define py_to_svn_revnum PyInt_AsLong
174 #endif
175
176 #endif /* _SUBVERTPY_UTIL_H_ */