From 917f85a1a4616a93fcbba30c82caff9c1c011b83 Mon Sep 17 00:00:00 2001 From: Arthur Schreiber Date: Thu, 9 Oct 2014 14:16:10 +0200 Subject: [PATCH] Extract shared functionality. --- src/merge.c | 104 +++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 59 deletions(-) diff --git a/src/merge.c b/src/merge.c index c7ca97c12..8252f6767 100644 --- a/src/merge.c +++ b/src/merge.c @@ -62,16 +62,14 @@ struct merge_diff_df_data { /* Merge base computation */ -int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]) +int merge_bases_many(git_commit_list **out, git_revwalk **walk_out, git_repository *repo, size_t length, const git_oid input_array[]) { - git_revwalk *walk; + git_revwalk *walk = NULL; git_vector list; git_commit_list *result = NULL; + git_commit_list_node *commit; int error = -1; unsigned int i; - git_commit_list_node *commit; - - assert(out && repo && input_array); if (length < 2) { giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length); @@ -82,104 +80,92 @@ int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const return -1; if (git_revwalk_new(&walk, repo) < 0) - goto cleanup; + goto on_error; for (i = 1; i < length; i++) { commit = git_revwalk__commit_lookup(walk, &input_array[i]); if (commit == NULL) - goto cleanup; + goto on_error; git_vector_insert(&list, commit); } commit = git_revwalk__commit_lookup(walk, &input_array[0]); if (commit == NULL) - goto cleanup; + goto on_error; if (git_merge__bases_many(&result, walk, commit, &list) < 0) - goto cleanup; + goto on_error; if (!result) { giterr_set(GITERR_MERGE, "No merge base found"); error = GIT_ENOTFOUND; - goto cleanup; + goto on_error; } + *out = result; + *walk_out = walk; + + git_vector_free(&list); + return 0; + +on_error: + git_vector_free(&list); + git_revwalk_free(walk); + return error; +} + +int git_merge_base_many(git_oid *out, git_repository *repo, size_t length, const git_oid input_array[]) +{ + git_revwalk *walk; + git_commit_list *result = NULL; + int error = 0; + + assert(out && repo && input_array); + + if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0) + return error; + git_oid_cpy(out, &result->item->oid); - error = 0; - -cleanup: git_commit_list_free(&result); git_revwalk_free(walk); - git_vector_free(&list); - return error; + + return 0; } int git_merge_bases_many(git_oidarray *out, git_repository *repo, size_t length, const git_oid input_array[]) { git_revwalk *walk; - git_vector list; - git_commit_list *current, *result = NULL; - int error = -1; - unsigned int i; - git_commit_list_node *commit; + git_commit_list *list, *result = NULL; + int error = 0; git_array_oid_t array; assert(out && repo && input_array); - if (length < 2) { - giterr_set(GITERR_INVALID, "At least two commits are required to find an ancestor. Provided 'length' was %u.", length); - return -1; - } - - if (git_vector_init(&list, length - 1, NULL) < 0) - return -1; - - if (git_revwalk_new(&walk, repo) < 0) - goto cleanup; - - for (i = 1; i < length; i++) { - commit = git_revwalk__commit_lookup(walk, &input_array[i]); - if (commit == NULL) - goto cleanup; - - git_vector_insert(&list, commit); - } - - commit = git_revwalk__commit_lookup(walk, &input_array[0]); - if (commit == NULL) - goto cleanup; - - if (git_merge__bases_many(&result, walk, commit, &list) < 0) - goto cleanup; - - if (!result) { - giterr_set(GITERR_MERGE, "No merge base found"); - error = GIT_ENOTFOUND; - goto cleanup; - } + if ((error = merge_bases_many(&result, &walk, repo, length, input_array)) < 0) + return error; git_array_init(array); - current = result; - while (current) { + list = result; + while (list) { git_oid *id = git_array_alloc(array); - if (id == NULL) + if (id == NULL) { + error = -1; goto cleanup; + } - git_oid_cpy(id, ¤t->item->oid); - current = current->next; + git_oid_cpy(id, &list->item->oid); + list = list->next; } git_oidarray__from_array(out, &array); - error = 0; - cleanup: git_commit_list_free(&result); git_revwalk_free(walk); - git_vector_free(&list); + return error; }