Skip to content

Commit 9dda6b7

Browse files
pks-tgitster
authored andcommitted
builtin/config: pull out function to handle config location
There's quite a bunch of options to git-config(1) that allow the user to specify which config location to use when reading or writing config options. The logic to handle this is thus by necessity also quite involved. Pull it out into a separate function so that we can reuse it in subsequent commits which introduce proper subcommands. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent daa3325 commit 9dda6b7

File tree

1 file changed

+68
-65
lines changed

1 file changed

+68
-65
lines changed

builtin/config.c

Lines changed: 68 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -623,83 +623,22 @@ static char *default_user_config(void)
623623
return strbuf_detach(&buf, NULL);
624624
}
625625

626-
static struct option builtin_config_options[] = {
627-
OPT_GROUP(N_("Config file location")),
628-
OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
629-
OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
630-
OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
631-
OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
632-
OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
633-
OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
634-
OPT_GROUP(N_("Action")),
635-
OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
636-
OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
637-
OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
638-
OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
639-
OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
640-
OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
641-
OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
642-
OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
643-
OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
644-
OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
645-
OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
646-
OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
647-
OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
648-
OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
649-
OPT_GROUP(N_("Type")),
650-
OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
651-
OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
652-
OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
653-
OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
654-
OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
655-
OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
656-
OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
657-
OPT_GROUP(N_("Other")),
658-
OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
659-
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
660-
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
661-
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
662-
OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
663-
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
664-
OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
665-
OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
666-
OPT_END(),
667-
};
668-
669-
static NORETURN void usage_builtin_config(void)
626+
static void handle_config_location(const char *prefix)
670627
{
671-
usage_with_options(builtin_config_usage, builtin_config_options);
672-
}
673-
674-
int cmd_config(int argc, const char **argv, const char *prefix)
675-
{
676-
int nongit = !startup_info->have_repository;
677-
char *value = NULL, *comment = NULL;
678-
int flags = 0;
679-
int ret = 0;
680-
struct key_value_info default_kvi = KVI_INIT;
681-
682-
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
683-
684-
argc = parse_options(argc, argv, prefix, builtin_config_options,
685-
builtin_config_usage,
686-
PARSE_OPT_STOP_AT_NON_OPTION);
687-
688628
if (use_global_config + use_system_config + use_local_config +
689629
use_worktree_config +
690630
!!given_config_source.file + !!given_config_source.blob > 1) {
691631
error(_("only one config file at a time"));
692632
usage_builtin_config();
693633
}
694634

695-
if (nongit) {
635+
if (!startup_info->have_repository) {
696636
if (use_local_config)
697637
die(_("--local can only be used inside a git repository"));
698638
if (given_config_source.blob)
699639
die(_("--blob can only be used inside a git repository"));
700640
if (use_worktree_config)
701641
die(_("--worktree can only be used inside a git repository"));
702-
703642
}
704643

705644
if (given_config_source.file &&
@@ -753,10 +692,74 @@ int cmd_config(int argc, const char **argv, const char *prefix)
753692
config_options.respect_includes = !given_config_source.file;
754693
else
755694
config_options.respect_includes = respect_includes_opt;
756-
if (!nongit) {
695+
if (startup_info->have_repository) {
757696
config_options.commondir = get_git_common_dir();
758697
config_options.git_dir = get_git_dir();
759698
}
699+
}
700+
701+
static struct option builtin_config_options[] = {
702+
OPT_GROUP(N_("Config file location")),
703+
OPT_BOOL(0, "global", &use_global_config, N_("use global config file")),
704+
OPT_BOOL(0, "system", &use_system_config, N_("use system config file")),
705+
OPT_BOOL(0, "local", &use_local_config, N_("use repository config file")),
706+
OPT_BOOL(0, "worktree", &use_worktree_config, N_("use per-worktree config file")),
707+
OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")),
708+
OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object")),
709+
OPT_GROUP(N_("Action")),
710+
OPT_CMDMODE(0, "get", &actions, N_("get value: name [<value-pattern>]"), ACTION_GET),
711+
OPT_CMDMODE(0, "get-all", &actions, N_("get all values: key [<value-pattern>]"), ACTION_GET_ALL),
712+
OPT_CMDMODE(0, "get-regexp", &actions, N_("get values for regexp: name-regex [<value-pattern>]"), ACTION_GET_REGEXP),
713+
OPT_CMDMODE(0, "get-urlmatch", &actions, N_("get value specific for the URL: section[.var] URL"), ACTION_GET_URLMATCH),
714+
OPT_CMDMODE(0, "replace-all", &actions, N_("replace all matching variables: name value [<value-pattern>]"), ACTION_REPLACE_ALL),
715+
OPT_CMDMODE(0, "add", &actions, N_("add a new variable: name value"), ACTION_ADD),
716+
OPT_CMDMODE(0, "unset", &actions, N_("remove a variable: name [<value-pattern>]"), ACTION_UNSET),
717+
OPT_CMDMODE(0, "unset-all", &actions, N_("remove all matches: name [<value-pattern>]"), ACTION_UNSET_ALL),
718+
OPT_CMDMODE(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
719+
OPT_CMDMODE(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
720+
OPT_CMDMODE('l', "list", &actions, N_("list all"), ACTION_LIST),
721+
OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
722+
OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
723+
OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
724+
OPT_GROUP(N_("Type")),
725+
OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
726+
OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
727+
OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
728+
OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
729+
OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
730+
OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
731+
OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
732+
OPT_GROUP(N_("Other")),
733+
OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
734+
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
735+
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
736+
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
737+
OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
738+
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
739+
OPT_STRING(0, "comment", &comment_arg, N_("value"), N_("human-readable comment string (# will be prepended as needed)")),
740+
OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
741+
OPT_END(),
742+
};
743+
744+
static NORETURN void usage_builtin_config(void)
745+
{
746+
usage_with_options(builtin_config_usage, builtin_config_options);
747+
}
748+
749+
int cmd_config(int argc, const char **argv, const char *prefix)
750+
{
751+
char *value = NULL, *comment = NULL;
752+
int flags = 0;
753+
int ret = 0;
754+
struct key_value_info default_kvi = KVI_INIT;
755+
756+
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
757+
758+
argc = parse_options(argc, argv, prefix, builtin_config_options,
759+
builtin_config_usage,
760+
PARSE_OPT_STOP_AT_NON_OPTION);
761+
762+
handle_config_location(prefix);
760763

761764
if (end_nul) {
762765
term = '\0';
@@ -858,7 +861,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
858861
char *config_file;
859862

860863
check_argc(argc, 0, 0);
861-
if (!given_config_source.file && nongit)
864+
if (!given_config_source.file && !startup_info->have_repository)
862865
die(_("not in a git directory"));
863866
if (given_config_source.use_stdin)
864867
die(_("editing stdin is not supported"));

0 commit comments

Comments
 (0)