From cb2c2796e01c6c211b006bd1d15041b7a08d9acb Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Tue, 24 Mar 2015 13:20:27 +0800 Subject: [PATCH 01/11] git-credential-store: support multiple credential files Previously, git-credential-store only supported storing credentials in a single file: ~/.git-credentials. In order to support the XDG base directory specification[1], git-credential-store needs to be able to lookup and erase credentials from multiple files, as well as to pick the appropriate file to write to so that the credentials can be found on subsequent lookups. [1] http://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html Note that some credential storage files may not be owned, readable or writable by the user, as they may be system-wide files that are meant to apply to every user. Instead of a single file path, lookup_credential(), remove_credential() and store_credential() now take a precedence-ordered string_list of file paths. lookup_credential() expects both user-specific and system-wide credential files to be provided to support the use case of system administrators setting default credentials for users. remove_credential() and store_credential() expect only the user-specific credential files to be provided as usually the only config files that users are allowed to edit are their own user-specific ones. lookup_credential() will read these (user-specific and system-wide) file paths in order until it finds the 1st matching credential and print it. As some files may be private and thus unreadable, any file which cannot be read will be ignored silently. remove_credential() will erase credentials from all (user-specific) files in the list. This is because if credentials are only erased from the file with the highest precedence, a matching credential may still be found in a file further down the list. (Note that due to the lockfile code, this requires the directory to be writable, which should be so for user-specific config files) store_credential() will write the credentials to the first existing (user-specific) file in the list. If none of the files in the list exist, store_credential() will write to the filename specified by the first item of the filename list. For backwards compatibility, this filename should be "~/.git-credentials". Helped-by: Matthieu Moy Helped-by: Junio C Hamano Helped-by: Jeff King Signed-off-by: Paul Tan Reviewed-by: Matthieu Moy Signed-off-by: Junio C Hamano --- credential-store.c | 81 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/credential-store.c b/credential-store.c index d435514cbe..c519f8025e 100644 --- a/credential-store.c +++ b/credential-store.c @@ -6,7 +6,7 @@ static struct lock_file credential_lock; -static void parse_credential_file(const char *fn, +static int parse_credential_file(const char *fn, struct credential *c, void (*match_cb)(struct credential *), void (*other_cb)(struct strbuf *)) @@ -14,18 +14,20 @@ static void parse_credential_file(const char *fn, FILE *fh; struct strbuf line = STRBUF_INIT; struct credential entry = CREDENTIAL_INIT; + int found_credential = 0; fh = fopen(fn, "r"); if (!fh) { - if (errno != ENOENT) + if (errno != ENOENT && errno != EACCES) die_errno("unable to open %s", fn); - return; + return found_credential; } while (strbuf_getline(&line, fh, '\n') != EOF) { credential_from_url(&entry, line.buf); if (entry.username && entry.password && credential_match(c, &entry)) { + found_credential = 1; if (match_cb) { match_cb(&entry); break; @@ -38,6 +40,7 @@ static void parse_credential_file(const char *fn, credential_clear(&entry); strbuf_release(&line); fclose(fh); + return found_credential; } static void print_entry(struct credential *c) @@ -64,21 +67,10 @@ static void rewrite_credential_file(const char *fn, struct credential *c, die_errno("unable to commit credential store"); } -static void store_credential(const char *fn, struct credential *c) +static void store_credential_file(const char *fn, struct credential *c) { struct strbuf buf = STRBUF_INIT; - /* - * Sanity check that what we are storing is actually sensible. - * In particular, we can't make a URL without a protocol field. - * Without either a host or pathname (depending on the scheme), - * we have no primary key. And without a username and password, - * we are not actually storing a credential. - */ - if (!c->protocol || !(c->host || c->path) || - !c->username || !c->password) - return; - strbuf_addf(&buf, "%s://", c->protocol); strbuf_addstr_urlencode(&buf, c->username, 1); strbuf_addch(&buf, ':'); @@ -95,8 +87,37 @@ static void store_credential(const char *fn, struct credential *c) strbuf_release(&buf); } -static void remove_credential(const char *fn, struct credential *c) +static void store_credential(const struct string_list *fns, struct credential *c) { + struct string_list_item *fn; + + /* + * Sanity check that what we are storing is actually sensible. + * In particular, we can't make a URL without a protocol field. + * Without either a host or pathname (depending on the scheme), + * we have no primary key. And without a username and password, + * we are not actually storing a credential. + */ + if (!c->protocol || !(c->host || c->path) || !c->username || !c->password) + return; + + for_each_string_list_item(fn, fns) + if (!access(fn->string, F_OK)) { + store_credential_file(fn->string, c); + return; + } + /* + * Write credential to the filename specified by fns->items[0], thus + * creating it + */ + if (fns->nr) + store_credential_file(fns->items[0].string, c); +} + +static void remove_credential(const struct string_list *fns, struct credential *c) +{ + struct string_list_item *fn; + /* * Sanity check that we actually have something to match * against. The input we get is a restrictive pattern, @@ -105,14 +126,20 @@ static void remove_credential(const char *fn, struct credential *c) * to empty input. So explicitly disallow it, and require that the * pattern have some actual content to match. */ - if (c->protocol || c->host || c->path || c->username) - rewrite_credential_file(fn, c, NULL); + if (!c->protocol && !c->host && !c->path && !c->username) + return; + for_each_string_list_item(fn, fns) + if (!access(fn->string, F_OK)) + rewrite_credential_file(fn->string, c, NULL); } -static int lookup_credential(const char *fn, struct credential *c) +static void lookup_credential(const struct string_list *fns, struct credential *c) { - parse_credential_file(fn, c, print_entry, NULL); - return c->username && c->password; + struct string_list_item *fn; + + for_each_string_list_item(fn, fns) + if (parse_credential_file(fn->string, c, print_entry, NULL)) + return; /* Found credential */ } int main(int argc, char **argv) @@ -123,6 +150,7 @@ int main(int argc, char **argv) }; const char *op; struct credential c = CREDENTIAL_INIT; + struct string_list fns = STRING_LIST_INIT_DUP; char *file = NULL; struct option options[] = { OPT_STRING(0, "file", &file, "path", @@ -139,20 +167,23 @@ int main(int argc, char **argv) if (!file) file = expand_user_path("~/.git-credentials"); - if (!file) + if (file) + string_list_append(&fns, file); + else die("unable to set up default path; use --file"); if (credential_read(&c, stdin) < 0) die("unable to read credential"); if (!strcmp(op, "get")) - lookup_credential(file, &c); + lookup_credential(&fns, &c); else if (!strcmp(op, "erase")) - remove_credential(file, &c); + remove_credential(&fns, &c); else if (!strcmp(op, "store")) - store_credential(file, &c); + store_credential(&fns, &c); else ; /* Ignore unknown operation. */ + string_list_clear(&fns, 0); return 0; } From 44b228985e59966588704eaa7c4ec9832ba0f8ee Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Tue, 24 Mar 2015 13:20:28 +0800 Subject: [PATCH 02/11] git-credential-store: support XDG_CONFIG_HOME Add $XDG_CONFIG_HOME/git/credentials to the default credential search path of git-credential-store. This allows git-credential-store to support user-specific configuration files in accordance with the XDG base directory specification[1]. [1] http://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html ~/.git-credentials has a higher precedence than $XDG_CONFIG_HOME/git/credentials when looking up credentials. This means that if any duplicate matching credentials are found in the xdg file (due to ~/.git-credentials being updated by old versions of git or outdated tools), they will not be used at all. This is to give the user some leeway in switching to old versions of git while keeping the xdg directory. This is consistent with the behavior of git-config. However, the higher precedence of ~/.git-credentials means that as long as ~/.git-credentials exist, all credentials will be written to the ~/.git-credentials file even if the user has an xdg file as having a ~/.git-credentials file indicates that the user wants to preserve backwards-compatibility. This is also consistent with the behavior of git-config. To make this precedence explicit in docs/git-credential-store, add a new section FILES that lists out the credential file paths in their order of precedence, and explain how the ordering affects the lookup, storage and erase operations. Also, update the documentation for --file to briefly explain the operations on multiple files if the --file option is not provided. Since the xdg file will not be used unless it actually exists, to prevent the situation where some credentials are present in the xdg file while some are present in the home file, users are recommended to not create the xdg file if they require compatibility with old versions of git or outdated tools. Note, though, that "erase" can be used to explicitly erase matching credentials from all files. Helped-by: Matthieu Moy Helped-by: Junio C Hamano Helped-by: Jeff King Helped-by: Eric Sunshine Signed-off-by: Paul Tan Reviewed-by: Matthieu Moy Signed-off-by: Junio C Hamano --- Documentation/git-credential-store.txt | 35 ++++++++++++++++++++++++-- credential-store.c | 13 +++++++--- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/Documentation/git-credential-store.txt b/Documentation/git-credential-store.txt index bc97071e76..e3c8f276b1 100644 --- a/Documentation/git-credential-store.txt +++ b/Documentation/git-credential-store.txt @@ -31,10 +31,41 @@ OPTIONS --file=:: - Use `` to store credentials. The file will have its + Use `` to lookup and store credentials. The file will have its filesystem permissions set to prevent other users on the system from reading it, but will not be encrypted or otherwise - protected. Defaults to `~/.git-credentials`. + protected. If not specified, credentials will be searched for from + `~/.git-credentials` and `$XDG_CONFIG_HOME/git/credentials`, and + credentials will be written to `~/.git-credentials` if it exists, or + `$XDG_CONFIG_HOME/git/credentials` if it exists and the former does + not. See also <>. + +[[FILES]] +FILES +----- + +If not set explicitly with '--file', there are two files where +git-credential-store will search for credentials in order of precedence: + +~/.git-credentials:: + User-specific credentials file. + +$XDG_CONFIG_HOME/git/credentials:: + Second user-specific credentials file. If '$XDG_CONFIG_HOME' is not set + or empty, `$HOME/.config/git/credentials` will be used. Any credentials + stored in this file will not be used if `~/.git-credentials` has a + matching credential as well. It is a good idea not to create this file + if you sometimes use older versions of Git that do not support it. + +For credential lookups, the files are read in the order given above, with the +first matching credential found taking precedence over credentials found in +files further down the list. + +Credential storage will by default write to the first existing file in the +list. If none of these files exist, `~/.git-credentials` will be created and +written to. + +When erasing credentials, matching credentials will be erased from all files. EXAMPLES -------- diff --git a/credential-store.c b/credential-store.c index c519f8025e..d62dc29d06 100644 --- a/credential-store.c +++ b/credential-store.c @@ -165,11 +165,16 @@ int main(int argc, char **argv) usage_with_options(usage, options); op = argv[0]; - if (!file) - file = expand_user_path("~/.git-credentials"); - if (file) + if (file) { string_list_append(&fns, file); - else + } else { + if ((file = expand_user_path("~/.git-credentials"))) + string_list_append_nodup(&fns, file); + home_config_paths(NULL, &file, "credentials"); + if (file) + string_list_append_nodup(&fns, file); + } + if (!fns.nr) die("unable to set up default path; use --file"); if (credential_read(&c, stdin) < 0) From 7e314539d6f3c1e53a826e8a857cef174a023510 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Tue, 24 Mar 2015 13:20:29 +0800 Subject: [PATCH 03/11] t0302: test credential-store support for XDG_CONFIG_HOME t0302 now tests git-credential-store's support for the XDG user-specific configuration file $XDG_CONFIG_HOME/git/credentials. Specifically: * Ensure that the XDG file is strictly opt-in. It should not be created by git at all times if it does not exist. * Conversely, if the XDG file exists, ~/.git-credentials should not be created at all times. * If both the XDG file and ~/.git-credentials exists, then both files should be used for credential lookups. However, credentials should only be written to ~/.git-credentials. * Credentials must be erased from both files. * $XDG_CONFIG_HOME can be a custom directory set by the user as per the XDG base directory specification. Test that git-credential-store respects that, but defaults to "~/.config/git/credentials" if it does not exist or is empty. Helped-by: Matthieu Moy Helped-by: Junio C Hamano Helped-by: Eric Sunshine Signed-off-by: Paul Tan Reviewed-by: Matthieu Moy Signed-off-by: Junio C Hamano --- t/t0302-credential-store.sh | 114 ++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/t/t0302-credential-store.sh b/t/t0302-credential-store.sh index f61b40c69b..4e1f8ecddd 100755 --- a/t/t0302-credential-store.sh +++ b/t/t0302-credential-store.sh @@ -6,4 +6,118 @@ test_description='credential-store tests' helper_test store +test_expect_success 'when xdg file does not exist, xdg file not created' ' + test_path_is_missing "$HOME/.config/git/credentials" && + test -s "$HOME/.git-credentials" +' + +test_expect_success 'setup xdg file' ' + rm -f "$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + >"$HOME/.config/git/credentials" +' + +helper_test store + +test_expect_success 'when xdg file exists, home file not created' ' + test -s "$HOME/.config/git/credentials" && + test_path_is_missing "$HOME/.git-credentials" +' + +test_expect_success 'setup custom xdg file' ' + rm -f "$HOME/.git-credentials" && + rm -f "$HOME/.config/git/credentials" && + mkdir -p "$HOME/xdg/git" && + >"$HOME/xdg/git/credentials" +' + +XDG_CONFIG_HOME="$HOME/xdg" +export XDG_CONFIG_HOME +helper_test store +unset XDG_CONFIG_HOME + +test_expect_success 'if custom xdg file exists, home and xdg files not created' ' + test_when_finished "rm -f $HOME/xdg/git/credentials" && + test -s "$HOME/xdg/git/credentials" && + test_path_is_missing "$HOME/.git-credentials" && + test_path_is_missing "$HOME/.config/git/credentials" +' + +test_expect_success 'get: use home file if both home and xdg files have matches' ' + echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" && + check fill store <<-\EOF + protocol=https + host=example.com + -- + protocol=https + host=example.com + username=home-user + password=home-pass + -- + EOF +' + +test_expect_success 'get: use xdg file if home file has no matches' ' + >"$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" && + check fill store <<-\EOF + protocol=https + host=example.com + -- + protocol=https + host=example.com + username=xdg-user + password=xdg-pass + -- + EOF +' + +test_expect_success 'get: use xdg file if home file is unreadable' ' + echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" && + chmod -r "$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" && + check fill store <<-\EOF + protocol=https + host=example.com + -- + protocol=https + host=example.com + username=xdg-user + password=xdg-pass + -- + EOF +' + +test_expect_success 'store: if both xdg and home files exist, only store in home file' ' + >"$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + >"$HOME/.config/git/credentials" && + check approve store <<-\EOF && + protocol=https + host=example.com + username=store-user + password=store-pass + EOF + echo "https://store-user:store-pass@example.com" >expected && + test_cmp expected "$HOME/.git-credentials" && + test_must_be_empty "$HOME/.config/git/credentials" +' + + +test_expect_success 'erase: erase matching credentials from both xdg and home files' ' + echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" && + mkdir -p "$HOME/.config/git" && + echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" && + check reject store <<-\EOF && + protocol=https + host=example.com + EOF + test_must_be_empty "$HOME/.git-credentials" && + test_must_be_empty "$HOME/.config/git/credentials" +' + test_done From efee5981d3d5d72f9cc7208ba7c5e9ce4afc8598 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Wed, 25 Mar 2015 13:23:21 -0700 Subject: [PATCH 04/11] t0302: "unreadable" test needs POSIXPERM Noticed and fixed by Eric Sunshine, confirmed by Johannes Sixt. Signed-off-by: Junio C Hamano --- t/t0302-credential-store.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/t0302-credential-store.sh b/t/t0302-credential-store.sh index 4e1f8ecddd..0979df93a1 100755 --- a/t/t0302-credential-store.sh +++ b/t/t0302-credential-store.sh @@ -75,7 +75,7 @@ test_expect_success 'get: use xdg file if home file has no matches' ' EOF ' -test_expect_success 'get: use xdg file if home file is unreadable' ' +test_expect_success POSIXPERM 'get: use xdg file if home file is unreadable' ' echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" && chmod -r "$HOME/.git-credentials" && mkdir -p "$HOME/.config/git" && From ea19289bc82351b7ac20ea2fd877e2bdde97ae34 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Tue, 21 Apr 2015 12:06:27 +0800 Subject: [PATCH 05/11] 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 --- cache.h | 7 +++++++ path.c | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/cache.h b/cache.h index f704af5df0..2cb5371ac2 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 e608993801..4edc1eb868 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; +} From 2527bbce25dcfd952064cf02057ad8729134ed44 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:00:59 +0800 Subject: [PATCH 06/11] 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 --- attr.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/attr.c b/attr.c index cd5469770a..c82904b6f2 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) { From 2845ce7ff1398e851d46b62154293378b74c466d Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:00 +0800 Subject: [PATCH 07/11] 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 --- dir.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/dir.c b/dir.c index 3f7a0256b6..cb8f5496ca 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)) From 64ab71db3a7e5b41a37580f8a3d15cb25b3e7093 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:01 +0800 Subject: [PATCH 08/11] 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 --- credential-store.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/credential-store.c b/credential-store.c index d62dc29d06..8ebfb97c7f 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); } From e682c9db1a7c6a81bb4c2e8e8c1d5191dae3bd9f Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:02 +0800 Subject: [PATCH 09/11] 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 --- builtin/commit.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/builtin/commit.c b/builtin/commit.c index 7d90c35915..b4aaaab5bc 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); From 509adc335274d2656829b18e1e83ccabda059ae3 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:03 +0800 Subject: [PATCH 10/11] 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 --- builtin/config.c | 6 ++---- config.c | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/builtin/config.c b/builtin/config.c index 15a7bea936..2f8bf7d7cf 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 752e2e227f..0d7af9eefe 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(), From 846e5dfbab5d5a0a85252c1400dc0371e02e75a8 Mon Sep 17 00:00:00 2001 From: Paul Tan Date: Wed, 6 May 2015 16:01:04 +0800 Subject: [PATCH 11/11] 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 --- cache.h | 1 - path.c | 28 ---------------------------- 2 files changed, 29 deletions(-) diff --git a/cache.h b/cache.h index 2cb5371ac2..aa8d377448 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 4edc1eb868..2436301cca 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();