-
-
Notifications
You must be signed in to change notification settings - Fork 404
Expand file tree
/
Copy pathutils.c
More file actions
331 lines (283 loc) · 8.29 KB
/
Copy pathutils.c
File metadata and controls
331 lines (283 loc) · 8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright 2010-2026 The pygit2 contributors
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "error.h"
#include "utils.h"
extern PyTypeObject ReferenceType;
extern PyTypeObject TreeType;
extern PyTypeObject CommitType;
extern PyTypeObject BlobType;
extern PyTypeObject TagType;
/**
* Attempt to convert a C string to a Python string with the given encoding.
* If the conversion fails, return a fallback string.
*/
PyObject *
to_unicode_safe(const char *value, const char *encoding)
{
PyObject *py_str;
if (!value) {
py_str = PyUnicode_FromString("None");
} else {
py_str = to_unicode(value, encoding, "replace");
if (!py_str) {
assert(PyErr_Occurred());
py_str = PyUnicode_FromString("(error)");
PyErr_Clear();
}
}
assert(!PyErr_Occurred());
assert(py_str);
return py_str;
}
char*
pgit_borrow_fsdefault(PyObject *value, PyObject **tvalue)
{
PyObject *str = PyOS_FSPath(value);
if (str == NULL) {
return NULL;
}
PyObject *bytes = PyUnicode_EncodeFSDefault(str);
if (bytes == NULL) {
return NULL;
}
*tvalue = bytes;
return PyBytes_AS_STRING(bytes);
}
/**
* Return a borrowed C string for a path inside a Git repository.
*
* The input may be a str, bytes, or os.PathLike. str/PathLike values are
* encoded as UTF-8 (with surrogateescape for round-trip of non-UTF-8 bytes).
* On macOS they are also normalized to NFC, matching Git's
* core.precomposeunicode default behaviour. bytes values are returned
* unchanged as raw path bytes.
*/
static PyObject *unicode_normalize = NULL;
static int
ensure_unicode_normalize(void)
{
if (unicode_normalize != NULL) {
return 0;
}
PyObject *mod = PyImport_ImportModule("unicodedata");
if (mod == NULL) {
return -1;
}
unicode_normalize = PyObject_GetAttrString(mod, "normalize");
Py_DECREF(mod);
if (unicode_normalize == NULL) {
return -1;
}
return 0;
}
char*
pgit_borrow_gitpath(PyObject *value, PyObject **tvalue)
{
PyObject *py_path = NULL;
if (PyUnicode_Check(value)) {
py_path = value;
Py_INCREF(py_path);
} else if (PyBytes_Check(value)) {
Py_INCREF(value);
*tvalue = value;
return PyBytes_AsString(value);
} else {
py_path = PyOS_FSPath(value);
if (py_path == NULL) {
return NULL;
}
}
if (PyBytes_Check(py_path)) {
*tvalue = py_path;
return PyBytes_AsString(py_path);
}
#ifdef __APPLE__
if (ensure_unicode_normalize() < 0) {
Py_DECREF(py_path);
return NULL;
}
PyObject *form = PyUnicode_FromString("NFC");
if (form == NULL) {
Py_DECREF(py_path);
return NULL;
}
PyObject *normalized = PyObject_CallFunctionObjArgs(
unicode_normalize, form, py_path, NULL);
Py_DECREF(form);
Py_DECREF(py_path);
if (normalized == NULL) {
return NULL;
}
PyObject *bytes = PyUnicode_AsEncodedString(
normalized, "utf-8", "surrogateescape");
Py_DECREF(normalized);
if (bytes == NULL) {
return NULL;
}
#else
PyObject *bytes = PyUnicode_AsEncodedString(
py_path, "utf-8", "surrogateescape");
Py_DECREF(py_path);
if (bytes == NULL) {
return NULL;
}
#endif
*tvalue = bytes;
return PyBytes_AsString(bytes);
}
/**
* Return a pointer to the underlying C string in 'value'. The pointer is
* guaranteed by 'tvalue', decrease its refcount when done with the string.
*/
const char*
pgit_borrow_encoding(PyObject *value, const char *encoding, const char *errors, PyObject **tvalue)
{
PyObject *py_value = NULL;
PyObject *py_str = NULL;
py_value = PyOS_FSPath(value);
if (py_value == NULL) {
Error_type_error("unexpected %.200s", value);
return NULL;
}
// Get new PyBytes reference from value
if (PyUnicode_Check(py_value)) { // Text string
py_str = PyUnicode_AsEncodedString(
py_value,
encoding ? encoding : "utf-8",
errors ? errors : "strict"
);
Py_DECREF(py_value);
if (py_str == NULL)
return NULL;
} else if (PyBytes_Check(py_value)) { // Byte string
py_str = py_value;
} else { // Type error
Error_type_error("unexpected %.200s", value);
Py_DECREF(py_value);
return NULL;
}
// Borrow c string from the new PyBytes reference
char *c_str = PyBytes_AsString(py_str);
if (c_str == NULL) {
Py_DECREF(py_str);
return NULL;
}
// Return the borrowed c string and the new PyBytes reference
*tvalue = py_str;
return c_str;
}
/**
* Return a borrowed c string with the representation of the given Unicode or
* Bytes object:
* - If value is Unicode return the UTF-8 representation
* - If value is Bytes return the raw sttring
* In both cases the returned string is owned by value and must not be
* modified, nor freed.
*/
const char*
pgit_borrow(PyObject *value)
{
if (PyUnicode_Check(value)) { // Text string
return PyUnicode_AsUTF8(value);
} else if (PyBytes_Check(value)) { // Byte string
return PyBytes_AsString(value);
}
// Type error
Error_type_error("unexpected %.200s", value);
return NULL;
}
char*
pgit_strdup(PyObject *value)
{
const char *str;
char *copy;
size_t len;
if (PyUnicode_Check(value)) {
str = PyUnicode_AsUTF8(value);
if (str == NULL)
return NULL;
len = strlen(str);
}
else {
Error_type_error("unexpected %.200s", value);
return NULL;
}
copy = malloc(len + 1);
if (copy == NULL) {
PyErr_NoMemory();
return NULL;
}
memcpy(copy, str, len + 1);
return copy;
}
static git_otype
py_type_to_git_type(PyTypeObject *py_type)
{
if (py_type == &CommitType)
return GIT_OBJECT_COMMIT;
else if (py_type == &TreeType)
return GIT_OBJECT_TREE;
else if (py_type == &BlobType)
return GIT_OBJECT_BLOB;
else if (py_type == &TagType)
return GIT_OBJECT_TAG;
PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJECT_INVALID; /* -1 */
}
git_otype
py_object_to_otype(PyObject *py_type)
{
long value;
if (py_type == Py_None)
return GIT_OBJECT_ANY;
if (PyLong_Check(py_type)) {
value = PyLong_AsLong(py_type);
if (value == -1 && PyErr_Occurred())
return GIT_OBJECT_INVALID;
/* TODO Check whether the value is a valid value */
return (git_otype)value;
}
if (PyType_Check(py_type))
return py_type_to_git_type((PyTypeObject *) py_type);
PyErr_SetString(PyExc_ValueError, "invalid target type");
return GIT_OBJECT_INVALID; /* -1 */
}
/**
* Convert an integer to a reference to an IntEnum or IntFlag in pygit2.enums.
*/
PyObject *
pygit2_enum(PyObject *enum_type, int value)
{
if (!enum_type) {
PyErr_SetString(PyExc_TypeError, "an enum has not been cached in _pygit2.cache_enums()");
return NULL;
}
PyObject *enum_instance = PyObject_CallFunction(enum_type, "(i)", value);
return enum_instance;
}