git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 3/5] commit: allow parsing arbitrary buffers with headers

From
brian m. carlson <sandals@crustytoothpaste.net>
Date
Jan 11, 2021, 03:58 UTC
Message-ID
<20210111035840.2437737-4-sandals@crustytoothpaste.net>
In-Reply-To
<20210111035840.2437737-1-sandals@crustytoothpaste.net>

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>
---
 commit.c | 20 ++++++++++++++++----
 commit.h |  9 +++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
diff --git a/commit.c b/commit.c
index 794dc8b593..23020c0bca 100644
--- a/commit.c
+++ b/commit.c
@@ -995,7 +995,7 @@ static const char *gpg_sig_headers[] = {
 	"gpgsig-sha256",
 };
 
-static int do_sign_commit(struct strbuf *buf, const char *keyid)
+int sign_with_header(struct strbuf *buf, const char *keyid)
 {
 	struct strbuf sig = STRBUF_INIT;
 	int inspos, copypos;
@@ -1035,13 +1035,26 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
 	return 0;
 }
 
+
+
 int parse_signed_commit(const struct commit *commit,
 			struct strbuf *payload, struct strbuf *signature,
 			const struct git_hash_algo *algop)
 {
-
 	unsigned long size;
 	const char *buffer = get_commit_buffer(commit, &size);
+	int ret = parse_buffer_signed_by_header(buffer, size, payload, signature, algop);
+
+	unuse_commit_buffer(commit, buffer);
+	return ret;
+}
+
+int parse_buffer_signed_by_header(const char *buffer,
+				  unsigned long size,
+				  struct strbuf *payload,
+				  struct strbuf *signature,
+				  const struct git_hash_algo *algop)
+{
 	int in_signature = 0, saw_signature = 0, other_signature = 0;
 	const char *line, *tail, *p;
 	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,
 		}
 		line = next;
 	}
-	unuse_commit_buffer(commit, buffer);
 	return saw_signature;
 }
 
@@ -1530,7 +1542,7 @@ int commit_tree_extended(const char *msg, size_t msg_len,
 	if (encoding_is_utf8 && !verify_utf8(&buffer))
 		fprintf(stderr, _(commit_utf8_warn));
 
-	if (sign_commit && do_sign_commit(&buffer, sign_commit)) {
+	if (sign_commit && sign_with_header(&buffer, sign_commit)) {
 		result = -1;
 		goto out;
 	}
diff --git a/commit.h b/commit.h
index 030aa65ab8..e2856ce8ef 100644
--- a/commit.h
+++ b/commit.h
@@ -360,4 +360,13 @@ int compare_commits_by_gen_then_commit_date(const void *a_, const void *b_, void
 LAST_ARG_MUST_BE_NULL
 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...);
 
+/* Sign a commit or tag buffer, storing the result in a header. */
+int sign_with_header(struct strbuf *buf, const char *keyid);
+/* Parse the signature out of a header. */
+int parse_buffer_signed_by_header(const char *buffer,
+				  unsigned long size,
+				  struct strbuf *payload,
+				  struct strbuf *signature,
+				  const struct git_hash_algo *algop);
+
 #endif /* COMMIT_H */
Previous: brian m. carlsonNext: brian m. carlson
Message 14 of 39 in “Support for commits signed by multiple algorithms”
  1. 0/5 Support for commits signed by multiple algorithmsbrian m. carlson, Jan 11, 2021
  2. 1/5 commit: ignore additional signatures when parsing signed commitsbrian m. carlson, Jan 11, 2021
  3. 3/5 commit: allow parsing arbitrary buffers with headersbrian m. carlson, Jan 11, 2021
  4. 2/5 gpg-interface: improve interface for parsing tagsbrian m. carlson, Jan 11, 2021
  5. Junio C HamanoJan 12, 2021
  6. brian m. carlsonJan 14, 2021
  7. Junio C HamanoJan 15, 2021
  8. 4/5 ref-filter: hoist signature parsingbrian m. carlson, Jan 11, 2021
  9. 5/6 fixup! commit: ignore additional signatures when parsing signed commitsbrian m. carlson, Jan 11, 2021
  10. brian m. carlsonJan 11, 2021
  11. 5/5 gpg-interface: remove other signature headers before verifyingbrian m. carlson, Jan 11, 2021
  12. 6/6 gpg-interface: remove other signature headers before verifyingbrian m. carlson, Jan 11, 2021
  13. 0/5 Support for commits signed by multiple algorithmsbrian m. carlson, Jan 11, 2021
  14. 3/5 commit: allow parsing arbitrary buffers with headersbrian m. carlson, Jan 11, 2021
  15. 4/5 ref-filter: hoist signature parsingbrian m. carlson, Jan 11, 2021
  16. 2/5 gpg-interface: improve interface for parsing tagsbrian m. carlson, Jan 11, 2021
  17. Junio C HamanoJan 12, 2021
  18. 5/5 gpg-interface: remove other signature headers before verifyingbrian m. carlson, Jan 11, 2021
  19. 1/5 commit: ignore additional signatures when parsing signed commitsbrian m. carlson, Jan 11, 2021
  20. SZEDER GáborJan 12, 2021
  21. Junio C HamanoJan 11, 2021
  22. brian m. carlsonJan 11, 2021
  23. Junio C HamanoJan 12, 2021
  24. brian m. carlsonJan 12, 2021
  25. 0/6 Support for commits signed by multiple algorithmsbrian m. carlson, Jan 18, 2021
  26. 1/6 ref-filter: switch some uses of unsigned long to size_tbrian m. carlson, Jan 18, 2021
  27. 2/6 commit: ignore additional signatures when parsing signed commitsbrian m. carlson, Jan 18, 2021
  28. 4/6 commit: allow parsing arbitrary buffers with headersbrian m. carlson, Jan 18, 2021
  29. 3/6 gpg-interface: improve interface for parsing tagsbrian m. carlson, Jan 18, 2021
  30. 6/6 gpg-interface: remove other signature headers before verifyingbrian m. carlson, Jan 18, 2021
  31. 5/6 ref-filter: hoist signature parsingbrian m. carlson, Jan 18, 2021
  32. 0/6 Support for commits signed by multiple algorithmsbrian m. carlson, Feb 11, 2021
  33. 4/6 commit: allow parsing arbitrary buffers with headersbrian m. carlson, Feb 11, 2021
  34. 1/6 ref-filter: switch some uses of unsigned long to size_tbrian m. carlson, Feb 11, 2021
  35. 2/6 commit: ignore additional signatures when parsing signed commitsbrian m. carlson, Feb 11, 2021
  36. 3/6 gpg-interface: improve interface for parsing tagsbrian m. carlson, Feb 11, 2021
  37. 5/6 ref-filter: hoist signature parsingbrian m. carlson, Feb 11, 2021
  38. 6/6 gpg-interface: remove other signature headers before verifyingbrian m. carlson, Feb 11, 2021
  39. Junio C HamanoFeb 11, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.