_diff_tree.c 11 KB

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