diff options
| author | Patrick Steinhardt <ps@pks.im> | 2019-01-23 10:44:02 +0100 |
|---|---|---|
| committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
| commit | ef507bc7bdd736d2379a0d0614b3db1341d77187 (patch) | |
| tree | a441206d2eef9ddfa3784b361910a9f03d1a5bf6 /src/tree.c | |
| parent | 7e926ef306166993131628b41a13f2b26aaf43de (diff) | |
| download | libgit2-ef507bc7bdd736d2379a0d0614b3db1341d77187.tar.gz | |
strmap: introduce `git_strmap_get` and use it throughout the tree
The current way of looking up an entry from a map is tightly coupled with the
map implementation, as one first has to look up the index of the key and then
retrieve the associated value by using the index. As a caller, you usually do
not care about any indices at all, though, so this is more complicated than
really necessary. Furthermore, it invites for errors to happen if the correct
error checking sequence is not being followed.
Introduce a new high-level function `git_strmap_get` that takes a map and a key
and returns a pointer to the associated value if such a key exists. Otherwise,
a `NULL` pointer is returned. Adjust all callers that can trivially be
converted.
Diffstat (limited to 'src/tree.c')
| -rw-r--r-- | src/tree.c | 15 |
1 files changed, 2 insertions, 13 deletions
diff --git a/src/tree.c b/src/tree.c index cc6f93db5..b1665cee3 100644 --- a/src/tree.c +++ b/src/tree.c @@ -723,16 +723,13 @@ int git_treebuilder_insert( { git_tree_entry *entry; int error; - size_t pos; assert(bld && id && filename); if ((error = check_entry(bld->repo, filename, id, filemode)) < 0) return error; - pos = git_strmap_lookup_index(bld->map, filename); - if (git_strmap_valid_index(bld->map, pos)) { - entry = git_strmap_value_at(bld->map, pos); + if ((entry = git_strmap_get(bld->map, filename)) != NULL) { git_oid_cpy((git_oid *) entry->oid, id); } else { entry = alloc_entry(filename, strlen(filename), id); @@ -757,16 +754,8 @@ int git_treebuilder_insert( static git_tree_entry *treebuilder_get(git_treebuilder *bld, const char *filename) { - git_tree_entry *entry = NULL; - size_t pos; - assert(bld && filename); - - pos = git_strmap_lookup_index(bld->map, filename); - if (git_strmap_valid_index(bld->map, pos)) - entry = git_strmap_value_at(bld->map, pos); - - return entry; + return git_strmap_get(bld->map, filename); } const git_tree_entry *git_treebuilder_get(git_treebuilder *bld, const char *filename) |
