Commit 759471056f1d9fe36dde447e0ce08ba3ce13a7be

Tyler Ang-Wanek 2019-07-02T09:53:49

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.

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))