_diff_tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. #if (PY_VERSION_HEX < 0x02050000)
  22. typedef int Py_ssize_t;
  23. #endif
  24. #if (PY_VERSION_HEX < 0x02060000)
  25. #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size)
  26. #endif
  27. static PyObject *tree_entry_cls = NULL, *null_entry = NULL,
  28. *defaultdict_cls = NULL, *int_cls = NULL;
  29. static int block_size;
  30. /**
  31. * Free an array of PyObject pointers, decrementing any references.
  32. */
  33. static void free_objects(PyObject **objs, Py_ssize_t n)
  34. {
  35. Py_ssize_t i;
  36. for (i = 0; i < n; i++)
  37. Py_XDECREF(objs[i]);
  38. PyMem_Free(objs);
  39. }
  40. /**
  41. * Get the entries of a tree, prepending the given path.
  42. *
  43. * :param path: The path to prepend, without trailing slashes.
  44. * :param path_len: The length of path.
  45. * :param tree: The Tree object to iterate.
  46. * :param n: Set to the length of result.
  47. * :return: A (C) array of PyObject pointers to TreeEntry objects for each path
  48. * in tree.
  49. */
  50. static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
  51. Py_ssize_t *n)
  52. {
  53. PyObject *iteritems, *items, **result = NULL;
  54. PyObject *old_entry, *name, *sha;
  55. Py_ssize_t i = 0, name_len, new_path_len;
  56. char *new_path;
  57. if (tree == Py_None) {
  58. *n = 0;
  59. result = PyMem_New(PyObject*, 0);
  60. if (!result) {
  61. PyErr_SetNone(PyExc_MemoryError);
  62. return NULL;
  63. }
  64. return result;
  65. }
  66. iteritems = PyObject_GetAttrString(tree, "iteritems");
  67. if (!iteritems)
  68. return NULL;
  69. items = PyObject_CallFunctionObjArgs(iteritems, Py_True, NULL);
  70. Py_DECREF(iteritems);
  71. if (!items) {
  72. return NULL;
  73. }
  74. /* The C implementation of iteritems returns a list, so depend on that. */
  75. if (!PyList_Check(items)) {
  76. PyErr_SetString(PyExc_TypeError,
  77. "Tree.iteritems() did not return a list");
  78. return NULL;
  79. }
  80. *n = PyList_Size(items);
  81. result = PyMem_New(PyObject*, *n);
  82. if (!result) {
  83. PyErr_SetNone(PyExc_MemoryError);
  84. goto error;
  85. }
  86. for (i = 0; i < *n; i++) {
  87. old_entry = PyList_GetItem(items, i);
  88. if (!old_entry)
  89. goto error;
  90. sha = PyTuple_GetItem(old_entry, 2);
  91. if (!sha)
  92. goto error;
  93. name = PyTuple_GET_ITEM(old_entry, 0);
  94. name_len = PyString_Size(name);
  95. if (PyErr_Occurred())
  96. goto error;
  97. new_path_len = name_len;
  98. if (path_len)
  99. new_path_len += path_len + 1;
  100. new_path = PyMem_Malloc(new_path_len);
  101. if (!new_path) {
  102. PyErr_SetNone(PyExc_MemoryError);
  103. goto error;
  104. }
  105. if (path_len) {
  106. memcpy(new_path, path, path_len);
  107. new_path[path_len] = '/';
  108. memcpy(new_path + path_len + 1, PyString_AS_STRING(name), name_len);
  109. } else {
  110. memcpy(new_path, PyString_AS_STRING(name), name_len);
  111. }
  112. result[i] = PyObject_CallFunction(tree_entry_cls, "s#OO", new_path,
  113. new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
  114. PyMem_Free(new_path);
  115. if (!result[i]) {
  116. goto error;
  117. }
  118. }
  119. Py_DECREF(items);
  120. return result;
  121. error:
  122. free_objects(result, i);
  123. Py_DECREF(items);
  124. return NULL;
  125. }
  126. /**
  127. * Use strcmp to compare the paths of two TreeEntry objects.
  128. */
  129. static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
  130. {
  131. PyObject *path1 = NULL, *path2 = NULL;
  132. int result = 0;
  133. path1 = PyObject_GetAttrString(entry1, "path");
  134. if (!path1)
  135. goto done;
  136. if (!PyString_Check(path1)) {
  137. PyErr_SetString(PyExc_TypeError, "path is not a string");
  138. goto done;
  139. }
  140. path2 = PyObject_GetAttrString(entry2, "path");
  141. if (!path2)
  142. goto done;
  143. if (!PyString_Check(path2)) {
  144. PyErr_SetString(PyExc_TypeError, "path is not a string");
  145. goto done;
  146. }
  147. result = strcmp(PyString_AS_STRING(path1), PyString_AS_STRING(path2));
  148. done:
  149. Py_XDECREF(path1);
  150. Py_XDECREF(path2);
  151. return result;
  152. }
  153. static PyObject *py_merge_entries(PyObject *self, PyObject *args)
  154. {
  155. PyObject *path, *tree1, *tree2, **entries1 = NULL, **entries2 = NULL;
  156. PyObject *e1, *e2, *pair, *result = NULL;
  157. Py_ssize_t path_len, n1 = 0, n2 = 0, i1 = 0, i2 = 0;
  158. char *path_str;
  159. int cmp;
  160. if (!PyArg_ParseTuple(args, "OOO", &path, &tree1, &tree2))
  161. return NULL;
  162. path_str = PyString_AsString(path);
  163. if (!path_str) {
  164. PyErr_SetString(PyExc_TypeError, "path is not a string");
  165. return NULL;
  166. }
  167. path_len = PyString_GET_SIZE(path);
  168. entries1 = tree_entries(path_str, path_len, tree1, &n1);
  169. if (!entries1)
  170. goto error;
  171. entries2 = tree_entries(path_str, path_len, tree2, &n2);
  172. if (!entries2)
  173. goto error;
  174. result = PyList_New(n1 + n2);
  175. if (!result)
  176. goto error;
  177. /* PyList_New sets the len of the list, not its allocated size, so we
  178. * need to trim it to the size we actually use. */
  179. Py_SIZE(result) = 0;
  180. while (i1 < n1 && i2 < n2) {
  181. cmp = entry_path_cmp(entries1[i1], entries2[i2]);
  182. if (PyErr_Occurred())
  183. goto error;
  184. if (!cmp) {
  185. e1 = entries1[i1++];
  186. e2 = entries2[i2++];
  187. } else if (cmp < 0) {
  188. e1 = entries1[i1++];
  189. e2 = null_entry;
  190. } else {
  191. e1 = null_entry;
  192. e2 = entries2[i2++];
  193. }
  194. pair = PyTuple_Pack(2, e1, e2);
  195. if (!pair)
  196. goto error;
  197. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  198. }
  199. while (i1 < n1) {
  200. pair = PyTuple_Pack(2, entries1[i1++], null_entry);
  201. if (!pair)
  202. goto error;
  203. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  204. }
  205. while (i2 < n2) {
  206. pair = PyTuple_Pack(2, null_entry, entries2[i2++]);
  207. if (!pair)
  208. goto error;
  209. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  210. }
  211. goto done;
  212. error:
  213. Py_XDECREF(result);
  214. result = NULL;
  215. done:
  216. free_objects(entries1, n1);
  217. free_objects(entries2, n2);
  218. return result;
  219. }
  220. static PyObject *py_is_tree(PyObject *self, PyObject *args)
  221. {
  222. PyObject *entry, *mode, *result;
  223. long lmode;
  224. if (!PyArg_ParseTuple(args, "O", &entry))
  225. return NULL;
  226. mode = PyObject_GetAttrString(entry, "mode");
  227. if (!mode)
  228. return NULL;
  229. if (mode == Py_None) {
  230. result = Py_False;
  231. } else {
  232. lmode = PyInt_AsLong(mode);
  233. if (lmode == -1 && PyErr_Occurred()) {
  234. Py_DECREF(mode);
  235. return NULL;
  236. }
  237. result = PyBool_FromLong(S_ISDIR((mode_t)lmode));
  238. }
  239. Py_INCREF(result);
  240. Py_DECREF(mode);
  241. return result;
  242. }
  243. static int add_hash(PyObject *get, PyObject *set, char *str, int n)
  244. {
  245. PyObject *str_obj = NULL, *hash_obj = NULL, *value = NULL,
  246. *set_value = NULL;
  247. long hash;
  248. /* It would be nice to hash without copying str into a PyString, but that
  249. * isn't exposed by the API. */
  250. str_obj = PyString_FromStringAndSize(str, n);
  251. if (!str_obj)
  252. goto error;
  253. hash = PyObject_Hash(str_obj);
  254. if (hash == -1)
  255. goto error;
  256. hash_obj = PyInt_FromLong(hash);
  257. if (!hash_obj)
  258. goto error;
  259. value = PyObject_CallFunctionObjArgs(get, hash_obj, NULL);
  260. if (!value)
  261. goto error;
  262. set_value = PyObject_CallFunction(set, "(Ol)", hash_obj,
  263. PyInt_AS_LONG(value) + n);
  264. if (!set_value)
  265. goto error;
  266. Py_DECREF(str_obj);
  267. Py_DECREF(hash_obj);
  268. Py_DECREF(value);
  269. Py_DECREF(set_value);
  270. return 0;
  271. error:
  272. Py_XDECREF(str_obj);
  273. Py_XDECREF(hash_obj);
  274. Py_XDECREF(value);
  275. Py_XDECREF(set_value);
  276. return -1;
  277. }
  278. static PyObject *py_count_blocks(PyObject *self, PyObject *args)
  279. {
  280. PyObject *obj, *chunks = NULL, *chunk, *counts = NULL, *get = NULL,
  281. *set = NULL;
  282. char *chunk_str, *block = NULL;
  283. Py_ssize_t num_chunks, chunk_len;
  284. int i, j, n = 0;
  285. char c;
  286. if (!PyArg_ParseTuple(args, "O", &obj))
  287. goto error;
  288. counts = PyObject_CallFunctionObjArgs(defaultdict_cls, int_cls, NULL);
  289. if (!counts)
  290. goto error;
  291. get = PyObject_GetAttrString(counts, "__getitem__");
  292. set = PyObject_GetAttrString(counts, "__setitem__");
  293. chunks = PyObject_CallMethod(obj, "as_raw_chunks", NULL);
  294. if (!chunks)
  295. goto error;
  296. if (!PyList_Check(chunks)) {
  297. PyErr_SetString(PyExc_TypeError,
  298. "as_raw_chunks() did not return a list");
  299. goto error;
  300. }
  301. num_chunks = PyList_GET_SIZE(chunks);
  302. block = PyMem_New(char, block_size);
  303. if (!block) {
  304. PyErr_SetNone(PyExc_MemoryError);
  305. goto error;
  306. }
  307. for (i = 0; i < num_chunks; i++) {
  308. chunk = PyList_GET_ITEM(chunks, i);
  309. if (!PyString_Check(chunk)) {
  310. PyErr_SetString(PyExc_TypeError, "chunk is not a string");
  311. goto error;
  312. }
  313. if (PyString_AsStringAndSize(chunk, &chunk_str, &chunk_len) == -1)
  314. goto error;
  315. for (j = 0; j < chunk_len; j++) {
  316. c = chunk_str[j];
  317. block[n++] = c;
  318. if (c == '\n' || n == block_size) {
  319. if (add_hash(get, set, block, n) == -1)
  320. goto error;
  321. n = 0;
  322. }
  323. }
  324. }
  325. if (n && add_hash(get, set, block, n) == -1)
  326. goto error;
  327. Py_DECREF(chunks);
  328. Py_DECREF(get);
  329. Py_DECREF(set);
  330. PyMem_Free(block);
  331. return counts;
  332. error:
  333. Py_XDECREF(chunks);
  334. Py_XDECREF(get);
  335. Py_XDECREF(set);
  336. Py_XDECREF(counts);
  337. PyMem_Free(block);
  338. return NULL;
  339. }
  340. static PyMethodDef py_diff_tree_methods[] = {
  341. { "_is_tree", (PyCFunction)py_is_tree, METH_VARARGS, NULL },
  342. { "_merge_entries", (PyCFunction)py_merge_entries, METH_VARARGS, NULL },
  343. { "_count_blocks", (PyCFunction)py_count_blocks, METH_VARARGS, NULL },
  344. { NULL, NULL, 0, NULL }
  345. };
  346. PyMODINIT_FUNC
  347. init_diff_tree(void)
  348. {
  349. PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
  350. PyObject *block_size_obj = NULL;
  351. m = Py_InitModule("_diff_tree", py_diff_tree_methods);
  352. if (!m)
  353. goto error;
  354. objects_mod = PyImport_ImportModule("dulwich.objects");
  355. if (!objects_mod)
  356. goto error;
  357. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  358. Py_DECREF(objects_mod);
  359. if (!tree_entry_cls)
  360. goto error;
  361. diff_tree_mod = PyImport_ImportModule("dulwich.diff_tree");
  362. if (!diff_tree_mod)
  363. goto error;
  364. null_entry = PyObject_GetAttrString(diff_tree_mod, "_NULL_ENTRY");
  365. if (!null_entry)
  366. goto error;
  367. block_size_obj = PyObject_GetAttrString(diff_tree_mod, "_BLOCK_SIZE");
  368. if (!block_size_obj)
  369. goto error;
  370. block_size = (int)PyInt_AsLong(block_size_obj);
  371. if (PyErr_Occurred())
  372. goto error;
  373. defaultdict_cls = PyObject_GetAttrString(diff_tree_mod, "defaultdict");
  374. if (!defaultdict_cls)
  375. goto error;
  376. /* This is kind of hacky, but I don't know of a better way to get the
  377. * PyObject* version of int. */
  378. int_cls = PyDict_GetItemString(PyEval_GetBuiltins(), "int");
  379. if (!int_cls) {
  380. PyErr_SetString(PyExc_NameError, "int");
  381. goto error;
  382. }
  383. Py_DECREF(objects_mod);
  384. Py_DECREF(diff_tree_mod);
  385. return;
  386. error:
  387. Py_XDECREF(objects_mod);
  388. Py_XDECREF(diff_tree_mod);
  389. Py_XDECREF(null_entry);
  390. Py_XDECREF(block_size_obj);
  391. Py_XDECREF(defaultdict_cls);
  392. Py_XDECREF(int_cls);
  393. return;
  394. }