Skip to content

Commit caa65e0

Browse files
authored
Merge pull request libgit2#7000 from libgit2/ethomson/object_type
2 parents 666bbed + 23da3a8 commit caa65e0

File tree

13 files changed

+76
-65
lines changed

13 files changed

+76
-65
lines changed

include/git2/deprecated.h

+10
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,16 @@ GIT_EXTERN(int) git_index_add_frombuffer(
665665
*/
666666
GIT_EXTERN(size_t) git_object__size(git_object_t type);
667667

668+
/**
669+
* Determine if the given git_object_t is a valid object type.
670+
*
671+
* @deprecated use `git_object_type_is_valid`
672+
*
673+
* @param type object type to test.
674+
* @return 1 if the type represents a valid object type, 0 otherwise
675+
*/
676+
GIT_EXTERN(int) git_object_typeisloose(git_object_t type);
677+
668678
/**@}*/
669679

670680
/** @name Deprecated Remote Functions

include/git2/object.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,12 @@ GIT_EXTERN(const char *) git_object_type2string(git_object_t type);
180180
GIT_EXTERN(git_object_t) git_object_string2type(const char *str);
181181

182182
/**
183-
* Determine if the given git_object_t is a valid loose object type.
183+
* Determine if the given git_object_t is a valid object type.
184184
*
185185
* @param type object type to test.
186-
* @return true if the type represents a valid loose object type,
187-
* false otherwise.
186+
* @return 1 if the type represents a valid loose object type, 0 otherwise
188187
*/
189-
GIT_EXTERN(int) git_object_typeisloose(git_object_t type);
188+
GIT_EXTERN(int) git_object_type_is_valid(git_object_t type);
190189

191190
/**
192191
* Recursively peel an object until an object of the specified type is met.

include/git2/types.h

+1-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ typedef enum {
7676
GIT_OBJECT_COMMIT = 1, /**< A commit object. */
7777
GIT_OBJECT_TREE = 2, /**< A tree (directory listing) object. */
7878
GIT_OBJECT_BLOB = 3, /**< A file revision object. */
79-
GIT_OBJECT_TAG = 4, /**< An annotated tag object. */
80-
GIT_OBJECT_OFS_DELTA = 6, /**< A delta, base is given by an offset. */
81-
GIT_OBJECT_REF_DELTA = 7 /**< A delta, base is given by object id. */
79+
GIT_OBJECT_TAG = 4 /**< An annotated tag object. */
8280
} git_object_t;
8381

8482
/**

src/libgit2/indexer.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ static int advance_delta_offset(git_indexer *idx, git_object_t type)
319319
{
320320
git_mwindow *w = NULL;
321321

322-
GIT_ASSERT_ARG(type == GIT_OBJECT_REF_DELTA || type == GIT_OBJECT_OFS_DELTA);
322+
GIT_ASSERT_ARG(type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA);
323323

324-
if (type == GIT_OBJECT_REF_DELTA) {
324+
if (type == GIT_PACKFILE_REF_DELTA) {
325325
idx->off += git_oid_size(idx->oid_type);
326326
} else {
327327
off64_t base_off;
@@ -813,7 +813,7 @@ static int read_stream_object(git_indexer *idx, git_indexer_progress *stats)
813813
git_hash_init(&idx->hash_ctx);
814814
git_str_clear(&idx->entry_data);
815815

816-
if (type == GIT_OBJECT_REF_DELTA || type == GIT_OBJECT_OFS_DELTA) {
816+
if (type == GIT_PACKFILE_REF_DELTA || type == GIT_PACKFILE_OFS_DELTA) {
817817
error = advance_delta_offset(idx, type);
818818
if (error == GIT_EBUFS) {
819819
idx->off = entry_start;
@@ -1094,7 +1094,7 @@ static int fix_thin_pack(git_indexer *idx, git_indexer_progress *stats)
10941094
if (error < 0)
10951095
return error;
10961096

1097-
if (type == GIT_OBJECT_REF_DELTA) {
1097+
if (type == GIT_PACKFILE_REF_DELTA) {
10981098
found_ref_delta = 1;
10991099
break;
11001100
}

src/libgit2/object.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ typedef struct {
3333
} git_object_def;
3434

3535
static git_object_def git_objects_table[] = {
36-
/* 0 = GIT_OBJECT__EXT1 */
36+
/* 0 = unused */
3737
{ "", 0, NULL, NULL, NULL },
3838

3939
/* 1 = GIT_OBJECT_COMMIT */
@@ -46,14 +46,7 @@ static git_object_def git_objects_table[] = {
4646
{ "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free },
4747

4848
/* 4 = GIT_OBJECT_TAG */
49-
{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free },
50-
51-
/* 5 = GIT_OBJECT__EXT2 */
52-
{ "", 0, NULL, NULL, NULL },
53-
/* 6 = GIT_OBJECT_OFS_DELTA */
54-
{ "OFS_DELTA", 0, NULL, NULL, NULL },
55-
/* 7 = GIT_OBJECT_REF_DELTA */
56-
{ "REF_DELTA", 0, NULL, NULL, NULL },
49+
{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free }
5750
};
5851

5952
int git_object__from_raw(
@@ -342,14 +335,21 @@ git_object_t git_object_stringn2type(const char *str, size_t len)
342335
return GIT_OBJECT_INVALID;
343336
}
344337

345-
int git_object_typeisloose(git_object_t type)
338+
int git_object_type_is_valid(git_object_t type)
346339
{
347340
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
348341
return 0;
349342

350343
return (git_objects_table[type].size > 0) ? 1 : 0;
351344
}
352345

346+
#ifndef GIT_DEPRECATE_HARD
347+
int git_object_typeisloose(git_object_t type)
348+
{
349+
return git_object_type_is_valid(type);
350+
}
351+
#endif
352+
353353
size_t git_object__size(git_object_t type)
354354
{
355355
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))

src/libgit2/odb.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ int git_odb__hashobj(git_oid *id, git_rawobj *obj, git_oid_t oid_type)
116116
GIT_ASSERT_ARG(id);
117117
GIT_ASSERT_ARG(obj);
118118

119-
if (!git_object_typeisloose(obj->type)) {
119+
if (!git_object_type_is_valid(obj->type)) {
120120
git_error_set(GIT_ERROR_INVALID, "invalid object type");
121121
return -1;
122122
}
@@ -219,7 +219,7 @@ int git_odb__hashfd(
219219
ssize_t read_len = 0;
220220
int error = 0;
221221

222-
if (!git_object_typeisloose(object_type)) {
222+
if (!git_object_type_is_valid(object_type)) {
223223
git_error_set(GIT_ERROR_INVALID, "invalid object type for hash");
224224
return -1;
225225
}

src/libgit2/odb_loose.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ static int read_loose_packlike(git_rawobj *out, git_str *obj)
243243
if ((error = parse_header_packlike(&hdr, &head_len, obj_data, obj_len)) < 0)
244244
goto done;
245245

246-
if (!git_object_typeisloose(hdr.type) || head_len > obj_len) {
246+
if (!git_object_type_is_valid(hdr.type) || head_len > obj_len) {
247247
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object");
248248
error = -1;
249249
goto done;
@@ -296,7 +296,7 @@ static int read_loose_standard(git_rawobj *out, git_str *obj)
296296
(error = parse_header(&hdr, &head_len, head, decompressed)) < 0)
297297
goto done;
298298

299-
if (!git_object_typeisloose(hdr.type)) {
299+
if (!git_object_type_is_valid(hdr.type)) {
300300
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object");
301301
error = -1;
302302
goto done;
@@ -436,7 +436,7 @@ static int read_header_loose(git_rawobj *out, git_str *loc)
436436
else
437437
error = read_header_loose_standard(out, obj, (size_t)obj_len);
438438

439-
if (!error && !git_object_typeisloose(out->type)) {
439+
if (!error && !git_object_type_is_valid(out->type)) {
440440
git_error_set(GIT_ERROR_ZLIB, "failed to read loose object header");
441441
error = -1;
442442
goto done;
@@ -954,7 +954,7 @@ static int loose_backend__readstream_packlike(
954954
if ((error = parse_header_packlike(hdr, &head_len, data, data_len)) < 0)
955955
return error;
956956

957-
if (!git_object_typeisloose(hdr->type)) {
957+
if (!git_object_type_is_valid(hdr->type)) {
958958
git_error_set(GIT_ERROR_ODB, "failed to inflate loose object");
959959
return -1;
960960
}
@@ -986,7 +986,7 @@ static int loose_backend__readstream_standard(
986986
(error = parse_header(hdr, &head_len, head, init)) < 0)
987987
return error;
988988

989-
if (!git_object_typeisloose(hdr->type)) {
989+
if (!git_object_type_is_valid(hdr->type)) {
990990
git_error_set(GIT_ERROR_ODB, "failed to inflate disk object");
991991
return -1;
992992
}

src/libgit2/pack-objects.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ static int write_object(
338338
goto done;
339339

340340
data_len = po->delta_size;
341-
type = GIT_OBJECT_REF_DELTA;
341+
type = GIT_PACKFILE_REF_DELTA;
342342
} else {
343343
if ((error = git_odb_read(&obj, pb->odb, &po->id)) < 0)
344344
goto done;
@@ -354,7 +354,7 @@ static int write_object(
354354
(error = git_hash_update(&pb->ctx, hdr, hdr_len)) < 0)
355355
goto done;
356356

357-
if (type == GIT_OBJECT_REF_DELTA) {
357+
if (type == GIT_PACKFILE_REF_DELTA) {
358358
if ((error = write_cb(po->delta->id.id, oid_size, cb_data)) < 0 ||
359359
(error = git_hash_update(&pb->ctx, po->delta->id.id, oid_size)) < 0)
360360
goto done;

src/libgit2/pack.c

+10-10
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ int git_packfile__object_header(size_t *out, unsigned char *hdr, size_t size, gi
390390
unsigned char *hdr_base;
391391
unsigned char c;
392392

393-
GIT_ASSERT_ARG(type >= GIT_OBJECT_COMMIT && type <= GIT_OBJECT_REF_DELTA);
393+
GIT_ASSERT_ARG(type >= GIT_OBJECT_COMMIT && type <= GIT_PACKFILE_REF_DELTA);
394394

395395
/* TODO: add support for chunked objects; see git.git 6c0d19b1 */
396396

@@ -532,7 +532,7 @@ int git_packfile_resolve_header(
532532
if (error < 0)
533533
return error;
534534

535-
if (type == GIT_OBJECT_OFS_DELTA || type == GIT_OBJECT_REF_DELTA) {
535+
if (type == GIT_PACKFILE_OFS_DELTA || type == GIT_PACKFILE_REF_DELTA) {
536536
size_t base_size;
537537
git_packfile_stream stream;
538538

@@ -553,12 +553,12 @@ int git_packfile_resolve_header(
553553
base_offset = 0;
554554
}
555555

556-
while (type == GIT_OBJECT_OFS_DELTA || type == GIT_OBJECT_REF_DELTA) {
556+
while (type == GIT_PACKFILE_OFS_DELTA || type == GIT_PACKFILE_REF_DELTA) {
557557
curpos = base_offset;
558558
error = git_packfile_unpack_header(&size, &type, p, &w_curs, &curpos);
559559
if (error < 0)
560560
return error;
561-
if (type != GIT_OBJECT_OFS_DELTA && type != GIT_OBJECT_REF_DELTA)
561+
if (type != GIT_PACKFILE_OFS_DELTA && type != GIT_PACKFILE_REF_DELTA)
562562
break;
563563

564564
error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, base_offset);
@@ -635,7 +635,7 @@ static int pack_dependency_chain(git_dependency_chain *chain_out,
635635
elem->type = type;
636636
elem->base_key = obj_offset;
637637

638-
if (type != GIT_OBJECT_OFS_DELTA && type != GIT_OBJECT_REF_DELTA)
638+
if (type != GIT_PACKFILE_OFS_DELTA && type != GIT_PACKFILE_REF_DELTA)
639639
break;
640640

641641
error = get_delta_base(&base_offset, p, &w_curs, &curpos, type, obj_offset);
@@ -675,7 +675,7 @@ int git_packfile_unpack(
675675
git_pack_cache_entry *cached = NULL;
676676
struct pack_chain_elem small_stack[SMALL_STACK_SIZE];
677677
size_t stack_size = 0, elem_pos, alloclen;
678-
git_object_t base_type;
678+
int base_type;
679679

680680
error = git_mutex_lock(&p->lock);
681681
if (error < 0) {
@@ -735,8 +735,8 @@ int git_packfile_unpack(
735735
if (error < 0)
736736
goto cleanup;
737737
break;
738-
case GIT_OBJECT_OFS_DELTA:
739-
case GIT_OBJECT_REF_DELTA:
738+
case GIT_PACKFILE_OFS_DELTA:
739+
case GIT_PACKFILE_REF_DELTA:
740740
error = packfile_error("dependency chain ends in a delta");
741741
goto cleanup;
742742
default:
@@ -983,7 +983,7 @@ int get_delta_base(
983983
* than the hash size is stupid, as then a REF_DELTA would be
984984
* smaller to store.
985985
*/
986-
if (type == GIT_OBJECT_OFS_DELTA) {
986+
if (type == GIT_PACKFILE_OFS_DELTA) {
987987
unsigned used = 0;
988988
unsigned char c = base_info[used++];
989989
size_t unsigned_base_offset = c & 127;
@@ -1000,7 +1000,7 @@ int get_delta_base(
10001000
return packfile_error("out of bounds");
10011001
base_offset = delta_obj_offset - unsigned_base_offset;
10021002
*curpos += used;
1003-
} else if (type == GIT_OBJECT_REF_DELTA) {
1003+
} else if (type == GIT_PACKFILE_REF_DELTA) {
10041004
git_oid base_oid;
10051005
git_oid_from_raw(&base_oid, base_info, p->oid_type);
10061006

src/libgit2/pack.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ typedef int git_pack_foreach_entry_offset_cb(
3333
#define PACK_SIGNATURE 0x5041434b /* "PACK" */
3434
#define PACK_VERSION 2
3535
#define pack_version_ok(v) ((v) == htonl(2))
36+
37+
#define GIT_PACKFILE_OFS_DELTA 6
38+
#define GIT_PACKFILE_REF_DELTA 7
39+
3640
struct git_pack_header {
3741
uint32_t hdr_signature;
3842
uint32_t hdr_version;

tests/libgit2/object/raw/hash.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ void test_object_raw_hash__hash_junk_data(void)
8787
junk_obj.data = some_data;
8888
hash_object_fail(&id, &junk_obj);
8989

90-
junk_obj.type = 0; /* EXT1 */
90+
junk_obj.type = 0; /* unused */
9191
hash_object_fail(&id, &junk_obj);
9292

93-
junk_obj.type = 5; /* EXT2 */
93+
junk_obj.type = 5; /* unused */
9494
hash_object_fail(&id, &junk_obj);
9595

96-
junk_obj.type = GIT_OBJECT_OFS_DELTA;
96+
junk_obj.type = 6; /* packfile offset delta */
9797
hash_object_fail(&id, &junk_obj);
9898

99-
junk_obj.type = GIT_OBJECT_REF_DELTA;
99+
junk_obj.type = 7; /* packfile ref delta */
100100
hash_object_fail(&id, &junk_obj);
101101

102102
junk_obj.type = 42;

tests/libgit2/object/raw/type2string.c

+20-20
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
void test_object_raw_type2string__convert_type_to_string(void)
88
{
99
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_INVALID), "");
10-
cl_assert_equal_s(git_object_type2string(0), ""); /* EXT1 */
10+
cl_assert_equal_s(git_object_type2string(0), ""); /* unused */
1111
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_COMMIT), "commit");
1212
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_TREE), "tree");
1313
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_BLOB), "blob");
1414
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_TAG), "tag");
15-
cl_assert_equal_s(git_object_type2string(5), ""); /* EXT2 */
16-
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_OFS_DELTA), "OFS_DELTA");
17-
cl_assert_equal_s(git_object_type2string(GIT_OBJECT_REF_DELTA), "REF_DELTA");
15+
cl_assert_equal_s(git_object_type2string(5), ""); /* unused */
16+
cl_assert_equal_s(git_object_type2string(6), ""); /* packfile offset delta */
17+
cl_assert_equal_s(git_object_type2string(7), ""); /* packfile ref delta */
1818

1919
cl_assert_equal_s(git_object_type2string(-2), "");
2020
cl_assert_equal_s(git_object_type2string(8), "");
@@ -29,26 +29,26 @@ void test_object_raw_type2string__convert_string_to_type(void)
2929
cl_assert(git_object_string2type("tree") == GIT_OBJECT_TREE);
3030
cl_assert(git_object_string2type("blob") == GIT_OBJECT_BLOB);
3131
cl_assert(git_object_string2type("tag") == GIT_OBJECT_TAG);
32-
cl_assert(git_object_string2type("OFS_DELTA") == GIT_OBJECT_OFS_DELTA);
33-
cl_assert(git_object_string2type("REF_DELTA") == GIT_OBJECT_REF_DELTA);
32+
cl_assert(git_object_string2type("OFS_DELTA") == GIT_OBJECT_INVALID);
33+
cl_assert(git_object_string2type("REF_DELTA") == GIT_OBJECT_INVALID);
3434

3535
cl_assert(git_object_string2type("CoMmIt") == GIT_OBJECT_INVALID);
3636
cl_assert(git_object_string2type("hohoho") == GIT_OBJECT_INVALID);
3737
}
3838

39-
void test_object_raw_type2string__check_type_is_loose(void)
39+
void test_object_raw_type2string__check_type_is_valid(void)
4040
{
41-
cl_assert(git_object_typeisloose(GIT_OBJECT_INVALID) == 0);
42-
cl_assert(git_object_typeisloose(0) == 0); /* EXT1 */
43-
cl_assert(git_object_typeisloose(GIT_OBJECT_COMMIT) == 1);
44-
cl_assert(git_object_typeisloose(GIT_OBJECT_TREE) == 1);
45-
cl_assert(git_object_typeisloose(GIT_OBJECT_BLOB) == 1);
46-
cl_assert(git_object_typeisloose(GIT_OBJECT_TAG) == 1);
47-
cl_assert(git_object_typeisloose(5) == 0); /* EXT2 */
48-
cl_assert(git_object_typeisloose(GIT_OBJECT_OFS_DELTA) == 0);
49-
cl_assert(git_object_typeisloose(GIT_OBJECT_REF_DELTA) == 0);
50-
51-
cl_assert(git_object_typeisloose(-2) == 0);
52-
cl_assert(git_object_typeisloose(8) == 0);
53-
cl_assert(git_object_typeisloose(1234) == 0);
41+
cl_assert(git_object_type_is_valid(GIT_OBJECT_INVALID) == 0);
42+
cl_assert(git_object_type_is_valid(0) == 0); /* unused */
43+
cl_assert(git_object_type_is_valid(GIT_OBJECT_COMMIT) == 1);
44+
cl_assert(git_object_type_is_valid(GIT_OBJECT_TREE) == 1);
45+
cl_assert(git_object_type_is_valid(GIT_OBJECT_BLOB) == 1);
46+
cl_assert(git_object_type_is_valid(GIT_OBJECT_TAG) == 1);
47+
cl_assert(git_object_type_is_valid(5) == 0); /* unused */
48+
cl_assert(git_object_type_is_valid(6) == 0); /* packfile offset delta */
49+
cl_assert(git_object_type_is_valid(7) == 0); /* packfile ref delta */
50+
51+
cl_assert(git_object_type_is_valid(-2) == 0);
52+
cl_assert(git_object_type_is_valid(8) == 0);
53+
cl_assert(git_object_type_is_valid(1234) == 0);
5454
}

tests/libgit2/repo/hashfile.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void test_repo_hashfile__simple(void)
3434

3535
/* hash with invalid type */
3636
cl_git_fail(git_odb__hashfile(&a, full.ptr, GIT_OBJECT_ANY, GIT_OID_SHA1));
37-
cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, GIT_OBJECT_OFS_DELTA, NULL));
37+
cl_git_fail(git_repository_hashfile(&b, _repo, full.ptr, 6, NULL));
3838

3939
git_str_dispose(&full);
4040
}

0 commit comments

Comments
 (0)