_diff_tree.c 12 KB

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