_diff_tree.c 10 KB

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