summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEdward Thomson <ethomson@edwardthomson.com>2021-08-29 09:36:01 -0400
committerEdward Thomson <ethomson@edwardthomson.com>2021-08-29 10:16:29 -0400
commitd3bdf33b58f16939a4fd43ab541dcd2ee535b6a3 (patch)
tree3e4016f2bbaa68a370a928f0524bf3c5845d6025 /src
parent0a79012e9df33db31046c653ab04c69eaeed200a (diff)
downloadlibgit2-d3bdf33b58f16939a4fd43ab541dcd2ee535b6a3.tar.gz
rebase: introduce git_commit_create_cb
Introduce a new mechanism for `git_rebase_commit` for callers to customize the experience. Instead of assuming that we produce the commit for them, provide a commit creation callback that allows callers to produce the commit themselves and return the resulting commit id.
Diffstat (limited to 'src')
-rw-r--r--src/rebase.c101
1 files changed, 66 insertions, 35 deletions
diff --git a/src/rebase.c b/src/rebase.c
index bf6581a1b..ddd82e910 100644
--- a/src/rebase.c
+++ b/src/rebase.c
@@ -943,6 +943,52 @@ int git_rebase_inmemory_index(
return 0;
}
+static int create_signed(
+ git_oid *out,
+ git_rebase *rebase,
+ const git_signature *author,
+ const git_signature *committer,
+ const char *message_encoding,
+ const char *message,
+ git_tree *tree,
+ size_t parent_count,
+ const git_commit **parents)
+{
+ git_buf commit_content = GIT_BUF_INIT,
+ commit_signature = GIT_BUF_INIT,
+ signature_field = GIT_BUF_INIT;
+ int error;
+
+ git_error_clear();
+
+ if ((error = git_commit_create_buffer(&commit_content,
+ rebase->repo, author, committer, message_encoding,
+ message, tree, parent_count, parents)) < 0)
+ goto done;
+
+ error = rebase->options.signing_cb(&commit_signature,
+ &signature_field, commit_content.ptr,
+ rebase->options.payload);
+
+ if (error) {
+ if (error != GIT_PASSTHROUGH)
+ git_error_set_after_callback_function(error, "signing_cb");
+
+ goto done;
+ }
+
+ error = git_commit_create_with_signature(out, rebase->repo,
+ commit_content.ptr,
+ commit_signature.size > 0 ? commit_signature.ptr : NULL,
+ signature_field.size > 0 ? signature_field.ptr : NULL);
+
+done:
+ git_buf_dispose(&commit_signature);
+ git_buf_dispose(&signature_field);
+ git_buf_dispose(&commit_content);
+ return error;
+}
+
static int rebase_commit__create(
git_commit **out,
git_rebase *rebase,
@@ -957,10 +1003,6 @@ static int rebase_commit__create(
git_commit *current_commit = NULL, *commit = NULL;
git_tree *parent_tree = NULL, *tree = NULL;
git_oid tree_id, commit_id;
- git_buf commit_content = GIT_BUF_INIT, commit_signature = GIT_BUF_INIT,
- signature_field = GIT_BUF_INIT;
- const char *signature_field_string = NULL,
- *commit_signature_string = NULL;
int error;
operation = git_array_get(rebase->operations, rebase->current);
@@ -991,37 +1033,29 @@ static int rebase_commit__create(
message = git_commit_message(current_commit);
}
- if ((error = git_commit_create_buffer(&commit_content, rebase->repo, author, committer,
- message_encoding, message, tree, 1, (const git_commit **)&parent_commit)) < 0)
- goto done;
-
- if (rebase->options.signing_cb) {
- git_error_clear();
- error = git_error_set_after_callback_function(rebase->options.signing_cb(
- &commit_signature, &signature_field, git_buf_cstr(&commit_content),
- rebase->options.payload), "commit signing_cb failed");
- if (error == GIT_PASSTHROUGH) {
- git_buf_dispose(&commit_signature);
- git_buf_dispose(&signature_field);
- git_error_clear();
- error = GIT_OK;
- } else if (error < 0)
- goto done;
- }
-
- if (git_buf_is_allocated(&commit_signature)) {
- GIT_ASSERT(git_buf_contains_nul(&commit_signature));
- commit_signature_string = git_buf_cstr(&commit_signature);
+ git_error_clear();
+ error = GIT_PASSTHROUGH;
+
+ if (rebase->options.commit_create_cb) {
+ error = rebase->options.commit_create_cb(&commit_id,
+ author, committer, message_encoding, message,
+ tree, 1, (const git_commit **)&parent_commit,
+ rebase->options.payload);
+
+ git_error_set_after_callback_function(error,
+ "commit_create_cb");
+ } else if (rebase->options.signing_cb) {
+ error = create_signed(&commit_id, rebase, author,
+ committer, message_encoding, message, tree,
+ 1, (const git_commit **)&parent_commit);
}
- if (git_buf_is_allocated(&signature_field)) {
- GIT_ASSERT(git_buf_contains_nul(&signature_field));
- signature_field_string = git_buf_cstr(&signature_field);
- }
+ if (error == GIT_PASSTHROUGH)
+ error = git_commit_create(&commit_id, rebase->repo, NULL,
+ author, committer, message_encoding, message,
+ tree, 1, (const git_commit **)&parent_commit);
- if ((error = git_commit_create_with_signature(&commit_id, rebase->repo,
- git_buf_cstr(&commit_content), commit_signature_string,
- signature_field_string)))
+ if (error)
goto done;
if ((error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0)
@@ -1033,9 +1067,6 @@ done:
if (error < 0)
git_commit_free(commit);
- git_buf_dispose(&commit_signature);
- git_buf_dispose(&signature_field);
- git_buf_dispose(&commit_content);
git_commit_free(current_commit);
git_tree_free(parent_tree);
git_tree_free(tree);