Skip to contentSkip to conversation

path.c: implement xdg_config_home()

PATCH Superseded Paul Tan 6 participants 26 messages Jun 15, 2016

lore
Share & tools
lore

Paul Tan

author
#0 lore raw

PATCH 1/7 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

---
 cache.h |  7 +++++++
 path.c  | 16 ++++++++++++++++
 2 files changed, 23 insertions(+)
cache.h +7 -0
index 3d3244b..7f9bab0 100644
@@ -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);
path.c +16 -0
index e608993..4c32d16 100644
@@ -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);
+}
Signature · 1 line
2.1.4
author
#1 lore raw

PATCH 2/7 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().

---
 attr.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
attr.c +2 -5
index 1f9eebd..868dce3 100644
@@ -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) {
Signature · 1 line
2.1.4
author
#2 lore raw

PATCH 3/7 dir.c: replace home_config_paths() with xdg_config_home()

In reply to 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().

---
 dir.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
dir.c +2 -5
index 0943a81..335dc7e 100644
@@ -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))
Signature · 1 line
2.1.4
author
#3 lore raw

PATCH 4/7 credential-store.c: replace home_config_paths() with xdg_config_home()

In reply to 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().

---
 credential-store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
credential-store.c +1 -1
index 8b22251..f692509 100644
@@ -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);
 	}
Signature · 1 line
2.1.4
author
#4 lore raw

PATCH 5/7 git-commit: replace use of home_config_paths()

In reply to 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().

---
 builtin/commit.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/commit.c +3 -5
index da79ac4..c2ebea4 100644
@@ -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);
 
Signature · 1 line
2.1.4
author
#5 lore raw

PATCH 6/7 git-config: replace use of home_config_paths()

In reply to 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().

---
 builtin/config.c | 6 ++----
 config.c         | 6 ++----
 2 files changed, 4 insertions(+), 8 deletions(-)
builtin/config.c +2 -4
index d32c532..5e61f4a 100644
@@ -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)
 			/*
config.c +2 -4
index 66c0a51..3a619da 100644
@@ -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(),
Signature · 1 line
2.1.4
author
#6 lore raw

PATCH 7/7 path.c: remove home_config_paths()

In reply to 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.

---
 cache.h |  1 -
 path.c  | 28 ----------------------------
 2 files changed, 29 deletions(-)
cache.h +0 -1
index 7f9bab0..19d75ef 100644
@@ -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)
path.c +0 -28
index 4c32d16..84ec589 100644
@@ -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();
Signature · 1 line
2.1.4
#7 lore raw
In reply to Paul Tan

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:

Quoted 5 lines · Paul Tan
cache.h +0 -0
index 3d3244b..7f9bab0 100644

cache.h char *strip_path_suffix(const char *path, const · line 845

Quoted 10 lines
cache.h +6 -0
@@ -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

author
#8 lore raw

PATCH v2 1/7 path.c: implement xdg_config_home()

Hi,

Quoted 5 lines · Johannes Schindelin

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.

Quoted 12 lines

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

Quoted 9 lines · Paul Tan
cache.h +0 -0
index 3d3244b..7f9bab0 100644
@@ -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);
 
Quoted 8 lines

cache.h char *strip_path_suffix(const char *path, const · line 845

Quoted 6 lines
cache.h +6 -0
+/**
+ * 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.

Quoted 3 lines

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

---
 cache.h |  8 ++++++++
 path.c  | 16 ++++++++++++++++
 2 files changed, 24 insertions(+)
cache.h +8 -0
index 3d3244b..2db10b8 100644
@@ -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);
path.c +16 -0
index e608993..8ee7191 100644
@@ -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);
+}
Signature · 1 line
2.1.4
Reviewed-by on v1 1/7
#24 lore raw
In reply to Paul Tan
Quoted 3 lines · Paul Tan

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

Signature · 2 lines
Matthieu Moy
http://www-verimag.imag.fr/~moy/
Reviewed-by
#25 lore raw
Quoted 10 lines · Matthieu Moy
Quoted 3 lines · Paul Tan

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.

Quoted 4 lines

Anyway, the series is

and

Quoted 8 lines

--
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#20 lore raw
In reply to Johannes Schindelin
Quoted 9 lines · Johannes Schindelin
Quoted 9 lines
cache.h +0 -0
index 3d3244b..7f9bab0 100644
@@ -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);
 
Quoted 11 lines

cache.h char *strip_path_suffix(const char *path, const char *suffix); · line 844

Quoted 6 lines
cache.h +6 -0
+/**
+ * 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.

#21 lore raw
Quoted 23 lines · Junio C Hamano
Quoted 9 lines · Johannes Schindelin
Quoted 9 lines
cache.h +0 -0
index 3d3244b..7f9bab0 100644
@@ -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);
 
Quoted 11 lines

cache.h char *strip_path_suffix(const char *path, const char *suffix); · line 844

Quoted 6 lines
cache.h +6 -0
+/**
+ * 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?

Quoted 4 lines

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.

#22 lore raw
Quoted 5 lines · Stefan Beller
Quoted 1 line · Junio C Hamano

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 ;-)

#23 lore raw

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

Quoted 14 lines · Junio C Hamano
Quoted 5 lines · Stefan Beller
Quoted 1 line · Junio C Hamano

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 ;-)

#9 lore raw
In reply to Paul Tan
Quoted 2 lines · Paul Tan

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.

Quoted 34 lines

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

---

cache.h +0 -0
index 3d3244b..2db10b8 100644

cache.h char *strip_path_suffix(const char *path, const char *suffix); · line 844

Quoted 9 lines
cache.h +6 -0
@@ -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.

Quoted 10 lines
cache.h +2 -0
+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);
path.c +0 -0
index e608993..8ee7191 100644

path.c int is_ntfs_dotgit(const char *name) · line 865

Quoted 10 lines
path.c +7 -0
@@ -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.

path.c int is_ntfs_dotgit(const char *name) · line 866

Quoted 1 line
path.c +1 -0
+       if (!config_home || !config_home[0]) {

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

path.c int is_ntfs_dotgit(const char *name) · line 873

Quoted 7 lines
path.c +7 -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;
Quoted 3 lines
path.c +1 -1
+}
--

2.1.4

author
#10 lore raw

Hi,

Quoted 36 lines · Eric Sunshine
Quoted 2 lines · Paul Tan

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.

Quoted 6 lines

---

cache.h +0 -0
index 3d3244b..2db10b8 100644

cache.h char *strip_path_suffix(const char *path, const char *suffix); · line 844

Quoted 9 lines
cache.h +6 -0
@@ -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?

Quoted 5 lines

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

Quoted 2 lines

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

Okay, will fix.

Quoted 43 lines

More below.

Quoted 10 lines
cache.h +2 -0
+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);
path.c +0 -0
index e608993..8ee7191 100644

path.c int is_ntfs_dotgit(const char *name) · line 865

Quoted 10 lines
path.c +7 -0
@@ -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.

path.c int is_ntfs_dotgit(const char *name) · line 866

Quoted 1 line
path.c +1 -0
+       if (!config_home || !config_home[0]) {

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

path.c int is_ntfs_dotgit(const char *name) · line 873

Quoted 7 lines
path.c +7 -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:

Quoted 9 lines
    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

author
#19 lore raw
In reply to Eric Sunshine

Hi,

Quoted 10 lines · Eric Sunshine
Quoted 2 lines · Paul Tan

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

#11 lore raw
In reply to Paul Tan
Quoted 26 lines · Paul Tan
Quoted 12 lines · Eric Sunshine
Quoted 4 lines · Paul Tan

+ * 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?

Quoted 4 lines

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".

Quoted 3 lines

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.

Quoted 17 lines
Quoted 17 lines

path.c int is_ntfs_dotgit(const char *name) · line 873

Quoted 8 lines
path.c +8 -0
+       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);
Quoted 8 lines
Quoted 5 lines
    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.

author
#12 lore raw

PATCH v3 1/7 path.c: implement xdg_config_home()

Hi,

Quoted 10 lines · Eric Sunshine

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

---
 cache.h |  7 +++++++
 path.c  | 15 +++++++++++++++
 2 files changed, 22 insertions(+)
cache.h +7 -0
index 3d3244b..3512d32 100644
@@ -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);
path.c +15 -0
index 595da81..c28b8f5 100644
@@ -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;
+}
Signature · 1 line
2.1.4
author
#13 lore raw

PATCH v3 2/7 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().

---

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

 attr.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
attr.c +2 -5
index 1f9eebd..868dce3 100644
@@ -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) {
Signature · 1 line
2.1.4
author
#14 lore raw

PATCH v3 3/7 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().

---

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

 dir.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
dir.c +2 -5
index 0943a81..335dc7e 100644
@@ -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))
Signature · 1 line
2.1.4
author
#15 lore raw

PATCH v3 4/7 credential-store.c: replace home_config_paths() with xdg_config_home()

In reply to 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().

---

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

 credential-store.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
credential-store.c +1 -1
index 8b22251..f692509 100644
@@ -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);
 	}
Signature · 1 line
2.1.4
author
#16 lore raw

PATCH v3 5/7 git-commit: replace use of home_config_paths()

In reply to 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().

---

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

 builtin/commit.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
builtin/commit.c +3 -5
index da79ac4..c2ebea4 100644
@@ -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);
 
Signature · 1 line
2.1.4
author
#17 lore raw

PATCH v3 6/7 git-config: replace use of home_config_paths()

In reply to 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().

---

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

 builtin/config.c | 6 ++----
 config.c         | 6 ++----
 2 files changed, 4 insertions(+), 8 deletions(-)
builtin/config.c +2 -4
index d32c532..5e61f4a 100644
@@ -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)
 			/*
config.c +2 -4
index 66c0a51..3a619da 100644
@@ -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(),
Signature · 1 line
2.1.4
author
#18 lore raw

PATCH v3 7/7 path.c: remove home_config_paths()

In reply to 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.

---

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

 cache.h |  1 -
 path.c  | 28 ----------------------------
 2 files changed, 29 deletions(-)
cache.h +0 -1
index 3512d32..f6970bb 100644
@@ -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)
path.c +0 -28
index c28b8f5..6b537cc 100644
@@ -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();
Signature · 1 line
2.1.4