Commit c836c332 by Vicent Marti

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>
parent 4569bfa5
...@@ -24,7 +24,9 @@ static struct { ...@@ -24,7 +24,9 @@ static struct {
{GIT_EINVALIDREFNAME, "The name of the reference is not valid"}, {GIT_EINVALIDREFNAME, "The name of the reference is not valid"},
{GIT_EREFCORRUPTED, "The specified reference has its data corrupted"}, {GIT_EREFCORRUPTED, "The specified reference has its data corrupted"},
{GIT_ETOONESTEDSYMREF, "The specified symbolic reference is too deeply nested"}, {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) const char *git_strerror(int num)
......
...@@ -151,6 +151,9 @@ ...@@ -151,6 +151,9 @@
/** The path is invalid */ /** The path is invalid */
#define GIT_EINVALIDPATH (GIT_ERROR - 19) #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_BEGIN_DECL
/** @} */ /** @} */
GIT_END_DECL GIT_END_DECL
......
...@@ -163,11 +163,14 @@ GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo); ...@@ -163,11 +163,14 @@ GIT_EXTERN(git_odb *) git_repository_database(git_repository *repo);
/** /**
* Get the Index file of a Git repository * 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 * @param repo a repository object
* @return a pointer to the Index object; * @return 0 on success; error code if the index could not be opened
* NULL if the index cannot 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 * Create a new in-memory repository object with
......
...@@ -99,10 +99,13 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, git_commit *commit); ...@@ -99,10 +99,13 @@ GIT_EXTERN(int) git_revwalk_hide(git_revwalk *walk, git_commit *commit);
/** /**
* Get the next commit from the revision traversal. * 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. * @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 * Change the sorting mode when iterating through the
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#include "common.h" #include "common.h"
#include "git2/oid.h" #include "git2/oid.h"
#include "git2/refs.h"
#include "hashtable.h" #include "hashtable.h"
#define GIT_REFS_DIR "refs/" #define GIT_REFS_DIR "refs/"
......
...@@ -374,16 +374,22 @@ void git_repository_free(git_repository *repo) ...@@ -374,16 +374,22 @@ void git_repository_free(git_repository *repo)
free(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 (repo->index == NULL) {
if (git_index_open_inrepo(&repo->index, repo) < GIT_SUCCESS) error = git_index_open_inrepo(&repo->index, repo);
return NULL; 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) git_odb *git_repository_database(git_repository *repo)
......
...@@ -220,23 +220,27 @@ static void prepare_walk(git_revwalk *walk) ...@@ -220,23 +220,27 @@ static void prepare_walk(git_revwalk *walk)
walk->walking = 1; 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; git_revwalk_commit *next;
assert(walk); assert(walk && commit);
if (!walk->walking) if (!walk->walking)
prepare_walk(walk); prepare_walk(walk);
*commit = NULL;
while ((next = walk->next(&walk->iterator)) != NULL) { while ((next = walk->next(&walk->iterator)) != NULL) {
if (!next->uninteresting) if (!next->uninteresting) {
return next->commit_object; *commit = next->commit_object;
return GIT_SUCCESS;
}
} }
/* No commits left to iterate */ /* No commits left to iterate */
git_revwalk_reset(walk); git_revwalk_reset(walk);
return NULL; return GIT_EREVWALKOVER;
} }
void git_revwalk_reset(git_revwalk *walk) void git_revwalk_reset(git_revwalk *walk)
......
...@@ -99,7 +99,7 @@ static int test_walk(git_revwalk *walk, git_commit *start_from, ...@@ -99,7 +99,7 @@ static int test_walk(git_revwalk *walk, git_commit *start_from,
result_array[i] = -1; result_array[i] = -1;
i = 0; i = 0;
while ((commit = git_revwalk_next(walk)) != NULL) while (git_revwalk_next(&commit, walk) == GIT_SUCCESS)
result_array[i++] = get_commit_index(commit); result_array[i++] = get_commit_index(commit);
for (i = 0; i < results_count; ++i) for (i = 0; i < results_count; ++i)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment