index: rework index_insert() Now index_insert() takes copy of index entry, not coping it by itself. Signed-off-by: Kirill A. Shutemov <kirill@shutemov.name>
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
diff --git a/src/index.c b/src/index.c
index 34ebc32..f609cda 100644
--- a/src/index.c
+++ b/src/index.c
@@ -357,22 +357,17 @@ static void index_entry_free(git_index_entry *entry)
free(entry);
}
-static int index_insert(git_index *index, const git_index_entry *source_entry, int replace)
+static int index_insert(git_index *index, git_index_entry *entry, int replace)
{
- git_index_entry *entry;
size_t path_length;
int position;
git_index_entry **entry_array;
- assert(index && source_entry);
+ assert(index && entry);
- if (source_entry->path == NULL)
+ if (entry->path == NULL)
return git__throw(GIT_EMISSINGOBJDATA, "Failed to insert into index. Entry has no path");
- entry = index_entry_dup(source_entry);
- if (!entry)
- return GIT_ENOMEM;
-
/* make sure that the path length flag is correct */
path_length = strlen(entry->path);
@@ -389,13 +384,13 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
*/
if (!replace) {
if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
- goto cleanup_oom;
+ return GIT_ENOMEM;
return GIT_SUCCESS;
}
/* look if an entry with this path already exists */
- position = git_index_find(index, source_entry->path);
+ position = git_index_find(index, entry->path);
/*
* if no entry exists add the entry at the end;
@@ -403,7 +398,7 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
*/
if (position == GIT_ENOTFOUND) {
if (git_vector_insert(&index->entries, entry) < GIT_SUCCESS)
- goto cleanup_oom;
+ return GIT_ENOMEM;
return GIT_SUCCESS;
}
@@ -414,10 +409,6 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
entry_array[position] = entry;
return GIT_SUCCESS;
-
-cleanup_oom:
- index_entry_free(entry);
- return GIT_ENOMEM;;
}
static int index_init_entry(git_index_entry *entry, git_index *index, const char *rel_path, int stage)
@@ -462,36 +453,65 @@ static int index_init_entry(git_index_entry *entry, git_index *index, const char
int git_index_add(git_index *index, const char *path, int stage)
{
int error;
- git_index_entry entry;
+ git_index_entry entry, *dup_entry;
if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to add to index");
- return index_insert(index, &entry, 1);
+ dup_entry = index_entry_dup(&entry);
+ if (dup_entry == NULL)
+ return GIT_ENOMEM;
+
+ return index_insert(index, dup_entry, 1);
}
int git_index_append(git_index *index, const char *path, int stage)
{
int error;
- git_index_entry entry;
+ git_index_entry entry, *dup_entry;
if ((error = index_init_entry(&entry, index, path, stage)) < GIT_SUCCESS)
return git__rethrow(error, "Failed to append to index");
- return index_insert(index, &entry, 0);
+ dup_entry = index_entry_dup(&entry);
+ if (dup_entry == NULL)
+ return GIT_ENOMEM;
+
+ return index_insert(index, dup_entry, 0);
+}
+
+static int index_add2(git_index *index, const git_index_entry *source_entry,
+ int replace)
+{
+ git_index_entry *entry = NULL;
+ int ret;
+
+ entry = index_entry_dup(source_entry);
+ if (entry == NULL) {
+ ret = GIT_ENOMEM;
+ goto err;
+ }
+
+ ret = index_insert(index, entry, replace);
+ if (ret)
+ goto err;
+
+ return ret;
+err:
+ index_entry_free(entry);
+ return git__rethrow(ret, "Failed to append to index");
}
int git_index_add2(git_index *index, const git_index_entry *source_entry)
{
- return index_insert(index, source_entry, 1);
+ return index_add2(index, source_entry, 1);
}
int git_index_append2(git_index *index, const git_index_entry *source_entry)
{
- return index_insert(index, source_entry, 0);
+ return index_add2(index, source_entry, 1);
}
-
int git_index_remove(git_index *index, int position)
{
git_vector_sort(&index->entries);