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.
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
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;
}