git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: Eric Sunshine <sunshine@sunshineco.com>
Cc: Jaydeep Das via GitGitGadget <gitgitgadget@gmail.com>,
	Git List <git@vger.kernel.org>,
	Jaydeep Das <jaydeepjd.8914@gmail.com>
Subject: Re: [PATCH v2] gpg-interface: add function for converting trust level to string
Date: Sat, 09 Jul 2022 13:52:18 -0700	[thread overview]
Message-ID: <xmqqwncmt3el.fsf@gitster.g> (raw)
In-Reply-To: <CAPig+cTX76ZMG_S-qOX_JDxYVWXRvtP2Ref4k8uM1KJaDwX9=w@mail.gmail.com> (Eric Sunshine's message of "Fri, 8 Jul 2022 20:58:22 -0400")

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Fri, Jul 8, 2022 at 7:28 AM Jaydeep Das via GitGitGadget
> <gitgitgadget@gmail.com> wrote:
>> 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`
>
> s/g(/g (/
>
>> will yield the string "ultimate".
>>
>> This will abstract out some code in `pretty.c` relating to gpg
>> signature trust levels.
>>
>> Signed-off-by: Jaydeep Das <jaydeepjd.8914@gmail.com>
>> ---
>> diff --git a/gpg-interface.h b/gpg-interface.h
>> @@ -71,6 +71,14 @@ 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".
>> + */
>> +char *gpg_trust_level_to_str(enum signature_trust_level level);
>
> It would be a good idea to update the function documentation to
> mention that the caller is responsible for freeing the returned
> string.

Given that there are small and fixed number of trust level strings,
I actually think that it would be more reasonable to return a static
string to the caller, something along the lines of the attached, so
that callers do not have to worry about freeing it.

Perhaps along the lines of ...

 gpg-interface.c | 19 ++++++++++++++++++-
 gpg-interface.h |  2 ++
 2 files changed, 20 insertions(+), 1 deletion(-)

diff --git c/gpg-interface.c w/gpg-interface.c
index 947b58ad4d..4a5b9d0f3a 100644
--- c/gpg-interface.c
+++ w/gpg-interface.c
@@ -165,9 +165,10 @@ static struct {
 	{ 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
 };
 
-static struct {
+static struct sigcheck_gpg_trust_level {
 	const char *key;
 	enum signature_trust_level value;
+	const char *downcased;
 } sigcheck_gpg_trust_level[] = {
 	{ "UNDEFINED", TRUST_UNDEFINED },
 	{ "NEVER", TRUST_NEVER },
@@ -176,6 +177,22 @@ static struct {
 	{ "ULTIMATE", TRUST_ULTIMATE },
 };
 
+const char *gpg_trust_level_to_string(enum signature_trust_level level)
+{
+	struct sigcheck_gpg_trust_level *trust;
+
+	if (level < 0 || ARRAY_SIZE(sigcheck_gpg_trust_level) <= level)
+		BUG("invalid trust_level requested: %d", level);
+
+	trust = &sigcheck_gpg_trust_level[level];
+	if (trust->value != level)
+		BUG("sigcheck_gpg_trust_level[] unsorted");
+
+	if (!trust->downcased)
+		trust->downcased = xstrdup_tolower(trust->key);
+	return trust->downcased;
+}
+
 static void replace_cstring(char **field, const char *line, const char *next)
 {
 	free(*field);
diff --git c/gpg-interface.h w/gpg-interface.h
index b30cbdcd3d..2dffcb836d 100644
--- c/gpg-interface.h
+++ w/gpg-interface.h
@@ -85,4 +85,6 @@ int check_signature(struct signature_check *sigc,
 void print_signature_buffer(const struct signature_check *sigc,
 			    unsigned flags);
 
+const char *gpg_trust_level_to_string(enum signature_trust_level);
+
 #endif

  parent reply	other threads:[~2022-07-09 20:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

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=xmqqwncmt3el.fsf@gitster.g \
    --to=gitster@pobox.com \
    --cc=git@vger.kernel.org \
    --cc=gitgitgadget@gmail.com \
    --cc=jaydeepjd.8914@gmail.com \
    --cc=sunshine@sunshineco.com \
    /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).