Commit eb8117b8 by Carlos Martín Nieto

error-handling: revwalk

parent bf787bd8
...@@ -214,38 +214,43 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo ...@@ -214,38 +214,43 @@ static int commit_quick_parse(git_revwalk *walk, commit_object *commit, git_rawo
} }
commit->parents = alloc_parents(commit, parents); commit->parents = alloc_parents(commit, parents);
if (commit->parents == NULL) GITERR_CHECK_ALLOC(commit->parents);
return GIT_ENOMEM;
buffer = parents_start; buffer = parents_start;
for (i = 0; i < parents; ++i) { for (i = 0; i < parents; ++i) {
git_oid oid; git_oid oid;
if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < GIT_SUCCESS) if (git_oid_fromstr(&oid, (char *)buffer + strlen("parent ")) < 0)
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Parent object is corrupted"); return -1;
commit->parents[i] = commit_lookup(walk, &oid); commit->parents[i] = commit_lookup(walk, &oid);
if (commit->parents[i] == NULL) if (commit->parents[i] == NULL)
return GIT_ENOMEM; return -1;
buffer += parent_len; buffer += parent_len;
} }
commit->out_degree = (unsigned short)parents; commit->out_degree = (unsigned short)parents;
if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) if ((buffer = memchr(buffer, '\n', buffer_end - buffer)) == NULL) {
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Object is corrupted"); giterr_set(GITERR_ODB, "Failed to parse commit. Object is corrupted");
return -1;
}
buffer = memchr(buffer, '>', buffer_end - buffer); buffer = memchr(buffer, '>', buffer_end - buffer);
if (buffer == NULL) if (buffer == NULL) {
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Can't find author"); giterr_set(GITERR_ODB, "Failed to parse commit. Can't find author");
return -1;
}
if (git__strtol32(&commit_time, (char *)buffer + 2, NULL, 10) < GIT_SUCCESS) if (git__strtol32(&commit_time, (char *)buffer + 2, NULL, 10) < 0) {
return git__throw(GIT_EOBJCORRUPTED, "Failed to parse commit. Can't parse commit time"); giterr_set(GITERR_ODB, "Failed to parse commit. Can't parse commit time");
return -1;
}
commit->time = (time_t)commit_time; commit->time = (time_t)commit_time;
commit->parsed = 1; commit->parsed = 1;
return GIT_SUCCESS; return 0;
} }
static int commit_parse(git_revwalk *walk, commit_object *commit) static int commit_parse(git_revwalk *walk, commit_object *commit)
...@@ -254,19 +259,20 @@ static int commit_parse(git_revwalk *walk, commit_object *commit) ...@@ -254,19 +259,20 @@ static int commit_parse(git_revwalk *walk, commit_object *commit)
int error; int error;
if (commit->parsed) if (commit->parsed)
return GIT_SUCCESS; return 0;
if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < GIT_SUCCESS) if ((error = git_odb_read(&obj, walk->odb, &commit->oid)) < 0)
return git__rethrow(error, "Failed to parse commit. Can't read object"); return -1;
if (obj->raw.type != GIT_OBJ_COMMIT) { if (obj->raw.type != GIT_OBJ_COMMIT) {
git_odb_object_free(obj); git_odb_object_free(obj);
return git__throw(GIT_EOBJTYPE, "Failed to parse commit. Object is no commit object"); giterr_set(GITERR_INVALID, "Failed to parse commit. Object is no commit object");
return -1;
} }
error = commit_quick_parse(walk, commit, &obj->raw); error = commit_quick_parse(walk, commit, &obj->raw);
git_odb_object_free(obj); git_odb_object_free(obj);
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to parse commit"); return error;
} }
static int interesting(git_pqueue *list) static int interesting(git_pqueue *list)
...@@ -368,18 +374,16 @@ int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *tw ...@@ -368,18 +374,16 @@ int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *tw
{ {
git_revwalk *walk; git_revwalk *walk;
git_vector list; git_vector list;
commit_list *result; commit_list *result = NULL;
commit_object *commit; commit_object *commit;
void *contents[1]; void *contents[1];
int error;
error = git_revwalk_new(&walk, repo); if (git_revwalk_new(&walk, repo) < 0)
if (error < GIT_SUCCESS) return -1;
return error;
commit = commit_lookup(walk, two); commit = commit_lookup(walk, two);
if (commit == NULL) if (commit == NULL)
goto cleanup; goto on_error;
/* This is just one value, so we can do it on the stack */ /* This is just one value, so we can do it on the stack */
memset(&list, 0x0, sizeof(git_vector)); memset(&list, 0x0, sizeof(git_vector));
...@@ -389,22 +393,25 @@ int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *tw ...@@ -389,22 +393,25 @@ int git_merge_base(git_oid *out, git_repository *repo, git_oid *one, git_oid *tw
commit = commit_lookup(walk, one); commit = commit_lookup(walk, one);
if (commit == NULL) if (commit == NULL)
goto cleanup; goto on_error;
error = merge_bases_many(&result, walk, commit, &list); if (merge_bases_many(&result, walk, commit, &list) < 0)
if (error < GIT_SUCCESS) goto on_error;
return error;
if (result == NULL) if (!result) {
git_revwalk_free(walk);
return GIT_ENOTFOUND; return GIT_ENOTFOUND;
}
git_oid_cpy(out, &result->item->oid); git_oid_cpy(out, &result->item->oid);
commit_list_free(&result); commit_list_free(&result);
cleanup:
git_revwalk_free(walk); git_revwalk_free(walk);
return GIT_SUCCESS; return 0;
on_error:
git_revwalk_free(walk);
return -1;
} }
static void mark_uninteresting(commit_object *commit) static void mark_uninteresting(commit_object *commit)
...@@ -425,18 +432,16 @@ static void mark_uninteresting(commit_object *commit) ...@@ -425,18 +432,16 @@ static void mark_uninteresting(commit_object *commit)
static int process_commit(git_revwalk *walk, commit_object *commit, int hide) static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
{ {
int error;
if (hide) if (hide)
mark_uninteresting(commit); mark_uninteresting(commit);
if (commit->seen) if (commit->seen)
return GIT_SUCCESS; return 0;
commit->seen = 1; commit->seen = 1;
if ((error = commit_parse(walk, commit)) < GIT_SUCCESS) if (commit_parse(walk, commit) < 0)
return git__rethrow(error, "Failed to process commit"); return -1;
return walk->enqueue(walk, commit); return walk->enqueue(walk, commit);
} }
...@@ -444,13 +449,13 @@ static int process_commit(git_revwalk *walk, commit_object *commit, int hide) ...@@ -444,13 +449,13 @@ static int process_commit(git_revwalk *walk, commit_object *commit, int hide)
static int process_commit_parents(git_revwalk *walk, commit_object *commit) static int process_commit_parents(git_revwalk *walk, commit_object *commit)
{ {
unsigned short i; unsigned short i;
int error = GIT_SUCCESS;
for (i = 0; i < commit->out_degree && error == GIT_SUCCESS; ++i) { for (i = 0; i < commit->out_degree; ++i) {
error = process_commit(walk, commit->parents[i], commit->uninteresting); if (process_commit(walk, commit->parents[i], commit->uninteresting) < 0)
return -1;
} }
return error == GIT_SUCCESS ? GIT_SUCCESS : git__rethrow(error, "Failed to process commit parents"); return 0;
} }
static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting) static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
...@@ -459,17 +464,17 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting) ...@@ -459,17 +464,17 @@ static int push_commit(git_revwalk *walk, const git_oid *oid, int uninteresting)
commit = commit_lookup(walk, oid); commit = commit_lookup(walk, oid);
if (commit == NULL) if (commit == NULL)
return git__throw(GIT_ENOTFOUND, "Failed to push commit. Object not found"); return -1;
commit->uninteresting = uninteresting; commit->uninteresting = uninteresting;
if (walk->one == NULL && !uninteresting) { if (walk->one == NULL && !uninteresting) {
walk->one = commit; walk->one = commit;
} else { } else {
if (git_vector_insert(&walk->twos, commit) < GIT_SUCCESS) if (git_vector_insert(&walk->twos, commit) < 0)
return GIT_ENOMEM; return -1;
} }
return GIT_SUCCESS; return 0;
} }
int git_revwalk_push(git_revwalk *walk, const git_oid *oid) int git_revwalk_push(git_revwalk *walk, const git_oid *oid)
...@@ -490,15 +495,17 @@ static int push_ref(git_revwalk *walk, const char *refname, int hide) ...@@ -490,15 +495,17 @@ static int push_ref(git_revwalk *walk, const char *refname, int hide)
git_reference *ref, *resolved; git_reference *ref, *resolved;
int error; int error;
error = git_reference_lookup(&ref, walk->repo, refname); if (git_reference_lookup(&ref, walk->repo, refname) < 0)
if (error < GIT_SUCCESS) return -1;
return error;
error = git_reference_resolve(&resolved, ref); error = git_reference_resolve(&resolved, ref);
git_reference_free(ref); git_reference_free(ref);
if (error < GIT_SUCCESS) if (error < 0)
return error; return -1;
error = push_commit(walk, git_reference_oid(resolved), hide); error = push_commit(walk, git_reference_oid(resolved), hide);
git_reference_free(resolved); git_reference_free(resolved);
return error; return error;
} }
...@@ -515,14 +522,13 @@ static int push_glob_cb(const char *refname, void *data_) ...@@ -515,14 +522,13 @@ static int push_glob_cb(const char *refname, void *data_)
if (!git__fnmatch(data->glob, refname, 0)) if (!git__fnmatch(data->glob, refname, 0))
return push_ref(data->walk, refname, data->hide); return push_ref(data->walk, refname, data->hide);
return GIT_SUCCESS; return 0;
} }
static int push_glob(git_revwalk *walk, const char *glob, int hide) static int push_glob(git_revwalk *walk, const char *glob, int hide)
{ {
git_buf buf = GIT_BUF_INIT; git_buf buf = GIT_BUF_INIT;
struct push_cb_data data; struct push_cb_data data;
int error;
regex_t preg; regex_t preg;
assert(walk && glob); assert(walk && glob);
...@@ -537,28 +543,32 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide) ...@@ -537,28 +543,32 @@ static int push_glob(git_revwalk *walk, const char *glob, int hide)
/* If no '?', '*' or '[' exist, we append '/ *' to the glob */ /* If no '?', '*' or '[' exist, we append '/ *' to the glob */
memset(&preg, 0x0, sizeof(regex_t)); memset(&preg, 0x0, sizeof(regex_t));
if (regcomp(&preg, "[?*[]", REG_EXTENDED)) { if (regcomp(&preg, "[?*[]", REG_EXTENDED)) {
error = git__throw(GIT_EOSERR, "Regex failed to compile"); giterr_set(GITERR_OS, "Regex failed to compile");
goto cleanup; git_buf_free(&buf);
return -1;
} }
if (regexec(&preg, glob, 0, NULL, 0)) if (regexec(&preg, glob, 0, NULL, 0))
git_buf_puts(&buf, "/*"); git_buf_puts(&buf, "/*");
if (git_buf_oom(&buf)) { if (git_buf_oom(&buf))
error = GIT_ENOMEM; goto on_error;
goto cleanup;
}
data.walk = walk; data.walk = walk;
data.glob = git_buf_cstr(&buf); data.glob = git_buf_cstr(&buf);
data.hide = hide; data.hide = hide;
error = git_reference_foreach(walk->repo, GIT_REF_LISTALL, push_glob_cb, &data); if (git_reference_foreach(walk->repo, GIT_REF_LISTALL, push_glob_cb, &data) < 0)
goto on_error;
cleanup:
regfree(&preg); regfree(&preg);
git_buf_free(&buf); git_buf_free(&buf);
return error; return 0;
on_error:
regfree(&preg);
git_buf_free(&buf);
return -1;
} }
int git_revwalk_push_glob(git_revwalk *walk, const char *glob) int git_revwalk_push_glob(git_revwalk *walk, const char *glob)
...@@ -613,16 +623,16 @@ static int revwalk_next_timesort(commit_object **object_out, git_revwalk *walk) ...@@ -613,16 +623,16 @@ static int revwalk_next_timesort(commit_object **object_out, git_revwalk *walk)
commit_object *next; commit_object *next;
while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) { while ((next = git_pqueue_pop(&walk->iterator_time)) != NULL) {
if ((error = process_commit_parents(walk, next)) < GIT_SUCCESS) if ((error = process_commit_parents(walk, next)) < 0)
return git__rethrow(error, "Failed to load next revision"); return -1;
if (!next->uninteresting) { if (!next->uninteresting) {
*object_out = next; *object_out = next;
return GIT_SUCCESS; return 0;
} }
} }
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision"); return GIT_EREVWALKOVER;
} }
static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk) static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk)
...@@ -631,16 +641,16 @@ static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk) ...@@ -631,16 +641,16 @@ static int revwalk_next_unsorted(commit_object **object_out, git_revwalk *walk)
commit_object *next; commit_object *next;
while ((next = commit_list_pop(&walk->iterator_rand)) != NULL) { while ((next = commit_list_pop(&walk->iterator_rand)) != NULL) {
if ((error = process_commit_parents(walk, next)) < GIT_SUCCESS) if ((error = process_commit_parents(walk, next)) < 0)
return git__rethrow(error, "Failed to load next revision"); return -1;
if (!next->uninteresting) { if (!next->uninteresting) {
*object_out = next; *object_out = next;
return GIT_SUCCESS; return 0;
} }
} }
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision"); return GIT_EREVWALKOVER;
} }
static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk) static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk)
...@@ -651,7 +661,7 @@ static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk) ...@@ -651,7 +661,7 @@ static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk)
for (;;) { for (;;) {
next = commit_list_pop(&walk->iterator_topo); next = commit_list_pop(&walk->iterator_topo);
if (next == NULL) if (next == NULL)
return git__throw(GIT_EREVWALKOVER, "Failed to load next revision"); return GIT_EREVWALKOVER;
if (next->in_degree > 0) { if (next->in_degree > 0) {
next->topo_delay = 1; next->topo_delay = 1;
...@@ -664,19 +674,19 @@ static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk) ...@@ -664,19 +674,19 @@ static int revwalk_next_toposort(commit_object **object_out, git_revwalk *walk)
if (--parent->in_degree == 0 && parent->topo_delay) { if (--parent->in_degree == 0 && parent->topo_delay) {
parent->topo_delay = 0; parent->topo_delay = 0;
if (commit_list_insert(parent, &walk->iterator_topo) == NULL) if (commit_list_insert(parent, &walk->iterator_topo) == NULL)
return GIT_ENOMEM; return -1;
} }
} }
*object_out = next; *object_out = next;
return GIT_SUCCESS; return 0;
} }
} }
static int revwalk_next_reverse(commit_object **object_out, git_revwalk *walk) static int revwalk_next_reverse(commit_object **object_out, git_revwalk *walk)
{ {
*object_out = commit_list_pop(&walk->iterator_reverse); *object_out = commit_list_pop(&walk->iterator_reverse);
return *object_out ? GIT_SUCCESS : GIT_EREVWALKOVER; return *object_out ? 0 : GIT_EREVWALKOVER;
} }
...@@ -688,36 +698,33 @@ static int prepare_walk(git_revwalk *walk) ...@@ -688,36 +698,33 @@ static int prepare_walk(git_revwalk *walk)
commit_list *bases = NULL; commit_list *bases = NULL;
/* first figure out what the merge bases are */ /* first figure out what the merge bases are */
error = merge_bases_many(&bases, walk, walk->one, &walk->twos); if (merge_bases_many(&bases, walk, walk->one, &walk->twos) < 0)
if (error < GIT_SUCCESS) return -1;
return error;
commit_list_free(&bases); commit_list_free(&bases);
error = process_commit(walk, walk->one, walk->one->uninteresting); if (process_commit(walk, walk->one, walk->one->uninteresting) < 0)
if (error < GIT_SUCCESS) return -1;
return error;
git_vector_foreach(&walk->twos, i, two) { git_vector_foreach(&walk->twos, i, two) {
error = process_commit(walk, two, two->uninteresting); if (process_commit(walk, two, two->uninteresting) < 0)
if (error < GIT_SUCCESS) return -1;
return error;
} }
if (walk->sorting & GIT_SORT_TOPOLOGICAL) { if (walk->sorting & GIT_SORT_TOPOLOGICAL) {
unsigned short i; unsigned short i;
while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS) { while ((error = walk->get_next(&next, walk)) == 0) {
for (i = 0; i < next->out_degree; ++i) { for (i = 0; i < next->out_degree; ++i) {
commit_object *parent = next->parents[i]; commit_object *parent = next->parents[i];
parent->in_degree++; parent->in_degree++;
} }
if (commit_list_insert(next, &walk->iterator_topo) == NULL) if (commit_list_insert(next, &walk->iterator_topo) == NULL)
return GIT_ENOMEM; return -1;
} }
if (error != GIT_EREVWALKOVER) if (error != GIT_EREVWALKOVER)
return git__rethrow(error, "Failed to prepare revision walk"); return -1;
walk->get_next = &revwalk_next_toposort; walk->get_next = &revwalk_next_toposort;
} }
...@@ -726,16 +733,16 @@ static int prepare_walk(git_revwalk *walk) ...@@ -726,16 +733,16 @@ static int prepare_walk(git_revwalk *walk)
while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS) while ((error = walk->get_next(&next, walk)) == GIT_SUCCESS)
if (commit_list_insert(next, &walk->iterator_reverse) == NULL) if (commit_list_insert(next, &walk->iterator_reverse) == NULL)
return GIT_ENOMEM; return -1;
if (error != GIT_EREVWALKOVER) if (error != GIT_EREVWALKOVER)
return git__rethrow(error, "Failed to prepare revision walk"); return -1;
walk->get_next = &revwalk_next_reverse; walk->get_next = &revwalk_next_reverse;
} }
walk->walking = 1; walk->walking = 1;
return GIT_SUCCESS; return 0;
} }
...@@ -744,12 +751,10 @@ static int prepare_walk(git_revwalk *walk) ...@@ -744,12 +751,10 @@ static int prepare_walk(git_revwalk *walk)
int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
{ {
int error;
git_revwalk *walk; git_revwalk *walk;
walk = git__malloc(sizeof(git_revwalk)); walk = git__malloc(sizeof(git_revwalk));
if (walk == NULL) GITERR_CHECK_ALLOC(walk);
return GIT_ENOMEM;
memset(walk, 0x0, sizeof(git_revwalk)); memset(walk, 0x0, sizeof(git_revwalk));
...@@ -759,7 +764,7 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) ...@@ -759,7 +764,7 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
if (walk->commits == NULL) { if (walk->commits == NULL) {
git__free(walk); git__free(walk);
return GIT_ENOMEM; return -1;
} }
git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp); git_pqueue_init(&walk->iterator_time, 8, commit_time_cmp);
...@@ -772,14 +777,13 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) ...@@ -772,14 +777,13 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo)
walk->repo = repo; walk->repo = repo;
error = git_repository_odb(&walk->odb, repo); if (git_repository_odb(&walk->odb, repo) < 0) {
if (error < GIT_SUCCESS) {
git_revwalk_free(walk); git_revwalk_free(walk);
return error; return -1;
} }
*revwalk_out = walk; *revwalk_out = walk;
return GIT_SUCCESS; return 0;
} }
void git_revwalk_free(git_revwalk *walk) void git_revwalk_free(git_revwalk *walk)
...@@ -844,8 +848,8 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk) ...@@ -844,8 +848,8 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
assert(walk && oid); assert(walk && oid);
if (!walk->walking) { if (!walk->walking) {
if ((error = prepare_walk(walk)) < GIT_SUCCESS) if ((error = prepare_walk(walk)) < 0)
return git__rethrow(error, "Failed to load next revision"); return -1;
} }
error = walk->get_next(&next, walk); error = walk->get_next(&next, walk);
...@@ -855,11 +859,11 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk) ...@@ -855,11 +859,11 @@ int git_revwalk_next(git_oid *oid, git_revwalk *walk)
return GIT_EREVWALKOVER; return GIT_EREVWALKOVER;
} }
if (error < GIT_SUCCESS) if (error < 0)
return git__rethrow(error, "Failed to load next revision"); return -1;
git_oid_cpy(oid, &next->oid); git_oid_cpy(oid, &next->oid);
return GIT_SUCCESS; return 0;
} }
void git_revwalk_reset(git_revwalk *walk) void git_revwalk_reset(git_revwalk *walk)
......
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