Commit 6bb7aa1318f7f31a346c71ef81d2b33c6fd41600

Vicent Marti 2010-05-28T01:48:59

Added new error codes. Improved error handling. Signed-off-by: Vicent Marti <tanoku@gmail.com> Signed-off-by: Andreas Ericsson <ae@op5.se>

diff --git a/src/commit.c b/src/commit.c
index e740db6..46c7d0d 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -81,7 +81,7 @@ int git_commit_parse_existing(git_commit *commit)
 		return 0;
 
 	if (git_odb_read(&commit_obj, commit->object.pool->db, &commit->object.id) < 0)
-		return -1;
+		return GIT_ENOTFOUND;
 
 	if (commit_obj.type != GIT_OBJ_COMMIT)
 		goto error_cleanup;
@@ -128,27 +128,27 @@ git_commit *git_commit_lookup(git_revpool *pool, const git_oid *id)
 int git_commit__parse_time(time_t *commit_time, char *buffer, const char *buffer_end)
 {
 	if (memcmp(buffer, "author ", 7) != 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	buffer = memchr(buffer, '\n', buffer_end - buffer);
 	if (buffer == 0 || ++buffer >= buffer_end)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	if (memcmp(buffer, "committer ", 10) != 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	buffer = memchr(buffer, '>', buffer_end - buffer);
 	if (buffer == 0 || ++buffer >= buffer_end)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	*commit_time = strtol(buffer, &buffer, 10);
 
 	if (*commit_time == 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	buffer = memchr(buffer, '\n', buffer_end - buffer);
 	if (buffer == 0 || ++buffer >= buffer_end)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	return (buffer < buffer_end) ? 0 : -1;
 }
@@ -161,16 +161,16 @@ int git_commit__parse_oid(git_oid *oid, char **buffer_out, const char *buffer_en
 	char *buffer = *buffer_out;
 
 	if (buffer + (header_len + sha_len + 1) > buffer_end)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	if (memcmp(buffer, header, header_len) != 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	if (buffer[header_len + sha_len] != '\n')
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	if (git_oid_mkstr(oid, buffer + header_len) < 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	*buffer_out = buffer + (header_len + sha_len + 1);
 
@@ -188,7 +188,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
 		return 0;
 
 	if (git_commit__parse_oid(&oid, &buffer, buffer_end, "tree ") < 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	/*
 	 * TODO: load tree into commit object
@@ -199,7 +199,7 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
 		git_commit *parent;
 
 		if ((parent = git_commit_lookup(commit->object.pool, &oid)) == NULL)
-			return -1;
+			return GIT_ENOTFOUND;
 
 		// Inherit uninteresting flag
 		if (commit->uninteresting)
@@ -209,21 +209,21 @@ int git_commit__parse_buffer(git_commit *commit, void *data, size_t len)
 	}
 
 	if (git_commit__parse_time(&commit->commit_time, buffer, buffer_end) < 0)
-		return -1;
+		return GIT_EOBJCORRUPTED;
 
 	commit->parsed = 1;
 
 	return 0;
 }
 
-void git_commit_list_push_back(git_commit_list *list, git_commit *commit)
+int git_commit_list_push_back(git_commit_list *list, git_commit *commit)
 {
 	git_commit_node *node = NULL;
 
 	node = git__malloc(sizeof(git_commit_list));
 
 	if (node == NULL)
-		return;
+		return GIT_ENOMEM;
 
 	node->commit = commit;
 	node->next = NULL;
@@ -237,16 +237,17 @@ void git_commit_list_push_back(git_commit_list *list, git_commit *commit)
 	}
 
 	list->size++;
+	return 0;
 }
 
-void git_commit_list_push_front(git_commit_list *list, git_commit *commit)
+int git_commit_list_push_front(git_commit_list *list, git_commit *commit)
 {
 	git_commit_node *node = NULL;
 
 	node = git__malloc(sizeof(git_commit_list));
 
 	if (node == NULL)
-		return;
+		return GIT_ENOMEM;
 
 	node->commit = commit;
 	node->next = list->head;
@@ -260,6 +261,7 @@ void git_commit_list_push_front(git_commit_list *list, git_commit *commit)
 	}
 
 	list->size++;
+	return 0;
 }
 
 
diff --git a/src/commit.h b/src/commit.h
index c62b80f..760529d 100644
--- a/src/commit.h
+++ b/src/commit.h
@@ -44,8 +44,8 @@ void git_commit__mark_uninteresting(git_commit *commit);
 int git_commit_parse_existing(git_commit *commit);
 
 
-void git_commit_list_push_back(git_commit_list *list, git_commit *commit);
-void git_commit_list_push_front(git_commit_list *list, git_commit *commit);
+int git_commit_list_push_back(git_commit_list *list, git_commit *commit);
+int git_commit_list_push_front(git_commit_list *list, git_commit *commit);
 
 git_commit *git_commit_list_pop_back(git_commit_list *list);
 git_commit *git_commit_list_pop_front(git_commit_list *list);
diff --git a/src/git/common.h b/src/git/common.h
index 19c6a20..2506dae 100644
--- a/src/git/common.h
+++ b/src/git/common.h
@@ -77,6 +77,12 @@
 /** Consult the OS error information. */
 #define GIT_EOSERR (GIT_ERROR - 4)
 
+/** The specified object is of invalid type */
+#define GIT_EOBJTYPE (GIT_ERROR - 5)
+
+/** The specified object has its data corrupted */
+#define GIT_EOBJCORRUPTED (GIT_ERROR - 6)
+
 GIT_BEGIN_DECL
 
 /** A revision traversal pool. */
diff --git a/src/git/revwalk.h b/src/git/revwalk.h
index 0a902f9..027a51a 100644
--- a/src/git/revwalk.h
+++ b/src/git/revwalk.h
@@ -67,14 +67,14 @@ GIT_EXTERN(void) gitrp_reset(git_revpool *pool);
  * @param pool the pool being used for the traversal.
  * @param commit the commit to start from.
  */
-GIT_EXTERN(void) gitrp_push(git_revpool *pool, git_commit *commit);
+GIT_EXTERN(int) gitrp_push(git_revpool *pool, git_commit *commit);
 
 /**
  * Mark a commit (and its ancestors) uninteresting for the output.
  * @param pool the pool being used for the traversal.
  * @param commit the commit that will be ignored during the traversal
  */
-GIT_EXTERN(void) gitrp_hide(git_revpool *pool, git_commit *commit);
+GIT_EXTERN(int) gitrp_hide(git_revpool *pool, git_commit *commit);
 
 /**
  * Get the next commit from the revision traversal.
diff --git a/src/revwalk.c b/src/revwalk.c
index 200a946..186a08f 100644
--- a/src/revwalk.c
+++ b/src/revwalk.c
@@ -62,17 +62,17 @@ void gitrp_sorting(git_revpool *pool, unsigned int sort_mode)
 	gitrp_reset(pool);
 }
 
-void gitrp_push(git_revpool *pool, git_commit *commit)
+int gitrp_push(git_revpool *pool, git_commit *commit)
 {
 	if (commit == NULL || commit->seen)
-		return;
+		return GIT_ENOTFOUND;
 
 	if (commit->object.pool != pool || pool->walking)
-		return;
+		return GIT_ERROR;
 
 	if (!commit->parsed) {
 		if (git_commit_parse_existing(commit) < 0)
-			return;
+			return GIT_EOBJCORRUPTED;
 	}
 
 	// Sanity check: make sure that if the commit
@@ -81,36 +81,48 @@ void gitrp_push(git_revpool *pool, git_commit *commit)
 	if (commit->uninteresting)
 		git_commit__mark_uninteresting(commit);
 
-	git_commit_list_push_back(&pool->roots, commit);
+	if (git_commit_list_push_back(&pool->roots, commit) < 0)
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
-void gitrp_hide(git_revpool *pool, git_commit *commit)
+int gitrp_hide(git_revpool *pool, git_commit *commit)
 {
 	if (pool->walking)
-		return;
+		return GIT_ERROR;
 
 	git_commit__mark_uninteresting(commit);
-	gitrp_push(pool, commit);
+	return gitrp_push(pool, commit);
 }
 
-void gitrp__enroot(git_revpool *pool, git_commit *commit)
+int gitrp__enroot(git_revpool *pool, git_commit *commit)
 {
+	int error;
 	git_commit_node *parents;
 
 	if (commit->seen)
-		return;
+		return 0;
 
-	if (commit->parsed == 0)
-		git_commit_parse_existing(commit);
+	if (commit->parsed == 0) {
+		if (git_commit_parse_existing(commit))
+			return GIT_EOBJCORRUPTED;
+	}
 
 	commit->seen = 1;
 
 	for (parents = commit->parents.head; parents != NULL; parents = parents->next) {
 		parents->commit->in_degree++;
-		gitrp__enroot(pool, parents->commit);
+
+		error = gitrp__enroot(pool, parents->commit);
+		if (error < 0)
+			return error;
 	}
 
-	git_commit_list_push_back(&pool->iterator, commit);
+	if (git_commit_list_push_back(&pool->iterator, commit))
+		return GIT_ENOMEM;
+
+	return 0;
 }
 
 void gitrp__prepare_walk(git_revpool *pool)
diff --git a/src/revwalk.h b/src/revwalk.h
index bff873e..8929a79 100644
--- a/src/revwalk.h
+++ b/src/revwalk.h
@@ -18,6 +18,6 @@ struct git_revpool {
 };
 
 void gitrp__prepare_walk(git_revpool *pool);
-void gitrp__enroot(git_revpool *pool, git_commit *commit);
+int gitrp__enroot(git_revpool *pool, git_commit *commit);
 
 #endif /* INCLUDE_revwalk_h__ */