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

Skip to content

Commit e78d2ac

Browse files
committed
odb: Refactor git_odb_expand_ids
1 parent 4416aa7 commit e78d2ac

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/odb.c

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -782,37 +782,42 @@ int git_odb_expand_ids(
782782

783783
for (i = 0; i < count; i++) {
784784
git_odb_expand_id *query = &ids[i];
785-
git_oid actual_id;
786-
git_otype query_type = (query->type == GIT_OBJ_ANY) ? 0 : query->type;
787-
git_otype actual_type = 0;
788785
int error = GIT_EAMBIGUOUS;
789786

790-
/* if we were given a full object ID, simply look it up */
791-
if (query->length >= GIT_OID_HEXSZ) {
792-
error = odb_otype_fast(&actual_type, db, &query->id);
793-
git_oid_cpy(&actual_id, &query->id);
787+
if (!query->type)
788+
query->type = GIT_OBJ_ANY;
789+
790+
/* if we have a short OID, expand it first */
791+
if (query->length >= GIT_OID_MINPREFIXLEN && query->length < GIT_OID_HEXSZ) {
792+
git_oid actual_id;
793+
794+
error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false);
795+
if (!error) {
796+
git_oid_cpy(&query->id, &actual_id);
797+
query->length = GIT_OID_HEXSZ;
798+
}
794799
}
795800

796801
/*
797-
* otherwise, resolve the short id to full if it's long enough, then
798-
* (optionally) read the header
802+
* now we ought to have a 40-char OID, either because we've expanded it
803+
* or because the user passed a full OID. Ensure its type is right.
799804
*/
800-
else if (query->length >= GIT_OID_MINPREFIXLEN) {
801-
error = odb_exists_prefix_1(&actual_id, db, &query->id, query->length, false);
802-
if (!error)
803-
error = odb_otype_fast(&actual_type, db, &actual_id);
804-
}
805+
if (query->length >= GIT_OID_HEXSZ) {
806+
git_otype actual_type;
805807

806-
/* Ensure that the looked up type matches the type we were expecting */
807-
if (query_type && query_type != actual_type)
808-
error = GIT_ENOTFOUND;
808+
error = odb_otype_fast(&actual_type, db, &query->id);
809+
if (!error) {
810+
if (query->type != GIT_OBJ_ANY && query->type != actual_type)
811+
error = GIT_ENOTFOUND;
812+
else
813+
query->type = actual_type;
814+
}
815+
}
809816

810817
switch (error) {
818+
/* no errors, so we've successfully expanded the OID */
811819
case 0:
812-
git_oid_cpy(&query->id, &actual_id);
813-
query->length = GIT_OID_HEXSZ;
814-
query->type = actual_type;
815-
break;
820+
continue;
816821

817822
/* the object is missing or ambiguous */
818823
case GIT_ENOTFOUND:

0 commit comments

Comments
 (0)