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>
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
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)