Skip to content

Commit 7594710

Browse files
committed
commit: git_commit_create_with_signature should support null signature
If provided with a null signature, skip adding the signature header and create the commit anyway.
1 parent 3b8a6f4 commit 7594710

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

include/git2/commit.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,8 @@ GIT_EXTERN(int) git_commit_create_buffer(
480480
*
481481
* @param out the resulting commit id
482482
* @param commit_content the content of the unsigned commit object
483-
* @param signature the signature to add to the commit
483+
* @param signature the signature to add to the commit. Leave `NULL`
484+
* to create a commit without adding a signature field.
484485
* @param signature_field which header field should contain this
485486
* signature. Leave `NULL` for the default of "gpgsig"
486487
* @return 0 or an error code

src/commit.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ static int git_commit__create_buffer_internal(
8080
}
8181

8282
static int validate_tree_and_parents(git_array_oid_t *parents, git_repository *repo, const git_oid *tree,
83-
git_commit_parent_callback parent_cb, void *parent_payload,
84-
const git_oid *current_id, bool validate)
83+
git_commit_parent_callback parent_cb, void *parent_payload,
84+
const git_oid *current_id, bool validate)
8585
{
8686
size_t i;
8787
int error;
@@ -152,8 +152,8 @@ static int git_commit__create_internal(
152152
goto cleanup;
153153

154154
error = git_commit__create_buffer_internal(&buf, author, committer,
155-
message_encoding, message, tree,
156-
&parents);
155+
message_encoding, message, tree,
156+
&parents);
157157

158158
if (error < 0)
159159
goto cleanup;
@@ -582,7 +582,7 @@ const char *git_commit_body(git_commit *commit)
582582
break;
583583

584584
if (*msg)
585-
commit->body = git__strndup(msg, end - msg + 1);
585+
commit->body = git__strndup(msg, end - msg + 1);
586586
}
587587

588588
return commit->body;
@@ -876,12 +876,15 @@ int git_commit_create_with_signature(
876876
return -1;
877877
}
878878

879-
field = signature_field ? signature_field : "gpgsig";
880-
881879
/* The header ends after the first LF */
882880
header_end++;
883881
git_buf_put(&commit, commit_content, header_end - commit_content);
884-
format_header_field(&commit, field, signature);
882+
883+
if (signature != NULL) {
884+
field = signature_field ? signature_field : "gpgsig";
885+
format_header_field(&commit, field, signature);
886+
}
887+
885888
git_buf_puts(&commit, header_end);
886889

887890
if (git_buf_oom(&commit))

0 commit comments

Comments
 (0)