Commit f1d01851 by Vicent Marti

oid: Uniformize ncmp methods

Drop redundant methods. The ncmp method is now public
parent fa48608e
...@@ -55,6 +55,7 @@ typedef struct { ...@@ -55,6 +55,7 @@ typedef struct {
/** /**
* Parse a hex formatted object id into a git_oid. * Parse a hex formatted object id into a git_oid.
*
* @param out oid structure the result is written into. * @param out oid structure the result is written into.
* @param str input hex string; must be pointing at the start of * @param str input hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes * the hex sequence and have at least the number of bytes
...@@ -65,6 +66,7 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str); ...@@ -65,6 +66,7 @@ GIT_EXTERN(int) git_oid_fromstr(git_oid *out, const char *str);
/** /**
* Copy an already raw oid into a git_oid structure. * Copy an already raw oid into a git_oid structure.
*
* @param out oid structure the result is written into. * @param out oid structure the result is written into.
* @param raw the raw input bytes to be copied. * @param raw the raw input bytes to be copied.
*/ */
...@@ -72,6 +74,7 @@ GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw); ...@@ -72,6 +74,7 @@ GIT_EXTERN(void) git_oid_fromraw(git_oid *out, const unsigned char *raw);
/** /**
* Format a git_oid into a hex string. * Format a git_oid into a hex string.
*
* @param str output hex string; must be pointing at the start of * @param str output hex string; must be pointing at the start of
* the hex sequence and have at least the number of bytes * the hex sequence and have at least the number of bytes
* needed for an oid encoded in hex (40 bytes). Only the * needed for an oid encoded in hex (40 bytes). Only the
...@@ -83,7 +86,7 @@ GIT_EXTERN(void) git_oid_fmt(char *str, const git_oid *oid); ...@@ -83,7 +86,7 @@ GIT_EXTERN(void) git_oid_fmt(char *str, const git_oid *oid);
/** /**
* Format a git_oid into a loose-object path string. * Format a git_oid into a loose-object path string.
* <p> *
* The resulting string is "aa/...", where "aa" is the first two * The resulting string is "aa/...", where "aa" is the first two
* hex digitis of the oid and "..." is the remaining 38 digits. * hex digitis of the oid and "..." is the remaining 38 digits.
* *
...@@ -98,6 +101,7 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid); ...@@ -98,6 +101,7 @@ GIT_EXTERN(void) git_oid_pathfmt(char *str, const git_oid *oid);
/** /**
* Format a git_oid into a newly allocated c-string. * Format a git_oid into a newly allocated c-string.
*
* @param oid the oid structure to format * @param oid the oid structure to format
* @return the c-string; NULL if memory is exhausted. Caller must * @return the c-string; NULL if memory is exhausted. Caller must
* deallocate the string with free(). * deallocate the string with free().
...@@ -106,7 +110,7 @@ GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid); ...@@ -106,7 +110,7 @@ GIT_EXTERN(char *) git_oid_allocfmt(const git_oid *oid);
/** /**
* Format a git_oid into a buffer as a hex format c-string. * Format a git_oid into a buffer as a hex format c-string.
* <p> *
* If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting * If the buffer is smaller than GIT_OID_HEXSZ+1, then the resulting
* oid c-string will be truncated to n-1 characters. If there are * oid c-string will be truncated to n-1 characters. If there are
* any input parameter errors (out == NULL, n == 0, oid == NULL), * any input parameter errors (out == NULL, n == 0, oid == NULL),
...@@ -123,6 +127,7 @@ GIT_EXTERN(char *) git_oid_to_string(char *out, size_t n, const git_oid *oid); ...@@ -123,6 +127,7 @@ GIT_EXTERN(char *) git_oid_to_string(char *out, size_t n, const git_oid *oid);
/** /**
* Copy an oid from one structure to another. * Copy an oid from one structure to another.
*
* @param out oid structure the result is written into. * @param out oid structure the result is written into.
* @param src oid structure to copy from. * @param src oid structure to copy from.
*/ */
...@@ -130,6 +135,7 @@ GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src); ...@@ -130,6 +135,7 @@ GIT_EXTERN(void) git_oid_cpy(git_oid *out, const git_oid *src);
/** /**
* Compare two oid structures. * Compare two oid structures.
*
* @param a first oid structure. * @param a first oid structure.
* @param b second oid structure. * @param b second oid structure.
* @return <0, 0, >0 if a < b, a == b, a > b. * @return <0, 0, >0 if a < b, a == b, a > b.
...@@ -139,12 +145,13 @@ GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b); ...@@ -139,12 +145,13 @@ GIT_EXTERN(int) git_oid_cmp(const git_oid *a, const git_oid *b);
/** /**
* Compare the first 'len' hexadecimal characters (packets of 4 bits) * Compare the first 'len' hexadecimal characters (packets of 4 bits)
* of two oid structures. * of two oid structures.
* @param len the number of hex chars to compare *
* @param a first oid structure. * @param a first oid structure.
* @param b second oid structure. * @param b second oid structure.
* @param len the number of hex chars to compare
* @return 0 in case of a match * @return 0 in case of a match
*/ */
GIT_EXTERN(int) git_oid_ncmp(unsigned int len, git_oid *a, git_oid *b); GIT_EXTERN(int) git_oid_ncmp(const git_oid *a, const git_oid *b, unsigned int len);
/** /**
* OID Shortener object * OID Shortener object
......
...@@ -26,10 +26,10 @@ ...@@ -26,10 +26,10 @@
#include "common.h" #include "common.h"
#include "git2/zlib.h" #include "git2/zlib.h"
#include "git2/object.h" #include "git2/object.h"
#include "git2/oid.h"
#include "fileops.h" #include "fileops.h"
#include "hash.h" #include "hash.h"
#include "odb.h" #include "odb.h"
#include "oid.h"
#include "delta-apply.h" #include "delta-apply.h"
#include "filebuf.h" #include "filebuf.h"
...@@ -491,8 +491,12 @@ int fn_locate_object_short_oid(void *state, char *pathbuf) { ...@@ -491,8 +491,12 @@ int fn_locate_object_short_oid(void *state, char *pathbuf) {
} }
if (!gitfo_exists(pathbuf) && gitfo_isdir(pathbuf)) { if (!gitfo_exists(pathbuf) && gitfo_isdir(pathbuf)) {
/* We are already in the directory matching the 2 first hex characters */ /* We are already in the directory matching the 2 first hex characters,
if (!git_oid_ncmp_hex(sstate->short_oid_len-2, sstate->short_oid+2, (unsigned char *)pathbuf + sstate->dir_len)) { * compare the first ncmp characters of the oids */
if (!memcmp(sstate->short_oid + 2,
(unsigned char *)pathbuf + sstate->dir_len,
sstate->short_oid_len - 2)) {
if (!sstate->found) { if (!sstate->found) {
sstate->res_oid[0] = sstate->short_oid[0]; sstate->res_oid[0] = sstate->short_oid[0];
sstate->res_oid[1] = sstate->short_oid[1]; sstate->res_oid[1] = sstate->short_oid[1];
......
...@@ -26,10 +26,10 @@ ...@@ -26,10 +26,10 @@
#include "common.h" #include "common.h"
#include "git2/zlib.h" #include "git2/zlib.h"
#include "git2/repository.h" #include "git2/repository.h"
#include "git2/oid.h"
#include "fileops.h" #include "fileops.h"
#include "hash.h" #include "hash.h"
#include "odb.h" #include "odb.h"
#include "oid.h"
#include "delta-apply.h" #include "delta-apply.h"
#include "sha1_lookup.h" #include "sha1_lookup.h"
...@@ -1011,7 +1011,7 @@ static int pack_entry_find_offset( ...@@ -1011,7 +1011,7 @@ static int pack_entry_find_offset(
if (pos < (int)p->num_objects) { if (pos < (int)p->num_objects) {
current = index + pos * stride; current = index + pos * stride;
if (!git_oid_ncmp_raw(len, short_oid->id, current)) { if (!git_oid_ncmp(short_oid, (const git_oid *)current, len)) {
found = 1; found = 1;
} }
} }
...@@ -1021,7 +1021,7 @@ static int pack_entry_find_offset( ...@@ -1021,7 +1021,7 @@ static int pack_entry_find_offset(
/* Check for ambiguousity */ /* Check for ambiguousity */
const unsigned char *next = current + stride; const unsigned char *next = current + stride;
if (!git_oid_ncmp_raw(len, short_oid->id, next)) { if (!git_oid_ncmp(short_oid, (const git_oid *)next, len)) {
found = 2; found = 2;
} }
} }
......
...@@ -172,9 +172,11 @@ int git_oid_cmp(const git_oid *a, const git_oid *b) ...@@ -172,9 +172,11 @@ int git_oid_cmp(const git_oid *a, const git_oid *b)
return memcmp(a->id, b->id, sizeof(a->id)); return memcmp(a->id, b->id, sizeof(a->id));
} }
int git_oid_ncmp(const git_oid *oid_a, const git_oid *oid_b, unsigned int len)
int git_oid_ncmp_raw(unsigned int len, const unsigned char *a, const unsigned char *b)
{ {
const unsigned char *a = oid_a->id;
const unsigned char *b = oid_b->id;
do { do {
if (*a != *b) if (*a != *b)
return 1; return 1;
...@@ -182,20 +184,12 @@ int git_oid_ncmp_raw(unsigned int len, const unsigned char *a, const unsigned ch ...@@ -182,20 +184,12 @@ int git_oid_ncmp_raw(unsigned int len, const unsigned char *a, const unsigned ch
b++; b++;
len -= 2; len -= 2;
} while (len > 1); } while (len > 1);
if (len) if (len)
if ((*a ^ *b) & 0xf0) if ((*a ^ *b) & 0xf0)
return 1; return 1;
return 0;
}
int git_oid_ncmp_hex(unsigned int len, const unsigned char *a, const unsigned char *b) return 0;
{
return memcmp(a, b, len);
}
int git_oid_ncmp(unsigned int len, git_oid *a, git_oid *b)
{
return git_oid_ncmp_raw(len, a->id, b->id);
} }
typedef short node_index; typedef short node_index;
......
#ifndef INCLUDE_oid_h__
#define INCLUDE_oid_h__
/**
* Compare the first ('len'*4) bits of two raw formatted oids.
* This can be useful for internal use.
* Return 0 if they match.
*/
int git_oid_ncmp_raw(unsigned int len, const unsigned char *a, const unsigned char *b);
/**
* Compare the first 'len' characters of two hex formatted oids.
* Return 0 if they match.
*/
int git_oid_ncmp_hex(unsigned int len, const unsigned char *a, const unsigned char *b);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment