From: nguyenhu@minatec.inpg.fr Date: Fri, 01 Jun 2012 22:07:13 GMT Subject: Re: [PATCHv3] Read from XDG configuration file, not write Message-ID: <20120602000713.Horde.oEKVanwdC4BPyT0RI-CkWSA@webmail.minatec.grenoble-inp.fr> Junio C Hamano wrote: > Hrm, xdg_git_path() returns allocated memory, and each call site > leaks its return value, no? > > I didn't mean a micro-helper function like xdg_git_path() when I > suggested refactoring. I meant a helper that figures out all the > necessary bits in one go. For example, can't the above call site > look more like this? > > static int get_value(const char *key_, const char *regex_) > { > int ret = -1; > char *global = NULL, *xdg = NULL, *repo_config = NULL; > const char *system_wide = NULL, *local; > struct config_include_data inc = CONFIG_INCLUDE_INIT; > config_fn_t fn; > void *data; > > local = given_config_file; > if (!local) { > local = repo_config = git_pathdup("config"); > if (git_config_system()) > system_wide = git_etc_gitconfig(); > home_config_paths(&global, &xdg); > } > ... > > And then the config.c::home_config_paths() may look like: > > void home_config_paths(char **global, char **xdg) > { > char *xdg_home = getenv("XDG_CONFIG_HOME"); > char *home = getenv("HOME"); > char *to_free = NULL; > > if (!home) { > *global = NULL; > } else { > if (!xdg_home) { > to_free = strdup(mkpath("%s/.config", home)); > xdg_home = to_free; > } > *global = xstrdup(mkpath("%s/.gitconfig", home)); > } > > if (!xdg_home) > *xdg = NULL; > else > *xdg = xstrdup(mkpath("%s/git/config", xdg_home)); > free(to_free); We adapted this code to allow reuse and introduce default values to core.excludesfile and core.attributesfile in the XDG directory too, in our V4 version.