{"thread":{"id":"53345","subject":"Git error message suggests an invalid switch","startedAt":"2020-04-28T23:24:59Z","lastAt":"2020-04-30T20:45:14Z","messageCount":8,"participants":["Robert Simpson","Denton Liu","Taylor Blau","Eric Sunshine","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"396518","messageId":"60af77100df823b4112c08e6c1b8533f.squirrel@socialwebmail.com","threadId":"53345","inReplyTo":null,"subject":"Git error message suggests an invalid switch","fromName":"Robert Simpson","fromEmail":"no-reply@mailscreen.com","sentAt":"2020-04-28T23:12:42Z","receivedAt":"2020-04-28T23:24:59Z","isPatch":false,"sender":{"key":"no-reply@mailscreen.com","avatar":null},"body":"The command below shows an error message that says \"try -b\". However \"-b\"\nis not a valid switch for the command, as indicated by \"unknown swtich\n'b'\" when that has been added to the command.\n\n--------------------------------------------------------------------\ngit switch \"master\" --track \"origin/master\"\n\nfatal: missing branch name; try -b\n\ngit switch -b \"master\" --track \"origin/master\"\n\nerror: unknown switch `b'\nusage: git switch [<options>] [<branch>]\n\n    -c, --create <branch>\n                          create and switch to a new branch\n    -C, --force-create <branch>\n                          create/reset and switch to a branch\n    --guess               second guess 'git switch <no-such-branch>'\n    --discard-changes     throw away local modifications\n    -q, --quiet           suppress progress reporting\n    --recurse-submodules[=<checkout>]\n                          control recursive updating of submodules\n    --progress            force progress reporting\n    -m, --merge           perform a 3-way merge with the new branch\n    --conflict <style>    conflict style (merge or diff3)\n    -d, --detach          detach HEAD at named commit\n    -t, --track           set upstream info for new branch\n    -f, --force           force checkout (throw away local modifications)\n    --orphan <new-branch>\n                          new unparented branch\n    --overwrite-ignore    update ignored files (default)\n    --ignore-other-worktrees\n                          do not check if another worktree is holding the\ngiven ref\n\n"},{"id":"396530","messageId":"0f7f9eefc056dd4d9b11dab737e00235b3243a2f.1588150804.git.liu.denton@gmail.com","threadId":"53345","inReplyTo":"60af77100df823b4112c08e6c1b8533f.squirrel@socialwebmail.com","subject":"[PATCH] switch: fix errors and comments related to -c and -C","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-04-29T09:00:19Z","receivedAt":"2020-04-29T09:00:31Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In d787d311db (checkout: split part of it to new command 'switch',\n2019-03-29), the `git switch` command was created by extracting the\ncommon functionality of cmd_checkout() in checkout_main(). However, in\nb7b5fce270 (switch: better names for -b and -B, 2019-03-29), these\nthe branch creation and force creation options for 'switch' were changed\nto -c and -C, respectively. As a result of this, error messages and\ncomments that previously referred to `-b` and `-B` became invalid for\n`git switch`.\n\nFor comments that refer to `-b` and `-B`, add `-c` and `-C` to the\ncomment.\n\nFor error messages that refer to `-b`, introduce `enum cmd_variant` and\nuse it to differentiate between `checkout` and `switch` when printing\nout error messages.\n\nAn alternative implementation which was considered involved inserting\noption name variants into a struct which is passed in by each command\nvariant. Even though this approach is more general and could be\napplicable for future differing option names, it seemed like an\nover-engineered solution when the current pair of options are the only\ndiffering ones. We should probably avoid adding options which have\ndifferent names anyway.\n\nReported-by: Robert Simpson <no-reply@MailScreen.com>\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n\nNotes:\n    Robert, is the email listed above correct? If not, please let me know\n    which email to use. (I hope that this reaches you somehow.)\n\n builtin/checkout.c | 26 ++++++++++++++++++--------\n 1 file changed, 18 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 8bc94d392b..0ca74cde08 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -1544,9 +1544,16 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,\n \treturn newopts;\n }\n \n+enum cmd_variant {\n+\tCMD_CHECKOUT,\n+\tCMD_SWITCH,\n+\tCMD_RESTORE\n+};\n+\n static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\t struct checkout_opts *opts, struct option *options,\n-\t\t\t const char * const usagestr[])\n+\t\t\t const char * const usagestr[],\n+\t\t\t enum cmd_variant variant)\n {\n \tstruct branch_info new_branch_info;\n \tint parseopt_flags = 0;\n@@ -1586,7 +1593,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)\n-\t\tdie(_(\"-b, -B and --orphan are mutually exclusive\"));\n+\t\tdie(variant == CMD_CHECKOUT ?\n+\t\t\t\t_(\"-b, -B and --orphan are mutually exclusive\") :\n+\t\t\t\t_(\"-c, -C and --orphan are mutually exclusive\"));\n \n \tif (opts->overlay_mode == 1 && opts->patch_mode)\n \t\tdie(_(\"-p and --overlay are mutually exclusive\"));\n@@ -1614,7 +1623,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t/*\n \t * From here on, new_branch will contain the branch to be checked out,\n \t * and new_branch_force and new_orphan_branch will tell us which one of\n-\t * -b/-B/--orphan is being used.\n+\t * -b/-B/-c/-C/--orphan is being used.\n \t */\n \tif (opts->new_branch_force)\n \t\topts->new_branch = opts->new_branch_force;\n@@ -1622,7 +1631,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \tif (opts->new_orphan_branch)\n \t\topts->new_branch = opts->new_orphan_branch;\n \n-\t/* --track without -b/-B/--orphan should DWIM */\n+\t/* --track without -b/-B/--orphan for checkout or -c/-C/--orphan for switch should DWIM */\n \tif (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {\n \t\tconst char *argv0 = argv[0];\n \t\tif (!argc || !strcmp(argv0, \"--\"))\n@@ -1631,7 +1640,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\tskip_prefix(argv0, \"remotes/\", &argv0);\n \t\targv0 = strchr(argv0, '/');\n \t\tif (!argv0 || !argv0[1])\n-\t\t\tdie(_(\"missing branch name; try -b\"));\n+\t\t\tdie(_(\"missing branch name; try -%c\"),\n+\t\t\t\t\tvariant == CMD_CHECKOUT ? 'b' : 'c');\n \t\topts->new_branch = argv0 + 1;\n \t}\n \n@@ -1785,7 +1795,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n \toptions = add_checkout_path_options(&opts, options);\n \n \tret = checkout_main(argc, argv, prefix, &opts,\n-\t\t\t    options, checkout_usage);\n+\t\t\t    options, checkout_usage, CMD_CHECKOUT);\n \tFREE_AND_NULL(options);\n \treturn ret;\n }\n@@ -1823,7 +1833,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)\n \toptions = add_common_switch_branch_options(&opts, options);\n \n \tret = checkout_main(argc, argv, prefix, &opts,\n-\t\t\t    options, switch_branch_usage);\n+\t\t\t    options, switch_branch_usage, CMD_SWITCH);\n \tFREE_AND_NULL(options);\n \treturn ret;\n }\n@@ -1860,7 +1870,7 @@ int cmd_restore(int argc, const char **argv, const char *prefix)\n \toptions = add_checkout_path_options(&opts, options);\n \n \tret = checkout_main(argc, argv, prefix, &opts,\n-\t\t\t    options, restore_usage);\n+\t\t\t    options, restore_usage, CMD_RESTORE);\n \tFREE_AND_NULL(options);\n \treturn ret;\n }\n-- \n2.26.2.548.gbb00c8a0a9\n\n"},{"id":"396562","messageId":"20200429160948.GB83442@syl.local","threadId":"53345","inReplyTo":"0f7f9eefc056dd4d9b11dab737e00235b3243a2f.1588150804.git.liu.denton@gmail.com","subject":"Re: [PATCH] switch: fix errors and comments related to -c and -C","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2020-04-29T16:09:48Z","receivedAt":"2020-04-29T16:09:52Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"Hi Denton,\n\nOn Wed, Apr 29, 2020 at 05:00:19AM -0400, Denton Liu wrote:\n> In d787d311db (checkout: split part of it to new command 'switch',\n> 2019-03-29), the `git switch` command was created by extracting the\n> common functionality of cmd_checkout() in checkout_main(). However, in\n> b7b5fce270 (switch: better names for -b and -B, 2019-03-29), these\n> the branch creation and force creation options for 'switch' were changed\n> to -c and -C, respectively. As a result of this, error messages and\n> comments that previously referred to `-b` and `-B` became invalid for\n> `git switch`.\n>\n> For comments that refer to `-b` and `-B`, add `-c` and `-C` to the\n> comment.\n\nI had to read this sentence a couple of times more than I would have\nliked to in order to grok it fully. Would it be perhaps clearer as:\n\n  Update comments in 'cmd_checkout()' that mention `-b` or `-B` to\n  instead refer to `-c` or `-C` when invoked from 'git switch'.\n\n?\n\n> For error messages that refer to `-b`, introduce `enum cmd_variant` and\n> use it to differentiate between `checkout` and `switch` when printing\n> out error messages.\n>\n> An alternative implementation which was considered involved inserting\n> option name variants into a struct which is passed in by each command\n> variant. Even though this approach is more general and could be\n> applicable for future differing option names, it seemed like an\n> over-engineered solution when the current pair of options are the only\n> differing ones. We should probably avoid adding options which have\n> different names anyway.\n\nYeah, I don't think we should spend much time trying to figure out a\ngeneral solution here when these are the only differing pair.\n\n> Reported-by: Robert Simpson <no-reply@MailScreen.com>\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>\n> Notes:\n>     Robert, is the email listed above correct? If not, please let me know\n>     which email to use. (I hope that this reaches you somehow.)\n\nI'll be shocked if this is his real email address ;).\n\n>  builtin/checkout.c | 26 ++++++++++++++++++--------\n>  1 file changed, 18 insertions(+), 8 deletions(-)\n>\n> diff --git a/builtin/checkout.c b/builtin/checkout.c\n> index 8bc94d392b..0ca74cde08 100644\n> --- a/builtin/checkout.c\n> +++ b/builtin/checkout.c\n> @@ -1544,9 +1544,16 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,\n>  \treturn newopts;\n>  }\n>\n> +enum cmd_variant {\n> +\tCMD_CHECKOUT,\n> +\tCMD_SWITCH,\n> +\tCMD_RESTORE\n> +};\n> +\n>  static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t\t\t struct checkout_opts *opts, struct option *options,\n> -\t\t\t const char * const usagestr[])\n> +\t\t\t const char * const usagestr[],\n> +\t\t\t enum cmd_variant variant)\n>  {\n>  \tstruct branch_info new_branch_info;\n>  \tint parseopt_flags = 0;\n> @@ -1586,7 +1593,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t}\n>\n>  \tif ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)\n> -\t\tdie(_(\"-b, -B and --orphan are mutually exclusive\"));\n> +\t\tdie(variant == CMD_CHECKOUT ?\n> +\t\t\t\t_(\"-b, -B and --orphan are mutually exclusive\") :\n> +\t\t\t\t_(\"-c, -C and --orphan are mutually exclusive\"));\n\nHmm. Do we need to generate an extra string for translation here? If the\nstring was instead:\n\n  _(\"%s and --orphan are mutually exclusive\")\n\nwhere the first format string is filled in something like:\n\n  die(_(\"%s and --orphan are mutually exclusive\"),\n      variant == CMD_CHECKOUT ? \"-b, -B\" : \"-c, -C\");\n\nmay save translators some work.\n\n>  \tif (opts->overlay_mode == 1 && opts->patch_mode)\n>  \t\tdie(_(\"-p and --overlay are mutually exclusive\"));\n> @@ -1614,7 +1623,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t/*\n>  \t * From here on, new_branch will contain the branch to be checked out,\n>  \t * and new_branch_force and new_orphan_branch will tell us which one of\n> -\t * -b/-B/--orphan is being used.\n> +\t * -b/-B/-c/-C/--orphan is being used.\n>  \t */\n>  \tif (opts->new_branch_force)\n>  \t\topts->new_branch = opts->new_branch_force;\n> @@ -1622,7 +1631,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \tif (opts->new_orphan_branch)\n>  \t\topts->new_branch = opts->new_orphan_branch;\n>\n> -\t/* --track without -b/-B/--orphan should DWIM */\n> +\t/* --track without -b/-B/--orphan for checkout or -c/-C/--orphan for switch should DWIM */\n\nThis line is getting a little long. Would you mind wrapping this as a\nmulti-line comment instead?\n\n>  \tif (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {\n>  \t\tconst char *argv0 = argv[0];\n>  \t\tif (!argc || !strcmp(argv0, \"--\"))\n> @@ -1631,7 +1640,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t\tskip_prefix(argv0, \"remotes/\", &argv0);\n>  \t\targv0 = strchr(argv0, '/');\n>  \t\tif (!argv0 || !argv0[1])\n> -\t\t\tdie(_(\"missing branch name; try -b\"));\n> +\t\t\tdie(_(\"missing branch name; try -%c\"),\n> +\t\t\t\t\tvariant == CMD_CHECKOUT ? 'b' : 'c');\n>  \t\topts->new_branch = argv0 + 1;\n>  \t}\n>\n> @@ -1785,7 +1795,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n>  \toptions = add_checkout_path_options(&opts, options);\n>\n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, checkout_usage);\n> +\t\t\t    options, checkout_usage, CMD_CHECKOUT);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n> @@ -1823,7 +1833,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)\n>  \toptions = add_common_switch_branch_options(&opts, options);\n>\n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, switch_branch_usage);\n> +\t\t\t    options, switch_branch_usage, CMD_SWITCH);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n> @@ -1860,7 +1870,7 @@ int cmd_restore(int argc, const char **argv, const char *prefix)\n>  \toptions = add_checkout_path_options(&opts, options);\n>\n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, restore_usage);\n> +\t\t\t    options, restore_usage, CMD_RESTORE);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n> --\n> 2.26.2.548.gbb00c8a0a9\n\nAll of the rest makes sense, thanks.\n\nThanks,\nTaylor\n"},{"id":"396566","messageId":"CAPig+cS-f7JOkhW7yf_h4bXx75Y_=_0e7uPTCfkBCHvc8WSUBw@mail.gmail.com","threadId":"53345","inReplyTo":"20200429160948.GB83442@syl.local","subject":"Re: [PATCH] switch: fix errors and comments related to -c and -C","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2020-04-29T16:31:33Z","receivedAt":"2020-04-29T16:31:49Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Wed, Apr 29, 2020 at 12:10 PM Taylor Blau <me@ttaylorr.com> wrote:\n> On Wed, Apr 29, 2020 at 05:00:19AM -0400, Denton Liu wrote:\n> > In d787d311db (checkout: split part of it to new command 'switch',\n> > 2019-03-29), the `git switch` command was created by extracting the\n> > common functionality of cmd_checkout() in checkout_main(). However, in\n> > b7b5fce270 (switch: better names for -b and -B, 2019-03-29), these\n> > the branch creation and force creation options for 'switch' were changed\n\ns/these the/the/\n\n> > to -c and -C, respectively. As a result of this, error messages and\n> > comments that previously referred to `-b` and `-B` became invalid for\n> > `git switch`.\n> >\n> > For comments that refer to `-b` and `-B`, add `-c` and `-C` to the\n> > comment.\n>\n> I had to read this sentence a couple of times more than I would have\n> liked to in order to grok it fully. Would it be perhaps clearer as:\n>\n>   Update comments in 'cmd_checkout()' that mention `-b` or `-B` to\n>   instead refer to `-c` or `-C` when invoked from 'git switch'.\n\nI had no problem groking Denton's wording but had to re-read this\nproposal several times before understanding it. I could try providing\nyet another proposal, however, I think the entire sentence can simply\nbe dropped (after all, it's just stating the obvious).\n\n> > +             die(variant == CMD_CHECKOUT ?\n> > +                             _(\"-b, -B and --orphan are mutually exclusive\") :\n> > +                             _(\"-c, -C and --orphan are mutually exclusive\"));\n>\n> Hmm. Do we need to generate an extra string for translation here? If the\n> string was instead:\n>\n>   _(\"%s and --orphan are mutually exclusive\")\n>\n> where the first format string is filled in something like:\n>\n>   die(_(\"%s and --orphan are mutually exclusive\"),\n>       variant == CMD_CHECKOUT ? \"-b, -B\" : \"-c, -C\");\n>\n> may save translators some work.\n\nWe don't know the grammatical or syntactic rules of each language, so\nhard-coding untranslatable \"-b, -B\" is contraindicated. Since the\noption letters ought not be translated (just as \"--orphan\" shouldn't\nbe), the letters themselves could be interpolated into the string.\nHowever, that's probably less helpful for translators since it\neliminates contextual clues. Therefore, it seems like a good idea to\nleave it as two separate translatable strings (as the patch already\ndoes).\n"},{"id":"396567","messageId":"xmqqv9lixnax.fsf@gitster.c.googlers.com","threadId":"53345","inReplyTo":"0f7f9eefc056dd4d9b11dab737e00235b3243a2f.1588150804.git.liu.denton@gmail.com","subject":"Re: [PATCH] switch: fix errors and comments related to -c and -C","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-04-29T16:35:18Z","receivedAt":"2020-04-29T16:35:25Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Denton Liu <liu.denton@gmail.com> writes:\n\n> An alternative implementation which was considered involved inserting\n> option name variants into a struct which is passed in by each command\n> variant. Even though this approach is more general and could be\n> applicable for future differing option names, it seemed like an\n> over-engineered solution when the current pair of options are the only\n> differing ones. We should probably avoid adding options which have\n> different names anyway.\n\nSure.  Or another alternative is to take \"-B/-b\" silently without\nadvertising.  It's not like the reason why we introduced \"-C/-c\" was\nbecause we wanted to reuse them in \"switch\" for other purposes.\n\n> Reported-by: Robert Simpson <no-reply@MailScreen.com>\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>\n> Notes:\n>     Robert, is the email listed above correct? If not, please let me know\n>     which email to use. (I hope that this reaches you somehow.)\n\nIf we do not get any response, it is OK to remove the fake e-mail\naddress and recording only the namee (this is only OK for trailers\nthat are not SoB; the Signed-off-by: trailers want to be more\nstrict).\n\n> +enum cmd_variant {\n> +\tCMD_CHECKOUT,\n> +\tCMD_SWITCH,\n> +\tCMD_RESTORE\n> +};\n\nYuck, but OK.  Does \"git restore\" even take -b/-c/--orphan option?\nI somehow doubt it.\n\nThis is too invasive for what it achieves.\n\nHow about having a file-scope global\n\n/* create-branch option (either b or c) */\nstatic char cb_option = 'b';\n\nand then ...\n\n> +\n>  static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t\t\t struct checkout_opts *opts, struct option *options,\n> -\t\t\t const char * const usagestr[])\n> +\t\t\t const char * const usagestr[],\n> +\t\t\t enum cmd_variant variant)\n>  {\n>  \tstruct branch_info new_branch_info;\n>  \tint parseopt_flags = 0;\n> @@ -1586,7 +1593,9 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t}\n>  \n>  \tif ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)\n> -\t\tdie(_(\"-b, -B and --orphan are mutually exclusive\"));\n> +\t\tdie(variant == CMD_CHECKOUT ?\n> +\t\t\t\t_(\"-b, -B and --orphan are mutually exclusive\") :\n> +\t\t\t\t_(\"-c, -C and --orphan are mutually exclusive\"));\n\n... use it here like\n\n\t\tdie(_(\"-%c, -%c and --orphan are mutually exclusive\"),\n\t\t      cb_option, toupper(cb_option));\n\n\n\n>  \tif (opts->overlay_mode == 1 && opts->patch_mode)\n>  \t\tdie(_(\"-p and --overlay are mutually exclusive\"));\n> @@ -1614,7 +1623,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t/*\n>  \t * From here on, new_branch will contain the branch to be checked out,\n>  \t * and new_branch_force and new_orphan_branch will tell us which one of\n> -\t * -b/-B/--orphan is being used.\n> +\t * -b/-B/-c/-C/--orphan is being used.\n>  \t */\n>  \tif (opts->new_branch_force)\n>  \t\topts->new_branch = opts->new_branch_force;\n> @@ -1622,7 +1631,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \tif (opts->new_orphan_branch)\n>  \t\topts->new_branch = opts->new_orphan_branch;\n>  \n> -\t/* --track without -b/-B/--orphan should DWIM */\n> +\t/* --track without -b/-B/--orphan for checkout or -c/-C/--orphan for switch should DWIM */\n\nWay overlong comment.  Just \n\n\t/* --track without -b/-B/-c/-C/--orphan should DWIM */\n\nis sufficient, no?\n\n>  \tif (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {\n>  \t\tconst char *argv0 = argv[0];\n>  \t\tif (!argc || !strcmp(argv0, \"--\"))\n> @@ -1631,7 +1640,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n>  \t\tskip_prefix(argv0, \"remotes/\", &argv0);\n>  \t\targv0 = strchr(argv0, '/');\n>  \t\tif (!argv0 || !argv0[1])\n> -\t\t\tdie(_(\"missing branch name; try -b\"));\n> +\t\t\tdie(_(\"missing branch name; try -%c\"),\n> +\t\t\t\t\tvariant == CMD_CHECKOUT ? 'b' : 'c');\n\nLikewise,\n\n\t\t\tdie(_(\"missing branch name; try -%c\"), cb_option);\n\n>  \t\topts->new_branch = argv0 + 1;\n>  \t}\n\nAnd override cb_option in one of these helpers, perhaps like ...\n\n> @@ -1785,7 +1795,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)\n>  \toptions = add_checkout_path_options(&opts, options);\n>  \n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, checkout_usage);\n> +\t\t\t    options, checkout_usage, CMD_CHECKOUT);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n> @@ -1823,7 +1833,7 @@ int cmd_switch(int argc, const char **argv, const char *prefix)\n>  \toptions = add_common_switch_branch_options(&opts, options);\n>  \n\n... here (the other one uses 'b')\n\n\tcb_option = 'c';\n\n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, switch_branch_usage);\n> +\t\t\t    options, switch_branch_usage, CMD_SWITCH);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n\n... and you do not have to change function signature of\ncheckout_main() at all.\n\n> @@ -1860,7 +1870,7 @@ int cmd_restore(int argc, const char **argv, const char *prefix)\n>  \toptions = add_checkout_path_options(&opts, options);\n>  \n>  \tret = checkout_main(argc, argv, prefix, &opts,\n> -\t\t\t    options, restore_usage);\n> +\t\t\t    options, restore_usage, CMD_RESTORE);\n>  \tFREE_AND_NULL(options);\n>  \treturn ret;\n>  }\n"},{"id":"396659","messageId":"75c9cf6ce95e753f50ce9b8b86b2c3ded5d8d38d.1588247662.git.liu.denton@gmail.com","threadId":"53345","inReplyTo":"0f7f9eefc056dd4d9b11dab737e00235b3243a2f.1588150804.git.liu.denton@gmail.com","subject":"[PATCH v2] switch: fix errors and comments related to -c and -C","fromName":"Denton Liu","fromEmail":"liu.denton@gmail.com","sentAt":"2020-04-30T11:54:57Z","receivedAt":"2020-04-30T11:55:04Z","isPatch":true,"sender":{"key":"liu.denton@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9620836?v=4"},"body":"In d787d311db (checkout: split part of it to new command 'switch',\n2019-03-29), the `git switch` command was created by extracting the\ncommon functionality of cmd_checkout() in checkout_main(). However, in\nb7b5fce270 (switch: better names for -b and -B, 2019-03-29), the branch\ncreation and force creation options for 'switch' were changed to -c and\n-C, respectively. As a result of this, error messages and comments that\npreviously referred to `-b` and `-B` became invalid for `git switch`.\n\nFor error messages that refer to `-b` and `-B`, use a format string\ninstead so that `-c` and `-C` can be printed when `git switch` is\ninvoked.\n\nReported-by: Robert Simpson\nSigned-off-by: Denton Liu <liu.denton@gmail.com>\n---\n\nNotes:\n    Robert, is the email listed above correct? If not, please let me know\n    which email to use. (I hope that this reaches you somehow.)\n\nRange-diff against v1:\n1:  0f7f9eefc0 ! 1:  75c9cf6ce9 switch: fix errors and comments related to -c and -C\n    @@ Commit message\n         In d787d311db (checkout: split part of it to new command 'switch',\n         2019-03-29), the `git switch` command was created by extracting the\n         common functionality of cmd_checkout() in checkout_main(). However, in\n    -    b7b5fce270 (switch: better names for -b and -B, 2019-03-29), these\n    -    the branch creation and force creation options for 'switch' were changed\n    -    to -c and -C, respectively. As a result of this, error messages and\n    -    comments that previously referred to `-b` and `-B` became invalid for\n    -    `git switch`.\n    +    b7b5fce270 (switch: better names for -b and -B, 2019-03-29), the branch\n    +    creation and force creation options for 'switch' were changed to -c and\n    +    -C, respectively. As a result of this, error messages and comments that\n    +    previously referred to `-b` and `-B` became invalid for `git switch`.\n     \n    -    For comments that refer to `-b` and `-B`, add `-c` and `-C` to the\n    -    comment.\n    +    For error messages that refer to `-b` and `-B`, use a format string\n    +    instead so that `-c` and `-C` can be printed when `git switch` is\n    +    invoked.\n     \n    -    For error messages that refer to `-b`, introduce `enum cmd_variant` and\n    -    use it to differentiate between `checkout` and `switch` when printing\n    -    out error messages.\n    -\n    -    An alternative implementation which was considered involved inserting\n    -    option name variants into a struct which is passed in by each command\n    -    variant. Even though this approach is more general and could be\n    -    applicable for future differing option names, it seemed like an\n    -    over-engineered solution when the current pair of options are the only\n    -    differing ones. We should probably avoid adding options which have\n    -    different names anyway.\n    -\n    -    Reported-by: Robert Simpson <no-reply@MailScreen.com>\n    +    Reported-by: Robert Simpson\n     \n     \n      ## Notes ##\n    @@ builtin/checkout.c: static struct option *add_checkout_path_options(struct check\n      \treturn newopts;\n      }\n      \n    -+enum cmd_variant {\n    -+\tCMD_CHECKOUT,\n    -+\tCMD_SWITCH,\n    -+\tCMD_RESTORE\n    -+};\n    ++/* create-branch option (either b or c) */\n    ++static char cb_option = 'b';\n     +\n      static int checkout_main(int argc, const char **argv, const char *prefix,\n      \t\t\t struct checkout_opts *opts, struct option *options,\n    --\t\t\t const char * const usagestr[])\n    -+\t\t\t const char * const usagestr[],\n    -+\t\t\t enum cmd_variant variant)\n    - {\n    - \tstruct branch_info new_branch_info;\n    - \tint parseopt_flags = 0;\n    + \t\t\t const char * const usagestr[])\n     @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const char *prefix,\n      \t}\n      \n      \tif ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)\n     -\t\tdie(_(\"-b, -B and --orphan are mutually exclusive\"));\n    -+\t\tdie(variant == CMD_CHECKOUT ?\n    -+\t\t\t\t_(\"-b, -B and --orphan are mutually exclusive\") :\n    -+\t\t\t\t_(\"-c, -C and --orphan are mutually exclusive\"));\n    ++\t\tdie(_(\"-%c, -%c and --orphan are mutually exclusive\"),\n    ++\t\t\t\tcb_option, toupper(cb_option));\n      \n      \tif (opts->overlay_mode == 1 && opts->patch_mode)\n      \t\tdie(_(\"-p and --overlay are mutually exclusive\"));\n    @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const\n      \t\topts->new_branch = opts->new_orphan_branch;\n      \n     -\t/* --track without -b/-B/--orphan should DWIM */\n    -+\t/* --track without -b/-B/--orphan for checkout or -c/-C/--orphan for switch should DWIM */\n    ++\t/* --track without -c/-C/-b/-B/--orphan should DWIM */\n      \tif (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {\n      \t\tconst char *argv0 = argv[0];\n      \t\tif (!argc || !strcmp(argv0, \"--\"))\n    @@ builtin/checkout.c: static int checkout_main(int argc, const char **argv, const\n      \t\tif (!argv0 || !argv0[1])\n     -\t\t\tdie(_(\"missing branch name; try -b\"));\n     +\t\t\tdie(_(\"missing branch name; try -%c\"),\n    -+\t\t\t\t\tvariant == CMD_CHECKOUT ? 'b' : 'c');\n    ++\t\t\t\t\tcb_option);\n      \t\topts->new_branch = argv0 + 1;\n      \t}\n      \n    -@@ builtin/checkout.c: int cmd_checkout(int argc, const char **argv, const char *prefix)\n    - \toptions = add_checkout_path_options(&opts, options);\n    - \n    - \tret = checkout_main(argc, argv, prefix, &opts,\n    --\t\t\t    options, checkout_usage);\n    -+\t\t\t    options, checkout_usage, CMD_CHECKOUT);\n    - \tFREE_AND_NULL(options);\n    - \treturn ret;\n    - }\n     @@ builtin/checkout.c: int cmd_switch(int argc, const char **argv, const char *prefix)\n    + \toptions = add_common_options(&opts, options);\n      \toptions = add_common_switch_branch_options(&opts, options);\n      \n    ++\tcb_option = 'c';\n    ++\n      \tret = checkout_main(argc, argv, prefix, &opts,\n    --\t\t\t    options, switch_branch_usage);\n    -+\t\t\t    options, switch_branch_usage, CMD_SWITCH);\n    + \t\t\t    options, switch_branch_usage);\n      \tFREE_AND_NULL(options);\n    - \treturn ret;\n    - }\n    -@@ builtin/checkout.c: int cmd_restore(int argc, const char **argv, const char *prefix)\n    - \toptions = add_checkout_path_options(&opts, options);\n    - \n    - \tret = checkout_main(argc, argv, prefix, &opts,\n    --\t\t\t    options, restore_usage);\n    -+\t\t\t    options, restore_usage, CMD_RESTORE);\n    - \tFREE_AND_NULL(options);\n    - \treturn ret;\n    - }\n\n builtin/checkout.c | 15 +++++++++++----\n 1 file changed, 11 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/checkout.c b/builtin/checkout.c\nindex 8bc94d392b..a45519a2b4 100644\n--- a/builtin/checkout.c\n+++ b/builtin/checkout.c\n@@ -1544,6 +1544,9 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts,\n \treturn newopts;\n }\n \n+/* create-branch option (either b or c) */\n+static char cb_option = 'b';\n+\n static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\t\t struct checkout_opts *opts, struct option *options,\n \t\t\t const char * const usagestr[])\n@@ -1586,7 +1589,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t}\n \n \tif ((!!opts->new_branch + !!opts->new_branch_force + !!opts->new_orphan_branch) > 1)\n-\t\tdie(_(\"-b, -B and --orphan are mutually exclusive\"));\n+\t\tdie(_(\"-%c, -%c and --orphan are mutually exclusive\"),\n+\t\t\t\tcb_option, toupper(cb_option));\n \n \tif (opts->overlay_mode == 1 && opts->patch_mode)\n \t\tdie(_(\"-p and --overlay are mutually exclusive\"));\n@@ -1614,7 +1618,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t/*\n \t * From here on, new_branch will contain the branch to be checked out,\n \t * and new_branch_force and new_orphan_branch will tell us which one of\n-\t * -b/-B/--orphan is being used.\n+\t * -b/-B/-c/-C/--orphan is being used.\n \t */\n \tif (opts->new_branch_force)\n \t\topts->new_branch = opts->new_branch_force;\n@@ -1622,7 +1626,7 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \tif (opts->new_orphan_branch)\n \t\topts->new_branch = opts->new_orphan_branch;\n \n-\t/* --track without -b/-B/--orphan should DWIM */\n+\t/* --track without -c/-C/-b/-B/--orphan should DWIM */\n \tif (opts->track != BRANCH_TRACK_UNSPECIFIED && !opts->new_branch) {\n \t\tconst char *argv0 = argv[0];\n \t\tif (!argc || !strcmp(argv0, \"--\"))\n@@ -1631,7 +1635,8 @@ static int checkout_main(int argc, const char **argv, const char *prefix,\n \t\tskip_prefix(argv0, \"remotes/\", &argv0);\n \t\targv0 = strchr(argv0, '/');\n \t\tif (!argv0 || !argv0[1])\n-\t\t\tdie(_(\"missing branch name; try -b\"));\n+\t\t\tdie(_(\"missing branch name; try -%c\"),\n+\t\t\t\t\tcb_option);\n \t\topts->new_branch = argv0 + 1;\n \t}\n \n@@ -1822,6 +1827,8 @@ int cmd_switch(int argc, const char **argv, const char *prefix)\n \toptions = add_common_options(&opts, options);\n \toptions = add_common_switch_branch_options(&opts, options);\n \n+\tcb_option = 'c';\n+\n \tret = checkout_main(argc, argv, prefix, &opts,\n \t\t\t    options, switch_branch_usage);\n \tFREE_AND_NULL(options);\n-- \n2.26.2.548.gbb00c8a0a9\n\n"},{"id":"396669","messageId":"20200430162151.GB30682@syl.local","threadId":"53345","inReplyTo":"75c9cf6ce95e753f50ce9b8b86b2c3ded5d8d38d.1588247662.git.liu.denton@gmail.com","subject":"Re: [PATCH v2] switch: fix errors and comments related to -c and -C","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2020-04-30T16:21:51Z","receivedAt":"2020-04-30T16:21:56Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"On Thu, Apr 30, 2020 at 07:54:57AM -0400, Denton Liu wrote:\n> In d787d311db (checkout: split part of it to new command 'switch',\n> 2019-03-29), the `git switch` command was created by extracting the\n> common functionality of cmd_checkout() in checkout_main(). However, in\n> b7b5fce270 (switch: better names for -b and -B, 2019-03-29), the branch\n> creation and force creation options for 'switch' were changed to -c and\n> -C, respectively. As a result of this, error messages and comments that\n> previously referred to `-b` and `-B` became invalid for `git switch`.\n>\n> For error messages that refer to `-b` and `-B`, use a format string\n> instead so that `-c` and `-C` can be printed when `git switch` is\n> invoked.\n>\n> Reported-by: Robert Simpson\n> Signed-off-by: Denton Liu <liu.denton@gmail.com>\n> ---\n>\n> Notes:\n>     Robert, is the email listed above correct? If not, please let me know\n>     which email to use. (I hope that this reaches you somehow.)\n\nNicely done. This revision looks great to me, thanks.\n\n  Reviewed-by: Taylor Blau <me@ttaylorr.com>\n\nThanks,\nTaylor\n"},{"id":"396696","messageId":"xmqqimhgr9d9.fsf@gitster.c.googlers.com","threadId":"53345","inReplyTo":"20200430162151.GB30682@syl.local","subject":"Re: [PATCH v2] switch: fix errors and comments related to -c and -C","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-04-30T20:45:06Z","receivedAt":"2020-04-30T20:45:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Taylor Blau <me@ttaylorr.com> writes:\n\n> Nicely done. This revision looks great to me, thanks.\n>\n>   Reviewed-by: Taylor Blau <me@ttaylorr.com>\n\nI'd squeeze this into a single line, though.\n\n> -\t\t\tdie(_(\"missing branch name; try -b\"));\n> +\t\t\tdie(_(\"missing branch name; try -%c\"),\n> +\t\t\t\t\tcb_option);\n\nWill queue with a local fix-up for the above.\n\nThanks.\n"}]}