THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

567 lines
13KB

  1. #include "cache.h"
  2. #include "object.h"
  3. #include "replace-object.h"
  4. #include "object-store.h"
  5. #include "blob.h"
  6. #include "tree.h"
  7. #include "commit.h"
  8. #include "tag.h"
  9. #include "alloc.h"
  10. #include "packfile.h"
  11. #include "commit-graph.h"
  12. unsigned int get_max_object_index(void)
  13. {
  14. return the_repository->parsed_objects->obj_hash_size;
  15. }
  16. struct object *get_indexed_object(unsigned int idx)
  17. {
  18. return the_repository->parsed_objects->obj_hash[idx];
  19. }
  20. static const char *object_type_strings[] = {
  21. NULL, /* OBJ_NONE = 0 */
  22. "commit", /* OBJ_COMMIT = 1 */
  23. "tree", /* OBJ_TREE = 2 */
  24. "blob", /* OBJ_BLOB = 3 */
  25. "tag", /* OBJ_TAG = 4 */
  26. };
  27. const char *type_name(unsigned int type)
  28. {
  29. if (type >= ARRAY_SIZE(object_type_strings))
  30. return NULL;
  31. return object_type_strings[type];
  32. }
  33. int type_from_string_gently(const char *str, ssize_t len, int gentle)
  34. {
  35. int i;
  36. if (len < 0)
  37. len = strlen(str);
  38. for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
  39. if (!strncmp(str, object_type_strings[i], len) &&
  40. object_type_strings[i][len] == '\0')
  41. return i;
  42. if (gentle)
  43. return -1;
  44. die(_("invalid object type \"%s\""), str);
  45. }
  46. /*
  47. * Return a numerical hash value between 0 and n-1 for the object with
  48. * the specified sha1. n must be a power of 2. Please note that the
  49. * return value is *not* consistent across computer architectures.
  50. */
  51. static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
  52. {
  53. return oidhash(oid) & (n - 1);
  54. }
  55. /*
  56. * Insert obj into the hash table hash, which has length size (which
  57. * must be a power of 2). On collisions, simply overflow to the next
  58. * empty bucket.
  59. */
  60. static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
  61. {
  62. unsigned int j = hash_obj(&obj->oid, size);
  63. while (hash[j]) {
  64. j++;
  65. if (j >= size)
  66. j = 0;
  67. }
  68. hash[j] = obj;
  69. }
  70. /*
  71. * Look up the record for the given sha1 in the hash map stored in
  72. * obj_hash. Return NULL if it was not found.
  73. */
  74. struct object *lookup_object(struct repository *r, const struct object_id *oid)
  75. {
  76. unsigned int i, first;
  77. struct object *obj;
  78. if (!r->parsed_objects->obj_hash)
  79. return NULL;
  80. first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
  81. while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
  82. if (oideq(oid, &obj->oid))
  83. break;
  84. i++;
  85. if (i == r->parsed_objects->obj_hash_size)
  86. i = 0;
  87. }
  88. if (obj && i != first) {
  89. /*
  90. * Move object to where we started to look for it so
  91. * that we do not need to walk the hash table the next
  92. * time we look for it.
  93. */
  94. SWAP(r->parsed_objects->obj_hash[i],
  95. r->parsed_objects->obj_hash[first]);
  96. }
  97. return obj;
  98. }
  99. /*
  100. * Increase the size of the hash map stored in obj_hash to the next
  101. * power of 2 (but at least 32). Copy the existing values to the new
  102. * hash map.
  103. */
  104. static void grow_object_hash(struct repository *r)
  105. {
  106. int i;
  107. /*
  108. * Note that this size must always be power-of-2 to match hash_obj
  109. * above.
  110. */
  111. int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
  112. struct object **new_hash;
  113. new_hash = xcalloc(new_hash_size, sizeof(struct object *));
  114. for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
  115. struct object *obj = r->parsed_objects->obj_hash[i];
  116. if (!obj)
  117. continue;
  118. insert_obj_hash(obj, new_hash, new_hash_size);
  119. }
  120. free(r->parsed_objects->obj_hash);
  121. r->parsed_objects->obj_hash = new_hash;
  122. r->parsed_objects->obj_hash_size = new_hash_size;
  123. }
  124. void *create_object(struct repository *r, const struct object_id *oid, void *o)
  125. {
  126. struct object *obj = o;
  127. obj->parsed = 0;
  128. obj->flags = 0;
  129. oidcpy(&obj->oid, oid);
  130. if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
  131. grow_object_hash(r);
  132. insert_obj_hash(obj, r->parsed_objects->obj_hash,
  133. r->parsed_objects->obj_hash_size);
  134. r->parsed_objects->nr_objs++;
  135. return obj;
  136. }
  137. void *object_as_type(struct repository *r, struct object *obj, enum object_type type, int quiet)
  138. {
  139. if (obj->type == type)
  140. return obj;
  141. else if (obj->type == OBJ_NONE) {
  142. if (type == OBJ_COMMIT)
  143. init_commit_node(r, (struct commit *) obj);
  144. else
  145. obj->type = type;
  146. return obj;
  147. }
  148. else {
  149. if (!quiet)
  150. error(_("object %s is a %s, not a %s"),
  151. oid_to_hex(&obj->oid),
  152. type_name(obj->type), type_name(type));
  153. return NULL;
  154. }
  155. }
  156. struct object *lookup_unknown_object(const struct object_id *oid)
  157. {
  158. struct object *obj = lookup_object(the_repository, oid);
  159. if (!obj)
  160. obj = create_object(the_repository, oid,
  161. alloc_object_node(the_repository));
  162. return obj;
  163. }
  164. struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
  165. {
  166. struct object *obj;
  167. *eaten_p = 0;
  168. obj = NULL;
  169. if (type == OBJ_BLOB) {
  170. struct blob *blob = lookup_blob(r, oid);
  171. if (blob) {
  172. if (parse_blob_buffer(blob, buffer, size))
  173. return NULL;
  174. obj = &blob->object;
  175. }
  176. } else if (type == OBJ_TREE) {
  177. struct tree *tree = lookup_tree(r, oid);
  178. if (tree) {
  179. obj = &tree->object;
  180. if (!tree->buffer)
  181. tree->object.parsed = 0;
  182. if (!tree->object.parsed) {
  183. if (parse_tree_buffer(tree, buffer, size))
  184. return NULL;
  185. *eaten_p = 1;
  186. }
  187. }
  188. } else if (type == OBJ_COMMIT) {
  189. struct commit *commit = lookup_commit(r, oid);
  190. if (commit) {
  191. if (parse_commit_buffer(r, commit, buffer, size, 1))
  192. return NULL;
  193. if (!get_cached_commit_buffer(r, commit, NULL)) {
  194. set_commit_buffer(r, commit, buffer, size);
  195. *eaten_p = 1;
  196. }
  197. obj = &commit->object;
  198. }
  199. } else if (type == OBJ_TAG) {
  200. struct tag *tag = lookup_tag(r, oid);
  201. if (tag) {
  202. if (parse_tag_buffer(r, tag, buffer, size))
  203. return NULL;
  204. obj = &tag->object;
  205. }
  206. } else {
  207. warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
  208. obj = NULL;
  209. }
  210. return obj;
  211. }
  212. struct object *parse_object_or_die(const struct object_id *oid,
  213. const char *name)
  214. {
  215. struct object *o = parse_object(the_repository, oid);
  216. if (o)
  217. return o;
  218. die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
  219. }
  220. struct object *parse_object(struct repository *r, const struct object_id *oid)
  221. {
  222. unsigned long size;
  223. enum object_type type;
  224. int eaten;
  225. const struct object_id *repl = lookup_replace_object(r, oid);
  226. void *buffer;
  227. struct object *obj;
  228. obj = lookup_object(r, oid);
  229. if (obj && obj->parsed)
  230. return obj;
  231. if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
  232. (!obj && repo_has_object_file(r, oid) &&
  233. oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
  234. if (check_object_signature(repl, NULL, 0, NULL) < 0) {
  235. error(_("hash mismatch %s"), oid_to_hex(oid));
  236. return NULL;
  237. }
  238. parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
  239. return lookup_object(r, oid);
  240. }
  241. buffer = repo_read_object_file(r, oid, &type, &size);
  242. if (buffer) {
  243. if (check_object_signature(repl, buffer, size, type_name(type)) < 0) {
  244. free(buffer);
  245. error(_("hash mismatch %s"), oid_to_hex(repl));
  246. return NULL;
  247. }
  248. obj = parse_object_buffer(r, oid, type, size,
  249. buffer, &eaten);
  250. if (!eaten)
  251. free(buffer);
  252. return obj;
  253. }
  254. return NULL;
  255. }
  256. struct object_list *object_list_insert(struct object *item,
  257. struct object_list **list_p)
  258. {
  259. struct object_list *new_list = xmalloc(sizeof(struct object_list));
  260. new_list->item = item;
  261. new_list->next = *list_p;
  262. *list_p = new_list;
  263. return new_list;
  264. }
  265. int object_list_contains(struct object_list *list, struct object *obj)
  266. {
  267. while (list) {
  268. if (list->item == obj)
  269. return 1;
  270. list = list->next;
  271. }
  272. return 0;
  273. }
  274. /*
  275. * A zero-length string to which object_array_entry::name can be
  276. * initialized without requiring a malloc/free.
  277. */
  278. static char object_array_slopbuf[1];
  279. void add_object_array_with_path(struct object *obj, const char *name,
  280. struct object_array *array,
  281. unsigned mode, const char *path)
  282. {
  283. unsigned nr = array->nr;
  284. unsigned alloc = array->alloc;
  285. struct object_array_entry *objects = array->objects;
  286. struct object_array_entry *entry;
  287. if (nr >= alloc) {
  288. alloc = (alloc + 32) * 2;
  289. REALLOC_ARRAY(objects, alloc);
  290. array->alloc = alloc;
  291. array->objects = objects;
  292. }
  293. entry = &objects[nr];
  294. entry->item = obj;
  295. if (!name)
  296. entry->name = NULL;
  297. else if (!*name)
  298. /* Use our own empty string instead of allocating one: */
  299. entry->name = object_array_slopbuf;
  300. else
  301. entry->name = xstrdup(name);
  302. entry->mode = mode;
  303. if (path)
  304. entry->path = xstrdup(path);
  305. else
  306. entry->path = NULL;
  307. array->nr = ++nr;
  308. }
  309. void add_object_array(struct object *obj, const char *name, struct object_array *array)
  310. {
  311. add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
  312. }
  313. /*
  314. * Free all memory associated with an entry; the result is
  315. * in an unspecified state and should not be examined.
  316. */
  317. static void object_array_release_entry(struct object_array_entry *ent)
  318. {
  319. if (ent->name != object_array_slopbuf)
  320. free(ent->name);
  321. free(ent->path);
  322. }
  323. struct object *object_array_pop(struct object_array *array)
  324. {
  325. struct object *ret;
  326. if (!array->nr)
  327. return NULL;
  328. ret = array->objects[array->nr - 1].item;
  329. object_array_release_entry(&array->objects[array->nr - 1]);
  330. array->nr--;
  331. return ret;
  332. }
  333. void object_array_filter(struct object_array *array,
  334. object_array_each_func_t want, void *cb_data)
  335. {
  336. unsigned nr = array->nr, src, dst;
  337. struct object_array_entry *objects = array->objects;
  338. for (src = dst = 0; src < nr; src++) {
  339. if (want(&objects[src], cb_data)) {
  340. if (src != dst)
  341. objects[dst] = objects[src];
  342. dst++;
  343. } else {
  344. object_array_release_entry(&objects[src]);
  345. }
  346. }
  347. array->nr = dst;
  348. }
  349. void object_array_clear(struct object_array *array)
  350. {
  351. int i;
  352. for (i = 0; i < array->nr; i++)
  353. object_array_release_entry(&array->objects[i]);
  354. FREE_AND_NULL(array->objects);
  355. array->nr = array->alloc = 0;
  356. }
  357. /*
  358. * Return true iff array already contains an entry with name.
  359. */
  360. static int contains_name(struct object_array *array, const char *name)
  361. {
  362. unsigned nr = array->nr, i;
  363. struct object_array_entry *object = array->objects;
  364. for (i = 0; i < nr; i++, object++)
  365. if (!strcmp(object->name, name))
  366. return 1;
  367. return 0;
  368. }
  369. void object_array_remove_duplicates(struct object_array *array)
  370. {
  371. unsigned nr = array->nr, src;
  372. struct object_array_entry *objects = array->objects;
  373. array->nr = 0;
  374. for (src = 0; src < nr; src++) {
  375. if (!contains_name(array, objects[src].name)) {
  376. if (src != array->nr)
  377. objects[array->nr] = objects[src];
  378. array->nr++;
  379. } else {
  380. object_array_release_entry(&objects[src]);
  381. }
  382. }
  383. }
  384. void clear_object_flags(unsigned flags)
  385. {
  386. int i;
  387. for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
  388. struct object *obj = the_repository->parsed_objects->obj_hash[i];
  389. if (obj)
  390. obj->flags &= ~flags;
  391. }
  392. }
  393. void clear_commit_marks_all(unsigned int flags)
  394. {
  395. int i;
  396. for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
  397. struct object *obj = the_repository->parsed_objects->obj_hash[i];
  398. if (obj && obj->type == OBJ_COMMIT)
  399. obj->flags &= ~flags;
  400. }
  401. }
  402. struct parsed_object_pool *parsed_object_pool_new(void)
  403. {
  404. struct parsed_object_pool *o = xmalloc(sizeof(*o));
  405. memset(o, 0, sizeof(*o));
  406. o->blob_state = allocate_alloc_state();
  407. o->tree_state = allocate_alloc_state();
  408. o->commit_state = allocate_alloc_state();
  409. o->tag_state = allocate_alloc_state();
  410. o->object_state = allocate_alloc_state();
  411. o->is_shallow = -1;
  412. o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
  413. o->buffer_slab = allocate_commit_buffer_slab();
  414. return o;
  415. }
  416. struct raw_object_store *raw_object_store_new(void)
  417. {
  418. struct raw_object_store *o = xmalloc(sizeof(*o));
  419. memset(o, 0, sizeof(*o));
  420. INIT_LIST_HEAD(&o->packed_git_mru);
  421. return o;
  422. }
  423. static void free_object_directory(struct object_directory *odb)
  424. {
  425. free(odb->path);
  426. odb_clear_loose_cache(odb);
  427. free(odb);
  428. }
  429. static void free_object_directories(struct raw_object_store *o)
  430. {
  431. while (o->odb) {
  432. struct object_directory *next;
  433. next = o->odb->next;
  434. free_object_directory(o->odb);
  435. o->odb = next;
  436. }
  437. }
  438. void raw_object_store_clear(struct raw_object_store *o)
  439. {
  440. FREE_AND_NULL(o->alternate_db);
  441. oidmap_free(o->replace_map, 1);
  442. FREE_AND_NULL(o->replace_map);
  443. free_commit_graph(o->commit_graph);
  444. o->commit_graph = NULL;
  445. o->commit_graph_attempted = 0;
  446. free_object_directories(o);
  447. o->odb_tail = NULL;
  448. o->loaded_alternates = 0;
  449. INIT_LIST_HEAD(&o->packed_git_mru);
  450. close_object_store(o);
  451. o->packed_git = NULL;
  452. }
  453. void parsed_object_pool_clear(struct parsed_object_pool *o)
  454. {
  455. /*
  456. * As objects are allocated in slabs (see alloc.c), we do
  457. * not need to free each object, but each slab instead.
  458. *
  459. * Before doing so, we need to free any additional memory
  460. * the objects may hold.
  461. */
  462. unsigned i;
  463. for (i = 0; i < o->obj_hash_size; i++) {
  464. struct object *obj = o->obj_hash[i];
  465. if (!obj)
  466. continue;
  467. if (obj->type == OBJ_TREE)
  468. free_tree_buffer((struct tree*)obj);
  469. else if (obj->type == OBJ_COMMIT)
  470. release_commit_memory(o, (struct commit*)obj);
  471. else if (obj->type == OBJ_TAG)
  472. release_tag_memory((struct tag*)obj);
  473. }
  474. FREE_AND_NULL(o->obj_hash);
  475. o->obj_hash_size = 0;
  476. free_commit_buffer_slab(o->buffer_slab);
  477. o->buffer_slab = NULL;
  478. clear_alloc_state(o->blob_state);
  479. clear_alloc_state(o->tree_state);
  480. clear_alloc_state(o->commit_state);
  481. clear_alloc_state(o->tag_state);
  482. clear_alloc_state(o->object_state);
  483. stat_validity_clear(o->shallow_stat);
  484. FREE_AND_NULL(o->blob_state);
  485. FREE_AND_NULL(o->tree_state);
  486. FREE_AND_NULL(o->commit_state);
  487. FREE_AND_NULL(o->tag_state);
  488. FREE_AND_NULL(o->object_state);
  489. FREE_AND_NULL(o->shallow_stat);
  490. }