@matheusbsbs / matheus-bagliano / commits / 5f1a63e0ef
Fork 0

Read configuration also from $HOME/.gitconfig

This patch is based on Pasky's, with three notable differences: - I did not yet update the documentation - I named it .gitconfig, not .gitrc - git-repo-config does not barf when a unique key is overridden locally The last means that if you have something like [alias] l = log --stat -M in ~/.gitconfig, and [alias] l = log --stat -M next.. in $GIT_DIR/config, then git-repo-config alias.l returns only one value, namely the value from $GIT_DIR/config. If you set the environment variable GIT_CONFIG, $HOME/.gitconfig is not read, and neither $GIT_DIR/config, but $GIT_CONFIG instead. If you set GIT_CONFIG_LOCAL instead, it is interpreted instead of $GIT_DIR/config, but $HOME/.gitconfig is still read. Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>

Johannes Schindelin committed Jun 20, 2006 at 01:48 UTC 5f1a63e0efc750c54a25644a36cf2905495a9b93
2 files changed +58 -15
config.c
+25 -9
@@ -317,17 +317,33 @@ int git_config_from_file(config_fn_t fn, const char *filename)
317
318 int git_config(config_fn_t fn)
319 {
320 - const char *filename = git_path("config");
321 - /* Forward-compatibility cue: $GIT_CONFIG makes git read _only_
322 - * the given config file, $GIT_CONFIG_LOCAL will make it process
323 - * it in addition to the global config file, the same way it would
324 - * the per-repository config file otherwise. */
325 - if (getenv("GIT_CONFIG")) {
326 - filename = getenv("GIT_CONFIG");
327 - } else if (getenv("GIT_CONFIG_LOCAL")) {
320 + int ret = 0;
321 + char *repo_config = NULL;
322 + const char *home = NULL, *filename;
323 +
324 + /* $GIT_CONFIG makes git read _only_ the given config file,
325 + * $GIT_CONFIG_LOCAL will make it process it in addition to the
326 + * global config file, the same way it would the per-repository
327 + * config file otherwise. */
328 + filename = getenv("GIT_CONFIG");
329 + if (!filename) {
330 + home = getenv("HOME");
331 filename = getenv("GIT_CONFIG_LOCAL");
332 + if (!filename)
333 + filename = repo_config = strdup(git_path("config"));
334 }
330 - return git_config_from_file(fn, filename);
335 +
336 + if (home) {
337 + char *user_config = strdup(mkpath("%s/.gitconfig", home));
338 + if (access(user_config, R_OK) > 0)
339 + ret = git_config_from_file(fn, user_config);
340 + free(user_config);
341 + }
342 +
343 + ret += git_config_from_file(fn, filename);
344 + if (repo_config)
345 + free(repo_config);
346 + return ret;
347 }
348
349 /*
repo-config.c
+33 -6
@@ -64,7 +64,22 @@ static int show_config(const char* key_, const char* value_)
64
65 static int get_value(const char* key_, const char* regex_)
66 {
67 + int ret = -1;
68 char *tl;
69 + char *global = NULL, *repo_config = NULL;
70 + const char *local;
71 +
72 + local = getenv("GIT_CONFIG");
73 + if (!local) {
74 + const char *home = getenv("HOME");
75 + local = getenv("GIT_CONFIG_LOCAL");
76 + if (!local)
77 + local = repo_config;
78 + else
79 + local = repo_config = strdup(git_path("config"));
80 + if (home)
81 + global = strdup(mkpath("%s/.gitconfig", home));
82 + }
83
84 key = strdup(key_);
85 for (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)
@@ -76,7 +91,7 @@ static int get_value(const char* key_, const char* regex_)
91 key_regexp = (regex_t*)malloc(sizeof(regex_t));
92 if (regcomp(key_regexp, key, REG_EXTENDED)) {
93 fprintf(stderr, "Invalid key pattern: %s\n", key_);
79 - return -1;
94 + goto free_strings;
95 }
96 }
97
@@ -89,11 +104,16 @@ static int get_value(const char* key_, const char* regex_)
104 regexp = (regex_t*)malloc(sizeof(regex_t));
105 if (regcomp(regexp, regex_, REG_EXTENDED)) {
106 fprintf(stderr, "Invalid pattern: %s\n", regex_);
92 - return -1;
107 + goto free_strings;
108 }
109 }
110
96 - git_config(show_config);
111 + if (do_all && global)
112 + git_config_from_file(show_config, global);
113 + git_config_from_file(show_config, local);
114 + if (!do_all && !seen && global)
115 + git_config_from_file(show_config, global);
116 +
117 free(key);
118 if (regexp) {
119 regfree(regexp);
@@ -101,9 +121,16 @@ static int get_value(const char* key_, const char* regex_)
121 }
122
123 if (do_all)
104 - return !seen;
105 -
106 - return (seen == 1) ? 0 : 1;
124 + ret = !seen;
125 + else
126 + ret = (seen == 1) ? 0 : 1;
127 +
128 +free_strings:
129 + if (repo_config)
130 + free(repo_config);
131 + if (global)
132 + free(global);
133 + return ret;
134 }
135
136 int main(int argc, const char **argv)