Junio C Hamano <gitster@xxxxxxxxx> 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. -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html