_diff_tree.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #define PY_SSIZE_T_CLEAN
  21. #include <Python.h>
  22. #include <sys/stat.h>
  23. #ifdef _MSC_VER
  24. typedef unsigned short mode_t;
  25. #endif
  26. static PyObject *tree_entry_cls = NULL, *null_entry = NULL,
  27. *defaultdict_cls = NULL, *int_cls = NULL;
  28. static int block_size;
  29. /**
  30. * Free an array of PyObject pointers, decrementing any references.
  31. */
  32. static void free_objects(PyObject **objs, Py_ssize_t n)
  33. {
  34. Py_ssize_t i;
  35. for (i = 0; i < n; i++)
  36. Py_XDECREF(objs[i]);
  37. PyMem_Free(objs);
  38. }
  39. /**
  40. * Get the entries of a tree, prepending the given path.
  41. *
  42. * Args:
  43. * path: The path to prepend, without trailing slashes.
  44. * path_len: The length of path.
  45. * tree: The Tree object to iterate.
  46. * n: Set to the length of result.
  47. * Returns: A (C) array of PyObject pointers to TreeEntry objects for each path
  48. * in tree.
  49. */
  50. static PyObject **tree_entries(char *path, Py_ssize_t path_len, PyObject *tree,
  51. Py_ssize_t *n)
  52. {
  53. PyObject *iteritems, *items, **result = NULL;
  54. PyObject *old_entry, *name, *sha;
  55. Py_ssize_t i = 0, name_len, new_path_len;
  56. char *new_path;
  57. if (tree == Py_None) {
  58. *n = 0;
  59. result = PyMem_New(PyObject*, 0);
  60. if (!result) {
  61. PyErr_NoMemory();
  62. return NULL;
  63. }
  64. return result;
  65. }
  66. iteritems = PyObject_GetAttrString(tree, "iteritems");
  67. if (!iteritems)
  68. return NULL;
  69. items = PyObject_CallFunctionObjArgs(iteritems, Py_True, NULL);
  70. Py_DECREF(iteritems);
  71. if (items == NULL) {
  72. return NULL;
  73. }
  74. /* The C implementation of iteritems returns a list, so depend on that. */
  75. if (!PyList_Check(items)) {
  76. PyErr_SetString(PyExc_TypeError,
  77. "Tree.iteritems() did not return a list");
  78. return NULL;
  79. }
  80. *n = PyList_Size(items);
  81. result = PyMem_New(PyObject*, *n);
  82. if (!result) {
  83. PyErr_NoMemory();
  84. goto error;
  85. }
  86. for (i = 0; i < *n; i++) {
  87. old_entry = PyList_GetItem(items, i);
  88. if (!old_entry)
  89. goto error;
  90. sha = PyTuple_GetItem(old_entry, 2);
  91. if (!sha)
  92. goto error;
  93. name = PyTuple_GET_ITEM(old_entry, 0);
  94. name_len = PyBytes_Size(name);
  95. if (PyErr_Occurred())
  96. goto error;
  97. new_path_len = name_len;
  98. if (path_len)
  99. new_path_len += path_len + 1;
  100. new_path = PyMem_Malloc(new_path_len);
  101. if (!new_path) {
  102. PyErr_NoMemory();
  103. goto error;
  104. }
  105. if (path_len) {
  106. memcpy(new_path, path, path_len);
  107. new_path[path_len] = '/';
  108. memcpy(new_path + path_len + 1, PyBytes_AS_STRING(name), name_len);
  109. } else {
  110. memcpy(new_path, PyBytes_AS_STRING(name), name_len);
  111. }
  112. result[i] = PyObject_CallFunction(tree_entry_cls, "y#OO", new_path,
  113. new_path_len, PyTuple_GET_ITEM(old_entry, 1), sha);
  114. PyMem_Free(new_path);
  115. if (!result[i]) {
  116. goto error;
  117. }
  118. }
  119. Py_DECREF(items);
  120. return result;
  121. error:
  122. if (result)
  123. free_objects(result, i);
  124. Py_DECREF(items);
  125. return NULL;
  126. }
  127. /**
  128. * Use strcmp to compare the paths of two TreeEntry objects.
  129. */
  130. static int entry_path_cmp(PyObject *entry1, PyObject *entry2)
  131. {
  132. PyObject *path1 = NULL, *path2 = NULL;
  133. int result = 0;
  134. path1 = PyObject_GetAttrString(entry1, "path");
  135. if (!path1)
  136. goto done;
  137. if (!PyBytes_Check(path1)) {
  138. PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
  139. goto done;
  140. }
  141. path2 = PyObject_GetAttrString(entry2, "path");
  142. if (!path2)
  143. goto done;
  144. if (!PyBytes_Check(path2)) {
  145. PyErr_SetString(PyExc_TypeError, "path is not a (byte)string");
  146. goto done;
  147. }
  148. result = strcmp(PyBytes_AS_STRING(path1), PyBytes_AS_STRING(path2));
  149. done:
  150. Py_XDECREF(path1);
  151. Py_XDECREF(path2);
  152. return result;
  153. }
  154. static PyObject *py_merge_entries(PyObject *self, PyObject *args)
  155. {
  156. PyObject *tree1, *tree2, **entries1 = NULL, **entries2 = NULL;
  157. PyObject *e1, *e2, *pair, *result = NULL;
  158. Py_ssize_t n1 = 0, n2 = 0, i1 = 0, i2 = 0, path_len;
  159. char *path_str;
  160. int cmp;
  161. if (!PyArg_ParseTuple(args, "y#OO", &path_str, &path_len, &tree1, &tree2))
  162. return NULL;
  163. entries1 = tree_entries(path_str, path_len, tree1, &n1);
  164. if (!entries1)
  165. goto error;
  166. entries2 = tree_entries(path_str, path_len, tree2, &n2);
  167. if (!entries2)
  168. goto error;
  169. result = PyList_New(0);
  170. if (!result)
  171. goto error;
  172. while (i1 < n1 && i2 < n2) {
  173. cmp = entry_path_cmp(entries1[i1], entries2[i2]);
  174. if (PyErr_Occurred())
  175. goto error;
  176. if (!cmp) {
  177. e1 = entries1[i1++];
  178. e2 = entries2[i2++];
  179. } else if (cmp < 0) {
  180. e1 = entries1[i1++];
  181. e2 = null_entry;
  182. } else {
  183. e1 = null_entry;
  184. e2 = entries2[i2++];
  185. }
  186. pair = PyTuple_Pack(2, e1, e2);
  187. if (!pair)
  188. goto error;
  189. PyList_Append(result, pair);
  190. Py_DECREF(pair);
  191. }
  192. while (i1 < n1) {
  193. pair = PyTuple_Pack(2, entries1[i1++], null_entry);
  194. if (!pair)
  195. goto error;
  196. PyList_Append(result, pair);
  197. Py_DECREF(pair);
  198. }
  199. while (i2 < n2) {
  200. pair = PyTuple_Pack(2, null_entry, entries2[i2++]);
  201. if (!pair)
  202. goto error;
  203. PyList_Append(result, pair);
  204. Py_DECREF(pair);
  205. }
  206. goto done;
  207. error:
  208. Py_XDECREF(result);
  209. result = NULL;
  210. done:
  211. if (entries1)
  212. free_objects(entries1, n1);
  213. if (entries2)
  214. free_objects(entries2, n2);
  215. return result;
  216. }
  217. /* Not all environments define S_ISDIR */
  218. #if !defined(S_ISDIR) && defined(S_IFMT) && defined(S_IFDIR)
  219. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  220. #endif
  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 = PyLong_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 Py_hash_t 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. Py_hash_t 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 = PyBytes_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 = PyLong_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. PyLong_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 (!PyBytes_Check(chunk)) {
  311. PyErr_SetString(PyExc_TypeError, "chunk is not a string");
  312. goto error;
  313. }
  314. if (PyBytes_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. static PyObject *
  348. moduleinit(void)
  349. {
  350. PyObject *m, *objects_mod = NULL, *diff_tree_mod = NULL;
  351. PyObject *block_size_obj = NULL;
  352. static struct PyModuleDef moduledef = {
  353. PyModuleDef_HEAD_INIT,
  354. "_diff_tree", /* m_name */
  355. NULL, /* m_doc */
  356. -1, /* m_size */
  357. py_diff_tree_methods, /* m_methods */
  358. NULL, /* m_reload */
  359. NULL, /* m_traverse */
  360. NULL, /* m_clear*/
  361. NULL, /* m_free */
  362. };
  363. m = PyModule_Create(&moduledef);
  364. if (!m)
  365. goto error;
  366. objects_mod = PyImport_ImportModule("dulwich.objects");
  367. if (!objects_mod)
  368. goto error;
  369. tree_entry_cls = PyObject_GetAttrString(objects_mod, "TreeEntry");
  370. Py_DECREF(objects_mod);
  371. if (!tree_entry_cls)
  372. goto error;
  373. diff_tree_mod = PyImport_ImportModule("dulwich.diff_tree");
  374. if (!diff_tree_mod)
  375. goto error;
  376. null_entry = PyObject_GetAttrString(diff_tree_mod, "_NULL_ENTRY");
  377. if (!null_entry)
  378. goto error;
  379. block_size_obj = PyObject_GetAttrString(diff_tree_mod, "_BLOCK_SIZE");
  380. if (!block_size_obj)
  381. goto error;
  382. block_size = (int)PyLong_AsLong(block_size_obj);
  383. if (PyErr_Occurred())
  384. goto error;
  385. defaultdict_cls = PyObject_GetAttrString(diff_tree_mod, "defaultdict");
  386. if (!defaultdict_cls)
  387. goto error;
  388. /* This is kind of hacky, but I don't know of a better way to get the
  389. * PyObject* version of int. */
  390. int_cls = PyDict_GetItemString(PyEval_GetBuiltins(), "int");
  391. if (!int_cls) {
  392. PyErr_SetString(PyExc_NameError, "int");
  393. goto error;
  394. }
  395. Py_DECREF(diff_tree_mod);
  396. return m;
  397. error:
  398. Py_XDECREF(objects_mod);
  399. Py_XDECREF(diff_tree_mod);
  400. Py_XDECREF(null_entry);
  401. Py_XDECREF(block_size_obj);
  402. Py_XDECREF(defaultdict_cls);
  403. Py_XDECREF(int_cls);
  404. return NULL;
  405. }
  406. PyMODINIT_FUNC
  407. PyInit__diff_tree(void)
  408. {
  409. return moduleinit();
  410. }