Commit c50a8ac2c734e4301223e39e4ddce98e6c368294

Patrick Steinhardt 2018-12-01T08:59:24

maps: use high-level function to check existence of keys Some callers were still using the tightly-coupled pattern of `lookup_index` and `valid_index` to verify that an entry exists in a map. Instead, use the more high-level `exists` functions to decouple map users from its implementation.

diff --git a/src/apply.c b/src/apply.c
index 4b7eedd..ad8ce2f 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -439,7 +439,6 @@ static int apply_one(
 	git_filemode_t pre_filemode;
 	git_index_entry pre_entry, post_entry;
 	bool skip_preimage = false;
-	size_t pos;
 	int error;
 
 	if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
@@ -464,8 +463,7 @@ static int apply_one(
 	 */
 	if (delta->status != GIT_DELTA_RENAMED &&
 	    delta->status != GIT_DELTA_ADDED) {
-		pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
-		if (git_strmap_valid_index(removed_paths, pos)) {
+		if (git_strmap_exists(removed_paths, delta->old_file.path)) {
 			error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
 			goto done;
 		}
diff --git a/src/offmap.c b/src/offmap.c
index eaf72ba..09f8a2e 100644
--- a/src/offmap.c
+++ b/src/offmap.c
@@ -77,6 +77,11 @@ int git_offmap_delete(git_offmap *map, const git_off_t key)
 	return 0;
 }
 
+int git_offmap_exists(git_offmap *map, const git_off_t key)
+{
+	return kh_get(off, map, key) != kh_end(map);
+}
+
 size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
 {
 	return kh_get(off, map, key);
@@ -87,11 +92,6 @@ int git_offmap_valid_index(git_offmap *map, size_t idx)
 	return idx != kh_end(map);
 }
 
-int git_offmap_exists(git_offmap *map, const git_off_t key)
-{
-	return kh_get(off, map, key) != kh_end(map);
-}
-
 int git_offmap_has_data(git_offmap *map, size_t idx)
 {
 	return kh_exist(map, idx);
diff --git a/src/offmap.h b/src/offmap.h
index e7187d3..f2a8cad 100644
--- a/src/offmap.h
+++ b/src/offmap.h
@@ -90,10 +90,18 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
  */
 int git_offmap_delete(git_offmap *map, const git_off_t key);
 
+/**
+ * Check whether a key exists in the given map.
+ *
+ * @param map map to query for the key
+ * @param key key to search for
+ * @return 0 if the key has not been found, 1 otherwise
+ */
+int git_offmap_exists(git_offmap *map, const git_off_t key);
+
 size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
 int git_offmap_valid_index(git_offmap *map, size_t idx);
 
-int git_offmap_exists(git_offmap *map, const git_off_t key);
 int git_offmap_has_data(git_offmap *map, size_t idx);
 
 git_off_t git_offmap_key_at(git_offmap *map, size_t idx);
diff --git a/src/oidmap.c b/src/oidmap.c
index d96551f..47a023f 100644
--- a/src/oidmap.c
+++ b/src/oidmap.c
@@ -83,6 +83,11 @@ int git_oidmap_delete(git_oidmap *map, const git_oid *key)
 	return 0;
 }
 
+int git_oidmap_exists(git_oidmap *map, const git_oid *key)
+{
+	return kh_get(oid, map, key) != kh_end(map);
+}
+
 size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
 {
 	return kh_get(oid, map, key);
@@ -93,11 +98,6 @@ int git_oidmap_valid_index(git_oidmap *map, size_t idx)
 	return idx != kh_end(map);
 }
 
-int git_oidmap_exists(git_oidmap *map, const git_oid *key)
-{
-	return kh_get(oid, map, key) != kh_end(map);
-}
-
 int git_oidmap_has_data(git_oidmap *map, size_t idx)
 {
 	return kh_exist(map, idx);
diff --git a/src/oidmap.h b/src/oidmap.h
index d8ffb92..048cbbb 100644
--- a/src/oidmap.h
+++ b/src/oidmap.h
@@ -90,10 +90,18 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);
  */
 int git_oidmap_delete(git_oidmap *map, const git_oid *key);
 
+/**
+ * Check whether a key exists in the given map.
+ *
+ * @param map map to query for the key
+ * @param key key to search for
+ * @return 0 if the key has not been found, 1 otherwise
+ */
+int git_oidmap_exists(git_oidmap *map, const git_oid *key);
+
 size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
 int git_oidmap_valid_index(git_oidmap *map, size_t idx);
 
-int git_oidmap_exists(git_oidmap *map, const git_oid *key);
 int git_oidmap_has_data(git_oidmap *map, size_t idx);
 
 const git_oid *git_oidmap_key(git_oidmap *map, size_t idx);
diff --git a/src/strmap.c b/src/strmap.c
index ffdf6f1..94c4668 100644
--- a/src/strmap.c
+++ b/src/strmap.c
@@ -76,6 +76,11 @@ int git_strmap_delete(git_strmap *map, const char *key)
 	return 0;
 }
 
+int git_strmap_exists(git_strmap *map, const char *key)
+{
+	return kh_get(str, map, key) != kh_end(map);
+}
+
 size_t git_strmap_lookup_index(git_strmap *map, const char *key)
 {
 	return kh_get(str, map, key);
@@ -86,11 +91,6 @@ int git_strmap_valid_index(git_strmap *map, size_t idx)
 	return idx != kh_end(map);
 }
 
-int git_strmap_exists(git_strmap *map, const char *key)
-{
-	return kh_get(str, map, key) != kh_end(map);
-}
-
 int git_strmap_has_data(git_strmap *map, size_t idx)
 {
 	return kh_exist(map, idx);
diff --git a/src/strmap.h b/src/strmap.h
index 3317ea7..6ed920c 100644
--- a/src/strmap.h
+++ b/src/strmap.h
@@ -88,10 +88,18 @@ int git_strmap_set(git_strmap *map, const char *key, void *value);
  */
 int git_strmap_delete(git_strmap *map, const char *key);
 
+/**
+ * Check whether a key exists in the given map.
+ *
+ * @param map map to query for the key
+ * @param key key to search for
+ * @return 0 if the key has not been found, 1 otherwise
+ */
+int git_strmap_exists(git_strmap *map, const char *key);
+
 size_t git_strmap_lookup_index(git_strmap *map, const char *key);
 int git_strmap_valid_index(git_strmap *map, size_t idx);
 
-int git_strmap_exists(git_strmap *map, const char *key);
 int git_strmap_has_data(git_strmap *map, size_t idx);
 
 const char *git_strmap_key(git_strmap *map, size_t idx);
diff --git a/src/submodule.c b/src/submodule.c
index aa4731e..c54b9df 100644
--- a/src/submodule.c
+++ b/src/submodule.c
@@ -1907,7 +1907,6 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
 {
 	lfc_data *data = payload;
 	const char *namestart, *property;
-	size_t pos;
 	git_strmap *map = data->map;
 	git_buf name = GIT_BUF_INIT;
 	git_submodule *sm;
@@ -1939,8 +1938,7 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
 	 * a new submodule, load the config and insert it. If it's
 	 * already inserted, we've already loaded it, so we skip.
 	 */
-	pos = git_strmap_lookup_index(map, name.ptr);
-	if (git_strmap_valid_index(map, pos)) {
+	if (git_strmap_exists(map, name.ptr)) {
 		error = 0;
 		goto done;
 	}