Commit 3f43678e889a7ee77b1cd2e40727f104ea3d7ac1

Vicent Marti 2010-11-07T01:24:45

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>

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)