index: extract index_entry_dup() from index_insert() 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
diff --git a/src/index.c b/src/index.c
index bdae26c..2eb0809 100644
--- a/src/index.c
+++ b/src/index.c
@@ -331,6 +331,24 @@ git_index_entry *git_index_get(git_index *index, unsigned int n)
return git_vector_get(&index->entries, n);
}
+static git_index_entry *index_entry_dup(const git_index_entry *source_entry)
+{
+ git_index_entry *entry;
+
+ entry = git__malloc(sizeof(git_index_entry));
+ if (!entry)
+ return NULL;
+
+ memcpy(entry, source_entry, sizeof(git_index_entry));
+
+ /* duplicate the path string so we own it */
+ entry->path = git__strdup(entry->path);
+ if (!entry->path)
+ return NULL;
+
+ return entry;
+}
+
static int index_insert(git_index *index, const git_index_entry *source_entry, int replace)
{
git_index_entry *entry;
@@ -343,15 +361,8 @@ static int index_insert(git_index *index, const git_index_entry *source_entry, i
if (source_entry->path == NULL)
return git__throw(GIT_EMISSINGOBJDATA, "Failed to insert into index. Entry has no path");
- entry = git__malloc(sizeof(git_index_entry));
- if (entry == NULL)
- return GIT_ENOMEM;
-
- memcpy(entry, source_entry, sizeof(git_index_entry));
-
- /* duplicate the path string so we own it */
- entry->path = git__strdup(entry->path);
- if (entry->path == NULL)
+ entry = index_entry_dup(source_entry);
+ if (!entry)
return GIT_ENOMEM;
/* make sure that the path length flag is correct */