_diff_tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. if (result)
  126. free_objects(result, i);
  127. Py_DECREF(items);
  128. return NULL;
  129. }
  130. /**
  131. * Use strcmp to compare the paths of two TreeEntry objects.
  132. */
  133. static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
  134. {
  135. PyObject *path1 = NULL, *path2 = NULL;
  136. int result = 0;
  137. path1 = PyObject_GetAttrString(entry1, "path");
  138. if (!path1)
  139. goto done;
  140. if (!PyString_Check(path1)) {
  141. PyErr_SetString(PyExc_TypeError, "path is not a string");
  142. goto done;
  143. }
  144. path2 = PyObject_GetAttrString(entry2, "path");
  145. if (!path2)
  146. goto done;
  147. if (!PyString_Check(path2)) {
  148. PyErr_SetString(PyExc_TypeError, "path is not a string");
  149. goto done;
  150. }
  151. result = strcmp(PyString_AS_STRING(path1), PyString_AS_STRING(path2));
  152. done:
  153. Py_XDECREF(path1);
  154. Py_XDECREF(path2);
  155. return result;
  156. }
  157. static PyObject *py_merge_entries(PyObject *self, PyObject *args)
  158. {
  159. PyObject *path, *tree1, *tree2, **entries1 = NULL, **entries2 = NULL;
  160. PyObject *e1, *e2, *pair, *result = NULL;
  161. Py_ssize_t path_len, n1 = 0, n2 = 0, i1 = 0, i2 = 0;
  162. char *path_str;
  163. int cmp;
  164. if (!PyArg_ParseTuple(args, "OOO", &path, &tree1, &tree2))
  165. return NULL;
  166. path_str = PyString_AsString(path);
  167. if (!path_str) {
  168. PyErr_SetString(PyExc_TypeError, "path is not a string");
  169. return NULL;
  170. }
  171. path_len = PyString_GET_SIZE(path);
  172. entries1 = tree_entries(path_str, path_len, tree1, &n1);
  173. if (!entries1)
  174. goto error;
  175. entries2 = tree_entries(path_str, path_len, tree2, &n2);
  176. if (!entries2)
  177. goto error;
  178. result = PyList_New(n1 + n2);
  179. if (!result)
  180. goto error;
  181. /* PyList_New sets the len of the list, not its allocated size, so we
  182. * need to trim it to the size we actually use. */
  183. Py_SIZE(result) = 0;
  184. while (i1 < n1 && i2 < n2) {
  185. cmp = entry_path_cmp(entries1[i1], entries2[i2]);
  186. if (PyErr_Occurred())
  187. goto error;
  188. if (!cmp) {
  189. e1 = entries1[i1++];
  190. e2 = entries2[i2++];
  191. } else if (cmp < 0) {
  192. e1 = entries1[i1++];
  193. e2 = null_entry;
  194. } else {
  195. e1 = null_entry;
  196. e2 = entries2[i2++];
  197. }
  198. pair = PyTuple_Pack(2, e1, e2);
  199. if (!pair)
  200. goto error;
  201. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  202. }
  203. while (i1 < n1) {
  204. pair = PyTuple_Pack(2, entries1[i1++], null_entry);
  205. if (!pair)
  206. goto error;
  207. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  208. }
  209. while (i2 < n2) {
  210. pair = PyTuple_Pack(2, null_entry, entries2[i2++]);
  211. if (!pair)
  212. goto error;
  213. PyList_SET_ITEM(result, Py_SIZE(result)++, pair);
  214. }
  215. goto done;
  216. error:
  217. Py_XDECREF(result);
  218. result = NULL;
  219. done:
  220. if (entries1)
  221. free_objects(entries1, n1);
  222. if (entries2)
  223. free_objects(entries2, n2);
  224. return result;
  225. }
  226. static PyObject *py_is_tree(PyObject *self, PyObject *args)
  227. {
  228. PyObject *entry, *mode, *result;
  229. long lmode;
  230. if (!PyArg_ParseTuple(args, "O", &entry))
  231. return NULL;
  232. mode = PyObject_GetAttrString(entry, "mode");
  233. if (!mode)
  234. return NULL;
  235. if (mode == Py_None) {
  236. result = Py_False;
  237. } else {
  238. lmode = PyInt_AsLong(mode);
  239. if (lmode == -1 && PyErr_Occurred()) {
  240. Py_DECREF(mode);
  241. return NULL;
  242. }
  243. result = PyBool_FromLong(S_ISDIR((mode_t)lmode));
  244. }
  245. Py_INCREF(result);
  246. Py_DECREF(mode);
  247. return result;
  248. }
  249. static int add_hash(PyObject *get, PyObject *set, char *str, int n)
  250. {
  251. PyObject *str_obj = NULL, *hash_obj = NULL, *value = NULL,
  252. *set_value = NULL;
  253. long hash;
  254. /* It would be nice to hash without copying str into a PyString, but that
  255. * isn't exposed by the API. */
  256. str_obj = PyString_FromStringAndSize(str, n);
  257. if (!str_obj)
  258. goto error;
  259. hash = PyObject_Hash(str_obj);
  260. if (hash == -1)
  261. goto error;
  262. hash_obj = PyInt_FromLong(hash);
  263. if (!hash_obj)
  264. goto error;
  265. value = PyObject_CallFunctionObjArgs(get, hash_obj, NULL);
  266. if (!value)
  267. goto error;
  268. set_value = PyObject_CallFunction(set, "(Ol)", hash_obj,
  269. PyInt_AS_LONG(value) + n);
  270. if (!set_value)
  271. goto error;
  272. Py_DECREF(str_obj);
  273. Py_DECREF(hash_obj);
  274. Py_DECREF(value);
  275. Py_DECREF(set_value);
  276. return 0;
  277. error:
  278. Py_XDECREF(str_obj);
  279. Py_XDECREF(hash_obj);
  280. Py_XDECREF(value);
  281. Py_XDECREF(set_value);
  282. return -1;
  283. }
  284. static PyObject *py_count_blocks(PyObject *self, PyObject *args)
  285. {
  286. PyObject *obj, *chunks = NULL, *chunk, *counts = NULL, *get = NULL,
  287. *set = NULL;
  288. char *chunk_str, *block = NULL;
  289. Py_ssize_t num_chunks, chunk_len;
  290. int i, j, n = 0;
  291. char c;
  292. if (!PyArg_ParseTuple(args, "O", &obj))
  293. goto error;
  294. counts = PyObject_CallFunctionObjArgs(defaultdict_cls, int_cls, NULL);
  295. if (!counts)
  296. goto error;
  297. get = PyObject_GetAttrString(counts, "__getitem__");
  298. set = PyObject_GetAttrString(counts, "__setitem__");
  299. chunks = PyObject_CallMethod(obj, "as_raw_chunks", NULL);
  300. if (!chunks)
  301. goto error;
  302. if (!PyList_Check(chunks)) {
  303. PyErr_SetString(PyExc_TypeError,
  304. "as_raw_chunks() did not return a list");
  305. goto error;
  306. }
  307. num_chunks = PyList_GET_SIZE(chunks);
  308. block = PyMem_New(char, block_size);
  309. if (!block) {
  310. PyErr_SetNone(PyExc_MemoryError);
  311. goto error;
  312. }
  313. for (i = 0; i < num_chunks; i++) {
  314. chunk = PyList_GET_ITEM(chunks, i);
  315. if (!PyString_Check(chunk)) {
  316. PyErr_SetString(PyExc_TypeError, "chunk is not a string");
  317. goto error;
  318. }
  319. if (PyString_AsStringAndSize(chunk, &chunk_str, &chunk_len) == -1)
  320. goto error;
  321. for (j = 0; j < chunk_len; j++) {
  322. c = chunk_str[j];
  323. block[n++] = c;
  324. if (c == '\n' || n == block_size) {
  325. if (add_hash(get, set, block, n) == -1)
  326. goto error;
  327. n = 0;
  328. }
  329. }
  330. }
  331. if (n && add_hash(get, set, block, n) == -1)
  332. goto error;
  333. Py_DECREF(chunks);
  334. Py_DECREF(get);
  335. Py_DECREF(set);
  336. PyMem_Free(block);
  337. return counts;
  338. error:
  339. Py_XDECREF(chunks);
  340. Py_XDECREF(get);
  341. Py_XDECREF(set);
  342. Py_XDECREF(counts);
  343. PyMem_Free(block);
  344. return NULL;
  345. }
  346. static PyMethodDef py_diff_tree_methods[] = {
  347. { "_is_tree", (PyCFunction)py_is_tree, METH_VARARGS, NULL },
  348. { "_merge_entries", (PyCFunction)py_merge_entries, METH_VARARGS, NULL },
  349. { "_count_blocks", (PyCFunction)py_count_blocks, METH_VARARGS, NULL },
  350. { NULL, NULL, 0, NULL }
  351. };
  352. PyMODINIT_FUNC
  353. init_diff_tree(void)
  354. {
  355. PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
  356. PyObject *block_size_obj = NULL;
  357. m = Py_InitModule("_diff_tree", py_diff_tree_methods);
  358. if (!m)
  359. goto error;
  360. objects_mod = PyImport_ImportModule("dulwich.objects");
  361. if (!objects_mod)
  362. goto error;
  363. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  364. Py_DECREF(objects_mod);
  365. if (!tree_entry_cls)
  366. goto error;
  367. diff_tree_mod = PyImport_ImportModule("dulwich.diff_tree");
  368. if (!diff_tree_mod)
  369. goto error;
  370. null_entry = PyObject_GetAttrString(diff_tree_mod, "_NULL_ENTRY");
  371. if (!null_entry)
  372. goto error;
  373. block_size_obj = PyObject_GetAttrString(diff_tree_mod, "_BLOCK_SIZE");
  374. if (!block_size_obj)
  375. goto error;
  376. block_size = (int)PyInt_AsLong(block_size_obj);
  377. if (PyErr_Occurred())
  378. goto error;
  379. defaultdict_cls = PyObject_GetAttrString(diff_tree_mod, "defaultdict");
  380. if (!defaultdict_cls)
  381. goto error;
  382. /* This is kind of hacky, but I don't know of a better way to get the
  383. * PyObject* version of int. */
  384. int_cls = PyDict_GetItemString(PyEval_GetBuiltins(), "int");
  385. if (!int_cls) {
  386. PyErr_SetString(PyExc_NameError, "int");
  387. goto error;
  388. }
  389. Py_DECREF(diff_tree_mod);
  390. return;
  391. error:
  392. Py_XDECREF(objects_mod);
  393. Py_XDECREF(diff_tree_mod);
  394. Py_XDECREF(null_entry);
  395. Py_XDECREF(block_size_obj);
  396. Py_XDECREF(defaultdict_cls);
  397. Py_XDECREF(int_cls);
  398. return;
  399. }