static int create_object(git_object **object_out, git_otype type) { git_object *object = NULL; assert(object_out); *object_out = NULL; switch (type) { case GIT_OBJ_COMMIT: case GIT_OBJ_TAG: case GIT_OBJ_BLOB: object = git__malloc(git_object__size(type)); if (object == NULL) return GIT_ENOMEM; memset(object, 0x0, git_object__size(type)); break; case GIT_OBJ_TREE: object = (git_object *)git_tree__new(); if (object == NULL) return GIT_ENOMEM; break; default: return GIT_EINVALIDTYPE; } *object_out = object; return GIT_SUCCESS; }
int git_repository_newobject(git_object **object_out, git_repository *repo, git_otype type) { git_object *object = NULL; assert(object_out && repo); *object_out = NULL; switch (type) { case GIT_OBJ_COMMIT: case GIT_OBJ_TAG: case GIT_OBJ_TREE: case GIT_OBJ_BLOB: break; default: return GIT_EINVALIDTYPE; } object = git__malloc(git_object__size(type)); if (object == NULL) return GIT_ENOMEM; memset(object, 0x0, git_object__size(type)); object->repo = repo; object->in_memory = 1; object->modified = 1; object->source.raw.type = type; *object_out = object; return GIT_SUCCESS; }
static int create_object(git_object **object_out, git_otype type) { git_object *object = NULL; assert(object_out); *object_out = NULL; switch (type) { case GIT_OBJ_COMMIT: case GIT_OBJ_TAG: case GIT_OBJ_BLOB: case GIT_OBJ_TREE: object = git__malloc(git_object__size(type)); GITERR_CHECK_ALLOC(object); memset(object, 0x0, git_object__size(type)); break; default: giterr_set(GITERR_INVALID, "The given type is invalid"); return -1; } object->type = type; *object_out = object; return 0; }
int git_object__from_odb_object( git_object **object_out, git_repository *repo, git_odb_object *odb_obj, git_otype type) { int error; size_t object_size; git_object_def *def; git_object *object = NULL; assert(object_out); *object_out = NULL; /* Validate type match */ if (type != GIT_OBJ_ANY && type != odb_obj->cached.type) { giterr_set(GITERR_INVALID, "The requested type does not match the type in the ODB"); return GIT_ENOTFOUND; } if ((object_size = git_object__size(odb_obj->cached.type)) == 0) { giterr_set(GITERR_INVALID, "The requested type is invalid"); return GIT_ENOTFOUND; } /* Allocate and initialize base object */ object = git__calloc(1, object_size); GITERR_CHECK_ALLOC(object); git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid); object->cached.type = odb_obj->cached.type; object->cached.size = odb_obj->cached.size; object->repo = repo; /* Parse raw object data */ def = &git_objects_table[odb_obj->cached.type]; assert(def->free && def->parse); if ((error = def->parse(object, odb_obj)) < 0) def->free(object); else *object_out = git_cache_store_parsed(&repo->objects, object); return error; }
int git_repository_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type) { git_object *object = NULL; git_rawobj obj_file; int error = GIT_SUCCESS; assert(repo && object_out && id); object = git_hashtable_lookup(repo->objects, id); if (object != NULL) { *object_out = object; return GIT_SUCCESS; } error = git_odb_read(&obj_file, repo->db, id); if (error < GIT_SUCCESS) return error; if (type != GIT_OBJ_ANY && type != obj_file.type) { git_rawobj_close(&obj_file); return GIT_EINVALIDTYPE; } type = obj_file.type; object = git__malloc(git_object__size(type)); if (object == NULL) return GIT_ENOMEM; memset(object, 0x0, git_object__size(type)); /* Initialize parent object */ git_oid_cpy(&object->id, id); object->repo = repo; memcpy(&object->source.raw, &obj_file, sizeof(git_rawobj)); object->source.open = 1; switch (type) { case GIT_OBJ_COMMIT: error = git_commit__parse((git_commit *)object); break; case GIT_OBJ_TREE: error = git_tree__parse((git_tree *)object); break; case GIT_OBJ_TAG: error = git_tag__parse((git_tag *)object); break; case GIT_OBJ_BLOB: error = git_blob__parse((git_blob *)object); break; default: break; } if (error < GIT_SUCCESS) { git_object_free(object); return error; } git_object__source_close(object); git_hashtable_insert(repo->objects, &object->id, object); *object_out = object; return GIT_SUCCESS; }