checkout: pass program-readable token to unified "main"

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>

Junio C Hamano committed Jan 29, 2026 at 11:06 UTC 2096948158ea0b253a68a81c691fdf88ff00589a
1 file changed +43 -20
builtin/checkout.c
+43 -20
@@ -43,22 +43,6 @@
43 #include "parallel-checkout.h"
44 #include "add-interactive.h"
45
46 -static const char * const checkout_usage[] = {
47 - N_("git checkout [<options>] <branch>"),
48 - N_("git checkout [<options>] [<branch>] -- <file>..."),
49 - NULL,
50 -};
51 -
52 -static const char * const switch_branch_usage[] = {
53 - N_("git switch [<options>] [<branch>]"),
54 - NULL,
55 -};
56 -
57 -static const char * const restore_usage[] = {
58 - N_("git restore [<options>] [--source=<branch>] <file>..."),
59 - NULL,
60 -};
61 -
46 struct checkout_opts {
47 int patch_mode;
48 int patch_context;
@@ -1293,6 +1277,13 @@ static void setup_new_branch_info_and_source_tree(
1277 }
1278 }
1279
1280 +
1281 +enum checkout_command {
1282 + CHECKOUT_CHECKOUT = 1,
1283 + CHECKOUT_SWITCH = 2,
1284 + CHECKOUT_RESTORE = 3,
1285 +};
1286 +
1287 static char *parse_remote_branch(const char *arg,
1288 struct object_id *rev,
1289 int could_be_checkout_paths)
@@ -1767,12 +1758,44 @@ static char cb_option = 'b';
1758
1759 static int checkout_main(int argc, const char **argv, const char *prefix,
1760 struct checkout_opts *opts, struct option *options,
1770 - const char * const usagestr[])
1761 + enum checkout_command which_command)
1762 {
1763 int parseopt_flags = 0;
1764 struct branch_info new_branch_info = { 0 };
1765 int ret;
1766
1767 + static const char * const checkout_usage[] = {
1768 + N_("git checkout [<options>] <branch>"),
1769 + N_("git checkout [<options>] [<branch>] -- <file>..."),
1770 + NULL,
1771 + };
1772 +
1773 + static const char * const switch_branch_usage[] = {
1774 + N_("git switch [<options>] [<branch>]"),
1775 + NULL,
1776 + };
1777 +
1778 + static const char * const restore_usage[] = {
1779 + N_("git restore [<options>] [--source=<branch>] <file>..."),
1780 + NULL,
1781 + };
1782 +
1783 + const char * const *usagestr;
1784 +
1785 + switch (which_command) {
1786 + case CHECKOUT_CHECKOUT:
1787 + usagestr = checkout_usage;
1788 + break;
1789 + case CHECKOUT_SWITCH:
1790 + usagestr = switch_branch_usage;
1791 + break;
1792 + case CHECKOUT_RESTORE:
1793 + usagestr = restore_usage;
1794 + break;
1795 + default:
1796 + BUG("no such checkout variant %d", which_command);
1797 + }
1798 +
1799 opts->overwrite_ignore = 1;
1800 opts->prefix = prefix;
1801 opts->show_progress = -1;
@@ -2032,7 +2055,7 @@ int cmd_checkout(int argc,
2055 options = add_checkout_path_options(&opts, options);
2056
2057 return checkout_main(argc, argv, prefix, &opts, options,
2035 - checkout_usage);
2058 + CHECKOUT_CHECKOUT);
2059 }
2060
2061 int cmd_switch(int argc,
@@ -2071,7 +2094,7 @@ int cmd_switch(int argc,
2094 cb_option = 'c';
2095
2096 return checkout_main(argc, argv, prefix, &opts, options,
2074 - switch_branch_usage);
2097 + CHECKOUT_SWITCH);
2098 }
2099
2100 int cmd_restore(int argc,
@@ -2107,5 +2130,5 @@ int cmd_restore(int argc,
2130 options = add_checkout_path_options(&opts, options);
2131
2132 return checkout_main(argc, argv, prefix, &opts, options,
2110 - restore_usage);
2133 + CHECKOUT_RESTORE);
2134 }