diff options
author | Patrick Steinhardt <ps@pks.im> | 2018-12-01 08:59:24 +0100 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
commit | c50a8ac2c734e4301223e39e4ddce98e6c368294 (patch) | |
tree | 85ad3a57e06b9405e7b525d4ca361cf9c137e7fd | |
parent | 84a089da3701db370b2c77e6866abe3a5065c542 (diff) | |
download | libgit2-c50a8ac2c734e4301223e39e4ddce98e6c368294.tar.gz |
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.
-rw-r--r-- | src/apply.c | 4 | ||||
-rw-r--r-- | src/offmap.c | 10 | ||||
-rw-r--r-- | src/offmap.h | 10 | ||||
-rw-r--r-- | src/oidmap.c | 10 | ||||
-rw-r--r-- | src/oidmap.h | 10 | ||||
-rw-r--r-- | src/strmap.c | 10 | ||||
-rw-r--r-- | src/strmap.h | 10 | ||||
-rw-r--r-- | src/submodule.c | 4 |
8 files changed, 44 insertions, 24 deletions
diff --git a/src/apply.c b/src/apply.c index 4b7eedd90..ad8ce2f1b 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 eaf72ba18..09f8a2eb0 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 e7187d391..f2a8cadcd 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 d96551f9a..47a023f99 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 d8ffb92ac..048cbbb0a 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 ffdf6f13d..94c466804 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 3317ea7b6..6ed920c37 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 aa4731ef4..c54b9df6f 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; } |