Make the Index API public Several private methods of the Index API are now public, including the methods to remove, get and add index entries. All the methods only take an integer value for the position of the entry to get/remove. To get or remove entries based on their path names, look them up first using the git_index_find method. Signed-off-by: Vicent Marti <tanoku@gmail.com>
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
diff --git a/src/git/index.h b/src/git/index.h
index ee41086..bdfb7e1 100644
--- a/src/git/index.h
+++ b/src/git/index.h
@@ -108,14 +108,32 @@ GIT_EXTERN(int) git_index_write(git_index *index);
GIT_EXTERN(int) git_index_find(git_index *index, const char *path);
/**
- * Add a new empty entry to the index.
+ * Add a new empty entry to the index with a given path.
*
* @param index an existing index object
* @param path filename pointed to by the entry
* @param stage stage for the entry
* @return 0 on success, otherwise an error code
*/
-GIT_EXTERN(int) git_index_add(git_index *index, const char *path, int stage);
+GIT_EXTERN(int) git_index_add_bypath(git_index *index, const char *path, int stage);
+
+/**
+ * Remove an entry from the index
+ *
+ * @param index an existing index object
+ * @param position position of the entry to remove
+ * @return 0 on success, otherwise an error code
+ */
+GIT_EXTERN(int) git_index_remove(git_index *index, int position);
+
+/**
+ * Add a new entry to the index
+ *
+ * @param index an existing index object
+ * @param source_entry new entry object
+ * @return 0 on success, otherwise an error code
+ */
+GIT_EXTERN(int) git_index_add(git_index *index, const git_index_entry *source_entry);
/**
* Get a pointer to one of the entries in the index
diff --git a/src/index.c b/src/index.c
index 71481cc..77de0a8 100644
--- a/src/index.c
+++ b/src/index.c
@@ -223,10 +223,11 @@ int git_index_write(git_index *index)
git_index_entry *git_index_get(git_index *index, int n)
{
+ assert(index);
return (n >= 0 && (unsigned int)n < index->entry_count) ? &index->entries[n] : NULL;
}
-int git_index_add(git_index *index, const char *filename, int stage)
+int git_index_add_bypath(git_index *index, const char *filename, int stage)
{
git_index_entry entry;
size_t path_length;
@@ -247,7 +248,7 @@ int git_index_add(git_index *index, const char *filename, int stage)
entry.path = git__strdup(filename);
- return git_index__append(index, &entry);
+ return git_index_add(index, &entry);
}
void git_index__sort(git_index *index)
@@ -274,7 +275,7 @@ void git_index__sort(git_index *index)
index->sorted = 1;
}
-int git_index__append(git_index *index, const git_index_entry *source_entry)
+int git_index_add(git_index *index, const git_index_entry *source_entry)
{
git_index_entry *offset;
@@ -303,18 +304,23 @@ int git_index__append(git_index *index, const git_index_entry *source_entry)
return GIT_SUCCESS;
}
-int git_index__remove_pos(git_index *index, unsigned int position)
+int git_index_remove(git_index *index, int position)
{
git_index_entry *offset;
size_t copy_size;
+ assert(index);
+
+ if (position < 0 || (unsigned int)position > index->entry_count)
+ return GIT_ENOTFOUND;
+
offset = &index->entries[position];
index->entry_count--;
copy_size = (index->entry_count - position) * sizeof(git_index_entry);
memcpy(offset, offset + sizeof(git_index_entry), copy_size);
- return 0;
+ return GIT_SUCCESS;
}
int git_index_find(git_index *index, const char *path)