Skip to content

Commit ab45887

Browse files
committed
index: replace map macros with inline functions
Traditionally, our maps were mostly implemented via macros that had weird call semantics. This shows in our index code, where we have macros that insert into an index map case-sensitively or insensitively, as they still return error codes via an error parameter. This is unwieldy and, most importantly, not necessary anymore, due to the introduction of our high-level map API and removal of macros. Replace them with inlined functions to make code easier to read.
1 parent cc4f4cb commit ab45887

File tree

1 file changed

+34
-43
lines changed

1 file changed

+34
-43
lines changed

src/index.c

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@
2727
#include "git2/config.h"
2828
#include "git2/sys/index.h"
2929

30-
#define INSERT_IN_MAP_EX(idx, map, e, err) do { \
31-
if ((idx)->ignore_case) \
32-
(err) = git_idxmap_icase_set((git_idxmap_icase *) (map), (e), (e)); \
33-
else \
34-
(err) = git_idxmap_set((map), (e), (e)); \
35-
} while (0)
36-
37-
#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
38-
39-
#define LOOKUP_IN_MAP(v, idx, k) do { \
40-
if ((idx)->ignore_case) \
41-
(v) = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, (k)); \
42-
else \
43-
(v) = git_idxmap_get(index->entries_map, (k)); \
44-
} while (0)
45-
46-
#define DELETE_IN_MAP(idx, e) do { \
47-
if ((idx)->ignore_case) \
48-
git_idxmap_icase_delete((git_idxmap_icase *) (idx)->entries_map, (e)); \
49-
else \
50-
git_idxmap_delete((idx)->entries_map, (e)); \
51-
} while (0)
52-
5330
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
5431
unsigned int flags,
5532
git_index_matched_path_cb cb, void *payload);
@@ -148,6 +125,22 @@ static int write_index(git_oid *checksum, git_index *index, git_filebuf *file);
148125
static void index_entry_free(git_index_entry *entry);
149126
static void index_entry_reuc_free(git_index_reuc_entry *reuc);
150127

128+
GIT_INLINE(int) index_map_set(git_idxmap *map, git_index_entry *e, bool ignore_case)
129+
{
130+
if (ignore_case)
131+
return git_idxmap_icase_set((git_idxmap_icase *) map, e, e);
132+
else
133+
return git_idxmap_set(map, e, e);
134+
}
135+
136+
GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignore_case)
137+
{
138+
if (ignore_case)
139+
return git_idxmap_icase_delete((git_idxmap_icase *) map, e);
140+
else
141+
return git_idxmap_delete(map, e);
142+
}
143+
151144
int git_index_entry_srch(const void *key, const void *array_member)
152145
{
153146
const struct entry_srch_key *srch_key = key;
@@ -507,7 +500,7 @@ static int index_remove_entry(git_index *index, size_t pos)
507500

508501
if (entry != NULL) {
509502
git_tree_cache_invalidate_path(index->tree, entry->path);
510-
DELETE_IN_MAP(index, entry);
503+
index_map_delete(index->entries_map, entry, index->ignore_case);
511504
}
512505

513506
error = git_vector_remove(&index->entries, pos);
@@ -859,7 +852,10 @@ const git_index_entry *git_index_get_bypath(
859852
key.path = path;
860853
GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
861854

862-
LOOKUP_IN_MAP(value, index, &key);
855+
if (index->ignore_case)
856+
value = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, &key);
857+
else
858+
value = git_idxmap_get(index->entries_map, &key);
863859

864860
if (!value) {
865861
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
@@ -1399,10 +1395,9 @@ static int index_insert(
13991395
* at the sorted position. (Since we re-sort after each insert to
14001396
* check for dups, this is actually cheaper in the long run.)
14011397
*/
1402-
if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0)
1398+
if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 ||
1399+
(error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
14031400
goto out;
1404-
1405-
INSERT_IN_MAP(index, entry, error);
14061401
}
14071402

14081403
index->dirty = 1;
@@ -1616,8 +1611,8 @@ int git_index_remove_bypath(git_index *index, const char *path)
16161611
int git_index__fill(git_index *index, const git_vector *source_entries)
16171612
{
16181613
const git_index_entry *source_entry = NULL;
1614+
int error = 0;
16191615
size_t i;
1620-
int ret = 0;
16211616

16221617
assert(index);
16231618

@@ -1631,27 +1626,26 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
16311626
git_vector_foreach(source_entries, i, source_entry) {
16321627
git_index_entry *entry = NULL;
16331628

1634-
if ((ret = index_entry_dup(&entry, index, source_entry)) < 0)
1629+
if ((error = index_entry_dup(&entry, index, source_entry)) < 0)
16351630
break;
16361631

16371632
index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
16381633
entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
16391634
entry->mode = git_index__create_mode(entry->mode);
16401635

1641-
if ((ret = git_vector_insert(&index->entries, entry)) < 0)
1636+
if ((error = git_vector_insert(&index->entries, entry)) < 0)
16421637
break;
16431638

1644-
INSERT_IN_MAP(index, entry, ret);
1645-
if (ret < 0)
1639+
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
16461640
break;
16471641

16481642
index->dirty = 1;
16491643
}
16501644

1651-
if (!ret)
1645+
if (!error)
16521646
git_vector_sort(&index->entries);
16531647

1654-
return ret;
1648+
return error;
16551649
}
16561650

16571651

@@ -1684,7 +1678,7 @@ int git_index_remove(git_index *index, const char *path, int stage)
16841678
remove_key.path = path;
16851679
GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage);
16861680

1687-
DELETE_IN_MAP(index, &remove_key);
1681+
index_map_delete(index->entries_map, &remove_key, index->ignore_case);
16881682

16891683
if (index_find(&position, index, path, 0, stage) < 0) {
16901684
git_error_set(
@@ -2636,9 +2630,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
26362630
goto done;
26372631
}
26382632

2639-
INSERT_IN_MAP(index, entry, error);
2640-
2641-
if (error < 0) {
2633+
if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) {
26422634
index_entry_free(entry);
26432635
goto done;
26442636
}
@@ -3141,9 +3133,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
31413133
goto cleanup;
31423134

31433135
git_vector_foreach(&entries, i, e) {
3144-
INSERT_IN_MAP_EX(index, entries_map, e, error);
3145-
3146-
if (error < 0) {
3136+
if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) {
31473137
git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
31483138
return error;
31493139
}
@@ -3265,7 +3255,8 @@ static int git_index_read_iterator(
32653255

32663256
if (add_entry) {
32673257
if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
3268-
INSERT_IN_MAP_EX(index, new_entries_map, add_entry, error);
3258+
error = index_map_set(new_entries_map, add_entry,
3259+
index->ignore_case);
32693260
}
32703261

32713262
if (remove_entry && error >= 0)

0 commit comments

Comments
 (0)