Skip to content

Commit c9b5e2a

Browse files
peffgitster
authored andcommitted
config: provide a version of git_config with more options
Callers may want to provide a specific version of a file in which to look for config. Right now this can be done by setting the magic global config_exclusive_filename variable. By providing a version of git_config that takes a filename, we can take a step towards making this magic global go away. Furthermore, by providing a more "advanced" interface, we now have a a natural place to add new options for callers like git-config, which care about tweaking the specifics of config lookup, without disturbing the large number of "simple" users (i.e., every other part of git). The astute reader of this patch may notice that the logic for handling config_exclusive_filename was taken out of git_config_early, but added into git_config. This means that git_config_early will no longer respect config_exclusive_filename. That's OK, because the only other caller of git_config_early is check_repository_format_gently, but the only function which sets config_exclusive_filename is cmd_config, which does not call check_repository_format_gently (and if it did, it would have been a bug, anyway, as we would be checking the repository format in the wrong file). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42bd39b commit c9b5e2a

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

Documentation/technical/api-config.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ will first feed the user-wide one to the callback, and then the
4747
repo-specific one; by overwriting, the higher-priority repo-specific
4848
value is left at the end).
4949

50+
The `git_config_with_options` function lets the caller examine config
51+
while adjusting some of the default behavior of `git_config`. It should
52+
almost never be used by "regular" git code that is looking up
53+
configuration variables. It is intended for advanced callers like
54+
`git-config`, which are intentionally tweaking the normal config-lookup
55+
process. It takes one extra parameter:
56+
57+
`filename`::
58+
If this parameter is non-NULL, it specifies the name of a file to
59+
parse for configuration, rather than looking in the usual files. Regular
60+
`git_config` defaults to `NULL`.
61+
5062
There is a special version of `git_config` called `git_config_early`.
5163
This version takes an additional parameter to specify the repository
5264
config, instead of having it looked up via `git_path`. This is useful

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ extern int git_config_from_file(config_fn_t fn, const char *, void *);
11131113
extern void git_config_push_parameter(const char *text);
11141114
extern int git_config_from_parameters(config_fn_t fn, void *data);
11151115
extern int git_config(config_fn_t fn, void *);
1116+
extern int git_config_with_options(config_fn_t fn, void *, const char *filename);
11161117
extern int git_config_early(config_fn_t fn, void *, const char *repo_config);
11171118
extern int git_parse_ulong(const char *, unsigned long *);
11181119
extern int git_config_int(const char *, const char *);

config.c

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,9 +879,6 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
879879
int ret = 0, found = 0;
880880
const char *home = NULL;
881881

882-
/* Setting $GIT_CONFIG makes git read _only_ the given config file. */
883-
if (config_exclusive_filename)
884-
return git_config_from_file(fn, config_exclusive_filename, data);
885882
if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
886883
ret += git_config_from_file(fn, git_etc_gitconfig(),
887884
data);
@@ -917,18 +914,31 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
917914
return ret == 0 ? found : ret;
918915
}
919916

920-
int git_config(config_fn_t fn, void *data)
917+
int git_config_with_options(config_fn_t fn, void *data,
918+
const char *filename)
921919
{
922920
char *repo_config = NULL;
923921
int ret;
924922

923+
/*
924+
* If we have a specific filename, use it. Otherwise, follow the
925+
* regular lookup sequence.
926+
*/
927+
if (filename)
928+
return git_config_from_file(fn, filename, data);
929+
925930
repo_config = git_pathdup("config");
926931
ret = git_config_early(fn, data, repo_config);
927932
if (repo_config)
928933
free(repo_config);
929934
return ret;
930935
}
931936

937+
int git_config(config_fn_t fn, void *data)
938+
{
939+
return git_config_with_options(fn, data, config_exclusive_filename);
940+
}
941+
932942
/*
933943
* Find all the stuff for git_config_set() below.
934944
*/

0 commit comments

Comments
 (0)