Skip to content

Commit

Permalink
oid: introduce git_oid_raw_ncmp
Browse files Browse the repository at this point in the history
  • Loading branch information
ethomson committed Apr 10, 2022
1 parent 526e886 commit 6d8c7ca
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
20 changes: 1 addition & 19 deletions src/libgit2/oid.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,25 +203,7 @@ int git_oid_equal(const git_oid *a, const git_oid *b)

int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, size_t len)
{
const unsigned char *a = oid_a->id;
const unsigned char *b = oid_b->id;

if (len > GIT_OID_HEXSZ)
len = GIT_OID_HEXSZ;

while (len > 1) {
if (*a != *b)
return 1;
a++;
b++;
len -= 2;
};

if (len)
if ((*a ^ *b) & 0xf0)
return 1;

return 0;
return git_oid_raw_ncmp(oid_a->id, oid_b->id, len);
}

int git_oid_strcmp(const git_oid *oid_a, const char *str)
Expand Down
23 changes: 23 additions & 0 deletions src/libgit2/oid.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,29 @@ extern const git_oid git_oid__empty_tree_sha1;
*/
char *git_oid_allocfmt(const git_oid *id);

GIT_INLINE(int) git_oid_raw_ncmp(
const unsigned char *sha1,
const unsigned char *sha2,
size_t len)
{
if (len > GIT_OID_HEXSZ)
len = GIT_OID_HEXSZ;

while (len > 1) {
if (*sha1 != *sha2)
return 1;
sha1++;
sha2++;
len -= 2;
};

if (len)
if ((*sha1 ^ *sha2) & 0xf0)
return 1;

return 0;
}

GIT_INLINE(int) git_oid_raw_cmp(
const unsigned char *sha1,
const unsigned char *sha2)
Expand Down

0 comments on commit 6d8c7ca

Please sign in to comment.