_diff_tree.c 12 KB

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