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

[PATCH v3 17/17] pretty: %G[?GS] placeholders

From
Junio C Hamano <gitster@pobox.com>
Date
Nov 9, 2011, 01:02 UTC
Message-ID
<1320800523-5407-18-git-send-email-gitster@pobox.com>
In-Reply-To
<1320800523-5407-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>
---
 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.8.rc1.82.g90e080
Previous: Junio C HamanoNext: Robin H. Johnson
Message 18 of 21 in “Pulling signed tags”
  1. 00/17 Pulling signed tagsJunio C Hamano, Nov 9, 2011
  2. 01/17 Split GPG interface into its own helper libraryJunio C Hamano, Nov 9, 2011
  3. 02/17 fetch: do not store peeled tag object names in FETCH_HEADJunio C Hamano, Nov 9, 2011
  4. 03/17 merge: notice local merging of tags and keep it unwrappedJunio C Hamano, Nov 9, 2011
  5. 04/17 fetch: allow "git fetch $there v1.0" to fetch a tagJunio C Hamano, Nov 9, 2011
  6. 05/17 refs DWIMmery: use the same rule for both "git fetch" and othersJunio C Hamano, Nov 9, 2011
  7. 06/17 fmt-merge-msg: avoid early returnsJunio C Hamano, Nov 9, 2011
  8. 07/17 fmt-merge-msg: package options into a structureJunio C Hamano, Nov 9, 2011
  9. 08/17 fmt-merge-msg: Add contents of merged tag in the merge messageJunio C Hamano, Nov 9, 2011
  10. 09/17 merge: make usage of commit->util more extensibleJunio C Hamano, Nov 9, 2011
  11. 10/17 merge: record tag objects without peeling in MERGE_HEADJunio C Hamano, Nov 9, 2011
  12. 11/17 commit: copy merged signed tags to headers of merge commitJunio C Hamano, Nov 9, 2011
  13. 12/17 merge: force edit mode when merging a tag objectJunio C Hamano, Nov 9, 2011
  14. 13/17 commit: teach --amend to carry forward extra headersJunio C Hamano, Nov 9, 2011
  15. 14/17 commit: teach --gpg-sign optionJunio C Hamano, Nov 9, 2011
  16. 15/17 log: --show-signatureJunio C Hamano, Nov 9, 2011
  17. 16/17 test "commit -S" and "log --show-signature"Junio C Hamano, Nov 9, 2011
  18. 17/17 pretty: %G[?GS] placeholdersJunio C Hamano, Nov 9, 2011
  19. Robin H. JohnsonNov 9, 2011
  20. 18/17 request-pull: use the annotated tag contentsJunio C Hamano, Nov 9, 2011
  21. Junio C HamanoNov 9, 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.