git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] gpg-interface: add function for converting trust level to string
@ 2022-07-07 13:57 Jaydeep Das via GitGitGadget
  2022-07-07 18:18 ` Junio C Hamano
  2022-07-08 11:24 ` [PATCH v2] " Jaydeep Das via GitGitGadget
  0 siblings, 2 replies; 13+ messages in thread
From: Jaydeep Das via GitGitGadget @ 2022-07-07 13:57 UTC (permalink / raw)
  To: git; +Cc: Jaydeep Das, Jaydeep P Das

From: Jaydeep P Das <jaydeepjd.8914@gmail.com>

Add new helper function `gpg_trust_level_to_str()` which will
convert a given member of `enum signature_trust_level` to its
corresponding string. For example, `TRUST_ULTIMATE`
will yield the string "ULTIMATE".

This will abstract out some code in `pretty.c` relating to gpg
signature trust levels.

Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Mentored-by: Hariom Verma <hariom18599@gmail.com>
Signed-off-by: Jaydeep Das <jaydeepjd.8914@gmail.com>
---
    gpg-interface: add function for converting trust level to string
    
    Add new helper function gpg_trust_level_to_str() which will convert a
    given member of enum signature_trust_level to its corresponding string.
    For example, TRUST_ULTIMATE will yield the string "ULTIMATE".
    
    This will abstract out some code in pretty.c relating to gpg signature
    trust levels.
    
    Mentored-by: Christian Couder chriscool@tuxfamily.org Mentored-by:
    Hariom Verma hariom18599@gmail.com Signed-off-by: Jaydeep Das
    jaydeepjd.8914@gmail.com

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1281%2FJDeepD%2Fgpg-wrap-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1281/JDeepD/gpg-wrap-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1281

 gpg-interface.c |  7 +++++++
 gpg-interface.h |  8 ++++++++
 pretty.c        | 23 ++++++-----------------
 3 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/gpg-interface.c b/gpg-interface.c
index 947b58ad4da..fe6e5ce5127 100644
--- a/gpg-interface.c
+++ b/gpg-interface.c
@@ -165,6 +165,7 @@ static struct {
 	{ 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
 };
 
+/* Keep the order same as enum signature_trust_level */
 static struct {
 	const char *key;
 	enum signature_trust_level value;
@@ -905,6 +906,12 @@ const char *get_signing_key(void)
 	return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
 }
 
+const char *gpg_trust_level_to_str(enum signature_trust_level level){
+	if (level < TRUST_UNDEFINED || level > TRUST_ULTIMATE)
+		return NULL;
+	return sigcheck_gpg_trust_level[level].key;
+}
+
 int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
 {
 	return use_format->sign_buffer(buffer, signature, signing_key);
diff --git a/gpg-interface.h b/gpg-interface.h
index b30cbdcd3da..48f7edd916b 100644
--- a/gpg-interface.h
+++ b/gpg-interface.h
@@ -71,6 +71,14 @@ size_t parse_signed_buffer(const char *buf, size_t size);
 int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
 		const char *signing_key);
 
+
+/*
+ * Returns corresponding string for a given member of
+ * enum signature_trust_level. For example, `TRUST_ULTIMATE` will
+ * return "ULTIMATE".
+ */
+const char *gpg_trust_level_to_str(enum signature_trust_level level);
+
 int git_gpg_config(const char *, const char *, void *);
 void set_signing_key(const char *);
 const char *get_signing_key(void);
diff --git a/pretty.c b/pretty.c
index ee6114e3f0a..f617dd601ac 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1347,7 +1347,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	const struct commit *commit = c->commit;
 	const char *msg = c->message;
 	struct commit_list *p;
-	const char *arg, *eol;
+	const char *arg, *eol, *sig_str;
 	size_t res;
 	char **slot;
 
@@ -1575,22 +1575,11 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 				strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
 			break;
 		case 'T':
-			switch (c->signature_check.trust_level) {
-			case TRUST_UNDEFINED:
-				strbuf_addstr(sb, "undefined");
-				break;
-			case TRUST_NEVER:
-				strbuf_addstr(sb, "never");
-				break;
-			case TRUST_MARGINAL:
-				strbuf_addstr(sb, "marginal");
-				break;
-			case TRUST_FULLY:
-				strbuf_addstr(sb, "fully");
-				break;
-			case TRUST_ULTIMATE:
-				strbuf_addstr(sb, "ultimate");
-				break;
+			sig_str = gpg_trust_level_to_str(c->signature_check.trust_level);
+			if (sig_str){
+				const char *sig_str_lower = xstrdup_tolower(sig_str);
+				strbuf_addstr(sb, sig_str_lower);
+				free((char *)sig_str_lower);
 			}
 			break;
 		default:

base-commit: 30cc8d0f147546d4dd77bf497f4dec51e7265bd8
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-07-11  5:12 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-07-07 13:57 [PATCH] gpg-interface: add function for converting trust level to string Jaydeep Das via GitGitGadget
2022-07-07 18:18 ` Junio C Hamano
2022-07-08 11:24 ` [PATCH v2] " Jaydeep Das via GitGitGadget
2022-07-09  0:58   ` Eric Sunshine
2022-07-09  3:51     ` jaydeepjd.8914
2022-07-09 20:52     ` Junio C Hamano
2022-07-10  5:44       ` Eric Sunshine
2022-07-10  5:48         ` Junio C Hamano
2022-07-10  6:21           ` Eric Sunshine
2022-07-11  3:51         ` Jaydeep Das
2022-07-09  4:43   ` [PATCH v3] " Jaydeep Das via GitGitGadget
2022-07-11  5:00     ` [PATCH v4] " Jaydeep Das via GitGitGadget
2022-07-11  5:12       ` Junio C Hamano

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).