]> git.ipfire.org Git - thirdparty/git.git/commitdiff
gpg-interface: refactor 'enum sign_mode' parsing
authorChristian Couder <christian.couder@gmail.com>
Wed, 17 Sep 2025 18:14:26 +0000 (20:14 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 17 Sep 2025 18:18:28 +0000 (11:18 -0700)
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>
builtin/fast-export.c
gpg-interface.c
gpg-interface.h

index c06ee0b213502e2fab005a3fc0bef017e657a2d9..dc2486f9a83a9b00632c68148b5f2277c58cfa74 100644 (file)
@@ -37,8 +37,6 @@ static const char *const fast_export_usage[] = {
        NULL
 };
 
-enum sign_mode { SIGN_ABORT, SIGN_VERBATIM, SIGN_STRIP, SIGN_WARN_VERBATIM, SIGN_WARN_STRIP };
-
 static int progress;
 static enum sign_mode signed_tag_mode = SIGN_ABORT;
 static enum sign_mode signed_commit_mode = SIGN_STRIP;
@@ -59,23 +57,16 @@ static struct hashmap anonymized_seeds;
 static struct revision_sources revision_sources;
 
 static int parse_opt_sign_mode(const struct option *opt,
-                                    const char *arg, int unset)
+                              const char *arg, int unset)
 {
        enum sign_mode *val = opt->value;
+
        if (unset)
                return 0;
-       else if (!strcmp(arg, "abort"))
-               *val = SIGN_ABORT;
-       else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
-               *val = SIGN_VERBATIM;
-       else if (!strcmp(arg, "warn-verbatim") || !strcmp(arg, "warn"))
-               *val = SIGN_WARN_VERBATIM;
-       else if (!strcmp(arg, "warn-strip"))
-               *val = SIGN_WARN_STRIP;
-       else if (!strcmp(arg, "strip"))
-               *val = SIGN_STRIP;
-       else
+
+       if (parse_sign_mode(arg, val))
                return error("Unknown %s mode: %s", opt->long_name, arg);
+
        return 0;
 }
 
index 06e7fb50603d22893e92c9c5f473e3c04d3396bf..2f4f0e32cb3b4f1fa245de4e7822cffaa5a31c9d 100644 (file)
@@ -1125,3 +1125,20 @@ out:
        FREE_AND_NULL(ssh_signing_key_file);
        return ret;
 }
+
+int parse_sign_mode(const char *arg, enum sign_mode *mode)
+{
+       if (!strcmp(arg, "abort"))
+               *mode = SIGN_ABORT;
+       else if (!strcmp(arg, "verbatim") || !strcmp(arg, "ignore"))
+               *mode = SIGN_VERBATIM;
+       else if (!strcmp(arg, "warn-verbatim") || !strcmp(arg, "warn"))
+               *mode = SIGN_WARN_VERBATIM;
+       else if (!strcmp(arg, "warn-strip"))
+               *mode = SIGN_WARN_STRIP;
+       else if (!strcmp(arg, "strip"))
+               *mode = SIGN_STRIP;
+       else
+               return -1;
+       return 0;
+}
index 60ddf8bbfa38338abf5fae26cbcafa0af2b21d14..50487aa14832743e2d294893319c3bfa70332448 100644 (file)
@@ -104,4 +104,19 @@ int check_signature(struct signature_check *sigc,
 void print_signature_buffer(const struct signature_check *sigc,
                            unsigned flags);
 
+/* Modes for --signed-tags=<mode> and --signed-commits=<mode> options. */
+enum sign_mode {
+       SIGN_ABORT,
+       SIGN_WARN_VERBATIM,
+       SIGN_VERBATIM,
+       SIGN_WARN_STRIP,
+       SIGN_STRIP,
+};
+
+/*
+ * Return 0 if `arg` can be parsed into an `enum sign_mode`. Return -1
+ * otherwise.
+ */
+int parse_sign_mode(const char *arg, enum sign_mode *mode);
+
 #endif