Skip to content

Commit 661fc57

Browse files
committed
idxmap: introduce high-level setter for key/value pairs
Currently, one would use the function `git_idxmap_insert` to insert key/value pairs into a map. This function has historically been a macro, which is why its syntax is kind of weird: instead of returning an error code directly, it instead has to be passed a pointer to where the return value shall be stored. This does not match libgit2's common idiom of directly returning error codes. Introduce a new function `git_idxmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert all callers of `git_idxmap_insert` to make use of it.
1 parent d00c24a commit 661fc57

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/idxmap.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,40 @@ void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
7777
return kh_val(map, idx);
7878
}
7979

80+
int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value)
81+
{
82+
size_t idx;
83+
int rval;
84+
85+
idx = kh_put(idx, map, key, &rval);
86+
if (rval < 0)
87+
return -1;
88+
89+
if (rval == 0)
90+
kh_key(map, idx) = key;
91+
92+
kh_val(map, idx) = value;
93+
94+
return 0;
95+
}
96+
97+
int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value)
98+
{
99+
size_t idx;
100+
int rval;
101+
102+
idx = kh_put(idxicase, map, key, &rval);
103+
if (rval < 0)
104+
return -1;
105+
106+
if (rval == 0)
107+
kh_key(map, idx) = key;
108+
109+
kh_val(map, idx) = value;
110+
111+
return 0;
112+
}
113+
80114
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key)
81115
{
82116
size_t idx = git_idxmap_icase_lookup_index(map, key);

src/idxmap.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@ void *git_idxmap_get(git_idxmap *map, const git_index_entry *key);
9696
*/
9797
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key);
9898

99+
/**
100+
* Set the entry for key to value.
101+
*
102+
* If the map has no corresponding entry for the given key, a new
103+
* entry will be created with the given value. If an entry exists
104+
* already, its value will be updated to match the given value.
105+
*
106+
* @param map map to create new entry in
107+
* @param key key to set
108+
* @param value value to associate the key with; may be NULL
109+
* @return zero if the key was successfully set, a negative error
110+
* code otherwise
111+
*/
112+
int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value);
113+
114+
/**
115+
* Set the entry for key to value.
116+
*
117+
* If the map has no corresponding entry for the given key, a new
118+
* entry will be created with the given value. If an entry exists
119+
* already, its value will be updated to match the given value.
120+
*
121+
* @param map map to create new entry in
122+
* @param key key to set
123+
* @param value value to associate the key with; may be NULL
124+
* @return zero if the key was successfully set, a negative error
125+
* code otherwise
126+
*/
127+
int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value);
128+
99129
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval);
100130
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval);
101131

src/index.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
#define INSERT_IN_MAP_EX(idx, map, e, err) do { \
3131
if ((idx)->ignore_case) \
32-
git_idxmap_icase_insert((git_idxmap_icase *) (map), (e), (e), (err)); \
32+
(err) = git_idxmap_icase_set((git_idxmap_icase *) (map), (e), (e)); \
3333
else \
34-
git_idxmap_insert((map), (e), (e), (err)); \
34+
(err) = git_idxmap_set((map), (e), (e)); \
3535
} while (0)
3636

3737
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
@@ -1402,7 +1402,7 @@ static int index_insert(
14021402
if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0)
14031403
goto out;
14041404

1405-
INSERT_IN_MAP(index, entry, &error);
1405+
INSERT_IN_MAP(index, entry, error);
14061406
}
14071407

14081408
index->dirty = 1;
@@ -1635,7 +1635,7 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
16351635
if ((ret = git_vector_insert(&index->entries, entry)) < 0)
16361636
break;
16371637

1638-
INSERT_IN_MAP(index, entry, &ret);
1638+
INSERT_IN_MAP(index, entry, ret);
16391639
if (ret < 0)
16401640
break;
16411641

@@ -2628,7 +2628,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
26282628
goto done;
26292629
}
26302630

2631-
INSERT_IN_MAP(index, entry, &error);
2631+
INSERT_IN_MAP(index, entry, error);
26322632

26332633
if (error < 0) {
26342634
index_entry_free(entry);
@@ -3131,7 +3131,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
31313131
git_idxmap_resize(entries_map, entries.length);
31323132

31333133
git_vector_foreach(&entries, i, e) {
3134-
INSERT_IN_MAP_EX(index, entries_map, e, &error);
3134+
INSERT_IN_MAP_EX(index, entries_map, e, error);
31353135

31363136
if (error < 0) {
31373137
git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
@@ -3252,7 +3252,7 @@ static int git_index_read_iterator(
32523252

32533253
if (add_entry) {
32543254
if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
3255-
INSERT_IN_MAP_EX(index, new_entries_map, add_entry, &error);
3255+
INSERT_IN_MAP_EX(index, new_entries_map, add_entry, error);
32563256
}
32573257

32583258
if (remove_entry && error >= 0)

0 commit comments

Comments
 (0)