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

[PATCH 7/5] pretty: %G[?GS] placeholders

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 22, 2011, 05:01 UTC
Message-ID
<7v7h3x7h6j.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<1319071023-31919-1-git-send-email-gitster@pobox.com>
Add new placeholders related to the GPG signature on signed commits.
 - %GG to show the raw verification message from GPG;
 - %G? to show either "G" for Good, "B" for Bad;
 - %GS to show the name of the signer.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * The 6th is the one that works with a bogus commit with NUL in it I sent
   out previously.
   This concludes the series; I'll leave the design and implementation of
   other useful placeholders to the list for now.
 pretty.c |   86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)
diff --git a/pretty.c b/pretty.c
index f45eb54..392d656 100644
--- a/pretty.c
+++ b/pretty.c
@@ -9,6 +9,7 @@
 #include "notes.h"
 #include "color.h"
 #include "reflog-walk.h"
+#include "gpg-interface.h"
 
 static char *user_format;
 static struct cmt_fmt_map {
@@ -640,6 +641,12 @@ struct format_commit_context {
 	const struct pretty_print_context *pretty_ctx;
 	unsigned commit_header_parsed:1;
 	unsigned commit_message_parsed:1;
+	unsigned commit_signature_parsed:1;
+	struct {
+		char *gpg_output;
+		char good_bad;
+		char *signer;
+	} signature;
 	char *message;
 	size_t width, indent1, indent2;
 
@@ -822,6 +829,59 @@ static void rewrap_message_tail(struct strbuf *sb,
 	c->indent2 = new_indent2;
 }
 
+static struct {
+	char result;
+	const char *check;
+} signature_check[] = {
+	{ 'G', ": Good signature from " },
+	{ 'B', ": BAD signature from " },
+};
+
+static void parse_signature_lines(struct format_commit_context *ctx)
+{
+	const char *buf = ctx->signature.gpg_output;
+	int i;
+
+	for (i = 0; i < ARRAY_SIZE(signature_check); i++) {
+		const char *found = strstr(buf, signature_check[i].check);
+		const char *next;
+		if (!found)
+			continue;
+		ctx->signature.good_bad = signature_check[i].result;
+		found += strlen(signature_check[i].check);
+		next = strchrnul(found, '\n');
+		ctx->signature.signer = xmemdupz(found, next - found);
+		break;
+	}
+}
+
+static void parse_commit_signature(struct format_commit_context *ctx)
+{
+	struct strbuf payload = STRBUF_INIT;
+	struct strbuf signature = STRBUF_INIT;
+	struct strbuf gpg_output = STRBUF_INIT;
+	int status;
+
+	ctx->commit_signature_parsed = 1;
+
+	if (parse_signed_commit(ctx->commit->object.sha1,
+				&payload, &signature) <= 0)
+		goto out;
+	status = verify_signed_buffer(payload.buf, payload.len,
+				      signature.buf, signature.len,
+				      &gpg_output);
+	if (status && !gpg_output.len)
+		goto out;
+	ctx->signature.gpg_output = strbuf_detach(&gpg_output, NULL);
+	parse_signature_lines(ctx);
+
+ out:
+	strbuf_release(&gpg_output);
+	strbuf_release(&payload);
+	strbuf_release(&signature);
+}
+
+
 static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 				void *context)
 {
@@ -974,6 +1034,30 @@ static size_t format_commit_one(struct strbuf *sb, const char *placeholder,
 		return 0;
 	}
 
+	if (placeholder[0] == 'G') {
+		if (!c->commit_signature_parsed)
+			parse_commit_signature(c);
+		switch (placeholder[1]) {
+		case 'G':
+			if (c->signature.gpg_output)
+				strbuf_addstr(sb, c->signature.gpg_output);
+			break;
+		case '?':
+			switch (c->signature.good_bad) {
+			case 'G':
+			case 'B':
+				strbuf_addch(sb, c->signature.good_bad);
+			}
+			break;
+		case 'S':
+			if (c->signature.signer)
+				strbuf_addstr(sb, c->signature.signer);
+			break;
+		}
+		return 2;
+	}
+
+
 	/* For the rest we have to parse the commit header. */
 	if (!c->commit_header_parsed)
 		parse_commit_header(c);
@@ -1114,6 +1198,8 @@ void format_commit_message(const struct commit *commit,
 
 	if (context.message != commit->buffer)
 		free(context.message);
+	free(context.signature.gpg_output);
+	free(context.signature.signer);
 }
 
 static void pp_header(const struct pretty_print_context *pp,
-- 
1.7.7.555.g02edb3
Previous: Junio C HamanoNext: Elia Pinto
Message 32 of 35 in “commit: teach --gpg-sign option”
  1. commit: teach --gpg-sign optionJunio C Hamano, Oct 6, 2011
  2. Shawn PearceOct 6, 2011
  3. Jonathan NiederOct 6, 2011
  4. Matthieu MoyOct 6, 2011
  5. Michael J GruberOct 6, 2011
  6. Junio C HamanoOct 6, 2011
  7. Robin H. JohnsonOct 6, 2011
  8. Michael J GruberOct 7, 2011
  9. Nguyen Thai Ngoc DuyOct 7, 2011
  10. Michael J GruberOct 9, 2011
  11. Robin H. JohnsonOct 9, 2011
  12. Junio C HamanoOct 9, 2011
  13. Robin H. JohnsonOct 11, 2011
  14. Michael J GruberOct 9, 2011
  15. Junio C HamanoOct 9, 2011
  16. Michael J GruberOct 10, 2011
  17. Junio C HamanoOct 10, 2011
  18. Junio C HamanoOct 9, 2011
  19. Michael J GruberOct 10, 2011
  20. Junio C HamanoOct 10, 2011
  21. Michael J GruberOct 11, 2011
  22. 0/3 Signed-commitJunio C Hamano, Oct 19, 2011
  23. 1/3 Split GPG interface into its own helper libraryJunio C Hamano, Oct 19, 2011
  24. 2/3 commit: teach --gpg-sign optionJunio C Hamano, Oct 19, 2011
  25. 3/3 log: --show-signatureJunio C Hamano, Oct 19, 2011
  26. 0/5 Signed-commitJunio C Hamano, Oct 20, 2011
  27. 1/5 Split GPG interface into its own helper libraryJunio C Hamano, Oct 20, 2011
  28. 2/5 commit: teach --gpg-sign optionJunio C Hamano, Oct 20, 2011
  29. 3/5 log: --show-signatureJunio C Hamano, Oct 20, 2011
  30. 4/5 t7004: extract generic "GPG testing" bitsJunio C Hamano, Oct 20, 2011
  31. 5/5 test "commit -S" and "log --show-signature"Junio C Hamano, Oct 20, 2011
  32. 7/5 pretty: %G[?GS] placeholdersJunio C Hamano, Oct 22, 2011
  33. Elia PintoOct 22, 2011
  34. Junio C HamanoOct 22, 2011
  35. Junio C HamanoOct 10, 2011

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.