object: introduce type_is_valid

There's no such thing as a "loose object type" or a "packed object
type". There are only object types. Introduce `type_is_valid` and
deprecate `typeisloose`.
This commit is contained in:
Edward Thomson
2025-01-03 09:21:16 +00:00
parent 842fcef3eb
commit 2d5942571c
6 changed files with 43 additions and 34 deletions

View File

@@ -665,6 +665,16 @@ GIT_EXTERN(int) git_index_add_frombuffer(
*/
GIT_EXTERN(size_t) git_object__size(git_object_t type);
/**
* Determine if the given git_object_t is a valid object type.
*
* @deprecated use `git_object_type_is_valid`
*
* @param type object type to test.
* @return 1 if the type represents a valid object type, 0 otherwise
*/
GIT_EXTERN(int) git_object_typeisloose(git_object_t type);
/**@}*/
/** @name Deprecated Remote Functions

View File

@@ -180,13 +180,12 @@ GIT_EXTERN(const char *) git_object_type2string(git_object_t type);
GIT_EXTERN(git_object_t) git_object_string2type(const char *str);
/**
* Determine if the given git_object_t is a valid loose object type.
* Determine if the given git_object_t is a valid object type.
*
* @param type object type to test.
* @return true if the type represents a valid loose object type,
* false otherwise.
* @return 1 if the type represents a valid loose object type, 0 otherwise
*/
GIT_EXTERN(int) git_object_typeisloose(git_object_t type);
GIT_EXTERN(int) git_object_type_is_valid(git_object_t type);
/**
* Recursively peel an object until an object of the specified type is met.

View File

@@ -33,7 +33,7 @@ typedef struct {
} git_object_def;
static git_object_def git_objects_table[] = {
/* 0 = GIT_OBJECT__EXT1 */
/* 0 = unused */
{ "", 0, NULL, NULL, NULL },
/* 1 = GIT_OBJECT_COMMIT */
@@ -46,14 +46,7 @@ static git_object_def git_objects_table[] = {
{ "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free },
/* 4 = GIT_OBJECT_TAG */
{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free },
/* 5 = GIT_OBJECT__EXT2 */
{ "", 0, NULL, NULL, NULL },
/* 6 = GIT_OBJECT_OFS_DELTA */
{ "OFS_DELTA", 0, NULL, NULL, NULL },
/* 7 = GIT_OBJECT_REF_DELTA */
{ "REF_DELTA", 0, NULL, NULL, NULL },
{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free }
};
int git_object__from_raw(
@@ -342,7 +335,7 @@ git_object_t git_object_stringn2type(const char *str, size_t len)
return GIT_OBJECT_INVALID;
}
int git_object_typeisloose(git_object_t type)
int git_object_type_is_valid(git_object_t type)
{
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
return 0;
@@ -350,6 +343,13 @@ int git_object_typeisloose(git_object_t type)
return (git_objects_table[type].size > 0) ? 1 : 0;
}
#ifndef GIT_DEPRECATE_HARD
int git_object_typeisloose(git_object_t type)
{
return git_object_type_is_valid(type);
}
#endif
size_t git_object__size(git_object_t type)
{
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))

View File

@@ -116,7 +116,7 @@ int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type)
GIT_ASSERT_ARG(id);
GIT_ASSERT_ARG(obj);
if (!git_object_typeisloose(obj->type)) {
if (!git_object_type_is_valid(obj->type)) {
git_error_set(GIT_ERROR_INVALID, "invalid object type");
return -1;
}
@@ -219,7 +219,7 @@ int git_odb__hashfd(
ssize_t read_len = 0;
int error = 0;
if (!git_object_typeisloose(object_type)) {
if (!git_object_type_is_valid(object_type)) {
git_error_set(GIT_ERROR_INVALID, "invalid object type for hash");
return -1;
}

View File

@@ -243,7 +243,7 @@ static int read_loose_packlike(git_rawobj *out, git_str *obj)
if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0)
goto done;
if (!git_object_typeisloose(hdr.type) || head_len > obj_len) {
if (!git_object_type_is_valid(hdr.type) || head_len > obj_len) {
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object");
error = -1;
goto done;
@@ -296,7 +296,7 @@ static int read_loose_standard(git_rawobj *out, git_str *obj)
(error = parse_header(&hdr, &head_len, head, decompressed)) < 0)
goto done;
if (!git_object_typeisloose(hdr.type)) {
if (!git_object_type_is_valid(hdr.type)) {
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object");
error = -1;
goto done;
@@ -436,7 +436,7 @@ static int read_header_loose(git_rawobj *out, git_str *loc)
else
error = read_header_loose_standard(out, obj, (size_t)obj_len);
if (!error && !git_object_typeisloose(out->type)) {
if (!error && !git_object_type_is_valid(out->type)) {
git_error_set(GIT_ERROR_ZLIB, "failed to read loose object header");
error = -1;
goto done;
@@ -954,7 +954,7 @@ static int loose_backend__readstream_packlike(
if ((error = parse_header_packlike(hdr, &head_len, data, data_len)) < 0)
return error;
if (!git_object_typeisloose(hdr->type)) {
if (!git_object_type_is_valid(hdr->type)) {
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object");
return -1;
}
@@ -986,7 +986,7 @@ static int loose_backend__readstream_standard(
(error = parse_header(hdr, &head_len, head, init)) < 0)
return error;
if (!git_object_typeisloose(hdr->type)) {
if (!git_object_type_is_valid(hdr->type)) {
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object");
return -1;
}

View File

@@ -36,19 +36,19 @@ void test_object_raw_type2string__convert_string_to_type(void)
cl_assert(git_object_string2type("hohoho") == GIT_OBJECT_INVALID);
}
void test_object_raw_type2string__check_type_is_loose(void)
void test_object_raw_type2string__check_type_is_valid(void)
{
cl_assert(git_object_typeisloose(GIT_OBJECT_INVALID) == 0);
cl_assert(git_object_typeisloose(0) == 0); /* EXT1 */
cl_assert(git_object_typeisloose(GIT_OBJECT_COMMIT) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_TREE) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_BLOB) == 1);
cl_assert(git_object_typeisloose(GIT_OBJECT_TAG) == 1);
cl_assert(git_object_typeisloose(5) == 0); /* EXT2 */
cl_assert(git_object_typeisloose(GIT_OBJECT_OFS_DELTA) == 0);
cl_assert(git_object_typeisloose(GIT_OBJECT_REF_DELTA) == 0);
cl_assert(git_object_type_is_valid(GIT_OBJECT_INVALID) == 0);
cl_assert(git_object_type_is_valid(0) == 0); /* EXT1 */
cl_assert(git_object_type_is_valid(GIT_OBJECT_COMMIT) == 1);
cl_assert(git_object_type_is_valid(GIT_OBJECT_TREE) == 1);
cl_assert(git_object_type_is_valid(GIT_OBJECT_BLOB) == 1);
cl_assert(git_object_type_is_valid(GIT_OBJECT_TAG) == 1);
cl_assert(git_object_type_is_valid(5) == 0); /* EXT2 */
cl_assert(git_object_type_is_valid(GIT_OBJECT_OFS_DELTA) == 0);
cl_assert(git_object_type_is_valid(GIT_OBJECT_REF_DELTA) == 0);
cl_assert(git_object_typeisloose(-2) == 0);
cl_assert(git_object_typeisloose(8) == 0);
cl_assert(git_object_typeisloose(1234) == 0);
cl_assert(git_object_type_is_valid(-2) == 0);
cl_assert(git_object_type_is_valid(8) == 0);
cl_assert(git_object_type_is_valid(1234) == 0);
}