From ea19289bc82351b7ac20ea2fd877e2bdde97ae34 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Tue, 21 Apr 2015 12:06:27 +0800 Subject: path.c: implement xdg_config_home() The XDG base dir spec[1] specifies that configuration files be stored in a subdirectory in $XDG_CONFIG_HOME. To construct such a configuration file path, home_config_paths() can be used. However, home_config_paths() combines distinct functionality: 1. Retrieve the home git config file path ~/.gitconfig 2. Construct the XDG config path of the file specified by `file`. This function was introduced in commit 21cf3227 ("read (but not write) from $XDG_CONFIG_HOME/git/config file"). While the intention of the function was to allow the home directory configuration file path and the xdg directory configuration file path to be retrieved with one function call, the hard-coding of the path ~/.gitconfig prevents it from being used for other configuration files. Furthermore, retrieving a file path relative to the user's home directory can be done with expand_user_path(). Hence, it can be seen that home_config_paths() introduces unnecessary complexity, especially if a user just wants to retrieve the xdg config file path. As such, implement a simpler function xdg_config_home() for constructing the XDG base dir spec configuration file path. This function, together with expand_user_path(), can replace all uses of home_config_paths(). [1] http://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html Helped-by: Eric Sunshine Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index f704af5..2cb5371 100644 --- a/cache.h +++ b/cache.h @@ -828,6 +828,13 @@ char *strip_path_suffix(const char *path, const char *suffix); int daemon_avoid_alias(const char *path); extern int is_ntfs_dotgit(const char *name); +/** + * Return a newly allocated string with the evaluation of + * "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise + * "$HOME/.config/git/$filename". Return NULL upon error. + */ +extern char *xdg_config_home(const char *filename); + /* object replacement */ #define LOOKUP_REPLACE_OBJECT 1 extern void *read_sha1_file_extended(const unsigned char *sha1, enum object_type *type, unsigned long *size, unsigned flag); diff --git a/path.c b/path.c index e608993..4edc1eb 100644 --- a/path.c +++ b/path.c @@ -856,3 +856,18 @@ int is_ntfs_dotgit(const char *name) len = -1; } } + +char *xdg_config_home(const char *filename) +{ + const char *home, *config_home; + + assert(filename); + config_home = getenv("XDG_CONFIG_HOME"); + if (config_home && *config_home) + return mkpathdup("%s/git/%s", config_home, filename); + + home = getenv("HOME"); + if (home) + return mkpathdup("%s/.config/git/%s", home, filename); + return NULL; +} -- cgit v0.10.2-6-g49f6 From 2527bbce25dcfd952064cf02057ad8729134ed44 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:00:59 +0800 Subject: attr.c: replace home_config_paths() with xdg_config_home() Since only the xdg attributes file path is required, simplify the code by using xdg_config_home() instead of home_config_paths(). Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/attr.c b/attr.c index cd54697..c82904b 100644 --- a/attr.c +++ b/attr.c @@ -478,7 +478,6 @@ static int git_attr_system(void) static void bootstrap_attr_stack(void) { struct attr_stack *elem; - char *xdg_attributes_file; if (attr_stack) return; @@ -497,10 +496,8 @@ static void bootstrap_attr_stack(void) } } - if (!git_attributes_file) { - home_config_paths(NULL, &xdg_attributes_file, "attributes"); - git_attributes_file = xdg_attributes_file; - } + if (!git_attributes_file) + git_attributes_file = xdg_config_home("attributes"); if (git_attributes_file) { elem = read_attr_from_file(git_attributes_file, 1); if (elem) { -- cgit v0.10.2-6-g49f6 From 2845ce7ff1398e851d46b62154293378b74c466d Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:00 +0800 Subject: dir.c: replace home_config_paths() with xdg_config_home() Since only the xdg excludes file path is required, simplify the code by replacing use of home_config_paths() with xdg_config_home(). Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/dir.c b/dir.c index 3f7a025..cb8f549 100644 --- a/dir.c +++ b/dir.c @@ -1622,14 +1622,11 @@ int remove_dir_recursively(struct strbuf *path, int flag) void setup_standard_excludes(struct dir_struct *dir) { const char *path; - char *xdg_path; dir->exclude_per_dir = ".gitignore"; path = git_path("info/exclude"); - if (!excludes_file) { - home_config_paths(NULL, &xdg_path, "ignore"); - excludes_file = xdg_path; - } + if (!excludes_file) + excludes_file = xdg_config_home("ignore"); if (!access_or_warn(path, R_OK, 0)) add_excludes_from_file(dir, path); if (excludes_file && !access_or_warn(excludes_file, R_OK, 0)) -- cgit v0.10.2-6-g49f6 From 64ab71db3a7e5b41a37580f8a3d15cb25b3e7093 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:01 +0800 Subject: credential-store.c: replace home_config_paths() with xdg_config_home() Since only the xdg credentials file path is required, and home_config_paths() is unable to construct the path ~/.git-credentials, simplify the code by replacing home_config_paths() with xdg_config_home(). Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/credential-store.c b/credential-store.c index d62dc29..8ebfb97 100644 --- a/credential-store.c +++ b/credential-store.c @@ -170,7 +170,7 @@ int main(int argc, char **argv) } else { if ((file = expand_user_path("~/.git-credentials"))) string_list_append_nodup(&fns, file); - home_config_paths(NULL, &file, "credentials"); + file = xdg_config_home("credentials"); if (file) string_list_append_nodup(&fns, file); } -- cgit v0.10.2-6-g49f6 From e682c9db1a7c6a81bb4c2e8e8c1d5191dae3bd9f Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:02 +0800 Subject: git-commit: replace use of home_config_paths() Since home_config_paths() combines two distinct functionality already implemented by expand_user_path() and xdg_config_home(), and hides the home config file path ~/.gitconfig. Make the code more explicit by replacing the use of home_config_paths() with expand_user_path() and xdg_config_home(). Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/builtin/commit.c b/builtin/commit.c index 7d90c35..b4aaaab 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1402,12 +1402,10 @@ int cmd_status(int argc, const char **argv, const char *prefix) static const char *implicit_ident_advice(void) { - char *user_config = NULL; - char *xdg_config = NULL; - int config_exists; + char *user_config = expand_user_path("~/.gitconfig"); + char *xdg_config = xdg_config_home("config"); + int config_exists = file_exists(user_config) || file_exists(xdg_config); - home_config_paths(&user_config, &xdg_config, "config"); - config_exists = file_exists(user_config) || file_exists(xdg_config); free(user_config); free(xdg_config); -- cgit v0.10.2-6-g49f6 From 509adc335274d2656829b18e1e83ccabda059ae3 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:03 +0800 Subject: git-config: replace use of home_config_paths() Since home_config_paths() combines distinct functionality already implemented by expand_user_path() and xdg_config_home(), and hides the home config file path ~/.gitconfig. Make the code more explicit by replacing the use of home_config_paths() with expand_user_path() and xdg_config_home(). Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/builtin/config.c b/builtin/config.c index 15a7bea..2f8bf7d 100644 --- a/builtin/config.c +++ b/builtin/config.c @@ -488,10 +488,8 @@ int cmd_config(int argc, const char **argv, const char *prefix) } if (use_global_config) { - char *user_config = NULL; - char *xdg_config = NULL; - - home_config_paths(&user_config, &xdg_config, "config"); + char *user_config = expand_user_path("~/.gitconfig"); + char *xdg_config = xdg_config_home("config"); if (!user_config) /* diff --git a/config.c b/config.c index 752e2e2..0d7af9e 100644 --- a/config.c +++ b/config.c @@ -1180,10 +1180,8 @@ int git_config_system(void) int git_config_early(config_fn_t fn, void *data, const char *repo_config) { int ret = 0, found = 0; - char *xdg_config = NULL; - char *user_config = NULL; - - home_config_paths(&user_config, &xdg_config, "config"); + char *xdg_config = xdg_config_home("config"); + char *user_config = expand_user_path("~/.gitconfig"); if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) { ret += git_config_from_file(fn, git_etc_gitconfig(), -- cgit v0.10.2-6-g49f6 From 846e5dfbab5d5a0a85252c1400dc0371e02e75a8 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:04 +0800 Subject: path.c: remove home_config_paths() home_config_paths() combines distinct functionality already implemented by expand_user_path() and xdg_config_home(), and it also hard-codes the path ~/.gitconfig, which makes it unsuitable to use for other home config file paths. Since its use will just add unnecessary complexity to the code, remove it. Signed-off-by: Paul Tan Signed-off-by: Junio C Hamano diff --git a/cache.h b/cache.h index 2cb5371..aa8d377 100644 --- a/cache.h +++ b/cache.h @@ -808,7 +808,6 @@ enum scld_error safe_create_leading_directories(char *path); enum scld_error safe_create_leading_directories_const(const char *path); int mkdir_in_gitdir(const char *path); -extern void home_config_paths(char **global, char **xdg, char *file); extern char *expand_user_path(const char *path); const char *enter_repo(const char *path, int strict); static inline int is_absolute_path(const char *path) diff --git a/path.c b/path.c index 4edc1eb..2436301 100644 --- a/path.c +++ b/path.c @@ -130,34 +130,6 @@ char *git_path(const char *fmt, ...) return ret; } -void home_config_paths(char **global, char **xdg, char *file) -{ - char *xdg_home = getenv("XDG_CONFIG_HOME"); - char *home = getenv("HOME"); - char *to_free = NULL; - - if (!home) { - if (global) - *global = NULL; - } else { - if (!xdg_home) { - to_free = mkpathdup("%s/.config", home); - xdg_home = to_free; - } - if (global) - *global = mkpathdup("%s/.gitconfig", home); - } - - if (xdg) { - if (!xdg_home) - *xdg = NULL; - else - *xdg = mkpathdup("%s/git/%s", xdg_home, file); - } - - free(to_free); -} - char *git_path_submodule(const char *path, const char *fmt, ...) { char *pathname = get_pathname(); -- cgit v0.10.2-6-g49f6