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

From: Junio C Hamano <[email-masked]>
Date: Thu, 07 Jul 2022 11:18:20 -0700

"Jaydeep Das via GitGitGadget" <[email-masked]> writes:

> 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);
> +			sig_str = gpg_trust_level_to_str(c->signature_check.trust_level);
> +			if (sig_str){

Missing SP before open-brace.

> +				const char *sig_str_lower = xstrdup_tolower(sig_str);
> +				strbuf_addstr(sb, sig_str_lower);
> +				free((char *)sig_str_lower);

Unnecessary const plus casting-away of it.  You are getting a copy
to work with, so there is no reason to declare sig_str_lower to be
"const".  This downcasing should be done in gpg_trust_level_to_str()
function, shouldn't it?  After all, the "str" version of the trust
level existing end-users are familiar with are the strings you
removed from pretty.c that are all lowercase.

Thanks.