2
0

_diff_tree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Copyright (C) 2010 Google, Inc.
  3. *
  4. * Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. * General Public License as public by the Free Software Foundation; version 2.0
  6. * or (at your option) any later version. You can redistribute it and/or
  7. * modify it under the terms of either of these two licenses.
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. *
  15. * You should have received a copy of the licenses; if not, see
  16. * <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. * and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. * License, Version 2.0.
  19. */
  20. #define PY_SSIZE_T_CLEAN
  21. #include <Python.h>
  22. #include <sys/stat.h>
  23. #ifdef _MSC_VER
  24. typedef unsigned short mode_t;
  25. #endif
  26. #if PY_MAJOR_VERSION < 3
  27. typedef long Py_hash_t;
  28. #endif
  29. #if PY_MAJOR_VERSION >= 3
  30. #define PyInt_FromLong PyLong_FromLong
  31. #define PyInt_AsLong PyLong_AsLong
  32. #define PyInt_AS_LONG PyLong_AS_LONG
  33. #define PyString_AS_STRING PyBytes_AS_STRING
  34. #define PyString_AsStringAndSize PyBytes_AsStringAndSize
  35. #define PyString_Check PyBytes_Check
  36. #define PyString_CheckExact PyBytes_CheckExact
  37. #define PyString_FromStringAndSize PyBytes_FromStringAndSize
  38. #define PyString_FromString PyBytes_FromString
  39. #define PyString_GET_SIZE PyBytes_GET_SIZE
  40. #define PyString_Size PyBytes_Size
  41. #define _PyString_Join _PyBytes_Join
  42. #endif
  43. static PyObject *tree_entry_cls = NULL, *null_entry = NULL,
  44. *defaultdict_cls = NULL, *int_cls = NULL;
  45. static int block_size;
  46. /**
  47. * Free an array of PyObject pointers, decrementing any references.
  48. */
  49. static void free_objects(PyObject **objs, Py_ssize_t n)
  50. {
  51. Py_ssize_t i;
  52. for (i = 0; i < n; i++)
  53. Py_XDECREF(objs[i]);
  54. PyMem_Free(objs);
  55. }
  56. /**
  57. * Get the entries of a tree, prepending the given path.
  58. *
  59. * Args:
  60. * path: The path to prepend, without trailing slashes.
  61. * path_len: The length of path.
  62. * tree: The Tree object to iterate.
  63. * n: Set to the length of result.
  64. * Returns: A (C) array of PyObject pointers to TreeEntry objects for each path
  65. * in tree.
  66. */
  67. static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
  68. Py_ssize_t *n)
  69. {
  70. PyObject *iteritems, *items, **result = NULL;
  71. PyObject *old_entry, *name, *sha;
  72. Py_ssize_t i = 0, name_len, new_path_len;
  73. char *new_path;
  74. if (tree == Py_None) {
  75. *n = 0;
  76. result = PyMem_New(PyObject*, 0);
  77. if (!result) {
  78. PyErr_NoMemory();
  79. return NULL;
  80. }
  81. return result;
  82. }
  83. iteritems = PyObject_GetAttrString(tree, "iteritems");
  84. if (!iteritems)
  85. return NULL;
  86. items = PyObject_CallFunctionObjArgs(iteritems, Py_True, NULL);
  87. Py_DECREF(iteritems);
  88. if (items == NULL) {
  89. return NULL;
  90. }
  91. /* The C implementation of iteritems returns a list, so depend on that. */
  92. if (!PyList_Check(items)) {
  93. PyErr_SetString(PyExc_TypeError,
  94. "Tree.iteritems() did not return a list");
  95. return NULL;
  96. }
  97. *n = PyList_Size(items);
  98. result = PyMem_New(PyObject*, *n);
  99. if (!result) {
  100. PyErr_NoMemory();
  101. goto error;
  102. }
  103. for (i = 0; i < *n; i++) {
  104. old_entry = PyList_GetItem(items, i);
  105. if (!old_entry)
  106. goto error;
  107. sha = PyTuple_GetItem(old_entry, 2);
  108. if (!sha)
  109. goto error;
  110. name = PyTuple_GET_ITEM(old_entry, 0);
  111. name_len = PyString_Size(name);
  112. if (PyErr_Occurred())
  113. goto error;
  114. new_path_len = name_len;
  115. if (path_len)
  116. new_path_len += path_len + 1;
  117. new_path = PyMem_Malloc(new_path_len);
  118. if (!new_path) {
  119. PyErr_NoMemory();
  120. goto error;
  121. }
  122. if (path_len) {
  123. memcpy(new_path, path, path_len);
  124. new_path[path_len] = '/';
  125. memcpy(new_path + path_len + 1, PyString_AS_STRING(name), name_len);
  126. } else {
  127. memcpy(new_path, PyString_AS_STRING(name), name_len);
  128. }
  129. #if PY_MAJOR_VERSION >= 3
  130. result[i] = PyObject_CallFunction(tree_entry_cls, "y#OO", new_path,
  131. new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
  132. #else
  133. result[i] = PyObject_CallFunction(tree_entry_cls, "s#OO", new_path,
  134. new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
  135. #endif
  136. PyMem_Free(new_path);
  137. if (!result[i]) {
  138. goto error;
  139. }
  140. }
  141. Py_DECREF(items);
  142. return result;
  143. error:
  144. if (result)
  145. free_objects(result, i);
  146. Py_DECREF(items);
  147. return NULL;
  148. }
  149. /**
  150. * Use strcmp to compare the paths of two TreeEntry objects.
  151. */
  152. static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
  153. {
  154. PyObject *path1 = NULL, *path2 = NULL;
  155. int result = 0;
  156. path1 = PyObject_GetAttrString(entry1, "path");
  157. if (!path1)
  158. goto done;
  159. if (!PyString_Check(path1)) {
  160. PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
  161. goto done;
  162. }
  163. path2 = PyObject_GetAttrString(entry2, "path");
  164. if (!path2)
  165. goto done;
  166. if (!PyString_Check(path2)) {
  167. PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
  168. goto done;
  169. }
  170. result = strcmp(PyString_AS_STRING(path1), PyString_AS_STRING(path2));
  171. done:
  172. Py_XDECREF(path1);
  173. Py_XDECREF(path2);
  174. return result;
  175. }
  176. static PyObject *py_merge_entries(PyObject *self, PyObject *args)
  177. {
  178. PyObject *tree1, *tree2, **entries1 = NULL, **entries2 = NULL;
  179. PyObject *e1, *e2, *pair, *result = NULL;
  180. Py_ssize_t n1 = 0, n2 = 0, i1 = 0, i2 = 0, path_len;
  181. char *path_str;
  182. int cmp;
  183. #if PY_MAJOR_VERSION >= 3
  184. if (!PyArg_ParseTuple(args, "y#OO", &path_str, &path_len, &tree1, &tree2))
  185. #else
  186. if (!PyArg_ParseTuple(args, "s#OO", &path_str, &path_len, &tree1, &tree2))
  187. #endif
  188. return NULL;
  189. entries1 = tree_entries(path_str, path_len, tree1, &n1);
  190. if (!entries1)
  191. goto error;
  192. entries2 = tree_entries(path_str, path_len, tree2, &n2);
  193. if (!entries2)
  194. goto error;
  195. result = PyList_New(0);
  196. if (!result)
  197. goto error;
  198. while (i1 < n1 && i2 < n2) {
  199. cmp = entry_path_cmp(entries1[i1], entries2[i2]);
  200. if (PyErr_Occurred())
  201. goto error;
  202. if (!cmp) {
  203. e1 = entries1[i1++];
  204. e2 = entries2[i2++];
  205. } else if (cmp < 0) {
  206. e1 = entries1[i1++];
  207. e2 = null_entry;
  208. } else {
  209. e1 = null_entry;
  210. e2 = entries2[i2++];
  211. }
  212. pair = PyTuple_Pack(2, e1, e2);
  213. if (!pair)
  214. goto error;
  215. PyList_Append(result, pair);
  216. Py_DECREF(pair);
  217. }
  218. while (i1 < n1) {
  219. pair = PyTuple_Pack(2, entries1[i1++], null_entry);
  220. if (!pair)
  221. goto error;
  222. PyList_Append(result, pair);
  223. Py_DECREF(pair);
  224. }
  225. while (i2 < n2) {
  226. pair = PyTuple_Pack(2, null_entry, entries2[i2++]);
  227. if (!pair)
  228. goto error;
  229. PyList_Append(result, pair);
  230. Py_DECREF(pair);
  231. }
  232. goto done;
  233. error:
  234. Py_XDECREF(result);
  235. result = NULL;
  236. done:
  237. if (entries1)
  238. free_objects(entries1, n1);
  239. if (entries2)
  240. free_objects(entries2, n2);
  241. return result;
  242. }
  243. static PyObject *py_is_tree(PyObject *self, PyObject *args)
  244. {
  245. PyObject *entry, *mode, *result;
  246. long lmode;
  247. if (!PyArg_ParseTuple(args, "O", &entry))
  248. return NULL;
  249. mode = PyObject_GetAttrString(entry, "mode");
  250. if (!mode)
  251. return NULL;
  252. if (mode == Py_None) {
  253. result = Py_False;
  254. Py_INCREF(result);
  255. } else {
  256. lmode = PyInt_AsLong(mode);
  257. if (lmode == -1 && PyErr_Occurred()) {
  258. Py_DECREF(mode);
  259. return NULL;
  260. }
  261. result = PyBool_FromLong(S_ISDIR((mode_t)lmode));
  262. }
  263. Py_DECREF(mode);
  264. return result;
  265. }
  266. static Py_hash_t add_hash(PyObject *get, PyObject *set, char *str, int n)
  267. {
  268. PyObject *str_obj = NULL, *hash_obj = NULL, *value = NULL,
  269. *set_value = NULL;
  270. Py_hash_t hash;
  271. /* It would be nice to hash without copying str into a PyString, but that
  272. * isn't exposed by the API. */
  273. str_obj = PyString_FromStringAndSize(str, n);
  274. if (!str_obj)
  275. goto error;
  276. hash = PyObject_Hash(str_obj);
  277. if (hash == -1)
  278. goto error;
  279. hash_obj = PyInt_FromLong(hash);
  280. if (!hash_obj)
  281. goto error;
  282. value = PyObject_CallFunctionObjArgs(get, hash_obj, NULL);
  283. if (!value)
  284. goto error;
  285. set_value = PyObject_CallFunction(set, "(Ol)", hash_obj,
  286. PyInt_AS_LONG(value) + n);
  287. if (!set_value)
  288. goto error;
  289. Py_DECREF(str_obj);
  290. Py_DECREF(hash_obj);
  291. Py_DECREF(value);
  292. Py_DECREF(set_value);
  293. return 0;
  294. error:
  295. Py_XDECREF(str_obj);
  296. Py_XDECREF(hash_obj);
  297. Py_XDECREF(value);
  298. Py_XDECREF(set_value);
  299. return -1;
  300. }
  301. static PyObject *py_count_blocks(PyObject *self, PyObject *args)
  302. {
  303. PyObject *obj, *chunks = NULL, *chunk, *counts = NULL, *get = NULL,
  304. *set = NULL;
  305. char *chunk_str, *block = NULL;
  306. Py_ssize_t num_chunks, chunk_len;
  307. int i, j, n = 0;
  308. char c;
  309. if (!PyArg_ParseTuple(args, "O", &obj))
  310. goto error;
  311. counts = PyObject_CallFunctionObjArgs(defaultdict_cls, int_cls, NULL);
  312. if (!counts)
  313. goto error;
  314. get = PyObject_GetAttrString(counts, "__getitem__");
  315. set = PyObject_GetAttrString(counts, "__setitem__");
  316. chunks = PyObject_CallMethod(obj, "as_raw_chunks", NULL);
  317. if (!chunks)
  318. goto error;
  319. if (!PyList_Check(chunks)) {
  320. PyErr_SetString(PyExc_TypeError,
  321. "as_raw_chunks() did not return a list");
  322. goto error;
  323. }
  324. num_chunks = PyList_GET_SIZE(chunks);
  325. block = PyMem_New(char, block_size);
  326. if (!block) {
  327. PyErr_NoMemory();
  328. goto error;
  329. }
  330. for (i = 0; i < num_chunks; i++) {
  331. chunk = PyList_GET_ITEM(chunks, i);
  332. if (!PyString_Check(chunk)) {
  333. PyErr_SetString(PyExc_TypeError, "chunk is not a string");
  334. goto error;
  335. }
  336. if (PyString_AsStringAndSize(chunk, &chunk_str, &chunk_len) == -1)
  337. goto error;
  338. for (j = 0; j < chunk_len; j++) {
  339. c = chunk_str[j];
  340. block[n++] = c;
  341. if (c == '\n' || n == block_size) {
  342. if (add_hash(get, set, block, n) == -1)
  343. goto error;
  344. n = 0;
  345. }
  346. }
  347. }
  348. if (n && add_hash(get, set, block, n) == -1)
  349. goto error;
  350. Py_DECREF(chunks);
  351. Py_DECREF(get);
  352. Py_DECREF(set);
  353. PyMem_Free(block);
  354. return counts;
  355. error:
  356. Py_XDECREF(chunks);
  357. Py_XDECREF(get);
  358. Py_XDECREF(set);
  359. Py_XDECREF(counts);
  360. PyMem_Free(block);
  361. return NULL;
  362. }
  363. static PyMethodDef py_diff_tree_methods[] = {
  364. { "_is_tree", (PyCFunction)py_is_tree, METH_VARARGS, NULL },
  365. { "_merge_entries", (PyCFunction)py_merge_entries, METH_VARARGS, NULL },
  366. { "_count_blocks", (PyCFunction)py_count_blocks, METH_VARARGS, NULL },
  367. { NULL, NULL, 0, NULL }
  368. };
  369. static PyObject *
  370. moduleinit(void)
  371. {
  372. PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
  373. PyObject *block_size_obj = NULL;
  374. #if PY_MAJOR_VERSION >= 3
  375. static struct PyModuleDef moduledef = {
  376. PyModuleDef_HEAD_INIT,
  377. "_diff_tree", /* m_name */
  378. NULL, /* m_doc */
  379. -1, /* m_size */
  380. py_diff_tree_methods, /* m_methods */
  381. NULL, /* m_reload */
  382. NULL, /* m_traverse */
  383. NULL, /* m_clear*/
  384. NULL, /* m_free */
  385. };
  386. m = PyModule_Create(&moduledef);
  387. #else
  388. m = Py_InitModule("_diff_tree", py_diff_tree_methods);
  389. #endif
  390. if (!m)
  391. goto error;
  392. objects_mod = PyImport_ImportModule("dulwich.objects");
  393. if (!objects_mod)
  394. goto error;
  395. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  396. Py_DECREF(objects_mod);
  397. if (!tree_entry_cls)
  398. goto error;
  399. diff_tree_mod = PyImport_ImportModule("dulwich.diff_tree");
  400. if (!diff_tree_mod)
  401. goto error;
  402. null_entry = PyObject_GetAttrString(diff_tree_mod, "_NULL_ENTRY");
  403. if (!null_entry)
  404. goto error;
  405. block_size_obj = PyObject_GetAttrString(diff_tree_mod, "_BLOCK_SIZE");
  406. if (!block_size_obj)
  407. goto error;
  408. block_size = (int)PyInt_AsLong(block_size_obj);
  409. if (PyErr_Occurred())
  410. goto error;
  411. defaultdict_cls = PyObject_GetAttrString(diff_tree_mod, "defaultdict");
  412. if (!defaultdict_cls)
  413. goto error;
  414. /* This is kind of hacky, but I don't know of a better way to get the
  415. * PyObject* version of int. */
  416. int_cls = PyDict_GetItemString(PyEval_GetBuiltins(), "int");
  417. if (!int_cls) {
  418. PyErr_SetString(PyExc_NameError, "int");
  419. goto error;
  420. }
  421. Py_DECREF(diff_tree_mod);
  422. return m;
  423. error:
  424. Py_XDECREF(objects_mod);
  425. Py_XDECREF(diff_tree_mod);
  426. Py_XDECREF(null_entry);
  427. Py_XDECREF(block_size_obj);
  428. Py_XDECREF(defaultdict_cls);
  429. Py_XDECREF(int_cls);
  430. return NULL;
  431. }
  432. #if PY_MAJOR_VERSION >= 3
  433. PyMODINIT_FUNC
  434. PyInit__diff_tree(void)
  435. {
  436. return moduleinit();
  437. }
  438. #else
  439. PyMODINIT_FUNC
  440. init_diff_tree(void)
  441. {
  442. moduleinit();
  443. }
  444. #endif