Skip to content

Commit 803978d

Browse files
JDeepDgitster
authored andcommitted
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. Mentored-by: Christian Couder <[email protected]> Mentored-by: Hariom Verma <[email protected]> Signed-off-by: Jaydeep Das <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4a4b31 commit 803978d

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

gpg-interface.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,17 @@ static struct {
165165
{ 0, "TRUST_", GPG_STATUS_TRUST_LEVEL },
166166
};
167167

168-
static struct {
168+
/* Keep the order same as enum signature_trust_level */
169+
static struct sigcheck_gpg_trust_level {
169170
const char *key;
171+
const char *display_key;
170172
enum signature_trust_level value;
171173
} sigcheck_gpg_trust_level[] = {
172-
{ "UNDEFINED", TRUST_UNDEFINED },
173-
{ "NEVER", TRUST_NEVER },
174-
{ "MARGINAL", TRUST_MARGINAL },
175-
{ "FULLY", TRUST_FULLY },
176-
{ "ULTIMATE", TRUST_ULTIMATE },
174+
{ "UNDEFINED", "undefined", TRUST_UNDEFINED },
175+
{ "NEVER", "never", TRUST_NEVER },
176+
{ "MARGINAL", "marginal", TRUST_MARGINAL },
177+
{ "FULLY", "fully", TRUST_FULLY },
178+
{ "ULTIMATE", "ultimate", TRUST_ULTIMATE },
177179
};
178180

179181
static void replace_cstring(char **field, const char *line, const char *next)
@@ -905,6 +907,20 @@ const char *get_signing_key(void)
905907
return git_committer_info(IDENT_STRICT | IDENT_NO_DATE);
906908
}
907909

910+
const char *gpg_trust_level_to_str(enum signature_trust_level level)
911+
{
912+
struct sigcheck_gpg_trust_level *trust;
913+
914+
if (level < 0 || level >= ARRAY_SIZE(sigcheck_gpg_trust_level))
915+
BUG("invalid trust level requested %d", level);
916+
917+
trust = &sigcheck_gpg_trust_level[level];
918+
if (trust->value != level)
919+
BUG("sigcheck_gpg_trust_level[] unsorted");
920+
921+
return sigcheck_gpg_trust_level[level].display_key;
922+
}
923+
908924
int sign_buffer(struct strbuf *buffer, struct strbuf *signature, const char *signing_key)
909925
{
910926
return use_format->sign_buffer(buffer, signature, signing_key);

gpg-interface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ size_t parse_signed_buffer(const char *buf, size_t size);
7171
int sign_buffer(struct strbuf *buffer, struct strbuf *signature,
7272
const char *signing_key);
7373

74+
75+
/*
76+
* Returns corresponding string in lowercase for a given member of
77+
* enum signature_trust_level. For example, `TRUST_ULTIMATE` will
78+
* return "ultimate".
79+
*/
80+
const char *gpg_trust_level_to_str(enum signature_trust_level level);
81+
7482
int git_gpg_config(const char *, const char *, void *);
7583
void set_signing_key(const char *);
7684
const char *get_signing_key(void);

pretty.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,23 +1575,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
15751575
strbuf_addstr(sb, c->signature_check.primary_key_fingerprint);
15761576
break;
15771577
case 'T':
1578-
switch (c->signature_check.trust_level) {
1579-
case TRUST_UNDEFINED:
1580-
strbuf_addstr(sb, "undefined");
1581-
break;
1582-
case TRUST_NEVER:
1583-
strbuf_addstr(sb, "never");
1584-
break;
1585-
case TRUST_MARGINAL:
1586-
strbuf_addstr(sb, "marginal");
1587-
break;
1588-
case TRUST_FULLY:
1589-
strbuf_addstr(sb, "fully");
1590-
break;
1591-
case TRUST_ULTIMATE:
1592-
strbuf_addstr(sb, "ultimate");
1593-
break;
1594-
}
1578+
strbuf_addstr(sb, gpg_trust_level_to_str(c->signature_check.trust_level));
15951579
break;
15961580
default:
15971581
return 0;

0 commit comments

Comments
 (0)