diff options
Diffstat (limited to 'src/commit.c')
-rw-r--r-- | src/commit.c | 175 |
1 files changed, 128 insertions, 47 deletions
diff --git a/src/commit.c b/src/commit.c index 685c642aa..9d675ac97 100644 --- a/src/commit.c +++ b/src/commit.c @@ -18,6 +18,7 @@ #include "message.h" #include "refs.h" #include "object.h" +#include "oidarray.h" void git_commit__free(void *_commit) { @@ -37,94 +38,143 @@ void git_commit__free(void *_commit) git__free(commit); } -static int git_commit__create_internal( - git_oid *id, +static int git_commit__create_buffer_internal( + git_buf *out, git_repository *repo, - const char *update_ref, const git_signature *author, const git_signature *committer, const char *message_encoding, const char *message, const git_oid *tree, - git_commit_parent_callback parent_cb, - void *parent_payload, - bool validate) + git_array_oid_t *parents) { - git_reference *ref = NULL; - int error = 0, matched_parent = 0; - const git_oid *current_id = NULL; - git_buf commit = GIT_BUF_INIT; size_t i = 0; - git_odb *odb; const git_oid *parent; - assert(id && repo && tree && parent_cb); + assert(out && repo && tree); - if (validate && !git_object__is_valid(repo, tree, GIT_OBJ_TREE)) - return -1; + git_oid__writebuf(out, "tree ", tree); - if (update_ref) { - error = git_reference_lookup_resolved(&ref, repo, update_ref, 10); - if (error < 0 && error != GIT_ENOTFOUND) - return error; + for (i = 0; i < git_array_size(*parents); i++) { + parent = git_array_get(*parents, i); + git_oid__writebuf(out, "parent ", parent); } - giterr_clear(); - if (ref) - current_id = git_reference_target(ref); + git_signature__writebuf(out, "author ", author); + git_signature__writebuf(out, "committer ", committer); + + if (message_encoding != NULL) + git_buf_printf(out, "encoding %s\n", message_encoding); + + git_buf_putc(out, '\n'); - git_oid__writebuf(&commit, "tree ", tree); + if (git_buf_puts(out, message) < 0) + goto on_error; + + return 0; + +on_error: + git_buf_free(out); + return -1; +} +static int validate_tree_and_parents(git_array_oid_t *parents, git_repository *repo, const git_oid *tree, + git_commit_parent_callback parent_cb, void *parent_payload, + const git_oid *current_id, bool validate) +{ + size_t i; + int error; + git_oid *parent_cpy; + const git_oid *parent; + + if (validate && !git_object__is_valid(repo, tree, GIT_OBJ_TREE)) + return -1; + + i = 0; while ((parent = parent_cb(i, parent_payload)) != NULL) { if (validate && !git_object__is_valid(repo, parent, GIT_OBJ_COMMIT)) { error = -1; goto on_error; } - git_oid__writebuf(&commit, "parent ", parent); - if (i == 0 && current_id && git_oid_equal(current_id, parent)) - matched_parent = 1; + parent_cpy = git_array_alloc(*parents); + GITERR_CHECK_ALLOC(parent_cpy); + + git_oid_cpy(parent_cpy, parent); i++; } - if (ref && !matched_parent) { - git_reference_free(ref); - git_buf_free(&commit); + if (current_id && git_oid_cmp(current_id, git_array_get(*parents, 0))) { giterr_set(GITERR_OBJECT, "failed to create commit: current tip is not the first parent"); - return GIT_EMODIFIED; + error = GIT_EMODIFIED; + goto on_error; } - git_signature__writebuf(&commit, "author ", author); - git_signature__writebuf(&commit, "committer ", committer); + return 0; - if (message_encoding != NULL) - git_buf_printf(&commit, "encoding %s\n", message_encoding); +on_error: + git_array_clear(*parents); + return error; +} - git_buf_putc(&commit, '\n'); +static int git_commit__create_internal( + git_oid *id, + git_repository *repo, + const char *update_ref, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_oid *tree, + git_commit_parent_callback parent_cb, + void *parent_payload, + bool validate) +{ + int error; + git_odb *odb; + git_reference *ref = NULL; + git_buf buf = GIT_BUF_INIT; + const git_oid *current_id = NULL; + git_array_oid_t parents = GIT_ARRAY_INIT; - if (git_buf_puts(&commit, message) < 0) - goto on_error; + if (update_ref) { + error = git_reference_lookup_resolved(&ref, repo, update_ref, 10); + if (error < 0 && error != GIT_ENOTFOUND) + return error; + } + giterr_clear(); + + if (ref) + current_id = git_reference_target(ref); + + if ((error = validate_tree_and_parents(&parents, repo, tree, parent_cb, parent_payload, current_id, validate)) < 0) + goto cleanup; + + error = git_commit__create_buffer_internal(&buf, repo, author, committer, + message_encoding, message, tree, + &parents); + + if (error < 0) + goto cleanup; if (git_repository_odb__weakptr(&odb, repo) < 0) - goto on_error; + goto cleanup; - if (git_odb_write(id, odb, commit.ptr, commit.size, GIT_OBJ_COMMIT) < 0) - goto on_error; + if (git_odb_write(id, odb, buf.ptr, buf.size, GIT_OBJ_COMMIT) < 0) + goto cleanup; - git_buf_free(&commit); if (update_ref != NULL) { error = git_reference__update_for_commit( repo, ref, update_ref, id, "commit"); - git_reference_free(ref); - return error; + goto cleanup; } - return 0; - -on_error: - git_buf_free(&commit); - return -1; +cleanup: + git_array_clear(parents); + git_reference_free(ref); + git_buf_free(&buf); + return error; } int git_commit_create_from_callback( @@ -739,3 +789,34 @@ cleanup: git_buf_clear(signed_data); return error; } + +int git_commit_create_buffer(git_buf *out, + git_repository *repo, + const git_signature *author, + const git_signature *committer, + const char *message_encoding, + const char *message, + const git_tree *tree, + size_t parent_count, + const git_commit *parents[]) +{ + int error; + commit_parent_data data = { parent_count, parents, repo }; + git_array_oid_t parents_arr = GIT_ARRAY_INIT; + const git_oid *tree_id; + + assert(tree && git_tree_owner(tree) == repo); + + tree_id = git_tree_id(tree); + + if ((error = validate_tree_and_parents(&parents_arr, repo, tree_id, commit_parent_from_array, &data, NULL, true)) < 0) + return error; + + error = git_commit__create_buffer_internal( + out, repo, author, committer, + message_encoding, message, tree_id, + &parents_arr); + + git_array_clear(parents_arr); + return error; +} |