gpg-interface: refactor 'enum sign_mode' parsing

The definition of 'enum sign_mode' as well as its parsing code are in "builtin/fast-export.c". This was fine because `git fast-export` was the only command with '--signed-tags=<mode>' or '--signed-commits=<mode>' options. In a following commit, we are going to add a similar option to `git fast-import`, which will be simpler, easier and cleaner if we can reuse the 'enum sign_mode' defintion and parsing code. So let's move that definition and parsing code from "builtin/fast-export.c" to "gpg-interface.{c,h}". While at it, let's fix a small indentation issue with the arguments of parse_opt_sign_mode(). Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Sep 17, 2025 at 20:14 UTC 2f8fd208c36bf2e88f949d0c4059214dfcb2a717
3 files changed +37 -14
builtin/fast-export.c
+5 -14
@@ -37,8 +37,6 @@ static const char *const fast_export_usage[] = {
37 NULL
38 };
39
40 -enum sign_mode { SIGN_ABORT, SIGN_VERBATIM, SIGN_STRIP, SIGN_WARN_VERBATIM, SIGN_WARN_STRIP };
41 -
40 static int progress;
41 static enum sign_mode signed_tag_mode = SIGN_ABORT;
42 static enum sign_mode signed_commit_mode = SIGN_STRIP;
@@ -59,23 +57,16 @@ static struct hashmap anonymized_seeds;
57 static struct revision_sources revision_sources;
58
59 static int parse_opt_sign_mode(const struct option *opt,
62 - const char *arg, int unset)
60 + const char *arg, int unset)
61 {
62 enum sign_mode *val = opt->value;
63 +
64 if (unset)
65 return 0;
67 - else if (!strcmp(arg, "abort"))
68 - *val = SIGN_ABORT;
69 - else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
70 - *val = SIGN_VERBATIM;
71 - else if (!strcmp(arg, "warn-verbatim") || !strcmp(arg, "warn"))
72 - *val = SIGN_WARN_VERBATIM;
73 - else if (!strcmp(arg, "warn-strip"))
74 - *val = SIGN_WARN_STRIP;
75 - else if (!strcmp(arg, "strip"))
76 - *val = SIGN_STRIP;
77 - else
66 +
67 + if (parse_sign_mode(arg, val))
68 return error("Unknown %s mode: %s", opt->long_name, arg);
69 +
70 return 0;
71 }
72
gpg-interface.c
+17
@@ -1125,3 +1125,20 @@ out:
1125 FREE_AND_NULL(ssh_signing_key_file);
1126 return ret;
1127 }
1128 +
1129 +int parse_sign_mode(const char *arg, enum sign_mode *mode)
1130 +{
1131 + if (!strcmp(arg, "abort"))
1132 + *mode = SIGN_ABORT;
1133 + else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
1134 + *mode = SIGN_VERBATIM;
1135 + else if (!strcmp(arg, "warn-verbatim") || !strcmp(arg, "warn"))
1136 + *mode = SIGN_WARN_VERBATIM;
1137 + else if (!strcmp(arg, "warn-strip"))
1138 + *mode = SIGN_WARN_STRIP;
1139 + else if (!strcmp(arg, "strip"))
1140 + *mode = SIGN_STRIP;
1141 + else
1142 + return -1;
1143 + return 0;
1144 +}
gpg-interface.h
+15
@@ -104,4 +104,19 @@ int check_signature(struct signature_check *sigc,
104 void print_signature_buffer(const struct signature_check *sigc,
105 unsigned flags);
106
107 +/* Modes for --signed-tags=<mode> and --signed-commits=<mode> options. */
108 +enum sign_mode {
109 + SIGN_ABORT,
110 + SIGN_WARN_VERBATIM,
111 + SIGN_VERBATIM,
112 + SIGN_WARN_STRIP,
113 + SIGN_STRIP,
114 +};
115 +
116 +/*
117 + * Return 0 if `arg` can be parsed into an `enum sign_mode`. Return -1
118 + * otherwise.
119 + */
120 +int parse_sign_mode(const char *arg, enum sign_mode *mode);
121 +
122 #endif