]> git.ipfire.org Git - thirdparty/git.git/commitdiff
checkout: pass program-readable token to unified "main"
authorJunio C Hamano <gitster@pobox.com>
Thu, 29 Jan 2026 19:06:15 +0000 (11:06 -0800)
committerJunio C Hamano <gitster@pobox.com>
Mon, 9 Feb 2026 16:33:17 +0000 (08:33 -0800)
The "git checkout", "git switch", and "git restore" commands share a
single implementation, checkout_main(), which switches error message
it gives using the usage string passed by each of these three
front-ends.

In order to be able to tweak behaviours of the commands based on
which one we are executing, invent an enum that denotes which one of
these three commands is currently executing, and pass that to
checkout_main() instead.  With this step, there is no externally
visible behaviour change, as this enum parameter is only used to
choose among the three usage strings.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c

index f9453473fe2a20076fd878bfb921f96e4ff7cc39..2411108856d60ee1e4b4a9b020eecc2f563805a0 100644 (file)
 #include "parallel-checkout.h"
 #include "add-interactive.h"
 
-static const char * const checkout_usage[] = {
-       N_("git checkout [<options>] <branch>"),
-       N_("git checkout [<options>] [<branch>] -- <file>..."),
-       NULL,
-};
-
-static const char * const switch_branch_usage[] = {
-       N_("git switch [<options>] [<branch>]"),
-       NULL,
-};
-
-static const char * const restore_usage[] = {
-       N_("git restore [<options>] [--source=<branch>] <file>..."),
-       NULL,
-};
-
 struct checkout_opts {
        int patch_mode;
        int patch_context;
@@ -1293,6 +1277,13 @@ static void setup_new_branch_info_and_source_tree(
        }
 }
 
+
+enum checkout_command {
+       CHECKOUT_CHECKOUT = 1,
+       CHECKOUT_SWITCH = 2,
+       CHECKOUT_RESTORE = 3,
+};
+
 static char *parse_remote_branch(const char *arg,
                                 struct object_id *rev,
                                 int could_be_checkout_paths)
@@ -1767,12 +1758,44 @@ static char cb_option = 'b';
 
 static int checkout_main(int argc, const char **argv, const char *prefix,
                         struct checkout_opts *opts, struct option *options,
-                        const char * const usagestr[])
+                        enum checkout_command which_command)
 {
        int parseopt_flags = 0;
        struct branch_info new_branch_info = { 0 };
        int ret;
 
+       static const char * const checkout_usage[] = {
+               N_("git checkout [<options>] <branch>"),
+               N_("git checkout [<options>] [<branch>] -- <file>..."),
+               NULL,
+       };
+
+       static const char * const switch_branch_usage[] = {
+               N_("git switch [<options>] [<branch>]"),
+               NULL,
+       };
+
+       static const char * const restore_usage[] = {
+               N_("git restore [<options>] [--source=<branch>] <file>..."),
+               NULL,
+       };
+
+       const char * const *usagestr;
+
+       switch (which_command) {
+       case CHECKOUT_CHECKOUT:
+               usagestr = checkout_usage;
+               break;
+       case CHECKOUT_SWITCH:
+               usagestr = switch_branch_usage;
+               break;
+       case CHECKOUT_RESTORE:
+               usagestr = restore_usage;
+               break;
+       default:
+               BUG("no such checkout variant %d", which_command);
+       }
+
        opts->overwrite_ignore = 1;
        opts->prefix = prefix;
        opts->show_progress = -1;
@@ -2032,7 +2055,7 @@ int cmd_checkout(int argc,
        options = add_checkout_path_options(&opts, options);
 
        return checkout_main(argc, argv, prefix, &opts, options,
-                            checkout_usage);
+                            CHECKOUT_CHECKOUT);
 }
 
 int cmd_switch(int argc,
@@ -2071,7 +2094,7 @@ int cmd_switch(int argc,
        cb_option = 'c';
 
        return checkout_main(argc, argv, prefix, &opts, options,
-                            switch_branch_usage);
+                            CHECKOUT_SWITCH);
 }
 
 int cmd_restore(int argc,
@@ -2107,5 +2130,5 @@ int cmd_restore(int argc,
        options = add_checkout_path_options(&opts, options);
 
        return checkout_main(argc, argv, prefix, &opts, options,
-                            restore_usage);
+                            CHECKOUT_RESTORE);
 }