Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62484f5

Browse files
author
Edward Thomson
committed
git_odb_expand_ids: accept git_odb_expand_id array
Take (and write to) an array of a struct, `git_odb_expand_id`.
1 parent 4b1f0f7 commit 62484f5

File tree

3 files changed

+89
-92
lines changed

3 files changed

+89
-92
lines changed

include/git2/odb.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,27 @@ GIT_EXTERN(int) git_odb_exists(git_odb *db, const git_oid *id);
173173
GIT_EXTERN(int) git_odb_exists_prefix(
174174
git_oid *out, git_odb *db, const git_oid *short_id, size_t len);
175175

176+
/**
177+
* The information about object IDs to query in `git_odb_expand_ids`,
178+
* which will be populated upon return.
179+
*/
180+
typedef struct git_odb_expand_id {
181+
/** The object ID to expand */
182+
git_oid id;
183+
184+
/**
185+
* The length of the object ID (in nibbles, or packets of 4 bits; the
186+
* number of hex characters)
187+
* */
188+
unsigned short length;
189+
190+
/**
191+
* The (optional) type of the object to search for; leave as `0` or set
192+
* to `GIT_OBJ_ANY` to query for any object matching the ID.
193+
*/
194+
git_otype type;
195+
} git_odb_expand_id;
196+
176197
/**
177198
* Determine if one or more objects can be found in the object database
178199
* by their abbreviated object ID and type. The given array will be
@@ -187,20 +208,14 @@ GIT_EXTERN(int) git_odb_exists_prefix(
187208
* not found (which is unlike other object database operations.)
188209
*
189210
* @param db The database to be searched for the given objects.
190-
* @param ids An array of object IDs to search for
191-
* @param id_lengths The corresponding length of each entry in the `ids`
192-
* array
193-
* @param types The corresponding type of each entry in the `ids` array
194-
* (or null to lookup an object of any type)
195-
* @param cnt The length of the `ids`, `id_lengths` and `types` arrays
211+
* @param ids An array of short object IDs to search for
212+
* @param count The length of the `ids` array
196213
* @return 0 on success or an error code on failure
197214
*/
198215
GIT_EXTERN(int) git_odb_expand_ids(
199216
git_odb *db,
200-
git_oid *ids,
201-
size_t *id_lengths,
202-
git_otype *types,
203-
size_t cnt);
217+
git_odb_expand_id *ids,
218+
size_t count);
204219

205220
/**
206221
* Refresh the object database to load newly added files.

src/odb.c

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -744,64 +744,51 @@ int git_odb_exists_prefix(
744744

745745
int git_odb_expand_ids(
746746
git_odb *db,
747-
git_oid *ids,
748-
size_t *id_lengths,
749-
git_otype *types,
750-
size_t cnt)
747+
git_odb_expand_id *ids,
748+
size_t count)
751749
{
752750
size_t len, i;
753751
int error;
754752

755-
assert(db && ids && id_lengths);
753+
assert(db && ids);
756754

757-
for (i = 0; i < cnt; i++) {
755+
for (i = 0; i < count; i++) {
756+
git_odb_expand_id *query = &ids[i];
758757
git_oid *actual_id = NULL, tmp;
758+
git_otype query_type = (query->type == GIT_OBJ_ANY) ? 0 : query->type;
759759
git_otype actual_type = 0;
760760

761761
/* if we were given a full object ID, simply look it up */
762-
if (id_lengths[i] >= GIT_OID_HEXSZ) {
763-
error = git_odb_read_header(&len, &actual_type, db, &ids[i]);
762+
if (query->length >= GIT_OID_HEXSZ) {
763+
error = git_odb_read_header(&len, &actual_type, db, &query->id);
764764
}
765765

766766
/* otherwise, resolve the short id to full, then (optionally)
767767
* read the header.
768768
*/
769-
else if (id_lengths[i] >= GIT_OID_MINPREFIXLEN) {
769+
else if (query->length >= GIT_OID_MINPREFIXLEN) {
770770
error = odb_exists_prefix_1(&tmp,
771-
db, &ids[i], id_lengths[i], false);
771+
db, &query->id, query->length, false);
772772

773773
if (!error) {
774774
actual_id = &tmp;
775-
776-
if (types)
777-
error = git_odb_read_header(&len, &actual_type, db, &tmp);
778-
else
779-
actual_type = GIT_OBJ_ANY;
775+
error = git_odb_read_header(&len, &actual_type, db, &tmp);
780776
}
781777
}
782778

783779
if (error < 0 && error != GIT_ENOTFOUND && error != GIT_EAMBIGUOUS)
784780
break;
785781

786-
error = 0;
787-
788-
if (types && types[i] != GIT_OBJ_ANY && types[i] != actual_type)
789-
actual_type = 0;
790-
791-
if (!actual_type) {
792-
memset(&ids[i], 0, sizeof(git_oid));
793-
id_lengths[i] = 0;
794-
795-
if (types)
796-
types[i] = 0;
797-
} else {
782+
if (error == 0 && (query_type == actual_type || !query_type)) {
798783
if (actual_id)
799-
git_oid_cpy(&ids[i], actual_id);
784+
git_oid_cpy(&query->id, actual_id);
800785

801-
id_lengths[i] = GIT_OID_HEXSZ;
802-
803-
if (types)
804-
types[i] = actual_type;
786+
query->length = GIT_OID_HEXSZ;
787+
query->type = actual_type;
788+
} else {
789+
memset(&query->id, 0, sizeof(git_oid));
790+
query->length = 0;
791+
query->type = 0;
805792
}
806793
}
807794

tests/odb/mixed.c

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -146,35 +146,31 @@ struct expand_id_test_data expand_id_test_data[] = {
146146
};
147147

148148
static void setup_prefix_query(
149-
git_oid **out_ids,
150-
size_t **out_lengths,
151-
git_otype **out_types,
149+
git_odb_expand_id **out_ids,
152150
size_t *out_num)
153151
{
154-
git_oid *ids;
155-
git_otype *types;
156-
size_t num, *lengths, i;
152+
git_odb_expand_id *ids;
153+
size_t num, i;
157154

158155
num = ARRAY_SIZE(expand_id_test_data);
159156

160-
cl_assert((ids = git__calloc(num, sizeof(git_oid))));
161-
cl_assert((lengths = git__calloc(num, sizeof(size_t))));
162-
cl_assert((types = git__calloc(num, sizeof(git_otype))));
157+
cl_assert((ids = git__calloc(num, sizeof(git_odb_expand_id))));
163158

164159
for (i = 0; i < num; i++) {
165-
lengths[i] = strlen(expand_id_test_data[i].lookup_id);
166-
git_oid_fromstrn(&ids[i], expand_id_test_data[i].lookup_id, lengths[i]);
167-
types[i] = expand_id_test_data[i].expected_type;
160+
git_odb_expand_id *id = &ids[i];
161+
162+
size_t len = strlen(expand_id_test_data[i].lookup_id);
163+
164+
git_oid_fromstrn(&id->id, expand_id_test_data[i].lookup_id, len);
165+
id->length = (unsigned short)len;
166+
id->type = expand_id_test_data[i].expected_type;
168167
}
169168

170169
*out_ids = ids;
171-
*out_lengths = lengths;
172-
*out_types = types;
173170
*out_num = num;
174171
}
175172

176-
static void assert_found_objects(
177-
git_oid *ids, size_t *lengths, git_otype *types)
173+
static void assert_found_objects(git_odb_expand_id *ids)
178174
{
179175
size_t num, i;
180176

@@ -191,71 +187,70 @@ static void assert_found_objects(
191187
expected_type = expand_id_test_data[i].expected_type;
192188
}
193189

194-
cl_assert_equal_i(expected_len, lengths[i]);
195-
cl_assert_equal_oid(&expected_id, &ids[i]);
196-
197-
if (types)
198-
cl_assert_equal_i(expected_type, types[i]);
190+
cl_assert_equal_oid(&expected_id, &ids[i].id);
191+
cl_assert_equal_i(expected_len, ids[i].length);
192+
cl_assert_equal_i(expected_type, ids[i].type);
199193
}
200194
}
201195

202-
static void assert_notfound_objects(
203-
git_oid *ids, size_t *lengths, git_otype *types)
196+
static void assert_notfound_objects(git_odb_expand_id *ids)
204197
{
205198
git_oid expected_id = {{0}};
206199
size_t num, i;
207200

208201
num = ARRAY_SIZE(expand_id_test_data);
209202

210203
for (i = 0; i < num; i++) {
211-
cl_assert_equal_i(0, lengths[i]);
212-
cl_assert_equal_oid(&expected_id, &ids[i]);
213-
214-
if (types)
215-
cl_assert_equal_i(0, types[i]);
204+
cl_assert_equal_oid(&expected_id, &ids[i].id);
205+
cl_assert_equal_i(0, ids[i].length);
206+
cl_assert_equal_i(0, ids[i].type);
216207
}
217208
}
218209

219210
void test_odb_mixed__expand_ids(void)
220211
{
221-
git_oid *ids;
222-
size_t i, num, *lengths;
223-
git_otype *types;
212+
git_odb_expand_id *ids;
213+
size_t i, num;
224214

225215
/* test looking for the actual (correct) types */
226216

227-
setup_prefix_query(&ids, &lengths, &types, &num);
228-
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
229-
assert_found_objects(ids, lengths, types);
230-
git__free(ids); git__free(lengths); git__free(types);
217+
setup_prefix_query(&ids, &num);
218+
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
219+
assert_found_objects(ids);
220+
git__free(ids);
221+
222+
/* test looking for an explicit `type == 0` */
231223

232-
/* test looking for no specified types (types array == NULL) */
224+
setup_prefix_query(&ids, &num);
225+
226+
for (i = 0; i < num; i++)
227+
ids[i].type = 0;
233228

234-
setup_prefix_query(&ids, &lengths, &types, &num);
235-
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, NULL, num));
236-
assert_found_objects(ids, lengths, NULL);
237-
git__free(ids); git__free(lengths); git__free(types);
229+
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
230+
assert_found_objects(ids);
231+
git__free(ids);
238232

239233
/* test looking for an explicit GIT_OBJ_ANY */
240234

241-
setup_prefix_query(&ids, &lengths, &types, &num);
235+
setup_prefix_query(&ids, &num);
242236

243237
for (i = 0; i < num; i++)
244-
types[i] = GIT_OBJ_ANY;
238+
ids[i].type = GIT_OBJ_ANY;
245239

246-
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
247-
assert_found_objects(ids, lengths, types);
248-
git__free(ids); git__free(lengths); git__free(types);
240+
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
241+
assert_found_objects(ids);
242+
git__free(ids);
249243

250244
/* test looking for the completely wrong type */
251245

252-
setup_prefix_query(&ids, &lengths, &types, &num);
246+
setup_prefix_query(&ids, &num);
253247

254248
for (i = 0; i < num; i++)
255-
types[i] = (types[i] == GIT_OBJ_BLOB) ? GIT_OBJ_TREE : GIT_OBJ_BLOB;
249+
ids[i].type = (ids[i].type == GIT_OBJ_BLOB) ?
250+
GIT_OBJ_TREE : GIT_OBJ_BLOB;
256251

257-
cl_git_pass(git_odb_expand_ids(_odb, ids, lengths, types, num));
258-
assert_notfound_objects(ids, lengths, types);
259-
git__free(ids); git__free(lengths); git__free(types);
252+
cl_git_pass(git_odb_expand_ids(_odb, ids, num));
253+
assert_notfound_objects(ids);
254+
git__free(ids);
260255
}
261256

0 commit comments

Comments
 (0)