Skip to content

Commit e37efa4

Browse files
ROGERSM94gitster
authored andcommitted
config: teach git_config_source to remember its scope
There are many situations where the scope of a config command is known beforehand, such as passing of '--local', '--file', etc. to an invocation of git config. However, this information is lost when moving from builtin/config.c to /config.c. This historically hasn't been a big deal, but to prepare for the upcoming --show-scope option we teach git_config_source to keep track of the source and the config machinery to use that information to set current_parsing_scope appropriately. Signed-off-by: Matthew Rogers <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5c105a8 commit e37efa4

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

builtin/config.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
622622
!strcmp(given_config_source.file, "-")) {
623623
given_config_source.file = NULL;
624624
given_config_source.use_stdin = 1;
625+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
625626
}
626627

627628
if (use_global_config) {
@@ -637,6 +638,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
637638
*/
638639
die(_("$HOME not set"));
639640

641+
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
642+
640643
if (access_or_warn(user_config, R_OK, 0) &&
641644
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
642645
given_config_source.file = xdg_config;
@@ -646,11 +649,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
646649
free(xdg_config);
647650
}
648651
}
649-
else if (use_system_config)
652+
else if (use_system_config) {
650653
given_config_source.file = git_etc_gitconfig();
651-
else if (use_local_config)
654+
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
655+
} else if (use_local_config) {
652656
given_config_source.file = git_pathdup("config");
653-
else if (use_worktree_config) {
657+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
658+
} else if (use_worktree_config) {
654659
struct worktree **worktrees = get_worktrees(0);
655660
if (repository_format_worktree_config)
656661
given_config_source.file = git_pathdup("config.worktree");
@@ -662,13 +667,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
662667
"section in \"git help worktree\" for details"));
663668
else
664669
given_config_source.file = git_pathdup("config");
670+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
665671
free_worktrees(worktrees);
666672
} else if (given_config_source.file) {
667673
if (!is_absolute_path(given_config_source.file) && prefix)
668674
given_config_source.file =
669675
prefix_filename(prefix, given_config_source.file);
676+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
677+
} else if (given_config_source.blob) {
678+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
670679
}
671680

681+
672682
if (respect_includes_opt == -1)
673683
config_options.respect_includes = !given_config_source.file;
674684
else

config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,9 @@ int config_with_options(config_fn_t fn, void *data,
17631763
data = &inc;
17641764
}
17651765

1766+
if (config_source)
1767+
current_parsing_scope = config_source->scope;
1768+
17661769
/*
17671770
* If we have a specific filename, use it. Otherwise, follow the
17681771
* regular lookup sequence.

config.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,21 @@ struct object_id;
3535

3636
#define CONFIG_REGEX_NONE ((void *)1)
3737

38+
enum config_scope {
39+
CONFIG_SCOPE_UNKNOWN = 0,
40+
CONFIG_SCOPE_SYSTEM,
41+
CONFIG_SCOPE_GLOBAL,
42+
CONFIG_SCOPE_LOCAL,
43+
CONFIG_SCOPE_WORKTREE,
44+
CONFIG_SCOPE_COMMAND,
45+
};
46+
const char *config_scope_name(enum config_scope scope);
47+
3848
struct git_config_source {
3949
unsigned int use_stdin:1;
4050
const char *file;
4151
const char *blob;
52+
enum config_scope scope;
4253
};
4354

4455
enum config_origin_type {
@@ -294,16 +305,6 @@ int config_error_nonbool(const char *);
294305

295306
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
296307

297-
enum config_scope {
298-
CONFIG_SCOPE_UNKNOWN = 0,
299-
CONFIG_SCOPE_SYSTEM,
300-
CONFIG_SCOPE_GLOBAL,
301-
CONFIG_SCOPE_LOCAL,
302-
CONFIG_SCOPE_WORKTREE,
303-
CONFIG_SCOPE_COMMAND,
304-
};
305-
const char *config_scope_name(enum config_scope scope);
306-
307308
enum config_scope current_config_scope(void);
308309
const char *current_config_origin_type(void);
309310
const char *current_config_name(void);

0 commit comments

Comments
 (0)