Skip to content

Commit c50a8ac

Browse files
committed
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 parent 84a089d commit c50a8ac

File tree

8 files changed

+44
-24
lines changed

8 files changed

+44
-24
lines changed

src/apply.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ static int apply_one(
439439
git_filemode_t pre_filemode;
440440
git_index_entry pre_entry, post_entry;
441441
bool skip_preimage = false;
442-
size_t pos;
443442
int error;
444443

445444
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
@@ -464,8 +463,7 @@ static int apply_one(
464463
*/
465464
if (delta->status != GIT_DELTA_RENAMED &&
466465
delta->status != GIT_DELTA_ADDED) {
467-
pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
468-
if (git_strmap_valid_index(removed_paths, pos)) {
466+
if (git_strmap_exists(removed_paths, delta->old_file.path)) {
469467
error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
470468
goto done;
471469
}

src/offmap.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ int git_offmap_delete(git_offmap *map, const git_off_t key)
7777
return 0;
7878
}
7979

80+
int git_offmap_exists(git_offmap *map, const git_off_t key)
81+
{
82+
return kh_get(off, map, key) != kh_end(map);
83+
}
84+
8085
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
8186
{
8287
return kh_get(off, map, key);
@@ -87,11 +92,6 @@ int git_offmap_valid_index(git_offmap *map, size_t idx)
8792
return idx != kh_end(map);
8893
}
8994

90-
int git_offmap_exists(git_offmap *map, const git_off_t key)
91-
{
92-
return kh_get(off, map, key) != kh_end(map);
93-
}
94-
9595
int git_offmap_has_data(git_offmap *map, size_t idx)
9696
{
9797
return kh_exist(map, idx);

src/offmap.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
9090
*/
9191
int git_offmap_delete(git_offmap *map, const git_off_t key);
9292

93+
/**
94+
* Check whether a key exists in the given map.
95+
*
96+
* @param map map to query for the key
97+
* @param key key to search for
98+
* @return 0 if the key has not been found, 1 otherwise
99+
*/
100+
int git_offmap_exists(git_offmap *map, const git_off_t key);
101+
93102
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
94103
int git_offmap_valid_index(git_offmap *map, size_t idx);
95104

96-
int git_offmap_exists(git_offmap *map, const git_off_t key);
97105
int git_offmap_has_data(git_offmap *map, size_t idx);
98106

99107
git_off_t git_offmap_key_at(git_offmap *map, size_t idx);

src/oidmap.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ int git_oidmap_delete(git_oidmap *map, const git_oid *key)
8383
return 0;
8484
}
8585

86+
int git_oidmap_exists(git_oidmap *map, const git_oid *key)
87+
{
88+
return kh_get(oid, map, key) != kh_end(map);
89+
}
90+
8691
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key)
8792
{
8893
return kh_get(oid, map, key);
@@ -93,11 +98,6 @@ int git_oidmap_valid_index(git_oidmap *map, size_t idx)
9398
return idx != kh_end(map);
9499
}
95100

96-
int git_oidmap_exists(git_oidmap *map, const git_oid *key)
97-
{
98-
return kh_get(oid, map, key) != kh_end(map);
99-
}
100-
101101
int git_oidmap_has_data(git_oidmap *map, size_t idx)
102102
{
103103
return kh_exist(map, idx);

src/oidmap.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,18 @@ int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);
9090
*/
9191
int git_oidmap_delete(git_oidmap *map, const git_oid *key);
9292

93+
/**
94+
* Check whether a key exists in the given map.
95+
*
96+
* @param map map to query for the key
97+
* @param key key to search for
98+
* @return 0 if the key has not been found, 1 otherwise
99+
*/
100+
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
101+
93102
size_t git_oidmap_lookup_index(git_oidmap *map, const git_oid *key);
94103
int git_oidmap_valid_index(git_oidmap *map, size_t idx);
95104

96-
int git_oidmap_exists(git_oidmap *map, const git_oid *key);
97105
int git_oidmap_has_data(git_oidmap *map, size_t idx);
98106

99107
const git_oid *git_oidmap_key(git_oidmap *map, size_t idx);

src/strmap.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ int git_strmap_delete(git_strmap *map, const char *key)
7676
return 0;
7777
}
7878

79+
int git_strmap_exists(git_strmap *map, const char *key)
80+
{
81+
return kh_get(str, map, key) != kh_end(map);
82+
}
83+
7984
size_t git_strmap_lookup_index(git_strmap *map, const char *key)
8085
{
8186
return kh_get(str, map, key);
@@ -86,11 +91,6 @@ int git_strmap_valid_index(git_strmap *map, size_t idx)
8691
return idx != kh_end(map);
8792
}
8893

89-
int git_strmap_exists(git_strmap *map, const char *key)
90-
{
91-
return kh_get(str, map, key) != kh_end(map);
92-
}
93-
9494
int git_strmap_has_data(git_strmap *map, size_t idx)
9595
{
9696
return kh_exist(map, idx);

src/strmap.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,18 @@ int git_strmap_set(git_strmap *map, const char *key, void *value);
8888
*/
8989
int git_strmap_delete(git_strmap *map, const char *key);
9090

91+
/**
92+
* Check whether a key exists in the given map.
93+
*
94+
* @param map map to query for the key
95+
* @param key key to search for
96+
* @return 0 if the key has not been found, 1 otherwise
97+
*/
98+
int git_strmap_exists(git_strmap *map, const char *key);
99+
91100
size_t git_strmap_lookup_index(git_strmap *map, const char *key);
92101
int git_strmap_valid_index(git_strmap *map, size_t idx);
93102

94-
int git_strmap_exists(git_strmap *map, const char *key);
95103
int git_strmap_has_data(git_strmap *map, size_t idx);
96104

97105
const char *git_strmap_key(git_strmap *map, size_t idx);

src/submodule.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,6 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
19071907
{
19081908
lfc_data *data = payload;
19091909
const char *namestart, *property;
1910-
size_t pos;
19111910
git_strmap *map = data->map;
19121911
git_buf name = GIT_BUF_INIT;
19131912
git_submodule *sm;
@@ -1939,8 +1938,7 @@ static int submodule_load_each(const git_config_entry *entry, void *payload)
19391938
* a new submodule, load the config and insert it. If it's
19401939
* already inserted, we've already loaded it, so we skip.
19411940
*/
1942-
pos = git_strmap_lookup_index(map, name.ptr);
1943-
if (git_strmap_valid_index(map, pos)) {
1941+
if (git_strmap_exists(map, name.ptr)) {
19441942
error = 0;
19451943
goto done;
19461944
}

0 commit comments

Comments
 (0)