Commit d0323a5f63219428aada1c36dfa8a712a695d778

Vicent Marti 2011-06-01T21:25:56

short-oid: Cleanup

diff --git a/include/git2/object.h b/include/git2/object.h
index cadb942..83d37a2 100644
--- a/include/git2/object.h
+++ b/include/git2/object.h
@@ -56,7 +56,11 @@ GIT_BEGIN_DECL
  * @param type the type of the object
  * @return a reference to the object
  */
-GIT_EXTERN(int) git_object_lookup(git_object **object, git_repository *repo, const git_oid *id, git_otype type);
+GIT_EXTERN(int) git_object_lookup(
+		git_object **object,
+		git_repository *repo,
+		const git_oid *id,
+		git_otype type);
 
 /**
  * Lookup a reference to one of the objects in a repostory,
@@ -85,8 +89,12 @@ GIT_EXTERN(int) git_object_lookup(git_object **object, git_repository *repo, con
  * @param type the type of the object
  * @return a reference to the object
  */
-GIT_EXTERN(int) git_object_lookup_short_oid(git_object **object_out, git_repository *repo,
-					const git_oid *id, unsigned int len, git_otype type);
+GIT_EXTERN(int) git_object_lookup_prefix(
+		git_object **object_out,
+		git_repository *repo,
+		const git_oid *id,
+		unsigned int len,
+		git_otype type);
 
 /**
  * Get the id (SHA1) of a repository object
diff --git a/include/git2/odb.h b/include/git2/odb.h
index 6c08ade..4296146 100644
--- a/include/git2/odb.h
+++ b/include/git2/odb.h
@@ -154,7 +154,7 @@ GIT_EXTERN(int) git_odb_read(git_odb_object **out, git_odb *db, const git_oid *i
  * - GIT_ENOTFOUND if the object is not in the database.
  * - GIT_EAMBIGUOUS if the prefix is ambiguous (several objects match the prefix)
  */
-GIT_EXTERN(int) git_odb_read_unique_short_oid(git_oid *out_oid, git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len);
+GIT_EXTERN(int) git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len);
 
 /**
  * Read the header of an object from the database, without
diff --git a/include/git2/odb_backend.h b/include/git2/odb_backend.h
index 44049b6..796d2b9 100644
--- a/include/git2/odb_backend.h
+++ b/include/git2/odb_backend.h
@@ -55,12 +55,12 @@ struct git_odb_backend {
 	 * remaining (GIT_OID_HEXSZ - len)*4 bits
 	 * are 0s.
 	 */
-	int (* read_unique_short_oid)(
+	int (* read_prefix)(
 			git_oid *,
 			void **, size_t *, git_otype *,
 			struct git_odb_backend *,
 			const git_oid *,
-			unsigned int len);
+			unsigned int);
 
 	int (* read_header)(
 			size_t *, git_otype *,
diff --git a/src/backends/hiredis.c b/src/backends/hiredis.c
index 739e3bb..111abf7 100644
--- a/src/backends/hiredis.c
+++ b/src/backends/hiredis.c
@@ -107,7 +107,7 @@ int hiredis_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_o
     return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to read backend");
 }
 
-int hiredis_backend__read_unique_short_oid(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
+int hiredis_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
 					const git_oid *short_oid, unsigned int len) {
 	if (len >= GIT_OID_HEXSZ) {
 		/* Just match the full identifier */
@@ -189,7 +189,7 @@ int git_odb_backend_hiredis(git_odb_backend **backend_out, const char *host, int
         goto cleanup;
 
     backend->parent.read = &hiredis_backend__read;
-    backend->parent.read_unique_short_oid = &hiredis_backend__read_unique_short_oid;
+    backend->parent.read_prefix = &hiredis_backend__read_prefix;
     backend->parent.read_header = &hiredis_backend__read_header;
     backend->parent.write = &hiredis_backend__write;
     backend->parent.exists = &hiredis_backend__exists;
diff --git a/src/backends/sqlite.c b/src/backends/sqlite.c
index c9c3b80..8626349 100644
--- a/src/backends/sqlite.c
+++ b/src/backends/sqlite.c
@@ -103,7 +103,7 @@ int sqlite_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_od
 	return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "SQLite backend: Failed to read");
 }
 
-int sqlite_backend__read_unique_short_oid(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
+int sqlite_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
 					const git_oid *short_oid, unsigned int len) {
 	if (len >= GIT_OID_HEXSZ) {
 		/* Just match the full identifier */
@@ -270,7 +270,7 @@ int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
 		goto cleanup;
 
 	backend->parent.read = &sqlite_backend__read;
-	backend->parent.read_unique_short_oid = &sqlite_backend__read_unique_short_oid;
+	backend->parent.read_prefix = &sqlite_backend__read_prefix;
 	backend->parent.read_header = &sqlite_backend__read_header;
 	backend->parent.write = &sqlite_backend__write;
 	backend->parent.exists = &sqlite_backend__exists;
diff --git a/src/object.c b/src/object.c
index db8d206..d14ca85 100644
--- a/src/object.c
+++ b/src/object.c
@@ -95,20 +95,20 @@ static int create_object(git_object **object_out, git_otype type)
 	return GIT_SUCCESS;
 }
 
-int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, const git_oid *id, unsigned int len, git_otype type)
+int git_object_lookup_prefix(git_object **object_out, git_repository *repo, const git_oid *id, unsigned int len, git_otype type)
 {
 	git_object *object = NULL;
 	git_odb_object *odb_obj;
 	int error = GIT_SUCCESS;
-	git_oid out_oid;
 
 	assert(repo && object_out && id);
 
 	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) {
+		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;
-	}
 
 	if (len == GIT_OID_HEXSZ)  {
 		/* We want to match the full id : we can first look up in the cache,
@@ -129,7 +129,6 @@ int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, c
 		 * but it may be much more costly for sqlite and hiredis.
 		 */
 		error = git_odb_read(&odb_obj, repo->db, id);
-		git_oid_cpy(&out_oid, id);
 	} else {
 		git_oid short_oid;
 
@@ -149,7 +148,7 @@ int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, c
 		 * - We never explore the cache, go right to exploring the backends
 		 * We chose the latter : we explore directly the backends.
 		 */
-		error = git_odb_read_unique_short_oid(&out_oid, &odb_obj, repo->db, &short_oid, len);
+		error = git_odb_read_prefix(&odb_obj, repo->db, &short_oid, len);
 	}
 
 	if (error < GIT_SUCCESS)
@@ -166,7 +165,7 @@ int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, c
 		return git__rethrow(error, "Failed to lookup object");
 
 	/* Initialize parent object */
-	git_oid_cpy(&object->cached.oid, &out_oid);
+	git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
 	object->repo = repo;
 
 	switch (type) {
@@ -202,7 +201,7 @@ int git_object_lookup_short_oid(git_object **object_out, git_repository *repo, c
 }
 
 int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type) {
-	return git_object_lookup_short_oid(object_out, repo, id, GIT_OID_HEXSZ, type);
+	return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type);
 }
 
 void git_object__free(void *_obj)
diff --git a/src/odb.c b/src/odb.c
index f98f9f9..080dfa2 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -488,26 +488,26 @@ int git_odb_read(git_odb_object **out, git_odb *db, const git_oid *id)
 	return error;
 }
 
-int git_odb_read_unique_short_oid(git_oid *out_oid, git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len)
+int git_odb_read_prefix(git_odb_object **out, git_odb *db, const git_oid *short_id, unsigned int len)
 {
 	unsigned int i;
 	int error = GIT_ENOTFOUND;
+	git_oid full_oid;
 	git_rawobj raw;
 	int found = 0;
 
-	assert(out && db && id);
+	assert(out && db);
 
 	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;
 
 	if (len == GIT_OID_HEXSZ) {
 		*out = git_cache_get(&db->cache, short_id);
-		if (*out != NULL) {
-			git_oid_cpy(out_oid, short_id);
+		if (*out != NULL)
 			return GIT_SUCCESS;
-		}
 	}
 
 	for (i = 0; i < db->backends.length && found < 2; ++i) {
@@ -515,7 +515,7 @@ int git_odb_read_unique_short_oid(git_oid *out_oid, git_odb_object **out, git_od
 		git_odb_backend *b = internal->backend;
 
 		if (b->read != NULL) {
-			error = b->read_unique_short_oid(out_oid, &raw.data, &raw.len, &raw.type, b, short_id, len);
+			error = b->read_prefix(&full_oid, &raw.data, &raw.len, &raw.type, b, short_id, len);
 			switch (error) {
 			case GIT_SUCCESS:
 				found++;
@@ -531,7 +531,7 @@ int git_odb_read_unique_short_oid(git_oid *out_oid, git_odb_object **out, git_od
 	}
 
 	if (found == 1) {
-		*out = git_cache_try_store(&db->cache, new_odb_object(out_oid, &raw));
+		*out = git_cache_try_store(&db->cache, new_odb_object(&full_oid, &raw));
 	} else if (found > 1) {
 		return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to read object. Ambiguous sha1 prefix");
 	} else {
diff --git a/src/odb_loose.c b/src/odb_loose.c
index 603a43f..deff59a 100644
--- a/src/odb_loose.c
+++ b/src/odb_loose.c
@@ -29,6 +29,7 @@
 #include "fileops.h"
 #include "hash.h"
 #include "odb.h"
+#include "oid.h"
 #include "delta-apply.h"
 #include "filebuf.h"
 
@@ -491,7 +492,7 @@ int fn_locate_object_short_oid(void *state, char *pathbuf) {
 
 	if (!gitfo_exists(pathbuf) && gitfo_isdir(pathbuf)) {
 		/* We are already in the directory matching the 2 first hex characters */
-		if (!git_oid_match_hex(sstate->short_oid_len-2, sstate->short_oid+2, pathbuf+sstate->dir_len)) {
+		if (!git_oid_match_hex(sstate->short_oid_len-2, sstate->short_oid+2, (unsigned char *)pathbuf + sstate->dir_len)) {
 			if (!sstate->found) {
 				sstate->res_oid[0] = sstate->short_oid[0];
 				sstate->res_oid[1] = sstate->short_oid[1];
@@ -500,11 +501,10 @@ int fn_locate_object_short_oid(void *state, char *pathbuf) {
 			sstate->found++;
 		}
 	}
-	if (sstate->found > 1) {
+	if (sstate->found > 1)
 		return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Ambiguous sha1 prefix within loose objects");
-	} else {
-		return GIT_SUCCESS;
-	}
+
+	return GIT_SUCCESS;
 }
 
 /* Locate an object matching a given short oid */
@@ -525,7 +525,7 @@ static int locate_object_short_oid(char *object_location, git_oid *res_oid, loos
 		object_location[dir_len++] = '/';
 
 	/* Convert raw oid to hex formatted oid */
-	git_oid_fmt(state.short_oid, short_oid);
+	git_oid_fmt((char *)state.short_oid, short_oid);
 	/* Explore OBJ_DIR/xx/ where xx is the beginning of hex formatted short oid */
 	sprintf(object_location+dir_len, "%.2s/", state.short_oid);
 
@@ -546,7 +546,7 @@ static int locate_object_short_oid(char *object_location, git_oid *res_oid, loos
 	}
 
 	/* Convert obtained hex formatted oid to raw */
-	error = git_oid_mkstr(res_oid, state.res_oid);
+	error = git_oid_mkstr(res_oid, (char *)state.res_oid);
 	if (error) {
 		return git__rethrow(error, "Failed to locate object from short oid");
 	}
@@ -616,8 +616,14 @@ int loose_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_o
 	return GIT_SUCCESS;
 }
 
-int loose_backend__read_unique_short_oid(git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend,
-					const git_oid *short_oid, unsigned int len)
+int loose_backend__read_prefix(
+	git_oid *out_oid,
+	void **buffer_p,
+	size_t *len_p,
+	git_otype *type_p,
+	git_odb_backend *backend,
+	const git_oid *short_oid,
+	unsigned int len)
 {
 	if (len < GIT_OID_MINPREFIXLEN)
 		return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to read loose backend. Prefix length is lower than %d.", GIT_OID_MINPREFIXLEN);
@@ -790,7 +796,7 @@ int git_odb_backend_loose(git_odb_backend **backend_out, const char *objects_dir
 	backend->fsync_object_files = 0;
 
 	backend->parent.read = &loose_backend__read;
-	backend->parent.read_unique_short_oid = &loose_backend__read_unique_short_oid;
+	backend->parent.read_prefix = &loose_backend__read_prefix;
 	backend->parent.read_header = &loose_backend__read_header;
 	backend->parent.writestream = &loose_backend__stream;
 	backend->parent.exists = &loose_backend__exists;
diff --git a/src/odb_pack.c b/src/odb_pack.c
index dbb772a..3b7352d 100644
--- a/src/odb_pack.c
+++ b/src/odb_pack.c
@@ -294,7 +294,7 @@ static int pack_entry_find(struct pack_entry *e,
  * 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,
+static int pack_entry_find_prefix(struct pack_entry *e,
 					struct pack_backend *backend,
 					const git_oid *short_oid,
 					unsigned int len);
@@ -1006,9 +1006,9 @@ static int pack_entry_find_offset(
 		current = index + pos * stride;
 	} else {
 		/* No object was found */
-		pos = - 1 - pos;
 		/* pos refers to the object with the "closest" oid to short_oid */
-		if (pos < p->num_objects) {
+		pos = - 1 - pos;
+		if (pos < (int)p->num_objects) {
 			current = index + pos * stride;
 
 			if (!git_oid_match_raw(len, short_oid->id, current)) {
@@ -1016,7 +1016,8 @@ static int pack_entry_find_offset(
 			}
 		}
 	}
-	if (found && pos + 1 < p->num_objects) {
+
+	if (found && pos + 1 < (int)p->num_objects) {
 		/* Check for ambiguousity */
 		const unsigned char *next = current + stride;
 
@@ -1106,8 +1107,11 @@ static int pack_entry_find(struct pack_entry *e, struct pack_backend *backend, c
 	return git__throw(GIT_ENOTFOUND, "Failed to find pack entry");
 }
 
-static int pack_entry_find_unique_short_oid(struct pack_entry *e, struct pack_backend *backend,
-					const git_oid *short_oid, unsigned int len)
+static int pack_entry_find_prefix(
+	struct pack_entry *e,
+	struct pack_backend *backend,
+	const git_oid *short_oid,
+	unsigned int len)
 {
 	int error;
 	size_t i;
@@ -1137,7 +1141,7 @@ static int pack_entry_find_unique_short_oid(struct pack_entry *e, struct pack_ba
 			return git__rethrow(error, "Failed to find pack entry. Ambiguous sha1 prefix");
 		} else if (error == GIT_SUCCESS) {
 			found++;
-			if (found > 1);
+			if (found > 1)
 				break;
 			backend->last_found = p;
 		}
@@ -1462,8 +1466,14 @@ int pack_backend__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_od
 	return GIT_SUCCESS;
 }
 
-int pack_backend__read_unique_short_oid(git_oid *out_oid, void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend,
-					const git_oid *short_oid, unsigned int len)
+int pack_backend__read_prefix(
+	git_oid *out_oid,
+	void **buffer_p,
+	size_t *len_p,
+	git_otype *type_p,
+	git_odb_backend *backend,
+	const git_oid *short_oid,
+	unsigned int len)
 {
 	if (len < GIT_OID_MINPREFIXLEN)
 		return git__throw(GIT_EAMBIGUOUSOIDPREFIX, "Failed to read pack backend. Prefix length is lower than %d.", GIT_OID_MINPREFIXLEN);
@@ -1480,7 +1490,7 @@ int pack_backend__read_unique_short_oid(git_oid *out_oid, void **buffer_p, size_
 		git_rawobj raw;
 		int error;
 
-		if ((error = pack_entry_find_unique_short_oid(&e, (struct pack_backend *)backend, short_oid, len)) < GIT_SUCCESS)
+		if ((error = pack_entry_find_prefix(&e, (struct pack_backend *)backend, short_oid, len)) < GIT_SUCCESS)
 			return git__rethrow(error, "Failed to read pack backend");
 
 		if ((error = packfile_unpack(&raw, (struct pack_backend *)backend, e.p, e.offset)) < GIT_SUCCESS)
@@ -1549,7 +1559,7 @@ int git_odb_backend_pack(git_odb_backend **backend_out, const char *objects_dir)
 	}
 
 	backend->parent.read = &pack_backend__read;
-	backend->parent.read_unique_short_oid = &pack_backend__read_unique_short_oid;
+	backend->parent.read_prefix = &pack_backend__read_prefix;
 	backend->parent.read_header = NULL;
 	backend->parent.exists = &pack_backend__exists;
 	backend->parent.free = &pack_backend__free;