commit: allow parsing arbitrary buffers with headers

Currently only commits are signed with headers. However, in the future, we'll also sign tags with headers as well. Let's refactor out a function called parse_buffer_signed_by_header which does exactly that. In addition, since we'll want to sign things other than commits this way, let's call the function sign_with_header instead of do_sign_commit. Signed-off-by: brian m. carlson <sandals@crustytoothpaste.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

brian m. carlson committed Feb 11, 2021 at 02:08 UTC 937032e14aaf1eab59c96dd78938be1c48c648e1
2 files changed +25 -4
commit.c
+16 -4
@@ -995,7 +995,7 @@ static const char *gpg_sig_headers[] = {
995 "gpgsig-sha256",
996 };
997
998 -static int do_sign_commit(struct strbuf *buf, const char *keyid)
998 +int sign_with_header(struct strbuf *buf, const char *keyid)
999 {
1000 struct strbuf sig = STRBUF_INIT;
1001 int inspos, copypos;
@@ -1035,13 +1035,26 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
1035 return 0;
1036 }
1037
1038 +
1039 +
1040 int parse_signed_commit(const struct commit *commit,
1041 struct strbuf *payload, struct strbuf *signature,
1042 const struct git_hash_algo *algop)
1043 {
1042 -
1044 unsigned long size;
1045 const char *buffer = get_commit_buffer(commit, &size);
1046 + int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
1047 +
1048 + unuse_commit_buffer(commit, buffer);
1049 + return ret;
1050 +}
1051 +
1052 +int parse_buffer_signed_by_header(const char *buffer,
1053 + unsigned long size,
1054 + struct strbuf *payload,
1055 + struct strbuf *signature,
1056 + const struct git_hash_algo *algop)
1057 +{
1058 int in_signature = 0, saw_signature = 0, other_signature = 0;
1059 const char *line, *tail, *p;
1060 const char *gpg_sig_header = gpg_sig_headers[hash_algo_by_ptr(algop)];
@@ -1078,7 +1091,6 @@ int parse_signed_commit(const struct commit *commit,
1091 }
1092 line = next;
1093 }
1081 - unuse_commit_buffer(commit, buffer);
1094 return saw_signature;
1095 }
1096
@@ -1532,7 +1544,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
1544 if (encoding_is_utf8 && !verify_utf8(&buffer))
1545 fprintf(stderr, _(commit_utf8_warn));
1546
1535 - if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
1547 + if (sign_commit && sign_with_header(&buffer, sign_commit)) {
1548 result = -1;
1549 goto out;
1550 }
commit.h
+9
@@ -360,4 +360,13 @@ int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void
360 LAST_ARG_MUST_BE_NULL
361 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
362
363 +/* Sign a commit or tag buffer, storing the result in a header. */
364 +int sign_with_header(struct strbuf *buf, const char *keyid);
365 +/* Parse the signature out of a header. */
366 +int parse_buffer_signed_by_header(const char *buffer,
367 + unsigned long size,
368 + struct strbuf *payload,
369 + struct strbuf *signature,
370 + const struct git_hash_algo *algop);
371 +
372 #endif /* COMMIT_H */