object.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 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((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(r, 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(r, repl, buffer, size,
  244. type_name(type)) < 0) {
  245. free(buffer);
  246. error(_("hash mismatch %s"), oid_to_hex(repl));
  247. return NULL;
  248. }
  249. obj = parse_object_buffer(r, oid, type, size,
  250. buffer, &eaten);
  251. if (!eaten)
  252. free(buffer);
  253. return obj;
  254. }
  255. return NULL;
  256. }
  257. struct object_list *object_list_insert(struct object *item,
  258. struct object_list **list_p)
  259. {
  260. struct object_list *new_list = xmalloc(sizeof(struct object_list));
  261. new_list->item = item;
  262. new_list->next = *list_p;
  263. *list_p = new_list;
  264. return new_list;
  265. }
  266. int object_list_contains(struct object_list *list, struct object *obj)
  267. {
  268. while (list) {
  269. if (list->item == obj)
  270. return 1;
  271. list = list->next;
  272. }
  273. return 0;
  274. }
  275. void object_list_free(struct object_list **list)
  276. {
  277. while (*list) {
  278. struct object_list *p = *list;
  279. *list = p->next;
  280. free(p);
  281. }
  282. }
  283. /*
  284. * A zero-length string to which object_array_entry::name can be
  285. * initialized without requiring a malloc/free.
  286. */
  287. static char object_array_slopbuf[1];
  288. void add_object_array_with_path(struct object *obj, const char *name,
  289. struct object_array *array,
  290. unsigned mode, const char *path)
  291. {
  292. unsigned nr = array->nr;
  293. unsigned alloc = array->alloc;
  294. struct object_array_entry *objects = array->objects;
  295. struct object_array_entry *entry;
  296. if (nr >= alloc) {
  297. alloc = (alloc + 32) * 2;
  298. REALLOC_ARRAY(objects, alloc);
  299. array->alloc = alloc;
  300. array->objects = objects;
  301. }
  302. entry = &objects[nr];
  303. entry->item = obj;
  304. if (!name)
  305. entry->name = NULL;
  306. else if (!*name)
  307. /* Use our own empty string instead of allocating one: */
  308. entry->name = object_array_slopbuf;
  309. else
  310. entry->name = xstrdup(name);
  311. entry->mode = mode;
  312. if (path)
  313. entry->path = xstrdup(path);
  314. else
  315. entry->path = NULL;
  316. array->nr = ++nr;
  317. }
  318. void add_object_array(struct object *obj, const char *name, struct object_array *array)
  319. {
  320. add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
  321. }
  322. /*
  323. * Free all memory associated with an entry; the result is
  324. * in an unspecified state and should not be examined.
  325. */
  326. static void object_array_release_entry(struct object_array_entry *ent)
  327. {
  328. if (ent->name != object_array_slopbuf)
  329. free(ent->name);
  330. free(ent->path);
  331. }
  332. struct object *object_array_pop(struct object_array *array)
  333. {
  334. struct object *ret;
  335. if (!array->nr)
  336. return NULL;
  337. ret = array->objects[array->nr - 1].item;
  338. object_array_release_entry(&array->objects[array->nr - 1]);
  339. array->nr--;
  340. return ret;
  341. }
  342. void object_array_filter(struct object_array *array,
  343. object_array_each_func_t want, void *cb_data)
  344. {
  345. unsigned nr = array->nr, src, dst;
  346. struct object_array_entry *objects = array->objects;
  347. for (src = dst = 0; src < nr; src++) {
  348. if (want(&objects[src], cb_data)) {
  349. if (src != dst)
  350. objects[dst] = objects[src];
  351. dst++;
  352. } else {
  353. object_array_release_entry(&objects[src]);
  354. }
  355. }
  356. array->nr = dst;
  357. }
  358. void object_array_clear(struct object_array *array)
  359. {
  360. int i;
  361. for (i = 0; i < array->nr; i++)
  362. object_array_release_entry(&array->objects[i]);
  363. FREE_AND_NULL(array->objects);
  364. array->nr = array->alloc = 0;
  365. }
  366. /*
  367. * Return true iff array already contains an entry with name.
  368. */
  369. static int contains_name(struct object_array *array, const char *name)
  370. {
  371. unsigned nr = array->nr, i;
  372. struct object_array_entry *object = array->objects;
  373. for (i = 0; i < nr; i++, object++)
  374. if (!strcmp(object->name, name))
  375. return 1;
  376. return 0;
  377. }
  378. void object_array_remove_duplicates(struct object_array *array)
  379. {
  380. unsigned nr = array->nr, src;
  381. struct object_array_entry *objects = array->objects;
  382. array->nr = 0;
  383. for (src = 0; src < nr; src++) {
  384. if (!contains_name(array, objects[src].name)) {
  385. if (src != array->nr)
  386. objects[array->nr] = objects[src];
  387. array->nr++;
  388. } else {
  389. object_array_release_entry(&objects[src]);
  390. }
  391. }
  392. }
  393. void clear_object_flags(unsigned 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)
  399. obj->flags &= ~flags;
  400. }
  401. }
  402. void clear_commit_marks_all(unsigned int flags)
  403. {
  404. int i;
  405. for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
  406. struct object *obj = the_repository->parsed_objects->obj_hash[i];
  407. if (obj && obj->type == OBJ_COMMIT)
  408. obj->flags &= ~flags;
  409. }
  410. }
  411. struct parsed_object_pool *parsed_object_pool_new(void)
  412. {
  413. struct parsed_object_pool *o = xmalloc(sizeof(*o));
  414. memset(o, 0, sizeof(*o));
  415. o->blob_state = allocate_alloc_state();
  416. o->tree_state = allocate_alloc_state();
  417. o->commit_state = allocate_alloc_state();
  418. o->tag_state = allocate_alloc_state();
  419. o->object_state = allocate_alloc_state();
  420. o->is_shallow = -1;
  421. o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
  422. o->buffer_slab = allocate_commit_buffer_slab();
  423. return o;
  424. }
  425. struct raw_object_store *raw_object_store_new(void)
  426. {
  427. struct raw_object_store *o = xmalloc(sizeof(*o));
  428. memset(o, 0, sizeof(*o));
  429. INIT_LIST_HEAD(&o->packed_git_mru);
  430. hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
  431. pthread_mutex_init(&o->replace_mutex, NULL);
  432. return o;
  433. }
  434. static void free_object_directory(struct object_directory *odb)
  435. {
  436. free(odb->path);
  437. odb_clear_loose_cache(odb);
  438. free(odb);
  439. }
  440. static void free_object_directories(struct raw_object_store *o)
  441. {
  442. while (o->odb) {
  443. struct object_directory *next;
  444. next = o->odb->next;
  445. free_object_directory(o->odb);
  446. o->odb = next;
  447. }
  448. }
  449. void raw_object_store_clear(struct raw_object_store *o)
  450. {
  451. FREE_AND_NULL(o->alternate_db);
  452. oidmap_free(o->replace_map, 1);
  453. FREE_AND_NULL(o->replace_map);
  454. pthread_mutex_destroy(&o->replace_mutex);
  455. free_commit_graph(o->commit_graph);
  456. o->commit_graph = NULL;
  457. o->commit_graph_attempted = 0;
  458. free_object_directories(o);
  459. o->odb_tail = NULL;
  460. o->loaded_alternates = 0;
  461. INIT_LIST_HEAD(&o->packed_git_mru);
  462. close_object_store(o);
  463. o->packed_git = NULL;
  464. hashmap_free(&o->pack_map);
  465. }
  466. void parsed_object_pool_clear(struct parsed_object_pool *o)
  467. {
  468. /*
  469. * As objects are allocated in slabs (see alloc.c), we do
  470. * not need to free each object, but each slab instead.
  471. *
  472. * Before doing so, we need to free any additional memory
  473. * the objects may hold.
  474. */
  475. unsigned i;
  476. for (i = 0; i < o->obj_hash_size; i++) {
  477. struct object *obj = o->obj_hash[i];
  478. if (!obj)
  479. continue;
  480. if (obj->type == OBJ_TREE)
  481. free_tree_buffer((struct tree*)obj);
  482. else if (obj->type == OBJ_COMMIT)
  483. release_commit_memory(o, (struct commit*)obj);
  484. else if (obj->type == OBJ_TAG)
  485. release_tag_memory((struct tag*)obj);
  486. }
  487. FREE_AND_NULL(o->obj_hash);
  488. o->obj_hash_size = 0;
  489. free_commit_buffer_slab(o->buffer_slab);
  490. o->buffer_slab = NULL;
  491. clear_alloc_state(o->blob_state);
  492. clear_alloc_state(o->tree_state);
  493. clear_alloc_state(o->commit_state);
  494. clear_alloc_state(o->tag_state);
  495. clear_alloc_state(o->object_state);
  496. stat_validity_clear(o->shallow_stat);
  497. FREE_AND_NULL(o->blob_state);
  498. FREE_AND_NULL(o->tree_state);
  499. FREE_AND_NULL(o->commit_state);
  500. FREE_AND_NULL(o->tag_state);
  501. FREE_AND_NULL(o->object_state);
  502. FREE_AND_NULL(o->shallow_stat);
  503. }