Added a GIT_OID_MINPREFIXLEN constant to define the minimum length allowed for oid prefixes (set to 4, like in git). Consequently updated some object lookup methods and their documentation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
diff --git a/include/git2/object.h b/include/git2/object.h
index 4cb6af0..cadb942 100644
--- a/include/git2/object.h
+++ b/include/git2/object.h
@@ -65,9 +65,9 @@ GIT_EXTERN(int) git_object_lookup(git_object **object, git_repository *repo, con
* The object obtained will be so that its identifier
* matches the first 'len' hexadecimal characters
* (packets of 4 bits) of the given 'id'.
- * 'len' must be long enough to identify a unique
- * object matching the prefix; otherwise the method will
- * fail.
+ * 'len' must be at least GIT_OID_MINPREFIXLEN, and
+ * long enough to identify a unique object matching
+ * the prefix; otherwise the method will fail.
*
* The generated reference is owned by the repository and
* should be closed with the `git_object_close` method
diff --git a/include/git2/odb.h b/include/git2/odb.h
index 483934a..6c08ade 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -132,9 +132,10 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i
* This method queries all available ODB backends
* trying to match the 'len' first hexadecimal
* characters of the 'short_id'.
- * The remaining bits (GIT_OID_HEXSZ-len)*4 bits of
+ * The remaining (GIT_OID_HEXSZ-len)*4 bits of
* 'short_id' must be 0s.
- * The prefix must be long enough to identify
+ * 'len' must be at least GIT_OID_MINPREFIXLEN,
+ * and the prefix must be long enough to identify
* a unique object in all the backends; the
* method will fail otherwise.
*
diff --git a/include/git2/oid.h b/include/git2/oid.h
index e8803ce..062b9ca 100644
--- a/include/git2/oid.h
+++ b/include/git2/oid.h
@@ -43,6 +43,10 @@ GIT_BEGIN_DECL
/** Size (in bytes) of a hex formatted oid */
#define GIT_OID_HEXSZ (GIT_OID_RAWSZ * 2)
+/** Minimum length (in number of hex characters,
+ * i.e. packets of 4 bits) of an oid prefix */
+#define GIT_OID_MINPREFIXLEN 4
+
/** Unique identity of any object (commit, tree, blob, tag). */
typedef struct {
/** raw binary formatted id */
diff --git a/src/object.c b/src/object.c
index e6e9767..db8d206 100644
--- a/src/object.c
+++ b/src/object.c
@@ -104,8 +104,8 @@ int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, c
assert(repo && object_out && id);
- if (len == 0)
- return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to lookup object. Prefix length should be not be 0.");
+ if (len < GIT_OID_MINPREFIXLEN)
+ return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to lookup object. Prefix length is lower than %d.", GIT_OID_MINPREFIXLEN);
if (len > GIT_OID_HEXSZ) {
len = GIT_OID_HEXSZ;
}
diff --git a/src/odb_pack.c b/src/odb_pack.c
index 6056083..070bde6 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -268,6 +268,8 @@ static off_t nth_packed_object_offset(const struct pack_file *p, uint32_t n);
* a prefix of an identifier.
* Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
* is ambiguous within the pack.
+ * This method assumes that len is between
+ * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
*/
static int pack_entry_find_offset(
off_t *offset_out,
@@ -289,6 +291,8 @@ static int pack_entry_find(struct pack_entry *e,
* a prefix of an identifier.
* Throws GIT_EAMBIGUOUSOIDPREFIX if short oid
* is ambiguous.
+ * This method assumes that len is between
+ * GIT_OID_MINPREFIXLEN and GIT_OID_HEXSZ.
*/
static int pack_entry_find_unique_short_oid(struct pack_entry *e,
struct pack_backend *backend,