Skip to content

Commit 2f8fd20

Browse files
chriscoolgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4975ec3 commit 2f8fd20

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

builtin/fast-export.c

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ static const char *const fast_export_usage[] = {
3737
NULL
3838
};
3939

40-
enum sign_mode { SIGN_ABORT, SIGN_VERBATIM, SIGN_STRIP, SIGN_WARN_VERBATIM, SIGN_WARN_STRIP };
41-
4240
static int progress;
4341
static enum sign_mode signed_tag_mode = SIGN_ABORT;
4442
static enum sign_mode signed_commit_mode = SIGN_STRIP;
@@ -59,23 +57,16 @@ static struct hashmap anonymized_seeds;
5957
static struct revision_sources revision_sources;
6058

6159
static int parse_opt_sign_mode(const struct option *opt,
62-
const char *arg, int unset)
60+
const char *arg, int unset)
6361
{
6462
enum sign_mode *val = opt->value;
63+
6564
if (unset)
6665
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))
7868
return error("Unknown %s mode: %s", opt->long_name, arg);
69+
7970
return 0;
8071
}
8172

gpg-interface.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,3 +1125,20 @@ static int sign_buffer_ssh(struct strbuf *buffer, struct strbuf *signature,
11251125
FREE_AND_NULL(ssh_signing_key_file);
11261126
return ret;
11271127
}
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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,19 @@ int check_signature(struct signature_check *sigc,
104104
void print_signature_buffer(const struct signature_check *sigc,
105105
unsigned flags);
106106

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+
107122
#endif

0 commit comments

Comments
 (0)