Skip to content

Commit d3bdf33

Browse files
committed
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.
1 parent 0a79012 commit d3bdf33

File tree

4 files changed

+349
-35
lines changed

4 files changed

+349
-35
lines changed

include/git2/commit.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,43 @@ GIT_EXTERN(int) git_commit_create_with_signature(
502502
*/
503503
GIT_EXTERN(int) git_commit_dup(git_commit **out, git_commit *source);
504504

505+
/**
506+
* Commit creation callback: used when a function is going to create
507+
* commits (for example, in `git_rebase_commit`) to allow callers to
508+
* override the commit creation behavior. For example, users may
509+
* wish to sign commits by providing this information to
510+
* `git_commit_create_buffer`, signing that buffer, then calling
511+
* `git_commit_create_with_signature`. The resultant commit id
512+
* should be set in the `out` object id parameter.
513+
*
514+
* @param out pointer that this callback will populate with the object
515+
* id of the commit that is created
516+
* @param author the author name and time of the commit
517+
* @param committer the committer name and time of the commit
518+
* @param message_encoding the encoding of the given message, or NULL
519+
* to assume UTF8
520+
* @param message the commit message
521+
* @param tree the tree to be committed
522+
* @param parent_count the number of parents for this commit
523+
* @param parents the commit parents
524+
* @param payload the payload pointer in the rebase options
525+
* @return 0 if this callback has created the commit and populated the out
526+
* parameter, GIT_PASSTHROUGH if the callback has not created a
527+
* commit and wants the calling function to create the commit as
528+
* if no callback had been specified, any other value to stop
529+
* and return a failure
530+
*/
531+
typedef int (*git_commit_create_cb)(
532+
git_oid *out,
533+
const git_signature *author,
534+
const git_signature *committer,
535+
const char *message_encoding,
536+
const char *message,
537+
const git_tree *tree,
538+
size_t parent_count,
539+
const git_commit *parents[],
540+
void *payload);
541+
505542
/**
506543
* Commit signing callback.
507544
*

include/git2/rebase.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,27 @@ typedef struct {
7474
*/
7575
git_checkout_options checkout_options;
7676

77+
/**
78+
* Optional callback that allows users to override commit
79+
* creation in `git_rebase_commit`. If specified, users can
80+
* create their own commit and provide the commit ID, which
81+
* may be useful for signing commits or otherwise customizing
82+
* the commit creation.
83+
*
84+
* If this callback returns `GIT_PASSTHROUGH`, then
85+
* `git_rebase_commit` will continue to create the commit.
86+
*/
87+
git_commit_create_cb commit_create_cb;
88+
7789
/**
7890
* If provided, this will be called with the commit content, allowing
7991
* a signature to be added to the rebase commit. Can be skipped with
8092
* GIT_PASSTHROUGH. If GIT_PASSTHROUGH is returned, a commit will be made
8193
* without a signature.
8294
* This field is only used when performing git_rebase_commit.
95+
*
96+
* This callback is not invoked if a `git_commit_create_cb` is
97+
* specified.
8398
*/
8499
git_commit_signing_cb signing_cb;
85100

src/rebase.c

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,52 @@ int git_rebase_inmemory_index(
943943
return 0;
944944
}
945945

946+
static int create_signed(
947+
git_oid *out,
948+
git_rebase *rebase,
949+
const git_signature *author,
950+
const git_signature *committer,
951+
const char *message_encoding,
952+
const char *message,
953+
git_tree *tree,
954+
size_t parent_count,
955+
const git_commit **parents)
956+
{
957+
git_buf commit_content = GIT_BUF_INIT,
958+
commit_signature = GIT_BUF_INIT,
959+
signature_field = GIT_BUF_INIT;
960+
int error;
961+
962+
git_error_clear();
963+
964+
if ((error = git_commit_create_buffer(&commit_content,
965+
rebase->repo, author, committer, message_encoding,
966+
message, tree, parent_count, parents)) < 0)
967+
goto done;
968+
969+
error = rebase->options.signing_cb(&commit_signature,
970+
&signature_field, commit_content.ptr,
971+
rebase->options.payload);
972+
973+
if (error) {
974+
if (error != GIT_PASSTHROUGH)
975+
git_error_set_after_callback_function(error, "signing_cb");
976+
977+
goto done;
978+
}
979+
980+
error = git_commit_create_with_signature(out, rebase->repo,
981+
commit_content.ptr,
982+
commit_signature.size > 0 ? commit_signature.ptr : NULL,
983+
signature_field.size > 0 ? signature_field.ptr : NULL);
984+
985+
done:
986+
git_buf_dispose(&commit_signature);
987+
git_buf_dispose(&signature_field);
988+
git_buf_dispose(&commit_content);
989+
return error;
990+
}
991+
946992
static int rebase_commit__create(
947993
git_commit **out,
948994
git_rebase *rebase,
@@ -957,10 +1003,6 @@ static int rebase_commit__create(
9571003
git_commit *current_commit = NULL, *commit = NULL;
9581004
git_tree *parent_tree = NULL, *tree = NULL;
9591005
git_oid tree_id, commit_id;
960-
git_buf commit_content = GIT_BUF_INIT, commit_signature = GIT_BUF_INIT,
961-
signature_field = GIT_BUF_INIT;
962-
const char *signature_field_string = NULL,
963-
*commit_signature_string = NULL;
9641006
int error;
9651007

9661008
operation = git_array_get(rebase->operations, rebase->current);
@@ -991,37 +1033,29 @@ static int rebase_commit__create(
9911033
message = git_commit_message(current_commit);
9921034
}
9931035

994-
if ((error = git_commit_create_buffer(&commit_content, rebase->repo, author, committer,
995-
message_encoding, message, tree, 1, (const git_commit **)&parent_commit)) < 0)
996-
goto done;
997-
998-
if (rebase->options.signing_cb) {
999-
git_error_clear();
1000-
error = git_error_set_after_callback_function(rebase->options.signing_cb(
1001-
&commit_signature, &signature_field, git_buf_cstr(&commit_content),
1002-
rebase->options.payload), "commit signing_cb failed");
1003-
if (error == GIT_PASSTHROUGH) {
1004-
git_buf_dispose(&commit_signature);
1005-
git_buf_dispose(&signature_field);
1006-
git_error_clear();
1007-
error = GIT_OK;
1008-
} else if (error < 0)
1009-
goto done;
1010-
}
1011-
1012-
if (git_buf_is_allocated(&commit_signature)) {
1013-
GIT_ASSERT(git_buf_contains_nul(&commit_signature));
1014-
commit_signature_string = git_buf_cstr(&commit_signature);
1036+
git_error_clear();
1037+
error = GIT_PASSTHROUGH;
1038+
1039+
if (rebase->options.commit_create_cb) {
1040+
error = rebase->options.commit_create_cb(&commit_id,
1041+
author, committer, message_encoding, message,
1042+
tree, 1, (const git_commit **)&parent_commit,
1043+
rebase->options.payload);
1044+
1045+
git_error_set_after_callback_function(error,
1046+
"commit_create_cb");
1047+
} else if (rebase->options.signing_cb) {
1048+
error = create_signed(&commit_id, rebase, author,
1049+
committer, message_encoding, message, tree,
1050+
1, (const git_commit **)&parent_commit);
10151051
}
10161052

1017-
if (git_buf_is_allocated(&signature_field)) {
1018-
GIT_ASSERT(git_buf_contains_nul(&signature_field));
1019-
signature_field_string = git_buf_cstr(&signature_field);
1020-
}
1053+
if (error == GIT_PASSTHROUGH)
1054+
error = git_commit_create(&commit_id, rebase->repo, NULL,
1055+
author, committer, message_encoding, message,
1056+
tree, 1, (const git_commit **)&parent_commit);
10211057

1022-
if ((error = git_commit_create_with_signature(&commit_id, rebase->repo,
1023-
git_buf_cstr(&commit_content), commit_signature_string,
1024-
signature_field_string)))
1058+
if (error)
10251059
goto done;
10261060

10271061
if ((error = git_commit_lookup(&commit, rebase->repo, &commit_id)) < 0)
@@ -1033,9 +1067,6 @@ static int rebase_commit__create(
10331067
if (error < 0)
10341068
git_commit_free(commit);
10351069

1036-
git_buf_dispose(&commit_signature);
1037-
git_buf_dispose(&signature_field);
1038-
git_buf_dispose(&commit_content);
10391070
git_commit_free(current_commit);
10401071
git_tree_free(parent_tree);
10411072
git_tree_free(tree);

0 commit comments

Comments
 (0)