{"thread":{"id":"30683","subject":"Re: [PATCHv3] Read from XDG configuration file, not write","startedAt":"2012-06-01T22:07:13Z","lastAt":"2012-06-01T22:07:13Z","messageCount":1,"participants":["nguyenhu@minatec.inpg.fr"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"192699","messageId":"20120602000713.Horde.oEKVanwdC4BPyT0RI-CkWSA@webmail.minatec.grenoble-inp.fr","threadId":"30683","inReplyTo":null,"subject":"Re: [PATCHv3] Read from XDG configuration file, not write","fromName":"","fromEmail":"nguyenhu@minatec.inpg.fr","sentAt":"2012-06-01T22:07:13Z","receivedAt":"2012-06-01T22:07:13Z","isPatch":false,"sender":{"key":"nguyenhu@minatec.inpg.fr","avatar":null},"body":"Junio C Hamano <gitster@pobox.com> wrote:\n> Hrm, xdg_git_path() returns allocated memory, and each call site\n> leaks its return value, no?\n>\n> I didn't mean a micro-helper function like xdg_git_path() when I\n> suggested refactoring.  I meant a helper that figures out all the\n> necessary bits in one go.  For example, can't the above call site\n> look more like this?\n>\n>       static int get_value(const char *key_, const char *regex_)\n>       {\n>               int ret = -1;\n>               char *global = NULL, *xdg = NULL, *repo_config = NULL;\n>               const char *system_wide = NULL, *local;\n>               struct config_include_data inc = CONFIG_INCLUDE_INIT;\n>               config_fn_t fn;\n>               void *data;\n>\n>               local = given_config_file;\n>               if (!local) {\n>                       local = repo_config = git_pathdup(\"config\");\n>                       if (git_config_system())\n>                               system_wide = git_etc_gitconfig();\n>                       home_config_paths(&global, &xdg);\n>               }\n>               ...\n>\n> And then the config.c::home_config_paths() may look like:\n>\n>       void home_config_paths(char **global, char **xdg)\n>       {\n>               char *xdg_home = getenv(\"XDG_CONFIG_HOME\");\n>               char *home = getenv(\"HOME\");\n>               char *to_free = NULL;\n>\n>               if (!home) {\n>                       *global = NULL;\n>               } else {\n>                       if (!xdg_home) {\n>                               to_free = strdup(mkpath(\"%s/.config\", home));\n>                               xdg_home = to_free;\n>                       }\n>                       *global = xstrdup(mkpath(\"%s/.gitconfig\", home));\n>               }\n>\n>               if (!xdg_home)\n>                       *xdg = NULL;\n>               else\n>                       *xdg = xstrdup(mkpath(\"%s/git/config\", xdg_home));\n>               free(to_free);\n\nWe adapted this code to allow reuse and introduce default values to\ncore.excludesfile and core.attributesfile in the XDG directory too,\nin our V4 version.\n"}]}