{"thread":{"id":"4573","subject":"[PATCH] Read configuration also from $HOME/.gitconfig","startedAt":"2006-06-19T23:48:03Z","lastAt":"2006-06-19T23:48:03Z","messageCount":1,"participants":["Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"22083","messageId":"Pine.LNX.4.63.0606200146490.26329@wbgn013.biozentrum.uni-wuerzburg.de","threadId":"4573","inReplyTo":null,"subject":"[PATCH] Read configuration also from $HOME/.gitconfig","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2006-06-19T23:48:03Z","receivedAt":"2006-06-19T23:48:03Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"\nThis patch is based on Pasky's, with three notable differences:\n\n- I did not yet update the documentation\n- I named it .gitconfig, not .gitrc\n- git-repo-config does not barf when a unique key is overridden locally\n\nThe last means that if you have something like\n\n\t[alias]\n\t\tl = log --stat -M\n\nin ~/.gitconfig, and\n\n\t[alias]\n\t\tl = log --stat -M next..\n\nin $GIT_DIR/config, then\n\n\tgit-repo-config alias.l\n\nreturns only one value, namely the value from $GIT_DIR/config.\n\nIf you set the environment variable GIT_CONFIG, $HOME/.gitconfig is not\nread, and neither $GIT_DIR/config, but $GIT_CONFIG instead.\n\nIf you set GIT_CONFIG_LOCAL instead, it is interpreted instead of\n$GIT_DIR/config, but $HOME/.gitconfig is still read.\n\nSigned-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\n---\n config.c      |   34 +++++++++++++++++++++++++---------\n repo-config.c |   39 +++++++++++++++++++++++++++++++++------\n 2 files changed, 58 insertions(+), 15 deletions(-)\n\ndiff --git a/config.c b/config.c\nindex 69aa05f..edbe5ed 100644\n--- a/config.c\n+++ b/config.c\n@@ -317,17 +317,33 @@ int git_config_from_file(config_fn_t fn,\n \n int git_config(config_fn_t fn)\n {\n-\tconst char *filename = git_path(\"config\");\n-\t/* Forward-compatibility cue: $GIT_CONFIG makes git read _only_\n-\t * the given config file, $GIT_CONFIG_LOCAL will make it process\n-\t * it in addition to the global config file, the same way it would\n-\t * the per-repository config file otherwise. */\n-\tif (getenv(\"GIT_CONFIG\")) {\n-\t\tfilename = getenv(\"GIT_CONFIG\");\n-\t} else if (getenv(\"GIT_CONFIG_LOCAL\")) {\n+\tint ret = 0;\n+\tchar *repo_config = NULL;\n+\tconst char *home = NULL, *filename;\n+\n+\t/* $GIT_CONFIG makes git read _only_ the given config file,\n+\t * $GIT_CONFIG_LOCAL will make it process it in addition to the\n+\t * global config file, the same way it would the per-repository\n+\t * config file otherwise. */\n+\tfilename = getenv(\"GIT_CONFIG\");\n+\tif (!filename) {\n+\t\thome = getenv(\"HOME\");\n \t\tfilename = getenv(\"GIT_CONFIG_LOCAL\");\n+\t\tif (!filename)\n+\t\t\tfilename = repo_config = strdup(git_path(\"config\"));\n \t}\n-\treturn git_config_from_file(fn, filename);\n+\n+\tif (home) {\n+\t\tconst char *user_config = strdup(mkpath(\"%s/.gitconfig\", home));\n+\t\tif (access(user_config, R_OK) > 0)\n+\t\t\tret = git_config_from_file(fn, user_config);\n+\t\tfree(user_config);\n+\t}\n+\n+\tret += git_config_from_file(fn, filename);\n+\tif (repo_config)\n+\t\tfree(repo_config);\n+\treturn ret;\n }\n \n /*\ndiff --git a/repo-config.c b/repo-config.c\nindex 08fc4cc..03f108f 100644\n--- a/repo-config.c\n+++ b/repo-config.c\n@@ -64,7 +64,22 @@ static int show_config(const char* key_,\n \n static int get_value(const char* key_, const char* regex_)\n {\n+\tint ret = -1;\n \tchar *tl;\n+\tchar *global = NULL, *repo_config = NULL;\n+\tconst char *local;\n+\n+\tlocal = getenv(\"GIT_CONFIG\");\n+\tif (!local) {\n+\t\tconst char *home = getenv(\"HOME\");\n+\t\tlocal = getenv(\"GIT_CONFIG_LOCAL\");\n+\t\tif (!local)\n+\t\t\tlocal = repo_config;\n+\t\telse\n+\t\t\tlocal = repo_config = strdup(git_path(\"config\"));\n+\t\tif (home)\n+\t\t\tglobal = strdup(mkpath(\"%s/.gitconfig\", home));\n+\t}\n \n \tkey = strdup(key_);\n \tfor (tl=key+strlen(key)-1; tl >= key && *tl != '.'; --tl)\n@@ -76,7 +91,7 @@ static int get_value(const char* key_, c\n \t\tkey_regexp = (regex_t*)malloc(sizeof(regex_t));\n \t\tif (regcomp(key_regexp, key, REG_EXTENDED)) {\n \t\t\tfprintf(stderr, \"Invalid key pattern: %s\\n\", key_);\n-\t\t\treturn -1;\n+\t\t\tgoto free_strings;\n \t\t}\n \t}\n \n@@ -89,11 +104,16 @@ static int get_value(const char* key_, c\n \t\tregexp = (regex_t*)malloc(sizeof(regex_t));\n \t\tif (regcomp(regexp, regex_, REG_EXTENDED)) {\n \t\t\tfprintf(stderr, \"Invalid pattern: %s\\n\", regex_);\n-\t\t\treturn -1;\n+\t\t\tgoto free_strings;\n \t\t}\n \t}\n \n-\tgit_config(show_config);\n+\tif (do_all && global)\n+\t\tgit_config_from_file(show_config, global);\n+\tgit_config_from_file(show_config, local);\n+\tif (!do_all && !seen && global)\n+\t\tgit_config_from_file(show_config, global);\n+\n \tfree(key);\n \tif (regexp) {\n \t\tregfree(regexp);\n@@ -101,9 +121,16 @@ static int get_value(const char* key_, c\n \t}\n \n \tif (do_all)\n-\t\treturn !seen;\n-\n-\treturn (seen == 1) ? 0 : 1;\n+\t\tret = !seen;\n+\telse\n+\t\tret =  (seen == 1) ? 0 : 1;\n+\n+free_strings:\n+\tif (repo_config)\n+\t\tfree(repo_config);\n+\tif (global)\n+\t\tfree(global);\n+\treturn ret;\n }\n \n int main(int argc, const char **argv)\n-- \n1.4.0.g6685\n"}]}