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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Feb 16, 2012 at 03:05 UTC c9b5e2a57d2a69e0c6183758445da2f230b5a9f0
3 files changed +27 -4
Documentation/technical/api-config.txt
+12
@@ -47,6 +47,18 @@ will first feed the user-wide one to the callback, and then the
47 repo-specific one; by overwriting, the higher-priority repo-specific
48 value is left at the end).
49
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 +
62 There is a special version of `git_config` called `git_config_early`.
63 This version takes an additional parameter to specify the repository
64 config, instead of having it looked up via `git_path`. This is useful
cache.h
+1
@@ -1113,6 +1113,7 @@ extern int git_config_from_file(config_fn_t fn, const char *, void *);
1113 extern void git_config_push_parameter(const char *text);
1114 extern int git_config_from_parameters(config_fn_t fn, void *data);
1115 extern int git_config(config_fn_t fn, void *);
1116 +extern int git_config_with_options(config_fn_t fn, void *, const char *filename);
1117 extern int git_config_early(config_fn_t fn, void *, const char *repo_config);
1118 extern int git_parse_ulong(const char *, unsigned long *);
1119 extern int git_config_int(const char *, const char *);
config.c
+14 -4
@@ -879,9 +879,6 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
879 int ret = 0, found = 0;
880 const char *home = NULL;
881
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);
882 if (git_config_system() && !access(git_etc_gitconfig(), R_OK)) {
883 ret += git_config_from_file(fn, git_etc_gitconfig(),
884 data);
@@ -917,11 +914,19 @@ int git_config_early(config_fn_t fn, void *data, const char *repo_config)
914 return ret == 0 ? found : ret;
915 }
916
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)
919 {
920 char *repo_config = NULL;
921 int ret;
922
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 +
930 repo_config = git_pathdup("config");
931 ret = git_config_early(fn, data, repo_config);
932 if (repo_config)
@@ -929,6 +934,11 @@ int git_config(config_fn_t fn, void *data)
934 return ret;
935 }
936
937 +int git_config(config_fn_t fn, void *data)
938 +{
939 + return git_config_with_options(fn, data, config_exclusive_filename);
940 +}
941 +
942 /*
943 * Find all the stuff for git_config_set() below.
944 */