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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
diff --git a/src/index.c b/src/index.c
index dc3d8f3..c3aaa48 100644
--- a/src/index.c
+++ b/src/index.c
@@ -27,29 +27,6 @@
#include "git2/config.h"
#include "git2/sys/index.h"
-#define INSERT_IN_MAP_EX(idx, map, e, err) do { \
- if ((idx)->ignore_case) \
- (err) = git_idxmap_icase_set((git_idxmap_icase *) (map), (e), (e)); \
- else \
- (err) = git_idxmap_set((map), (e), (e)); \
- } while (0)
-
-#define INSERT_IN_MAP(idx, e, err) INSERT_IN_MAP_EX(idx, (idx)->entries_map, e, err)
-
-#define LOOKUP_IN_MAP(v, idx, k) do { \
- if ((idx)->ignore_case) \
- (v) = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, (k)); \
- else \
- (v) = git_idxmap_get(index->entries_map, (k)); \
- } while (0)
-
-#define DELETE_IN_MAP(idx, e) do { \
- if ((idx)->ignore_case) \
- git_idxmap_icase_delete((git_idxmap_icase *) (idx)->entries_map, (e)); \
- else \
- git_idxmap_delete((idx)->entries_map, (e)); \
- } while (0)
-
static int index_apply_to_wd_diff(git_index *index, int action, const git_strarray *paths,
unsigned int flags,
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);
static void index_entry_free(git_index_entry *entry);
static void index_entry_reuc_free(git_index_reuc_entry *reuc);
+GIT_INLINE(int) index_map_set(git_idxmap *map, git_index_entry *e, bool ignore_case)
+{
+ if (ignore_case)
+ return git_idxmap_icase_set((git_idxmap_icase *) map, e, e);
+ else
+ return git_idxmap_set(map, e, e);
+}
+
+GIT_INLINE(int) index_map_delete(git_idxmap *map, git_index_entry *e, bool ignore_case)
+{
+ if (ignore_case)
+ return git_idxmap_icase_delete((git_idxmap_icase *) map, e);
+ else
+ return git_idxmap_delete(map, e);
+}
+
int git_index_entry_srch(const void *key, const void *array_member)
{
const struct entry_srch_key *srch_key = key;
@@ -507,7 +500,7 @@ static int index_remove_entry(git_index *index, size_t pos)
if (entry != NULL) {
git_tree_cache_invalidate_path(index->tree, entry->path);
- DELETE_IN_MAP(index, entry);
+ index_map_delete(index->entries_map, entry, index->ignore_case);
}
error = git_vector_remove(&index->entries, pos);
@@ -859,7 +852,10 @@ const git_index_entry *git_index_get_bypath(
key.path = path;
GIT_INDEX_ENTRY_STAGE_SET(&key, stage);
- LOOKUP_IN_MAP(value, index, &key);
+ if (index->ignore_case)
+ value = git_idxmap_icase_get((git_idxmap_icase *) index->entries_map, &key);
+ else
+ value = git_idxmap_get(index->entries_map, &key);
if (!value) {
git_error_set(GIT_ERROR_INDEX, "index does not contain '%s'", path);
@@ -1399,10 +1395,9 @@ static int index_insert(
* at the sorted position. (Since we re-sort after each insert to
* check for dups, this is actually cheaper in the long run.)
*/
- if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0)
+ if ((error = git_vector_insert_sorted(&index->entries, entry, index_no_dups)) < 0 ||
+ (error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
goto out;
-
- INSERT_IN_MAP(index, entry, error);
}
index->dirty = 1;
@@ -1616,8 +1611,8 @@ int git_index_remove_bypath(git_index *index, const char *path)
int git_index__fill(git_index *index, const git_vector *source_entries)
{
const git_index_entry *source_entry = NULL;
+ int error = 0;
size_t i;
- int ret = 0;
assert(index);
@@ -1631,27 +1626,26 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
git_vector_foreach(source_entries, i, source_entry) {
git_index_entry *entry = NULL;
- if ((ret = index_entry_dup(&entry, index, source_entry)) < 0)
+ if ((error = index_entry_dup(&entry, index, source_entry)) < 0)
break;
index_entry_adjust_namemask(entry, ((struct entry_internal *)entry)->pathlen);
entry->flags_extended |= GIT_INDEX_ENTRY_UPTODATE;
entry->mode = git_index__create_mode(entry->mode);
- if ((ret = git_vector_insert(&index->entries, entry)) < 0)
+ if ((error = git_vector_insert(&index->entries, entry)) < 0)
break;
- INSERT_IN_MAP(index, entry, ret);
- if (ret < 0)
+ if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0)
break;
index->dirty = 1;
}
- if (!ret)
+ if (!error)
git_vector_sort(&index->entries);
- return ret;
+ return error;
}
@@ -1684,7 +1678,7 @@ int git_index_remove(git_index *index, const char *path, int stage)
remove_key.path = path;
GIT_INDEX_ENTRY_STAGE_SET(&remove_key, stage);
- DELETE_IN_MAP(index, &remove_key);
+ index_map_delete(index->entries_map, &remove_key, index->ignore_case);
if (index_find(&position, index, path, 0, stage) < 0) {
git_error_set(
@@ -2636,9 +2630,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
goto done;
}
- INSERT_IN_MAP(index, entry, error);
-
- if (error < 0) {
+ if ((error = index_map_set(index->entries_map, entry, index->ignore_case)) < 0) {
index_entry_free(entry);
goto done;
}
@@ -3141,9 +3133,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
goto cleanup;
git_vector_foreach(&entries, i, e) {
- INSERT_IN_MAP_EX(index, entries_map, e, error);
-
- if (error < 0) {
+ if ((error = index_map_set(entries_map, e, index->ignore_case)) < 0) {
git_error_set(GIT_ERROR_INDEX, "failed to insert entry into map");
return error;
}
@@ -3265,7 +3255,8 @@ static int git_index_read_iterator(
if (add_entry) {
if ((error = git_vector_insert(&new_entries, add_entry)) == 0)
- INSERT_IN_MAP_EX(index, new_entries_map, add_entry, error);
+ error = index_map_set(new_entries_map, add_entry,
+ index->ignore_case);
}
if (remove_entry && error >= 0)