mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-03 20:02:04 +00:00
git_odb_expand_ids: accept git_odb_expand_id array
Take (and write to) an array of a struct, `git_odb_expand_id`.
This commit is contained in:
parent
4b1f0f79ac
commit
62484f52d1
@ -173,6 +173,27 @@ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
|
|||||||
GIT_EXTERN(int) git_odb_exists_prefix(
|
GIT_EXTERN(int) git_odb_exists_prefix(
|
||||||
git_oid *out, git_odb *db, const git_oid *short_id, size_t len);
|
git_oid *out, git_odb *db, const git_oid *short_id, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The information about object IDs to query in `git_odb_expand_ids`,
|
||||||
|
* which will be populated upon return.
|
||||||
|
*/
|
||||||
|
typedef struct git_odb_expand_id {
|
||||||
|
/** The object ID to expand */
|
||||||
|
git_oid id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The length of the object ID (in nibbles, or packets of 4 bits; the
|
||||||
|
* number of hex characters)
|
||||||
|
* */
|
||||||
|
unsigned short length;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The (optional) type of the object to search for; leave as `0` or set
|
||||||
|
* to `GIT_OBJ_ANY` to query for any object matching the ID.
|
||||||
|
*/
|
||||||
|
git_otype type;
|
||||||
|
} git_odb_expand_id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if one or more objects can be found in the object database
|
* Determine if one or more objects can be found in the object database
|
||||||
* by their abbreviated object ID and type. The given array will be
|
* by their abbreviated object ID and type. The given array will be
|
||||||
@ -187,20 +208,14 @@ GIT_EXTERN(int) git_odb_exists_prefix(
|
|||||||
* not found (which is unlike other object database operations.)
|
* not found (which is unlike other object database operations.)
|
||||||
*
|
*
|
||||||
* @param db The database to be searched for the given objects.
|
* @param db The database to be searched for the given objects.
|
||||||
* @param ids An array of object IDs to search for
|
* @param ids An array of short object IDs to search for
|
||||||
* @param id_lengths The corresponding length of each entry in the `ids`
|
* @param count The length of the `ids` array
|
||||||
* array
|
|
||||||
* @param types The corresponding type of each entry in the `ids` array
|
|
||||||
* (or null to lookup an object of any type)
|
|
||||||
* @param cnt The length of the `ids`, `id_lengths` and `types` arrays
|
|
||||||
* @return 0 on success or an error code on failure
|
* @return 0 on success or an error code on failure
|
||||||
*/
|
*/
|
||||||
GIT_EXTERN(int) git_odb_expand_ids(
|
GIT_EXTERN(int) git_odb_expand_ids(
|
||||||
git_odb *db,
|
git_odb *db,
|
||||||
git_oid *ids,
|
git_odb_expand_id *ids,
|
||||||
size_t *id_lengths,
|
size_t count);
|
||||||
git_otype *types,
|
|
||||||
size_t cnt);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refresh the object database to load newly added files.
|
* Refresh the object database to load newly added files.
|
||||||
|
51
src/odb.c
51
src/odb.c
@ -744,64 +744,51 @@ int git_odb_exists_prefix(
|
|||||||
|
|
||||||
int git_odb_expand_ids(
|
int git_odb_expand_ids(
|
||||||
git_odb *db,
|
git_odb *db,
|
||||||
git_oid *ids,
|
git_odb_expand_id *ids,
|
||||||
size_t *id_lengths,
|
size_t count)
|
||||||
git_otype *types,
|
|
||||||
size_t cnt)
|
|
||||||
{
|
{
|
||||||
size_t len, i;
|
size_t len, i;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
assert(db && ids && id_lengths);
|
assert(db && ids);
|
||||||
|
|
||||||
for (i = 0; i < cnt; i++) {
|
for (i = 0; i < count; i++) {
|
||||||
|
git_odb_expand_id *query = &ids[i];
|
||||||
git_oid *actual_id = NULL, tmp;
|
git_oid *actual_id = NULL, tmp;
|
||||||
|
git_otype query_type = (query->type == GIT_OBJ_ANY) ? 0 : query->type;
|
||||||
git_otype actual_type = 0;
|
git_otype actual_type = 0;
|
||||||
|
|
||||||
/* if we were given a full object ID, simply look it up */
|
/* if we were given a full object ID, simply look it up */
|
||||||
if (id_lengths[i] >= GIT_OID_HEXSZ) {
|
if (query->length >= GIT_OID_HEXSZ) {
|
||||||
error = git_odb_read_header(&len, &actual_type, db, &ids[i]);
|
error = git_odb_read_header(&len, &actual_type, db, &query->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* otherwise, resolve the short id to full, then (optionally)
|
/* otherwise, resolve the short id to full, then (optionally)
|
||||||
* read the header.
|
* read the header.
|
||||||
*/
|
*/
|
||||||
else if (id_lengths[i] >= GIT_OID_MINPREFIXLEN) {
|
else if (query->length >= GIT_OID_MINPREFIXLEN) {
|
||||||
error = odb_exists_prefix_1(&tmp,
|
error = odb_exists_prefix_1(&tmp,
|
||||||
db, &ids[i], id_lengths[i], false);
|
db, &query->id, query->length, false);
|
||||||
|
|
||||||
if (!error) {
|
if (!error) {
|
||||||
actual_id = &tmp;
|
actual_id = &tmp;
|
||||||
|
error = git_odb_read_header(&len, &actual_type, db, &tmp);
|
||||||
if (types)
|
|
||||||
error = git_odb_read_header(&len, &actual_type, db, &tmp);
|
|
||||||
else
|
|
||||||
actual_type = GIT_OBJ_ANY;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error < 0 && error != GIT_ENOTFOUND && error != GIT_EAMBIGUOUS)
|
if (error < 0 && error != GIT_ENOTFOUND && error != GIT_EAMBIGUOUS)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
error = 0;
|
if (error == 0 && (query_type == actual_type || !query_type)) {
|
||||||
|
|
||||||
if (types && types[i] != GIT_OBJ_ANY && types[i] != actual_type)
|
|
||||||
actual_type = 0;
|
|
||||||
|
|
||||||
if (!actual_type) {
|
|
||||||
memset(&ids[i], 0, sizeof(git_oid));
|
|
||||||
id_lengths[i] = 0;
|
|
||||||
|
|
||||||
if (types)
|
|
||||||
types[i] = 0;
|
|
||||||
} else {
|
|
||||||
if (actual_id)
|
if (actual_id)
|
||||||
git_oid_cpy(&ids[i], actual_id);
|
git_oid_cpy(&query->id, actual_id);
|
||||||
|
|
||||||
id_lengths[i] = GIT_OID_HEXSZ;
|
query->length = GIT_OID_HEXSZ;
|
||||||
|
query->type = actual_type;
|
||||||
if (types)
|
} else {
|
||||||
types[i] = actual_type;
|
memset(&query->id, 0, sizeof(git_oid));
|
||||||
|
query->length = 0;
|
||||||
|
query->type = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,35 +146,31 @@ struct expand_id_test_data expand_id_test_data[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static void setup_prefix_query(
|
static void setup_prefix_query(
|
||||||
git_oid **out_ids,
|
git_odb_expand_id **out_ids,
|
||||||
size_t **out_lengths,
|
|
||||||
git_otype **out_types,
|
|
||||||
size_t *out_num)
|
size_t *out_num)
|
||||||
{
|
{
|
||||||
git_oid *ids;
|
git_odb_expand_id *ids;
|
||||||
git_otype *types;
|
size_t num, i;
|
||||||
size_t num, *lengths, i;
|
|
||||||
|
|
||||||
num = ARRAY_SIZE(expand_id_test_data);
|
num = ARRAY_SIZE(expand_id_test_data);
|
||||||
|
|
||||||
cl_assert((ids = git__calloc(num, sizeof(git_oid))));
|
cl_assert((ids = git__calloc(num, sizeof(git_odb_expand_id))));
|
||||||
cl_assert((lengths = git__calloc(num, sizeof(size_t))));
|
|
||||||
cl_assert((types = git__calloc(num, sizeof(git_otype))));
|
|
||||||
|
|
||||||
for (i = 0; i < num; i++) {
|
for (i = 0; i < num; i++) {
|
||||||
lengths[i] = strlen(expand_id_test_data[i].lookup_id);
|
git_odb_expand_id *id = &ids[i];
|
||||||
git_oid_fromstrn(&ids[i], expand_id_test_data[i].lookup_id, lengths[i]);
|
|
||||||
types[i] = expand_id_test_data[i].expected_type;
|
size_t len = strlen(expand_id_test_data[i].lookup_id);
|
||||||
|
|
||||||
|
git_oid_fromstrn(&id->id, expand_id_test_data[i].lookup_id, len);
|
||||||
|
id->length = (unsigned short)len;
|
||||||
|
id->type = expand_id_test_data[i].expected_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
*out_ids = ids;
|
*out_ids = ids;
|
||||||
*out_lengths = lengths;
|
|
||||||
*out_types = types;
|
|
||||||
*out_num = num;
|
*out_num = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void assert_found_objects(
|
static void assert_found_objects(git_odb_expand_id *ids)
|
||||||
git_oid *ids, size_t *lengths, git_otype *types)
|
|
||||||
{
|
{
|
||||||
size_t num, i;
|
size_t num, i;
|
||||||
|
|
||||||
@ -191,16 +187,13 @@ static void assert_found_objects(
|
|||||||
expected_type = expand_id_test_data[i].expected_type;
|
expected_type = expand_id_test_data[i].expected_type;
|
||||||
}
|
}
|
||||||
|
|
||||||
cl_assert_equal_i(expected_len, lengths[i]);
|
cl_assert_equal_oid(&expected_id, &ids[i].id);
|
||||||
cl_assert_equal_oid(&expected_id, &ids[i]);
|
cl_assert_equal_i(expected_len, ids[i].length);
|
||||||
|
cl_assert_equal_i(expected_type, ids[i].type);
|
||||||
if (types)
|
|
||||||
cl_assert_equal_i(expected_type, types[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void assert_notfound_objects(
|
static void assert_notfound_objects(git_odb_expand_id *ids)
|
||||||
git_oid *ids, size_t *lengths, git_otype *types)
|
|
||||||
{
|
{
|
||||||
git_oid expected_id = {{0}};
|
git_oid expected_id = {{0}};
|
||||||
size_t num, i;
|
size_t num, i;
|
||||||
@ -208,54 +201,56 @@ static void assert_notfound_objects(
|
|||||||
num = ARRAY_SIZE(expand_id_test_data);
|
num = ARRAY_SIZE(expand_id_test_data);
|
||||||
|
|
||||||
for (i = 0; i < num; i++) {
|
for (i = 0; i < num; i++) {
|
||||||
cl_assert_equal_i(0, lengths[i]);
|
cl_assert_equal_oid(&expected_id, &ids[i].id);
|
||||||
cl_assert_equal_oid(&expected_id, &ids[i]);
|
cl_assert_equal_i(0, ids[i].length);
|
||||||
|
cl_assert_equal_i(0, ids[i].type);
|
||||||
if (types)
|
|
||||||
cl_assert_equal_i(0, types[i]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_odb_mixed__expand_ids(void)
|
void test_odb_mixed__expand_ids(void)
|
||||||
{
|
{
|
||||||
git_oid *ids;
|
git_odb_expand_id *ids;
|
||||||
size_t i, num, *lengths;
|
size_t i, num;
|
||||||
git_otype *types;
|
|
||||||
|
|
||||||
/* test looking for the actual (correct) types */
|
/* test looking for the actual (correct) types */
|
||||||
|
|
||||||
setup_prefix_query(&ids, &lengths, &types, &num);
|
setup_prefix_query(&ids, &num);
|
||||||
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
|
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
|
||||||
assert_found_objects(ids, lengths, types);
|
assert_found_objects(ids);
|
||||||
git__free(ids); git__free(lengths); git__free(types);
|
git__free(ids);
|
||||||
|
|
||||||
/* test looking for no specified types (types array == NULL) */
|
/* test looking for an explicit `type == 0` */
|
||||||
|
|
||||||
setup_prefix_query(&ids, &lengths, &types, &num);
|
setup_prefix_query(&ids, &num);
|
||||||
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, NULL, num));
|
|
||||||
assert_found_objects(ids, lengths, NULL);
|
for (i = 0; i < num; i++)
|
||||||
git__free(ids); git__free(lengths); git__free(types);
|
ids[i].type = 0;
|
||||||
|
|
||||||
|
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
|
||||||
|
assert_found_objects(ids);
|
||||||
|
git__free(ids);
|
||||||
|
|
||||||
/* test looking for an explicit GIT_OBJ_ANY */
|
/* test looking for an explicit GIT_OBJ_ANY */
|
||||||
|
|
||||||
setup_prefix_query(&ids, &lengths, &types, &num);
|
setup_prefix_query(&ids, &num);
|
||||||
|
|
||||||
for (i = 0; i < num; i++)
|
for (i = 0; i < num; i++)
|
||||||
types[i] = GIT_OBJ_ANY;
|
ids[i].type = GIT_OBJ_ANY;
|
||||||
|
|
||||||
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
|
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
|
||||||
assert_found_objects(ids, lengths, types);
|
assert_found_objects(ids);
|
||||||
git__free(ids); git__free(lengths); git__free(types);
|
git__free(ids);
|
||||||
|
|
||||||
/* test looking for the completely wrong type */
|
/* test looking for the completely wrong type */
|
||||||
|
|
||||||
setup_prefix_query(&ids, &lengths, &types, &num);
|
setup_prefix_query(&ids, &num);
|
||||||
|
|
||||||
for (i = 0; i < num; i++)
|
for (i = 0; i < num; i++)
|
||||||
types[i] = (types[i] == GIT_OBJ_BLOB) ? GIT_OBJ_TREE : GIT_OBJ_BLOB;
|
ids[i].type = (ids[i].type == GIT_OBJ_BLOB) ?
|
||||||
|
GIT_OBJ_TREE : GIT_OBJ_BLOB;
|
||||||
|
|
||||||
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
|
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
|
||||||
assert_notfound_objects(ids, lengths, types);
|
assert_notfound_objects(ids);
|
||||||
git__free(ids); git__free(lengths); git__free(types);
|
git__free(ids);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user