Re: [PATCH v3 3/3] branch: add '--no-name-prefix' option
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Mar 6, 2026, 21:38 UTC
- Message-ID
- <xmqqzf4ky6xb.fsf@gitster.g>
- In-Reply-To
- <8f45374007fddfa4cc81e536ae6e095b0d67d5e6.1772802872.git.gitgitgadget@gmail.com>
"VALERI Yoann via GitGitGadget" <gitgitgadget@gmail.com> writes:
> - OPT_STRING(0, "name-prefix", &name_prefix, N_("name"), N_("prefix for the branch to create")),
> + OPT_STRING_F(0, "name-prefix", &name_prefix, N_("name"), N_("prefix for the branch to create"), PARSE_OPT_NONEG),
> + OPT_BOOL(0, "no-name-prefix", &no_name_prefix, N_("do not use any prefix for the branch to create")),You do not want a separate "no-*" entry in the options[] table. If we look at parse-options.c to see how OPTION_STRING is handled, we find:
case OPTION_STRING: if (unset) *(const char **)opt->value = NULL; else if (opt->flags & PARSE_OPT_OPTARG && !p->opt) *(const char **)opt->value = (const char *)opt->defval; else return get_arg(p, opt, flags, (const char **)opt->value); return 0;
which tells us
* "--no-name-prefix" is caught by "if (unset)" and causes the name_prefix variable set to NULL.
* if we give OPT_OPTARG, we can allow "--name-prefix" (with no parameter) to default to opt->defval value; this feature is not very useful in our application.
* Otherwise we get the string after "--name-prefix=".
So you do not need to do anything strange. To correctly implement the order of handling configuration and command line, you would do:
* initialize name_prefix to NULL. By default no name_prefix is used.
* early in the cmd_branch() before you call parse_options(), consult the configuration and pick up branch.nameprefix and set it to name_prefix variable.
* then you call parse_options(). If the command line has "--no-name-prefix", "if (unset)" kicks in and clears the name_prefix variable pointed at by the opt->value. If the command line has "--name-prefix=blah", the name_prefix variable, which may have obtained a value from the configuration, is overwritten with "blah". If the command line does not do anything, then the name_prefix variable will retain whatever value it got from the configuration.