Commit c836c332f17ff2da8bdf6d18fb3d59eac2586ca9

Vicent Marti 2011-02-05T09:29:37

Make more methods return error codes git_revwalk_next now returns an error code when the iteration is over. git_repository_index now returns an error code when the index file could not be opened. Signed-off-by: Vicent Marti <tanoku@gmail.com>

diff --git a/src/errors.c b/src/errors.c
index 34a15b2..3616fdb 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -24,7 +24,9 @@ static struct {
 	{GIT_EINVALIDREFNAME, "The name of the reference is not valid"},
 	{GIT_EREFCORRUPTED, "The specified reference has its data corrupted"},
 	{GIT_ETOONESTEDSYMREF, "The specified symbolic reference is too deeply nested"},
-	{GIT_EPACKEDREFSCORRUPTED, "The pack-refs file is either corrupted of its format is not currently supported"}
+	{GIT_EPACKEDREFSCORRUPTED, "The pack-refs file is either corrupted of its format is not currently supported"},
+	{GIT_EINVALIDPATH, "The path is invalid" },
+	{GIT_EREVWALKOVER, "The revision walker is empty; there are no more commits left to iterate"}
 };
 
 const char *git_strerror(int num)
diff --git a/src/git2/common.h b/src/git2/common.h
index aae707c..3500a2b 100644
--- a/src/git2/common.h
+++ b/src/git2/common.h
@@ -151,6 +151,9 @@
 /** The path is invalid */
 #define GIT_EINVALIDPATH (GIT_ERROR - 19)
 
+/** The revision walker is empty; there are no more commits left to iterate */
+#define GIT_EREVWALKOVER (GIT_ERROR - 20)
+
 GIT_BEGIN_DECL
 /** @} */
 GIT_END_DECL
diff --git a/src/git2/repository.h b/src/git2/repository.h
index ecf3db9..ec74305 100644
--- a/src/git2/repository.h
+++ b/src/git2/repository.h
@@ -163,11 +163,14 @@ GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo);
 /**
  * Get the Index file of a Git repository
  *
+ * This is a cheap operation; the index is only opened on the first call,
+ * and subsequent calls only retrieve the previous pointer.
+ *
+ * @param index Pointer where to store the index
  * @param repo a repository object
- * @return a pointer to the Index object; 
- *	NULL if the index cannot be opened
+ * @return 0 on success; error code if the index could not be opened
  */
-GIT_EXTERN(git_index *) git_repository_index(git_repository *rpeo);
+GIT_EXTERN(int) git_repository_index(git_index **index, git_repository *repo);
 
 /**
  * Create a new in-memory repository object with
diff --git a/src/git2/revwalk.h b/src/git2/revwalk.h
index 960039a..8411104 100644
--- a/src/git2/revwalk.h
+++ b/src/git2/revwalk.h
@@ -99,10 +99,13 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, git_commit *commit);
 
 /**
  * Get the next commit from the revision traversal.
+ *
+ * @param commit Pointer where to store the next commit
  * @param walk the walker to pop the commit from.
- * @return next commit; NULL if there is no more output.
+ * @return GIT_SUCCESS if the next commit was found;
+ *	GIT_EREVWALKOVER if there are no commits left to iterate
  */
-GIT_EXTERN(git_commit *) git_revwalk_next(git_revwalk *walk);
+GIT_EXTERN(int) git_revwalk_next(git_commit **commit, git_revwalk *walk);
 
 /**
  * Change the sorting mode when iterating through the
diff --git a/src/refs.h b/src/refs.h
index 3b6d567..70196aa 100644
--- a/src/refs.h
+++ b/src/refs.h
@@ -3,6 +3,7 @@
 
 #include "common.h"
 #include "git2/oid.h"
+#include "git2/refs.h"
 #include "hashtable.h"
 
 #define GIT_REFS_DIR "refs/"
diff --git a/src/repository.c b/src/repository.c
index d010d8c..37d5a49 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -374,16 +374,22 @@ void git_repository_free(git_repository *repo)
 	free(repo);
 }
 
-git_index *git_repository_index(git_repository *repo)
+int git_repository_index(git_index **index_out, git_repository *repo)
 {
+	int error;
+
+	assert(index_out && repo);
+
 	if (repo->index == NULL) {
-		if (git_index_open_inrepo(&repo->index, repo) < GIT_SUCCESS)
-			return NULL;
+		error = git_index_open_inrepo(&repo->index, repo);
+		if (error < GIT_SUCCESS)
+			return error;
 
-		assert(repo->index);
+		assert(repo->index != NULL);
 	}
 
-	return repo->index;
+	*index_out = repo->index;
+	return GIT_SUCCESS;
 }
 
 git_odb *git_repository_database(git_repository *repo)
diff --git a/src/revwalk.c b/src/revwalk.c
index df8f47a..2237e33 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -220,23 +220,27 @@ static void prepare_walk(git_revwalk *walk)
 	walk->walking = 1;
 }
 
-git_commit *git_revwalk_next(git_revwalk *walk)
+int git_revwalk_next(git_commit **commit, git_revwalk *walk)
 {
 	git_revwalk_commit *next;
 
-	assert(walk);
+	assert(walk && commit);
 
 	if (!walk->walking)
 		prepare_walk(walk);
 
+	*commit = NULL;
+
 	while ((next = walk->next(&walk->iterator)) != NULL) {
-		if (!next->uninteresting)
-			return next->commit_object;
+		if (!next->uninteresting) {
+			*commit = next->commit_object;
+			return GIT_SUCCESS;
+		}
 	}
 
 	/* No commits left to iterate */
 	git_revwalk_reset(walk);
-	return NULL;
+	return GIT_EREVWALKOVER;
 }
 
 void git_revwalk_reset(git_revwalk *walk)
diff --git a/tests/t05-revwalk.c b/tests/t05-revwalk.c
index 06f933f..473ea33 100644
--- a/tests/t05-revwalk.c
+++ b/tests/t05-revwalk.c
@@ -99,7 +99,7 @@ static int test_walk(git_revwalk *walk, git_commit *start_from,
 		result_array[i] = -1;
 
 	i = 0;
-	while ((commit = git_revwalk_next(walk)) != NULL)
+	while (git_revwalk_next(&commit, walk) == GIT_SUCCESS)
 		result_array[i++] = get_commit_index(commit);
 
 	for (i = 0; i < results_count; ++i)