Commit a129941ac162e81f59d57737e2debcc9e456858d

Edward Thomson 2020-01-16T17:44:55

Merge pull request #5351 from pks-t/pks/index-map-macros index: replace map macros with inline functions

diff --git a/src/index.c b/src/index.c
index dc3d8f3..e2a8c7b 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,30 @@ 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);
+}
+
+GIT_INLINE(int) index_map_resize(git_idxmap *map, size_t count, bool ignore_case)
+{
+	if (ignore_case)
+		return git_idxmap_icase_resize((git_idxmap_icase *) map, count);
+	else
+		return git_idxmap_resize(map, count);
+}
+
 int git_index_entry_srch(const void *key, const void *array_member)
 {
 	const struct entry_srch_key *srch_key = key;
@@ -507,7 +508,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 +860,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 +1403,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 +1619,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);
 
@@ -1625,33 +1628,33 @@ int git_index__fill(git_index *index, const git_vector *source_entries)
 		return 0;
 
 	if (git_vector_size_hint(&index->entries, source_entries->length) < 0 ||
-	    git_idxmap_resize(index->entries_map, (size_t)(source_entries->length * 1.3)) < 0)
+	    index_map_resize(index->entries_map, (size_t)(source_entries->length * 1.3),
+			     index->ignore_case) < 0)
 		return -1;
 
 	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 +1687,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(
@@ -2614,11 +2617,7 @@ static int parse_index(git_index *index, const char *buffer, size_t buffer_size)
 
 	assert(!index->entries.length);
 
-	if (index->ignore_case &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) index->entries_map,
-					     header.entry_count)) < 0)
-		return error;
-	else if ((error = git_idxmap_resize(index->entries_map, header.entry_count)) < 0)
+	if ((error = index_map_resize(index->entries_map, header.entry_count, index->ignore_case)) < 0)
 		return error;
 
 	/* Parse all the entries */
@@ -2636,9 +2635,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;
 		}
@@ -3133,17 +3130,11 @@ int git_index_read_tree(git_index *index, const git_tree *tree)
 	if ((error = git_tree_walk(tree, GIT_TREEWALK_POST, read_tree_cb, &data)) < 0)
 		goto cleanup;
 
-	if (index->ignore_case &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) entries_map,
-					     entries.length)) < 0)
-		goto cleanup;
-	else if ((error = git_idxmap_resize(entries_map, entries.length)) < 0)
+	if ((error = index_map_resize(entries_map, entries.length, index->ignore_case)) < 0)
 		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;
 		}
@@ -3195,12 +3186,8 @@ static int git_index_read_iterator(
 	    (error = git_idxmap_new(&new_entries_map)) < 0)
 		goto done;
 
-	if (index->ignore_case && new_length_hint &&
-	    (error = git_idxmap_icase_resize((git_idxmap_icase *) new_entries_map,
-					     new_length_hint)) < 0)
-		goto done;
-	else if (new_length_hint &&
-	         (error = git_idxmap_resize(new_entries_map, new_length_hint)) < 0)
+	if (new_length_hint && (error = index_map_resize(new_entries_map, new_length_hint,
+							 index->ignore_case)) < 0)
 		goto done;
 
 	opts.flags = GIT_ITERATOR_DONT_IGNORE_CASE |
@@ -3265,7 +3252,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)