All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/7] path.c: implement xdg_config_home()
@ 2015-04-12  7:46 Paul Tan
  2015-04-12  7:46 ` [PATCH 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
                   ` (7 more replies)
  0 siblings, 8 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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

Signed-off-by: Paul Tan <pyokagan@gmail.com>
---
 cache.h |  7 +++++++
 path.c  | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/cache.h b/cache.h
index 3d3244b..7f9bab0 100644
--- a/cache.h
+++ b/cache.h
@@ -836,6 +836,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);
 
+/**
+ * Returns the newly allocated string "$XDG_CONFIG_HOME/git/%s".  If
+ * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
+ * "$HOME/.config/git/%s". Returns NULL if an error occurred.
+ */
+extern char *xdg_config_home(const char *fn);
+
 /* 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..4c32d16 100644
--- a/path.c
+++ b/path.c
@@ -856,3 +856,19 @@ int is_ntfs_dotgit(const char *name)
 			len = -1;
 		}
 }
+
+char *xdg_config_home(const char *fn)
+{
+	const char *config_home = getenv("XDG_CONFIG_HOME");
+
+	if (!fn)
+		fn = "";
+	if (!config_home || !config_home[0]) {
+		const char *home = getenv("HOME");
+
+		if (!home)
+			return NULL;
+		return mkpathdup("%s/.config/git/%s", home, fn);
+	} else
+		return mkpathdup("%s/git/%s", config_home, fn);
+}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 2/7] attr.c: replace home_config_paths() with xdg_config_home()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-12  7:46 ` [PATCH 3/7] dir.c: " Paul Tan
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 attr.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/attr.c b/attr.c
index 1f9eebd..868dce3 100644
--- a/attr.c
+++ b/attr.c
@@ -488,7 +488,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;
@@ -507,10 +506,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) {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 3/7] dir.c: replace home_config_paths() with xdg_config_home()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
  2015-04-12  7:46 ` [PATCH 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-12  7:46 ` [PATCH 4/7] credential-store.c: " Paul Tan
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 dir.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/dir.c b/dir.c
index 0943a81..335dc7e 100644
--- a/dir.c
+++ b/dir.c
@@ -1665,14 +1665,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))
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 4/7] credential-store.c: replace home_config_paths() with xdg_config_home()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
  2015-04-12  7:46 ` [PATCH 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
  2015-04-12  7:46 ` [PATCH 3/7] dir.c: " Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-12  7:46 ` [PATCH 5/7] git-commit: replace use of home_config_paths() Paul Tan
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 credential-store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/credential-store.c b/credential-store.c
index 8b22251..f692509 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);
 	}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 5/7] git-commit: replace use of home_config_paths()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
                   ` (2 preceding siblings ...)
  2015-04-12  7:46 ` [PATCH 4/7] credential-store.c: " Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-12  7:46 ` [PATCH 6/7] git-config: " Paul Tan
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 builtin/commit.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index da79ac4..c2ebea4 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1398,12 +1398,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);
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 6/7] git-config: replace use of home_config_paths()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
                   ` (3 preceding siblings ...)
  2015-04-12  7:46 ` [PATCH 5/7] git-commit: replace use of home_config_paths() Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-12  7:46 ` [PATCH 7/7] path.c: remove home_config_paths() Paul Tan
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 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 d32c532..5e61f4a 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 66c0a51..3a619da 100644
--- a/config.c
+++ b/config.c
@@ -1185,10 +1185,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(),
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 7/7] path.c: remove home_config_paths()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
                   ` (4 preceding siblings ...)
  2015-04-12  7:46 ` [PATCH 6/7] git-config: " Paul Tan
@ 2015-04-12  7:46 ` Paul Tan
  2015-04-13 15:50 ` [PATCH 1/7] path.c: implement xdg_config_home() Johannes Schindelin
  2015-04-13 21:43 ` Matthieu Moy
  7 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-12  7:46 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---
 cache.h |  1 -
 path.c  | 28 ----------------------------
 2 files changed, 29 deletions(-)

diff --git a/cache.h b/cache.h
index 7f9bab0..19d75ef 100644
--- a/cache.h
+++ b/cache.h
@@ -816,7 +816,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 4c32d16..84ec589 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();
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
                   ` (5 preceding siblings ...)
  2015-04-12  7:46 ` [PATCH 7/7] path.c: remove home_config_paths() Paul Tan
@ 2015-04-13 15:50 ` Johannes Schindelin
  2015-04-14 17:28   ` [PATCH v2 " Paul Tan
  2015-04-14 20:39   ` [PATCH " Junio C Hamano
  2015-04-13 21:43 ` Matthieu Moy
  7 siblings, 2 replies; 26+ messages in thread
From: Johannes Schindelin @ 2015-04-13 15:50 UTC (permalink / raw)
  To: Paul Tan; +Cc: git, Junio C Hamano

Hi Paul,

maybe it would be a good idea to add a `0/7` mail that describes the overall goal of this patch series, much like a Pull Request? I found it very useful -- even for myself -- to set a description via `git branch --edit-description` and to let `git format-patch` use that via the `--cover-letter` option.

below just two minor nits because the rest of the patches looks fine to me:

On 2015-04-12 09:46, Paul Tan wrote:


> diff --git a/cache.h b/cache.h
> index 3d3244b..7f9bab0 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -836,6 +836,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);
>  
> +/**
> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/%s".  If
> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
> + * "$HOME/.config/git/%s". Returns NULL if an error occurred.
> + */
> +extern char *xdg_config_home(const char *fn);

Should this not be inserted close to home_config_paths()? Also, the name "fn" sounds more like "function" than like "filename" to me, especially keeping the name `config_fn_t` in mind. Maybe call the parameter "filename" to avoid confusion?

Thanks,
Dscho

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
                   ` (6 preceding siblings ...)
  2015-04-13 15:50 ` [PATCH 1/7] path.c: implement xdg_config_home() Johannes Schindelin
@ 2015-04-13 21:43 ` Matthieu Moy
  2015-04-14  0:18   ` Stefan Beller
  7 siblings, 1 reply; 26+ messages in thread
From: Matthieu Moy @ 2015-04-13 21:43 UTC (permalink / raw)
  To: Paul Tan; +Cc: git

Paul Tan <pyokagan@gmail.com> writes:

> 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().

Indeed. The code looks much better after your patch series than before.

I agree with Dscho's remark that "fn" sounds like "function" more than
"filename". Perhaps just "name" would be better.

Anyway, the series is

Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr>

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-13 21:43 ` Matthieu Moy
@ 2015-04-14  0:18   ` Stefan Beller
  0 siblings, 0 replies; 26+ messages in thread
From: Stefan Beller @ 2015-04-14  0:18 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Paul Tan, git

On Mon, Apr 13, 2015 at 2:43 PM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Paul Tan <pyokagan@gmail.com> writes:
>
>> 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().
>
> Indeed. The code looks much better after your patch series than before.
>
> I agree with Dscho's remark that "fn" sounds like "function" more than
> "filename". Perhaps just "name" would be better.

I'd go with fname but that's just bikeshedding now.

>
> Anyway, the series is
>
> Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr>

and
Reviewed-by: Stefan Beller <sbeller@google.com>

>
> --
> Matthieu Moy
> http://www-verimag.imag.fr/~moy/
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v2 1/7] path.c: implement xdg_config_home()
  2015-04-13 15:50 ` [PATCH 1/7] path.c: implement xdg_config_home() Johannes Schindelin
@ 2015-04-14 17:28   ` Paul Tan
  2015-04-16 21:41     ` Eric Sunshine
  2015-04-14 20:39   ` [PATCH " Junio C Hamano
  1 sibling, 1 reply; 26+ messages in thread
From: Paul Tan @ 2015-04-14 17:28 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, Junio C Hamano, Matthieu Moy, Stefan Beller

Hi,

On Mon, Apr 13, 2015 at 05:50:49PM +0200, Johannes Schindelin wrote:
> maybe it would be a good idea to add a `0/7` mail that describes the
> overall goal of this patch series, much like a Pull Request? I found
> it very useful -- even for myself -- to set a description via `git
> branch --edit-description` and to let `git format-patch` use that via
> the `--cover-letter` option.

In this case I felt that the first patch's commit message was
descriptive enough and a cover message would simply repeat it.

> below just two minor nits because the rest of the patches looks fine to me:
> 
> On 2015-04-12 09:46, Paul Tan wrote:
> > diff --git a/cache.h b/cache.h
> > index 3d3244b..7f9bab0 100644
> > --- a/cache.h
> > +++ b/cache.h
> > @@ -836,6 +836,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);
> >  
> > +/**
> > + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/%s".  If
> > + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
> > + * "$HOME/.config/git/%s". Returns NULL if an error occurred.
> > + */
> > +extern char *xdg_config_home(const char *fn);
> 
> Should this not be inserted close to home_config_paths()?

A personal style thing, but I wanted to add the function's docstring to
cache.h (where I personally think it belongs), but I didn't want to
break up the huge block of path function declarations. Hence, it was
added at the end.

> Also, the name "fn" sounds more like "function" than like "filename"
> to me, especially keeping the name `config_fn_t` in mind. Maybe call
> the parameter "filename" to avoid confusion?

That's true, especially since there is still lots of horizontal space
for this name.

Below is the fixed patch. I also decided to return NULL if `filename` is
NULL because such an input usually indicated an uncaught error. The
docstring has also been modified to be a little clearer.

Thanks all for the reviews.

---- >8 ----
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

Signed-off-by: Paul Tan <pyokagan@gmail.com>
---
 cache.h |  8 ++++++++
 path.c  | 16 ++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/cache.h b/cache.h
index 3d3244b..2db10b8 100644
--- a/cache.h
+++ b/cache.h
@@ -836,6 +836,14 @@ 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);
 
+/**
+ * Returns the newly allocated string "$XDG_CONFIG_HOME/git/{filename}".  If
+ * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
+ * "$HOME/.config/git/{filename}". Returns NULL if filename is NULL or an error
+ * occurred.
+ */
+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..8ee7191 100644
--- a/path.c
+++ b/path.c
@@ -856,3 +856,19 @@ int is_ntfs_dotgit(const char *name)
 			len = -1;
 		}
 }
+
+char *xdg_config_home(const char *filename)
+{
+	const char *config_home = getenv("XDG_CONFIG_HOME");
+
+	if (!filename)
+		return NULL;
+	if (!config_home || !config_home[0]) {
+		const char *home = getenv("HOME");
+
+		if (!home)
+			return NULL;
+		return mkpathdup("%s/.config/git/%s", home, filename);
+	} else
+		return mkpathdup("%s/git/%s", config_home, filename);
+}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-13 15:50 ` [PATCH 1/7] path.c: implement xdg_config_home() Johannes Schindelin
  2015-04-14 17:28   ` [PATCH v2 " Paul Tan
@ 2015-04-14 20:39   ` Junio C Hamano
  2015-04-14 22:28     ` Stefan Beller
  1 sibling, 1 reply; 26+ messages in thread
From: Junio C Hamano @ 2015-04-14 20:39 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: Paul Tan, git

Johannes Schindelin <johannes.schindelin@gmx.de> writes:

>> diff --git a/cache.h b/cache.h
>> index 3d3244b..7f9bab0 100644
>> --- a/cache.h
>> +++ b/cache.h
>> @@ -836,6 +836,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);
>>  
>> +/**
>> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/%s".  If
>> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
>> + * "$HOME/.config/git/%s". Returns NULL if an error occurred.
>> + */
>> +extern char *xdg_config_home(const char *fn);
>
> Should this not be inserted close to home_config_paths()? Also, the
> name "fn" sounds more like "function" than like "filename" to me,
> especially keeping the name `config_fn_t` in mind. Maybe call the
> parameter "filename" to avoid confusion?

It is OK to omit the name in the extern declaration here.  We have
to have a sensible variable name in the definition in path.c, of
course ;-), and "filename" sounds like a very sensible suggestion.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-14 20:39   ` [PATCH " Junio C Hamano
@ 2015-04-14 22:28     ` Stefan Beller
  2015-04-14 22:30       ` Junio C Hamano
  0 siblings, 1 reply; 26+ messages in thread
From: Stefan Beller @ 2015-04-14 22:28 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, Paul Tan, git

On Tue, Apr 14, 2015 at 1:39 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Johannes Schindelin <johannes.schindelin@gmx.de> writes:
>
>>> diff --git a/cache.h b/cache.h
>>> index 3d3244b..7f9bab0 100644
>>> --- a/cache.h
>>> +++ b/cache.h
>>> @@ -836,6 +836,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);
>>>
>>> +/**
>>> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/%s".  If
>>> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
>>> + * "$HOME/.config/git/%s". Returns NULL if an error occurred.
>>> + */
>>> +extern char *xdg_config_home(const char *fn);
>>
>> Should this not be inserted close to home_config_paths()? Also, the
>> name "fn" sounds more like "function" than like "filename" to me,
>> especially keeping the name `config_fn_t` in mind. Maybe call the
>> parameter "filename" to avoid confusion?
>
> It is OK to omit the name in the extern declaration here.  We have
> to have a sensible variable name in the definition in path.c, of
> course ;-), and "filename" sounds like a very sensible suggestion.
>

It is OK, but I think this is bad practice.
Have a look at strbuf.h, there you'll find:

    extern int strbuf_getwholeline_fd(struct strbuf *, int, int);

It's not clear what the 2 ints are, probably a fd and a max size?
Even if guessed correctly, you'd still need another guess for the order.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-14 22:28     ` Stefan Beller
@ 2015-04-14 22:30       ` Junio C Hamano
  2015-04-14 22:34         ` Stefan Beller
  0 siblings, 1 reply; 26+ messages in thread
From: Junio C Hamano @ 2015-04-14 22:30 UTC (permalink / raw)
  To: Stefan Beller; +Cc: Johannes Schindelin, Paul Tan, git

Stefan Beller <sbeller@google.com> writes:

> On Tue, Apr 14, 2015 at 1:39 PM, Junio C Hamano <gitster@pobox.com> wrote:
>
>> It is OK to omit the name in the extern declaration here.
>
> It is OK, but I think this is bad practice.

Take a special note on the word "here", meaning "in this particular
case."  It is perfectly fine when the meaning of the parameter is
clear from its type.

I was assuming that our developers have common sense to disambiguate
ambiguous cases, of course ;-)

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 1/7] path.c: implement xdg_config_home()
  2015-04-14 22:30       ` Junio C Hamano
@ 2015-04-14 22:34         ` Stefan Beller
  0 siblings, 0 replies; 26+ messages in thread
From: Stefan Beller @ 2015-04-14 22:34 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Johannes Schindelin, Paul Tan, git

I need to learn to read the whole sentence. :(
Apologies.

On Tue, Apr 14, 2015 at 3:30 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Stefan Beller <sbeller@google.com> writes:
>
>> On Tue, Apr 14, 2015 at 1:39 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>
>>> It is OK to omit the name in the extern declaration here.
>>
>> It is OK, but I think this is bad practice.
>
> Take a special note on the word "here", meaning "in this particular
> case."  It is perfectly fine when the meaning of the parameter is
> clear from its type.
>
> I was assuming that our developers have common sense to disambiguate
> ambiguous cases, of course ;-)

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v2 1/7] path.c: implement xdg_config_home()
  2015-04-14 17:28   ` [PATCH v2 " Paul Tan
@ 2015-04-16 21:41     ` Eric Sunshine
  2015-04-18  7:51       ` Paul Tan
  2015-04-18  8:48       ` [PATCH v2 1/7] path.c: implement xdg_config_home() Paul Tan
  0 siblings, 2 replies; 26+ messages in thread
From: Eric Sunshine @ 2015-04-16 21:41 UTC (permalink / raw)
  To: Paul Tan
  Cc: Johannes Schindelin, Git List, Junio C Hamano, Matthieu Moy,
	Stefan Beller

On Tue, Apr 14, 2015 at 1:28 PM, Paul Tan <pyokagan@gmail.com> wrote:
> Below is the fixed patch. I also decided to return NULL if `filename` is
> NULL because such an input usually indicated an uncaught error.

Unfortunately, this blurs the line between programmer error (passing
NULL for filename) and a user/configuration error (XDG_CONFIG_HOME and
HOME being undefined). If there is indeed no valid interpretation for
filename==NULL, then it may be better to die() or assert() here to
flag the programmer error as early as possible, rather than returning
NULL.

More below.

> ---- >8 ----
> 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
>
> Signed-off-by: Paul Tan <pyokagan@gmail.com>
> ---
> diff --git a/cache.h b/cache.h
> index 3d3244b..2db10b8 100644
> --- a/cache.h
> +++ b/cache.h
> @@ -836,6 +836,14 @@ 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);
>
> +/**
> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/{filename}".  If
> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
> + * "$HOME/.config/git/{filename}". Returns NULL if filename is NULL or an error
> + * occurred.
> + */

This is better than the original which said "$XDG_CONFIG_HOME/git/%s",
but is still potentially confusing. When I read the earlier iteration,
I was left with the impression that it was returning that literal
string, with '$' and '%s' embedded, and that the caller would have to
process it further to have '$' and '%s' expanded. Perhaps rephrasing
it something like this will help?

    Return a newly allocated string with value xdg+"/git/"+filename
    where xdg is the interpolated value of $XDG_CONFIG_HOME if
    defined and non-empty, otherwise "$HOME/.config". Return NULL
    upon error.

Also, for consistency with other API documentation, say "Return"
rather than "Returns".

More below.

> +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..8ee7191 100644
> --- a/path.c
> +++ b/path.c
> @@ -856,3 +856,19 @@ int is_ntfs_dotgit(const char *name)
>                         len = -1;
>                 }
>  }
> +
> +char *xdg_config_home(const char *filename)
> +{
> +       const char *config_home = getenv("XDG_CONFIG_HOME");
> +
> +       if (!filename)
> +               return NULL;

See above regarding conflation of programmer error and user/configuration error.

> +       if (!config_home || !config_home[0]) {

On this project, *config_home is usually favored over config_home[0].

> +               const char *home = getenv("HOME");
> +
> +               if (!home)
> +                       return NULL;
> +               return mkpathdup("%s/.config/git/%s", home, filename);
> +       } else
> +               return mkpathdup("%s/git/%s", config_home, filename);

This logic is more difficult to follow than it ought to be due to the
use of 'config_home' so distant from the 'if' which checked it, and
due to the nested 'if'. It could be expressed more straight-forwardly
as:

    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;

> +}
> --
> 2.1.4

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v2 1/7] path.c: implement xdg_config_home()
  2015-04-16 21:41     ` Eric Sunshine
@ 2015-04-18  7:51       ` Paul Tan
  2015-04-20  0:39         ` Eric Sunshine
  2015-04-18  8:48       ` [PATCH v2 1/7] path.c: implement xdg_config_home() Paul Tan
  1 sibling, 1 reply; 26+ messages in thread
From: Paul Tan @ 2015-04-18  7:51 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Johannes Schindelin, Git List, Junio C Hamano, Matthieu Moy,
	Stefan Beller

Hi,

On Fri, Apr 17, 2015 at 5:41 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Tue, Apr 14, 2015 at 1:28 PM, Paul Tan <pyokagan@gmail.com> wrote:
>> Below is the fixed patch. I also decided to return NULL if `filename` is
>> NULL because such an input usually indicated an uncaught error.
>
> Unfortunately, this blurs the line between programmer error (passing
> NULL for filename) and a user/configuration error (XDG_CONFIG_HOME and
> HOME being undefined). If there is indeed no valid interpretation for
> filename==NULL, then it may be better to die() or assert() here to
> flag the programmer error as early as possible, rather than returning
> NULL.
>
> More below.
>
>> ---
>> diff --git a/cache.h b/cache.h
>> index 3d3244b..2db10b8 100644
>> --- a/cache.h
>> +++ b/cache.h
>> @@ -836,6 +836,14 @@ 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);
>>
>> +/**
>> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/{filename}".  If
>> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
>> + * "$HOME/.config/git/{filename}". Returns NULL if filename is NULL or an error
>> + * occurred.
>> + */
>
> This is better than the original which said "$XDG_CONFIG_HOME/git/%s",
> but is still potentially confusing. When I read the earlier iteration,
> I was left with the impression that it was returning that literal
> string, with '$' and '%s' embedded, and that the caller would have to
> process it further to have '$' and '%s' expanded. Perhaps rephrasing
> it something like this will help?
>
>     Return a newly allocated string with value xdg+"/git/"+filename
>     where xdg is the interpolated value of $XDG_CONFIG_HOME if
>     defined and non-empty, otherwise "$HOME/.config". Return NULL
>     upon error.
>

Personally I think interpolated strings are easier to read than
concatenated strings. $VARIABLE (all upper case) in shell scripting is
understood to be an environment variable, and $variable (all lower
case) to be a local variable.

Thinking about it again, I should not be using python-style format
strings either ;-). So I would write it as
"$XDG_CONFIG_HOME/git/$filename".

But anyway, I don't have strong opinions on documentation, so I will
leave this to majority opinion. I will change it if you strongly
disagree :-).

> Also, for consistency with other API documentation, say "Return"
> rather than "Returns".

Okay, will fix.

>
> More below.
>
>> +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..8ee7191 100644
>> --- a/path.c
>> +++ b/path.c
>> @@ -856,3 +856,19 @@ int is_ntfs_dotgit(const char *name)
>>                         len = -1;
>>                 }
>>  }
>> +
>> +char *xdg_config_home(const char *filename)
>> +{
>> +       const char *config_home = getenv("XDG_CONFIG_HOME");
>> +
>> +       if (!filename)
>> +               return NULL;
>
> See above regarding conflation of programmer error and user/configuration error.
>
>> +       if (!config_home || !config_home[0]) {
>
> On this project, *config_home is usually favored over config_home[0].
>
>> +               const char *home = getenv("HOME");
>> +
>> +               if (!home)
>> +                       return NULL;
>> +               return mkpathdup("%s/.config/git/%s", home, filename);
>> +       } else
>> +               return mkpathdup("%s/git/%s", config_home, filename);
>
> This logic is more difficult to follow than it ought to be due to the
> use of 'config_home' so distant from the 'if' which checked it, and
> due to the nested 'if'. It could be expressed more straight-forwardly
> as:
>
>     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;
>

Ah, flipping the conditionals definitely makes it look nicer. I guess
I will need your sign off to use your code? Thanks!

Regards,
Paul

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v2 1/7] path.c: implement xdg_config_home()
  2015-04-16 21:41     ` Eric Sunshine
  2015-04-18  7:51       ` Paul Tan
@ 2015-04-18  8:48       ` Paul Tan
  1 sibling, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-18  8:48 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Johannes Schindelin, Git List, Junio C Hamano, Matthieu Moy,
	Stefan Beller

Hi,

On Fri, Apr 17, 2015 at 5:41 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Tue, Apr 14, 2015 at 1:28 PM, Paul Tan <pyokagan@gmail.com> wrote:
>> Below is the fixed patch. I also decided to return NULL if `filename` is
>> NULL because such an input usually indicated an uncaught error.
>
> Unfortunately, this blurs the line between programmer error (passing
> NULL for filename) and a user/configuration error (XDG_CONFIG_HOME and
> HOME being undefined). If there is indeed no valid interpretation for
> filename==NULL, then it may be better to die() or assert() here to
> flag the programmer error as early as possible, rather than returning
> NULL.

I'm inclined to agree, but off the top of my head an API user may wish to do:

    xdg_config_home(function_which_returns_NULL_on_error())

And wish for the error to propagate, but that may be considered sloppy
programming and so it's better to treat a NULL input as a bug.

Will fix this by calling assert(). Thanks.

Regards,
Paul

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v2 1/7] path.c: implement xdg_config_home()
  2015-04-18  7:51       ` Paul Tan
@ 2015-04-20  0:39         ` Eric Sunshine
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
  0 siblings, 1 reply; 26+ messages in thread
From: Eric Sunshine @ 2015-04-20  0:39 UTC (permalink / raw)
  To: Paul Tan
  Cc: Johannes Schindelin, Git List, Junio C Hamano, Matthieu Moy,
	Stefan Beller

On Sat, Apr 18, 2015 at 3:51 AM, Paul Tan <pyokagan@gmail.com> wrote:
> On Fri, Apr 17, 2015 at 5:41 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> On Tue, Apr 14, 2015 at 1:28 PM, Paul Tan <pyokagan@gmail.com> wrote:
>>> + * Returns the newly allocated string "$XDG_CONFIG_HOME/git/{filename}".  If
>>> + * $XDG_CONFIG_HOME is unset or empty, returns the newly allocated string
>>> + * "$HOME/.config/git/{filename}". Returns NULL if filename is NULL or an error
>>> + * occurred.
>> This is better than the original which said "$XDG_CONFIG_HOME/git/%s",
>> but is still potentially confusing. When I read the earlier iteration,
>> I was left with the impression that it was returning that literal
>> string, with '$' and '%s' embedded, and that the caller would have to
>> process it further to have '$' and '%s' expanded. Perhaps rephrasing
>> it something like this will help?
>>
>>     Return a newly allocated string with value xdg+"/git/"+filename
>>     where xdg is the interpolated value of $XDG_CONFIG_HOME if
>>     defined and non-empty, otherwise "$HOME/.config". Return NULL
>>     upon error.
>
> Personally I think interpolated strings are easier to read than
> concatenated strings. $VARIABLE (all upper case) in shell scripting is
> understood to be an environment variable, and $variable (all lower
> case) to be a local variable.
> Thinking about it again, I should not be using python-style format
> strings either ;-). So I would write it as
> "$XDG_CONFIG_HOME/git/$filename".
>
> But anyway, I don't have strong opinions on documentation, so I will
> leave this to majority opinion. I will change it if you strongly
> disagree :-).

Other than being enuinely confused by the original, and having to
check the actual implementation for clarification, I don't feel
strongly about it either. Perhaps mentioning "evaluation" like this
might help?

    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.

More below.

>>> +       if (!config_home || !config_home[0]) {
>>> +               const char *home = getenv("HOME");
>>> +
>>> +               if (!home)
>>> +                       return NULL;
>>> +               return mkpathdup("%s/.config/git/%s", home, filename);
>>> +       } else
>>> +               return mkpathdup("%s/git/%s", config_home, filename);
>>
>> This logic is more difficult to follow than it ought to be due to the
>> use of 'config_home' so distant from the 'if' which checked it, and
>> due to the nested 'if'. It could be expressed more straight-forwardly
>> as:
>>
>>     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;
>
> Ah, flipping the conditionals definitely makes it look nicer. I guess
> I will need your sign off to use your code? Thanks!

My sign-off is probably overkill. I merely re-arranged some lines of
code which you wrote. A simple Helped-by: is sufficient if you want to
mention my name.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v3 1/7] path.c: implement xdg_config_home()
  2015-04-20  0:39         ` Eric Sunshine
@ 2015-04-21  4:06           ` Paul Tan
  2015-05-06  8:00             ` [PATCH v3 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
                               ` (5 more replies)
  0 siblings, 6 replies; 26+ messages in thread
From: Paul Tan @ 2015-04-21  4:06 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Johannes Schindelin, Git List, Junio C Hamano, Matthieu Moy,
	Stefan Beller

Hi,

On Sun, Apr 19, 2015 at 08:39:44PM -0400, Eric Sunshine wrote:
> Other than being enuinely confused by the original, and having to
> check the actual implementation for clarification, I don't feel
> strongly about it either. Perhaps mentioning "evaluation" like this
> might help?
> 
>     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.
> 

This is perfect, thanks.

Re-rolled patch below now uses assert() to check if filename is
non-NULL, and re-arranges the control flow.

--- >8 ---

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 <sunshine@sunshineco.com>
Signed-off-by: Paul Tan <pyokagan@gmail.com>
---
 cache.h |  7 +++++++
 path.c  | 15 +++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/cache.h b/cache.h
index 3d3244b..3512d32 100644
--- a/cache.h
+++ b/cache.h
@@ -836,6 +836,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 595da81..c28b8f5 100644
--- a/path.c
+++ b/path.c
@@ -851,3 +851,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;
+}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 2/7] attr.c: replace home_config_paths() with xdg_config_home()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
@ 2015-05-06  8:00             ` Paul Tan
  2015-05-06  8:01             ` [PATCH v3 3/7] dir.c: " Paul Tan
                               ` (4 subsequent siblings)
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:00 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 attr.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/attr.c b/attr.c
index 1f9eebd..868dce3 100644
--- a/attr.c
+++ b/attr.c
@@ -488,7 +488,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;
@@ -507,10 +506,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) {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 3/7] dir.c: replace home_config_paths() with xdg_config_home()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
  2015-05-06  8:00             ` [PATCH v3 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
@ 2015-05-06  8:01             ` Paul Tan
  2015-05-06  8:01             ` [PATCH v3 4/7] credential-store.c: " Paul Tan
                               ` (3 subsequent siblings)
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:01 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 dir.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/dir.c b/dir.c
index 0943a81..335dc7e 100644
--- a/dir.c
+++ b/dir.c
@@ -1665,14 +1665,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))
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 4/7] credential-store.c: replace home_config_paths() with xdg_config_home()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
  2015-05-06  8:00             ` [PATCH v3 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
  2015-05-06  8:01             ` [PATCH v3 3/7] dir.c: " Paul Tan
@ 2015-05-06  8:01             ` Paul Tan
  2015-05-06  8:01             ` [PATCH v3 5/7] git-commit: replace use of home_config_paths() Paul Tan
                               ` (2 subsequent siblings)
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:01 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 credential-store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/credential-store.c b/credential-store.c
index 8b22251..f692509 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);
 	}
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 5/7] git-commit: replace use of home_config_paths()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
                               ` (2 preceding siblings ...)
  2015-05-06  8:01             ` [PATCH v3 4/7] credential-store.c: " Paul Tan
@ 2015-05-06  8:01             ` Paul Tan
  2015-05-06  8:01             ` [PATCH v3 6/7] git-config: " Paul Tan
  2015-05-06  8:01             ` [PATCH v3 7/7] path.c: remove home_config_paths() Paul Tan
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:01 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 builtin/commit.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index da79ac4..c2ebea4 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1398,12 +1398,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);
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 6/7] git-config: replace use of home_config_paths()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
                               ` (3 preceding siblings ...)
  2015-05-06  8:01             ` [PATCH v3 5/7] git-commit: replace use of home_config_paths() Paul Tan
@ 2015-05-06  8:01             ` Paul Tan
  2015-05-06  8:01             ` [PATCH v3 7/7] path.c: remove home_config_paths() Paul Tan
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:01 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 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 d32c532..5e61f4a 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 66c0a51..3a619da 100644
--- a/config.c
+++ b/config.c
@@ -1185,10 +1185,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(),
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v3 7/7] path.c: remove home_config_paths()
  2015-04-21  4:06           ` [PATCH v3 " Paul Tan
                               ` (4 preceding siblings ...)
  2015-05-06  8:01             ` [PATCH v3 6/7] git-config: " Paul Tan
@ 2015-05-06  8:01             ` Paul Tan
  5 siblings, 0 replies; 26+ messages in thread
From: Paul Tan @ 2015-05-06  8:01 UTC (permalink / raw)
  To: git; +Cc: Paul Tan

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 <pyokagan@gmail.com>
---

Re-sending the patch series. There are no changes.

 cache.h |  1 -
 path.c  | 28 ----------------------------
 2 files changed, 29 deletions(-)

diff --git a/cache.h b/cache.h
index 3512d32..f6970bb 100644
--- a/cache.h
+++ b/cache.h
@@ -816,7 +816,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 c28b8f5..6b537cc 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();
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2015-05-06  8:02 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-12  7:46 [PATCH 1/7] path.c: implement xdg_config_home() Paul Tan
2015-04-12  7:46 ` [PATCH 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
2015-04-12  7:46 ` [PATCH 3/7] dir.c: " Paul Tan
2015-04-12  7:46 ` [PATCH 4/7] credential-store.c: " Paul Tan
2015-04-12  7:46 ` [PATCH 5/7] git-commit: replace use of home_config_paths() Paul Tan
2015-04-12  7:46 ` [PATCH 6/7] git-config: " Paul Tan
2015-04-12  7:46 ` [PATCH 7/7] path.c: remove home_config_paths() Paul Tan
2015-04-13 15:50 ` [PATCH 1/7] path.c: implement xdg_config_home() Johannes Schindelin
2015-04-14 17:28   ` [PATCH v2 " Paul Tan
2015-04-16 21:41     ` Eric Sunshine
2015-04-18  7:51       ` Paul Tan
2015-04-20  0:39         ` Eric Sunshine
2015-04-21  4:06           ` [PATCH v3 " Paul Tan
2015-05-06  8:00             ` [PATCH v3 2/7] attr.c: replace home_config_paths() with xdg_config_home() Paul Tan
2015-05-06  8:01             ` [PATCH v3 3/7] dir.c: " Paul Tan
2015-05-06  8:01             ` [PATCH v3 4/7] credential-store.c: " Paul Tan
2015-05-06  8:01             ` [PATCH v3 5/7] git-commit: replace use of home_config_paths() Paul Tan
2015-05-06  8:01             ` [PATCH v3 6/7] git-config: " Paul Tan
2015-05-06  8:01             ` [PATCH v3 7/7] path.c: remove home_config_paths() Paul Tan
2015-04-18  8:48       ` [PATCH v2 1/7] path.c: implement xdg_config_home() Paul Tan
2015-04-14 20:39   ` [PATCH " Junio C Hamano
2015-04-14 22:28     ` Stefan Beller
2015-04-14 22:30       ` Junio C Hamano
2015-04-14 22:34         ` Stefan Beller
2015-04-13 21:43 ` Matthieu Moy
2015-04-14  0:18   ` Stefan Beller

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.