From: Junio C Hamano <gitster@pobox.com>
To: git@vger.kernel.org
Cc: Simon Cheng <cyqsimon@gmail.com>
Subject: [PATCH 1/2] checkout: pass program-readable token to unified "main"
Date: Tue, 27 Jan 2026 11:29:35 -0800 [thread overview]
Message-ID: <20260127192936.904719-2-gitster@pobox.com> (raw)
In-Reply-To: <20260127192936.904719-1-gitster@pobox.com>
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 | 63 +++++++++++++++++++++++++++++++---------------
1 file changed, 43 insertions(+), 20 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index f9453473fe..4f189fde48 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -43,22 +43,6 @@
#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);
}
--
2.53.0-rc2-135-gb1217c0133
next prev parent reply other threads:[~2026-01-27 19:29 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-27 19:29 [PATCH 0/2] Improving advise messages from "switch" Junio C Hamano
2026-01-27 19:29 ` Junio C Hamano [this message]
2026-01-27 19:29 ` [PATCH 2/2] checkout: tell "parse_remote_branch" which command is calling it Junio C Hamano
2026-01-27 20:35 ` Kristoffer Haugsbakk
2026-01-27 21:22 ` Junio C Hamano
2026-01-29 19:06 ` [PATCH v2 0/2] Improving advise messages from "switch" Junio C Hamano
2026-01-29 19:06 ` [PATCH v2 1/2] checkout: pass program-readable token to unified "main" Junio C Hamano
2026-02-06 16:05 ` Patrick Steinhardt
2026-02-19 22:21 ` Junio C Hamano
2026-01-29 19:06 ` [PATCH v2 2/2] checkout: tell "parse_remote_branch" which command is calling it Junio C Hamano
2026-02-06 16:05 ` Patrick Steinhardt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260127192936.904719-2-gitster@pobox.com \
--to=gitster@pobox.com \
--cc=cyqsimon@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox