_diff_tree.c 12 KB

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