vendor/libgit2/src/pack-objects.c in rugged-0.27.9 vs vendor/libgit2/src/pack-objects.c in rugged-0.27.10

- old
+ new

@@ -36,13 +36,19 @@ git_buf buf; }; struct pack_write_context { git_indexer *indexer; - git_transfer_progress *stats; + git_indexer_progress *stats; }; +struct walk_object { + git_oid id; + unsigned int uninteresting:1, + seen:1; +}; + #ifdef GIT_THREADS #define GIT_PACKBUILDER__MUTEX_OP(pb, mtx, op) do { \ int result = git_mutex_##op(&(pb)->mtx); \ assert(!result); \ @@ -97,11 +103,11 @@ #define config_get(KEY,DST,DFLT) do { \ ret = git_config_get_int64(&val, config, KEY); \ if (!ret) { \ if (!git__is_sizet(val)) { \ - giterr_set(GITERR_CONFIG, \ + git_error_set(GIT_ERROR_CONFIG, \ "configuration value '%s' is too large", KEY); \ ret = -1; \ goto out; \ } \ (DST) = (size_t)val; \ @@ -131,21 +137,19 @@ git_packbuilder *pb; *out = NULL; pb = git__calloc(1, sizeof(*pb)); - GITERR_CHECK_ALLOC(pb); + GIT_ERROR_CHECK_ALLOC(pb); - pb->object_ix = git_oidmap_alloc(); - if (!pb->object_ix) + if (git_oidmap_new(&pb->object_ix) < 0) goto on_error; - pb->walk_objects = git_oidmap_alloc(); - if (!pb->walk_objects) + if (git_oidmap_new(&pb->walk_objects) < 0) goto on_error; - git_pool_init(&pb->object_pool, sizeof(git_walk_object)); + git_pool_init(&pb->object_pool, sizeof(struct walk_object)); pb->repo = repo; pb->nr_threads = 1; /* do not spawn any thread by default */ if (git_hash_ctx_init(&pb->ctx) < 0 || @@ -158,11 +162,11 @@ if (git_mutex_init(&pb->cache_mutex) || git_mutex_init(&pb->progress_mutex) || git_cond_init(&pb->progress_cond)) { - giterr_set(GITERR_OS, "failed to initialize packbuilder mutex"); + git_error_set(GIT_ERROR_OS, "failed to initialize packbuilder mutex"); goto on_error; } #endif @@ -186,29 +190,29 @@ #endif return pb->nr_threads; } -static void rehash(git_packbuilder *pb) +static int rehash(git_packbuilder *pb) { git_pobject *po; - khiter_t pos; size_t i; - int ret; git_oidmap_clear(pb->object_ix); + for (i = 0, po = pb->object_list; i < pb->nr_objects; i++, po++) { - pos = git_oidmap_put(pb->object_ix, &po->id, &ret); - git_oidmap_set_value_at(pb->object_ix, pos, po); + if (git_oidmap_set(pb->object_ix, &po->id, po) < 0) + return -1; } + + return 0; } int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid, const char *name) { git_pobject *po; - khiter_t pos; size_t newsize; int ret; assert(pb && oid); @@ -216,24 +220,26 @@ * have any work to do */ if (git_oidmap_exists(pb->object_ix, oid)) return 0; if (pb->nr_objects >= pb->nr_alloc) { - GITERR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024); - GITERR_CHECK_ALLOC_MULTIPLY(&newsize, newsize, 3 / 2); + GIT_ERROR_CHECK_ALLOC_ADD(&newsize, pb->nr_alloc, 1024); + GIT_ERROR_CHECK_ALLOC_MULTIPLY(&newsize, newsize / 2, 3); if (!git__is_uint32(newsize)) { - giterr_set(GITERR_NOMEMORY, "packfile too large to fit in memory."); + git_error_set(GIT_ERROR_NOMEMORY, "packfile too large to fit in memory."); return -1; } pb->nr_alloc = newsize; pb->object_list = git__reallocarray(pb->object_list, pb->nr_alloc, sizeof(*po)); - GITERR_CHECK_ALLOC(pb->object_list); - rehash(pb); + GIT_ERROR_CHECK_ALLOC(pb->object_list); + + if (rehash(pb) < 0) + return -1; } po = pb->object_list + pb->nr_objects; memset(po, 0x0, sizeof(*po)); @@ -242,17 +248,14 @@ pb->nr_objects++; git_oid_cpy(&po->id, oid); po->hash = name_hash(name); - pos = git_oidmap_put(pb->object_ix, &po->id, &ret); - if (ret < 0) { - giterr_set_oom(); - return ret; + if (git_oidmap_set(pb->object_ix, &po->id, po) < 0) { + git_error_set_oom(); + return -1; } - assert(ret != 0); - git_oidmap_set_value_at(pb->object_ix, pos, po); pb->done = false; if (pb->progress_cb) { double current_time = git__timer(); @@ -264,11 +267,11 @@ ret = pb->progress_cb( GIT_PACKBUILDER_ADDING_OBJECTS, pb->nr_objects, 0, pb->progress_cb_payload); if (ret) - return giterr_set_after_callback(ret); + return git_error_set_after_callback(ret); } } return 0; } @@ -293,11 +296,11 @@ if (error < 0 && error != GIT_EBUFS) goto on_error; if (error == GIT_EBUFS || delta_size != po->delta_size) { - giterr_set(GITERR_INVALID, "delta size changed"); + git_error_set(GIT_ERROR_INVALID, "delta size changed"); goto on_error; } *out = delta_buf; @@ -316,11 +319,11 @@ git_pobject *po, int (*write_cb)(void *buf, size_t size, void *cb_data), void *cb_data) { git_odb_object *obj = NULL; - git_otype type; + git_object_t type; unsigned char hdr[10], *zbuf = NULL; void *data = NULL; size_t hdr_len, zbuf_len = COMPRESS_BUFLEN, data_len; int error; @@ -334,11 +337,11 @@ data = po->delta_data; else if ((error = get_delta(&data, pb->odb, po)) < 0) goto done; data_len = po->delta_size; - type = GIT_OBJ_REF_DELTA; + type = GIT_OBJECT_REF_DELTA; } else { if ((error = git_odb_read(&obj, pb->odb, &po->id)) < 0) goto done; data = (void *)git_odb_object_data(obj); @@ -351,11 +354,11 @@ if ((error = write_cb(hdr, hdr_len, cb_data)) < 0 || (error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0) goto done; - if (type == GIT_OBJ_REF_DELTA) { + if (type == GIT_OBJECT_REF_DELTA) { if ((error = write_cb(po->delta->id.id, GIT_OID_RAWSZ, cb_data)) < 0 || (error = git_hash_update(&pb->ctx, po->delta->id.id, GIT_OID_RAWSZ)) < 0) goto done; } @@ -366,11 +369,11 @@ if ((error = write_cb(data, data_len, cb_data)) < 0 || (error = git_hash_update(&pb->ctx, data, data_len)) < 0) goto done; } else { zbuf = git__malloc(zbuf_len); - GITERR_CHECK_ALLOC(zbuf); + GIT_ERROR_CHECK_ALLOC(zbuf); git_zstream_reset(&pb->zstream); git_zstream_set_input(&pb->zstream, data, data_len); while (!git_zstream_done(&pb->zstream)) { @@ -508,19 +511,16 @@ static int cb_tag_foreach(const char *name, git_oid *oid, void *data) { git_packbuilder *pb = data; git_pobject *po; - khiter_t pos; GIT_UNUSED(name); - pos = git_oidmap_lookup_index(pb->object_ix, oid); - if (!git_oidmap_valid_index(pb->object_ix, pos)) + if ((po = git_oidmap_get(pb->object_ix, oid)) == NULL) return 0; - po = git_oidmap_value_at(pb->object_ix, pos); po->tagged = 1; /* TODO: peel objects */ return 0; @@ -588,22 +588,22 @@ /* * And then all remaining commits and tags. */ for (i = last_untagged; i < pb->nr_objects; i++) { git_pobject *po = pb->object_list + i; - if (po->type != GIT_OBJ_COMMIT && - po->type != GIT_OBJ_TAG) + if (po->type != GIT_OBJECT_COMMIT && + po->type != GIT_OBJECT_TAG) continue; add_to_write_order(wo, &wo_end, po); } /* * And then all the trees. */ for (i = last_untagged; i < pb->nr_objects; i++) { git_pobject *po = pb->object_list + i; - if (po->type != GIT_OBJ_TREE) + if (po->type != GIT_OBJECT_TREE) continue; add_to_write_order(wo, &wo_end, po); } /* @@ -615,11 +615,11 @@ add_family_to_write_order(wo, &wo_end, po); } if (wo_end != pb->nr_objects) { git__free(wo); - giterr_set(GITERR_INVALID, "invalid write order"); + git_error_set(GIT_ERROR_INVALID, "invalid write order"); return NULL; } return wo; } @@ -639,11 +639,11 @@ write_order = compute_write_order(pb); if (write_order == NULL) return -1; if (!git__is_uint32(pb->nr_objects)) { - giterr_set(GITERR_INVALID, "too many objects"); + git_error_set(GIT_ERROR_INVALID, "too many objects"); return -1; } /* Write pack header */ ph.hdr_signature = htonl(PACK_SIGNATURE); @@ -796,17 +796,17 @@ if (git_odb_read(&obj, pb->odb, &trg_object->id) < 0) return -1; sz = git_odb_object_size(obj); trg->data = git__malloc(sz); - GITERR_CHECK_ALLOC(trg->data); + GIT_ERROR_CHECK_ALLOC(trg->data); memcpy(trg->data, git_odb_object_data(obj), sz); git_odb_object_free(obj); if (sz != trg_size) { - giterr_set(GITERR_INVALID, + git_error_set(GIT_ERROR_INVALID, "inconsistent target object length"); return -1; } *mem_usage += sz; @@ -818,17 +818,17 @@ !git__is_ulong(obj_sz = git_odb_object_size(obj))) return -1; sz = obj_sz; src->data = git__malloc(sz); - GITERR_CHECK_ALLOC(src->data); + GIT_ERROR_CHECK_ALLOC(src->data); memcpy(src->data, git_odb_object_data(obj), sz); git_odb_object_free(obj); if (sz != src_size) { - giterr_set(GITERR_INVALID, + git_error_set(GIT_ERROR_INVALID, "inconsistent source object length"); return -1; } *mem_usage += sz; @@ -870,11 +870,11 @@ git__free(delta_buf); return -1; } trg_object->delta_data = git__realloc(delta_buf, delta_size); - GITERR_CHECK_ALLOC(trg_object->delta_data); + GIT_ERROR_CHECK_ALLOC(trg_object->delta_data); } else { /* create delta when writing the pack */ git_packbuilder__cache_unlock(pb); git__free(delta_buf); } @@ -936,11 +936,11 @@ ret = pb->progress_cb( GIT_PACKBUILDER_DELTAFICATION, count, pb->nr_objects, pb->progress_cb_payload); if (ret) - return giterr_set_after_callback(ret); + return git_error_set_after_callback(ret); } } return 0; } @@ -955,11 +955,11 @@ size_t mem_usage = 0; size_t i; int error = -1; array = git__calloc(window, sizeof(struct unpacked)); - GITERR_CHECK_ALLOC(array); + GIT_ERROR_CHECK_ALLOC(array); for (;;) { struct unpacked *n = array + idx; size_t max_depth, j, best_base = SIZE_MAX; @@ -1041,11 +1041,11 @@ if (git_zstream_deflatebuf(&zbuf, po->delta_data, po->delta_size) < 0) goto on_error; git__free(po->delta_data); po->delta_data = git__malloc(zbuf.size); - GITERR_CHECK_ALLOC(po->delta_data); + GIT_ERROR_CHECK_ALLOC(po->delta_data); memcpy(po->delta_data, zbuf.ptr, zbuf.size); po->z_delta_size = zbuf.size; git_buf_clear(&zbuf); @@ -1093,11 +1093,11 @@ for (i = 0; i < window; ++i) { git__free(array[i].index); git__free(array[i].data); } git__free(array); - git_buf_free(&zbuf); + git_buf_dispose(&zbuf); return error; } #ifdef GIT_THREADS @@ -1134,11 +1134,11 @@ me->working = 0; git_cond_signal(&me->pb->progress_cond); git_packbuilder__progress_unlock(me->pb); if (git_mutex_lock(&me->mutex)) { - giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex"); + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); return NULL; } while (!me->data_ready) git_cond_wait(&me->cond, &me->mutex); @@ -1172,11 +1172,11 @@ find_deltas(pb, list, &list_size, window, depth); return 0; } p = git__mallocarray(pb->nr_threads, sizeof(*p)); - GITERR_CHECK_ALLOC(p); + GIT_ERROR_CHECK_ALLOC(p); /* Partition the work among the threads */ for (i = 0; i < pb->nr_threads; ++i) { size_t sub_size = list_size / (pb->nr_threads - i); @@ -1213,11 +1213,11 @@ git_cond_init(&p[i].cond); ret = git_thread_create(&p[i].thread, threaded_find_deltas, &p[i]); if (ret) { - giterr_set(GITERR_THREAD, "unable to create thread"); + git_error_set(GIT_ERROR_THREAD, "unable to create thread"); return -1; } active_threads++; } @@ -1282,11 +1282,11 @@ target->remaining = sub_size; target->working = 1; git_packbuilder__progress_unlock(pb); if (git_mutex_lock(&target->mutex)) { - giterr_set(GITERR_THREAD, "unable to lock packfile condition mutex"); + git_error_set(GIT_ERROR_THREAD, "unable to lock packfile condition mutex"); git__free(p); return -1; } target->data_ready = 1; @@ -1323,11 +1323,11 @@ */ if (pb->progress_cb) pb->progress_cb(GIT_PACKBUILDER_DELTAFICATION, 0, pb->nr_objects, pb->progress_cb_payload); delta_list = git__mallocarray(pb->nr_objects, sizeof(*delta_list)); - GITERR_CHECK_ALLOC(delta_list); + GIT_ERROR_CHECK_ALLOC(delta_list); for (i = 0; i < pb->nr_objects; ++i) { git_pobject *po = pb->object_list + i; /* Make sure the item is within our size limits */ @@ -1377,25 +1377,29 @@ int git_packbuilder_write( git_packbuilder *pb, const char *path, unsigned int mode, - git_transfer_progress_cb progress_cb, + git_indexer_progress_cb progress_cb, void *progress_cb_payload) { + git_indexer_options opts = GIT_INDEXER_OPTIONS_INIT; git_indexer *indexer; - git_transfer_progress stats; + git_indexer_progress stats; struct pack_write_context ctx; int t; PREPARE_PACK; + opts.progress_cb = progress_cb; + opts.progress_cb_payload = progress_cb_payload; + if (git_indexer_new( - &indexer, path, mode, pb->odb, progress_cb, progress_cb_payload) < 0) + &indexer, path, mode, pb->odb, &opts) < 0) return -1; - if (!git_repository__cvar(&t, pb->repo, GIT_CVAR_FSYNCOBJECTFILES) && t) + if (!git_repository__configmap_lookup(&t, pb->repo, GIT_CONFIGMAP_FSYNCOBJECTFILES) && t) git_indexer__set_fsync(indexer, 1); ctx.indexer = indexer; ctx.stats = &stats; @@ -1424,11 +1428,11 @@ { int error; struct tree_walk_context *ctx = payload; /* A commit inside a tree represents a submodule commit and should be skipped. */ - if (git_tree_entry_type(entry) == GIT_OBJ_COMMIT) + if (git_tree_entry_type(entry) == GIT_OBJECT_COMMIT) return 0; if (!(error = git_buf_sets(&ctx->buf, root)) && !(error = git_buf_puts(&ctx->buf, git_tree_entry_name(entry)))) error = git_packbuilder_insert( @@ -1461,42 +1465,42 @@ if (!(error = git_tree_lookup(&tree, pb->repo, oid)) && !(error = git_packbuilder_insert(pb, oid, NULL))) error = git_tree_walk(tree, GIT_TREEWALK_PRE, cb_tree_walk, &context); git_tree_free(tree); - git_buf_free(&context.buf); + git_buf_dispose(&context.buf); return error; } int git_packbuilder_insert_recur(git_packbuilder *pb, const git_oid *id, const char *name) { git_object *obj; int error; assert(pb && id); - if ((error = git_object_lookup(&obj, pb->repo, id, GIT_OBJ_ANY)) < 0) + if ((error = git_object_lookup(&obj, pb->repo, id, GIT_OBJECT_ANY)) < 0) return error; switch (git_object_type(obj)) { - case GIT_OBJ_BLOB: + case GIT_OBJECT_BLOB: error = git_packbuilder_insert(pb, id, name); break; - case GIT_OBJ_TREE: + case GIT_OBJECT_TREE: error = git_packbuilder_insert_tree(pb, id); break; - case GIT_OBJ_COMMIT: + case GIT_OBJECT_COMMIT: error = git_packbuilder_insert_commit(pb, id); break; - case GIT_OBJ_TAG: + case GIT_OBJECT_TAG: if ((error = git_packbuilder_insert(pb, id, name)) < 0) goto cleanup; error = git_packbuilder_insert_recur(pb, git_tag_target_id((git_tag *) obj), NULL); break; default: - giterr_set(GITERR_INVALID, "unknown object type"); + git_error_set(GIT_ERROR_INVALID, "unknown object type"); error = -1; } cleanup: git_object_free(obj); @@ -1511,50 +1515,47 @@ size_t git_packbuilder_written(git_packbuilder *pb) { return pb->nr_written; } -int lookup_walk_object(git_walk_object **out, git_packbuilder *pb, const git_oid *id) +static int lookup_walk_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) { - git_walk_object *obj; + struct walk_object *obj; obj = git_pool_mallocz(&pb->object_pool, 1); if (!obj) { - giterr_set_oom(); + git_error_set_oom(); return -1; } git_oid_cpy(&obj->id, id); *out = obj; return 0; } -static int retrieve_object(git_walk_object **out, git_packbuilder *pb, const git_oid *id) +static int retrieve_object(struct walk_object **out, git_packbuilder *pb, const git_oid *id) { + struct walk_object *obj; int error; - khiter_t pos; - git_walk_object *obj; - pos = git_oidmap_lookup_index(pb->walk_objects, id); - if (git_oidmap_valid_index(pb->walk_objects, pos)) { - obj = git_oidmap_value_at(pb->walk_objects, pos); - } else { + if ((obj = git_oidmap_get(pb->walk_objects, id)) == NULL) { if ((error = lookup_walk_object(&obj, pb, id)) < 0) return error; - git_oidmap_insert(pb->walk_objects, &obj->id, obj, &error); + if ((error = git_oidmap_set(pb->walk_objects, &obj->id, obj)) < 0) + return error; } *out = obj; return 0; } static int mark_blob_uninteresting(git_packbuilder *pb, const git_oid *id) { int error; - git_walk_object *obj; + struct walk_object *obj; if ((error = retrieve_object(&obj, pb, id)) < 0) return error; obj->uninteresting = 1; @@ -1562,11 +1563,11 @@ return 0; } static int mark_tree_uninteresting(git_packbuilder *pb, const git_oid *id) { - git_walk_object *obj; + struct walk_object *obj; git_tree *tree; int error; size_t i; if ((error = retrieve_object(&obj, pb, id)) < 0) @@ -1582,15 +1583,15 @@ for (i = 0; i < git_tree_entrycount(tree); i++) { const git_tree_entry *entry = git_tree_entry_byindex(tree, i); const git_oid *entry_id = git_tree_entry_id(entry); switch (git_tree_entry_type(entry)) { - case GIT_OBJ_TREE: + case GIT_OBJECT_TREE: if ((error = mark_tree_uninteresting(pb, entry_id)) < 0) goto cleanup; break; - case GIT_OBJ_BLOB: + case GIT_OBJECT_BLOB: if ((error = mark_blob_uninteresting(pb, entry_id)) < 0) goto cleanup; break; default: /* it's a submodule or something unknown, we don't want it */ @@ -1634,11 +1635,11 @@ int insert_tree(git_packbuilder *pb, git_tree *tree) { size_t i; int error; git_tree *subtree; - git_walk_object *obj; + struct walk_object *obj; const char *name; if ((error = retrieve_object(&obj, pb, git_tree_id(tree))) < 0) return error; @@ -1652,22 +1653,22 @@ for (i = 0; i < git_tree_entrycount(tree); i++) { const git_tree_entry *entry = git_tree_entry_byindex(tree, i); const git_oid *entry_id = git_tree_entry_id(entry); switch (git_tree_entry_type(entry)) { - case GIT_OBJ_TREE: + case GIT_OBJECT_TREE: if ((error = git_tree_lookup(&subtree, pb->repo, entry_id)) < 0) return error; error = insert_tree(pb, subtree); git_tree_free(subtree); if (error < 0) return error; break; - case GIT_OBJ_BLOB: + case GIT_OBJECT_BLOB: if ((error = retrieve_object(&obj, pb, entry_id)) < 0) return error; if (obj->uninteresting) continue; name = git_tree_entry_name(entry); @@ -1682,11 +1683,11 @@ return error; } -int insert_commit(git_packbuilder *pb, git_walk_object *obj) +int insert_commit(git_packbuilder *pb, struct walk_object *obj) { int error; git_commit *commit = NULL; git_tree *tree = NULL; @@ -1712,10 +1713,10 @@ int git_packbuilder_insert_walk(git_packbuilder *pb, git_revwalk *walk) { int error; git_oid id; - git_walk_object *obj; + struct walk_object *obj; assert(pb && walk); if ((error = mark_edges_uninteresting(pb, walk->user_input)) < 0) return error;