]> git.ipfire.org Git - thirdparty/git.git/commitdiff
replay: start using parse_options API
authorElijah Newren <newren@gmail.com>
Fri, 24 Nov 2023 11:10:32 +0000 (12:10 +0100)
committerJunio C Hamano <gitster@pobox.com>
Sun, 26 Nov 2023 01:10:48 +0000 (10:10 +0900)
Instead of manually parsing arguments, let's start using the parse_options
API. This way this new builtin will look more standard, and in some
upcoming commits will more easily be able to handle more command line
options.

Note that we plan to later use standard revision ranges instead of
hardcoded "<oldbase> <branch>" arguments. When we will use standard
revision ranges, it will be easier to check if there are no spurious
arguments if we keep ARGV[0], so let's call parse_options() with
PARSE_OPT_KEEP_ARGV0 even if we don't need ARGV[0] right now to avoid
some useless code churn.

Co-authored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/replay.c

index 1998134683375e95b0858fee0361acad36e0c216..7998f6ed0409e8aa15b89542e803c5d40ce6a682 100644 (file)
@@ -15,7 +15,7 @@
 #include "lockfile.h"
 #include "merge-ort.h"
 #include "object-name.h"
-#include "read-cache-ll.h"
+#include "parse-options.h"
 #include "refs.h"
 #include "revision.h"
 #include "sequencer.h"
@@ -92,6 +92,7 @@ static struct commit *create_commit(struct tree *tree,
 int cmd_replay(int argc, const char **argv, const char *prefix)
 {
        struct commit *onto;
+       const char *onto_name = NULL;
        struct commit *last_commit = NULL, *last_picked_commit = NULL;
        struct object_id head;
        struct lock_file lock = LOCK_INIT;
@@ -105,16 +106,32 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
        struct strbuf branch_name = STRBUF_INIT;
        int ret = 0;
 
-       if (argc == 2 && !strcmp(argv[1], "-h")) {
-               printf("usage: (EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>\n");
-               exit(129);
+       const char * const replay_usage[] = {
+               N_("(EXPERIMENTAL!) git replay --onto <newbase> <oldbase> <branch>"),
+               NULL
+       };
+       struct option replay_options[] = {
+               OPT_STRING(0, "onto", &onto_name,
+                          N_("revision"),
+                          N_("replay onto given commit")),
+               OPT_END()
+       };
+
+       argc = parse_options(argc, argv, prefix, replay_options, replay_usage,
+                            PARSE_OPT_KEEP_ARGV0 | PARSE_OPT_KEEP_UNKNOWN_OPT);
+
+       if (!onto_name) {
+               error(_("option --onto is mandatory"));
+               usage_with_options(replay_usage, replay_options);
        }
 
-       if (argc != 5 || strcmp(argv[1], "--onto"))
-               die("usage: read the code, figure out how to use it, then do so");
+       if (argc != 3) {
+               error(_("bad number of arguments"));
+               usage_with_options(replay_usage, replay_options);
+       }
 
-       onto = peel_committish(argv[2]);
-       strbuf_addf(&branch_name, "refs/heads/%s", argv[4]);
+       onto = peel_committish(onto_name);
+       strbuf_addf(&branch_name, "refs/heads/%s", argv[2]);
 
        /* Sanity check */
        if (repo_get_oid(the_repository, "HEAD", &head))
@@ -126,6 +143,7 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
                BUG("Could not read index");
 
        repo_init_revisions(the_repository, &revs, prefix);
+
        revs.verbose_header = 1;
        revs.max_parents = 1;
        revs.cherry_mark = 1;
@@ -134,7 +152,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
        revs.right_only = 1;
        revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
        revs.topo_order = 1;
-       strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
+
+       strvec_pushl(&rev_walk_args, "", argv[2], "--not", argv[1], NULL);
 
        if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) {
                ret = error(_("unhandled options"));
@@ -197,8 +216,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
                               &last_commit->object.oid,
                               &last_picked_commit->object.oid,
                               REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
-                       error(_("could not update %s"), argv[4]);
-                       die("Failed to update %s", argv[4]);
+                       error(_("could not update %s"), argv[2]);
+                       die("Failed to update %s", argv[2]);
                }
                if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
                        die(_("unable to update HEAD"));
@@ -210,8 +229,8 @@ int cmd_replay(int argc, const char **argv, const char *prefix)
                               &last_commit->object.oid,
                               &head,
                               REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
-                       error(_("could not update %s"), argv[4]);
-                       die("Failed to update %s", argv[4]);
+                       error(_("could not update %s"), argv[2]);
+                       die("Failed to update %s", argv[2]);
                }
        }
        ret = (result.clean == 0);