Commit 036daa59e94bbe1a14ccd8fa018618393a0c62fb

Patrick Steinhardt 2017-01-25T14:11:42

khash: use `git_map_exists` where applicable

diff --git a/src/indexer.c b/src/indexer.c
index 8b5fa6e..b5d5fd0 100644
--- a/src/indexer.c
+++ b/src/indexer.c
@@ -333,9 +333,7 @@ on_error:
 
 GIT_INLINE(bool) has_entry(git_indexer *idx, git_oid *id)
 {
-	khiter_t k;
-	k = kh_get(oid, idx->pack->idx_cache, id);
-	return (k != kh_end(idx->pack->idx_cache));
+	return git_oidmap_exists(idx->pack->idx_cache, id);
 }
 
 static int save_entry(git_indexer *idx, struct entry *entry, struct git_pack_entry *pentry, git_off_t entry_start)
diff --git a/src/odb_mempack.c b/src/odb_mempack.c
index b9bdf0b..6aa9ad8 100644
--- a/src/odb_mempack.c
+++ b/src/odb_mempack.c
@@ -72,13 +72,8 @@ static int impl__write(git_odb_backend *_backend, const git_oid *oid, const void
 static int impl__exists(git_odb_backend *backend, const git_oid *oid)
 {
 	struct memory_packer_db *db = (struct memory_packer_db *)backend;
-	khiter_t pos;
-
-	pos = kh_get(oid, db->objects, oid);
-	if (pos != kh_end(db->objects))
-		return 1;
 
-	return 0;
+	return git_oidmap_exists(db->objects, oid);
 }
 
 static int impl__read(void **buffer_p, size_t *len_p, git_otype *type_p, git_odb_backend *backend, const git_oid *oid)
diff --git a/src/oidmap.h b/src/oidmap.h
index 2cf208f..5364862 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -36,6 +36,8 @@ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
 #define git_oidmap_lookup_index(h, k) kh_get(oid, h, k)
 #define git_oidmap_valid_index(h, idx) (idx != kh_end(h))
 
+#define git_oidmap_exists(h, k) (kh_get(oid, h, k) != kh_end(h))
+
 #define git_oidmap_value_at(h, idx) kh_val(h, idx)
 
 #define git_oidmap_insert(h, key, val, rval) do { \
diff --git a/src/pack-objects.c b/src/pack-objects.c
index 2e5de98..2038167 100644
--- a/src/pack-objects.c
+++ b/src/pack-objects.c
@@ -216,8 +216,7 @@ int git_packbuilder_insert(git_packbuilder *pb, const git_oid *oid,
 
 	/* If the object already exists in the hash table, then we don't
 	 * have any work to do */
-	pos = kh_get(oid, pb->object_ix, oid);
-	if (pos != kh_end(pb->object_ix))
+	if (git_oidmap_exists(pb->object_ix, oid))
 		return 0;
 
 	if (pb->nr_objects >= pb->nr_alloc) {
diff --git a/src/pack.c b/src/pack.c
index 0f2fa5c..8a20434 100644
--- a/src/pack.c
+++ b/src/pack.c
@@ -164,7 +164,7 @@ static int cache_add(
 			return -1;
 		}
 		/* Add it to the cache if nobody else has */
-		exists = kh_get(off, cache->entries, offset) != kh_end(cache->entries);
+		exists = git_offmap_exists(cache->entries, offset);
 		if (!exists) {
 			while (cache->memory_used + base->len > cache->memory_limit)
 				free_lowest_entry(cache);