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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
diff --git a/include/git2/commit.h b/include/git2/commit.h
index a50ff58..2304484 100644
--- a/include/git2/commit.h
+++ b/include/git2/commit.h
@@ -480,7 +480,8 @@ GIT_EXTERN(int) git_commit_create_buffer(
*
* @param out the resulting commit id
* @param commit_content the content of the unsigned commit object
- * @param signature the signature to add to the commit
+ * @param signature the signature to add to the commit. Leave `NULL`
+ * to create a commit without adding a signature field.
* @param signature_field which header field should contain this
* signature. Leave `NULL` for the default of "gpgsig"
* @return 0 or an error code
diff --git a/src/commit.c b/src/commit.c
index 513fdcc..49550d3 100644
--- a/src/commit.c
+++ b/src/commit.c
@@ -80,8 +80,8 @@ on_error:
}
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)
+ git_commit_parent_callback parent_cb, void *parent_payload,
+ const git_oid *current_id, bool validate)
{
size_t i;
int error;
@@ -152,8 +152,8 @@ static int git_commit__create_internal(
goto cleanup;
error = git_commit__create_buffer_internal(&buf, author, committer,
- message_encoding, message, tree,
- &parents);
+ message_encoding, message, tree,
+ &parents);
if (error < 0)
goto cleanup;
@@ -582,7 +582,7 @@ const char *git_commit_body(git_commit *commit)
break;
if (*msg)
- commit->body = git__strndup(msg, end - msg + 1);
+ commit->body = git__strndup(msg, end - msg + 1);
}
return commit->body;
@@ -876,12 +876,15 @@ int git_commit_create_with_signature(
return -1;
}
- field = signature_field ? signature_field : "gpgsig";
-
/* The header ends after the first LF */
header_end++;
git_buf_put(&commit, commit_content, header_end - commit_content);
- format_header_field(&commit, field, signature);
+
+ if (signature != NULL) {
+ field = signature_field ? signature_field : "gpgsig";
+ format_header_field(&commit, field, signature);
+ }
+
git_buf_puts(&commit, header_end);
if (git_buf_oom(&commit))