git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Passaro <john.a.passaro@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, alex.crezoff@gmail.com, peff@peff.net,
	mgorny@gentoo.org, John Passaro <john.a.passaro@gmail.com>
Subject: [PATCH 1/4] pretty: expose raw commit signature
Date: Thu, 13 Dec 2018 16:22:53 -0500	[thread overview]
Message-ID: <20181213212256.48122-2-john.a.passaro@gmail.com> (raw)
In-Reply-To: <20181213212256.48122-1-john.a.passaro@gmail.com>

Add new pretty-format placeholders %GR and %G+ to support inspecting
gpgsig commit header in pretty format, even if GPG is not available.

Signed-off-by: John Passaro <john.a.passaro@gmail.com>
---
 Documentation/pretty-formats.txt |  2 ++
 pretty.c                         | 36 ++++++++++++++++++++++++++++++--
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 417b638cd8..582454a4f7 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -142,6 +142,8 @@ The placeholders are:
 ifndef::git-rev-list[]
 - '%N': commit notes
 endif::git-rev-list[]
+- '%GR': contents of the commits signature (blank when unsigned)
+- '%G+': "Y" if the commit is signed, "N" otherwise
 - '%GG': raw verification message from GPG for a signed commit
 - '%G?': show "G" for a good (valid) signature,
   "B" for a bad signature,
diff --git a/pretty.c b/pretty.c
index b83a3ecd23..d142b457b5 100644
--- a/pretty.c
+++ b/pretty.c
@@ -768,6 +768,9 @@ struct format_commit_context {
 	unsigned commit_header_parsed:1;
 	unsigned commit_message_parsed:1;
 	struct signature_check signature_check;
+	unsigned signature_checked:2;
+	struct strbuf signature;
+	struct strbuf signature_payload;
 	enum flush_type flush_type;
 	enum trunc_type truncate;
 	const char *message;
@@ -1228,8 +1231,30 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	}
 
 	if (placeholder[0] == 'G') {
-		if (!c->signature_check.result)
-			check_commit_signature(c->commit, &(c->signature_check));
+		if (!c->signature_checked) {
+			parse_signed_commit(c->commit, &(c->signature_payload), &(c->signature));
+			c->signature_checked = 1;
+		}
+		switch (placeholder[1]) {
+		case 'R':
+			strbuf_addbuf(sb, &(c->signature));
+			break;
+		case '+':
+			strbuf_addch(sb, c->signature.len ? 'Y' : 'N');
+			break;
+		default:
+			goto do_signature_check;
+		}
+		return 2;
+
+do_signature_check:
+		if (c->signature_checked < 2) {
+			if (c->signature.len)
+				check_signature(c->signature_payload.buf, c->signature_payload.len,
+						c->signature.buf, c->signature.len,
+						&(c->signature_check));
+			c->signature_checked = 2;
+		}
 		switch (placeholder[1]) {
 		case 'G':
 			if (c->signature_check.gpg_output)
@@ -1246,6 +1271,9 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 			case 'Y':
 			case 'R':
 				strbuf_addch(sb, c->signature_check.result);
+				break;
+			case 0: // i.e. no signature so we never ran the check
+				strbuf_addch(sb, 'N');
 			}
 			break;
 		case 'S':
@@ -1527,6 +1555,8 @@ void format_commit_message(const struct commit *commit,
 	context.commit = commit;
 	context.pretty_ctx = pretty_ctx;
 	context.wrap_start = sb->len;
+	strbuf_init(&context.signature, 0);
+	strbuf_init(&context.signature_payload, 0);
 	/*
 	 * convert a commit message to UTF-8 first
 	 * as far as 'format_commit_item' assumes it in UTF-8
@@ -1556,6 +1586,8 @@ void format_commit_message(const struct commit *commit,
 			strbuf_attach(sb, out, outsz, outsz + 1);
 	}
 
+	strbuf_release(&context.signature);
+	strbuf_release(&context.signature_payload);
 	free(context.commit_encoding);
 	unuse_commit_buffer(commit, context.message);
 }
-- 
2.19.1


  reply	other threads:[~2018-12-13 21:23 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-12-13 21:22 [PATCH 0/4] Expose gpgsig in pretty-print John Passaro
2018-12-13 21:22 ` John Passaro [this message]
2018-12-13 21:22 ` [PATCH 2/4] t/t7510-signed-commit.sh: test new placeholders John Passaro
2018-12-13 21:22 ` [PATCH 3/4] doc, tests: pretty behavior when gpg missing John Passaro
2018-12-13 21:22 ` [PATCH 4/4] docs/pretty-formats: add explanation + copy edits John Passaro
2018-12-14  4:11 ` [PATCH 0/4] Expose gpgsig in pretty-print Michał Górny
2018-12-14 16:07   ` John Passaro
2018-12-14 16:48     ` Michał Górny
2018-12-14 23:10       ` John Passaro
2018-12-14 23:13         ` John Passaro
2018-12-17 20:24     ` Jeff King
2018-12-19  5:59       ` John Passaro
2018-12-21 13:52         ` Michał Górny
2018-12-15  0:26   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181213212256.48122-2-john.a.passaro@gmail.com \
    --to=john.a.passaro@gmail.com \
    --cc=alex.crezoff@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=mgorny@gentoo.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).