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

[PATCH v3] gpg-interface: add function for converting trust level to string

From
Jaydeep Das via GitGitGadget <gitgitgadget@gmail.com>
Date
Jul 9, 2022, 04:43 UTC
Message-ID
<pull.1281.v3.git.1657341817595.gitgitgadget@gmail.com>
In-Reply-To
<pull.1281.v2.git.1657279447515.gitgitgadget@gmail.com>
From: Jaydeep 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 (in lowercase). 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
    in lowercase. For example, TRUST_ULTIMATE will yield the string
    "ultimate".
    
    This will abstract out some code in pretty.c relating to gpg signature
    trust levels.
    
    Changes since v1:
    
     * gpg_trust_level_to_str() now returns the string in lowercase.
    
    Changes since v2:
    
     * Updated docs.
    
    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-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1281/JDeepD/gpg-wrap-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1281
Range-diff vs v2:
 1:  640decc2afe ! 1:  933d6caa916 gpg-interface: add function for converting trust level to string
     @@ Commit message
      
          Add new helper function `gpg_trust_level_to_str()` which will
          convert a given member of `enum signature_trust_level` to its
     -    corresponding string(in lowercase). For example, `TRUST_ULTIMATE`
     +    corresponding string (in lowercase). For example, `TRUST_ULTIMATE`
          will yield the string "ultimate".
      
          This will abstract out some code in `pretty.c` relating to gpg
     @@ gpg-interface.h: size_t parse_signed_buffer(const char *buf, size_t size);
      +/*
      + * Returns corresponding string in lowercase for a given member of
      + * enum signature_trust_level. For example, `TRUST_ULTIMATE` will
     -+ * return "ultimate".
     ++ * return "ultimate". Since it uses xstrdup_tolower(), which uses
     ++ * xmallocz(), the caller has to free up the memory for returned string
     ++ * after usage.
      + */
      +char *gpg_trust_level_to_str(enum signature_trust_level level);
      +
 gpg-interface.c |  7 +++++++
 gpg-interface.h | 10 ++++++++++
 pretty.c        | 22 +++++-----------------
 3 files changed, 22 insertions(+), 17 deletions(-)
diff --git a/gpg-interface.c b/gpg-interface.c
index 947b58ad4da..4ef660a09fc 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);
 }
 
+char *gpg_trust_level_to_str(enum signature_trust_level level){
+	if (level < TRUST_UNDEFINED || level > TRUST_ULTIMATE)
+		return NULL;
+	return xstrdup_tolower(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..eda8b32015c 100644
--- a/gpg-interface.h
+++ b/gpg-interface.h
@@ -71,6 +71,16 @@ 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 in lowercase for a given member of
+ * enum signature_trust_level. For example, `TRUST_ULTIMATE` will
+ * return "ultimate". Since it uses xstrdup_tolower(), which uses
+ * xmallocz(), the caller has to free up the memory for returned string
+ * after usage.
+ */
+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..5ee03d6fe09 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1348,6 +1348,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 	const char *msg = c->message;
 	struct commit_list *p;
 	const char *arg, *eol;
+	char *sig_str;
 	size_t res;
 	char **slot;
 
@@ -1575,23 +1576,10 @@ 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)
+				strbuf_addstr(sb, sig_str);
+			free(sig_str);
 			break;
 		default:
 			return 0;

base-commit: 30cc8d0f147546d4dd77bf497f4dec51e7265bd8
-- 
gitgitgadget
Previous: Jaydeep DasNext: Jaydeep Das via GitGitGadget
Message 11 of 13 in “gpg-interface: add function for converting trust level to string”
  1. gpg-interface: add function for converting trust level to stringJaydeep Das via GitGitGadget, Jul 7, 2022
  2. Junio C HamanoJul 7, 2022
  3. gpg-interface: add function for converting trust level to stringJaydeep Das via GitGitGadget, Jul 8, 2022
  4. Eric SunshineJul 9, 2022
  5. jaydeepjd.8914@gmail.comJul 9, 2022
  6. Junio C HamanoJul 9, 2022
  7. Eric SunshineJul 10, 2022
  8. Junio C HamanoJul 10, 2022
  9. Eric SunshineJul 10, 2022
  10. Jaydeep DasJul 11, 2022
  11. gpg-interface: add function for converting trust level to stringJaydeep Das via GitGitGadget, Jul 9, 2022
  12. gpg-interface: add function for converting trust level to stringJaydeep Das via GitGitGadget, Jul 11, 2022
  13. Junio C HamanoJul 11, 2022

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.