path.c: implement xdg_config_home()
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
@@ -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 1extern void *read_sha1_file_extended (const unsigned char *sha1,enum object_type *type,unsigned long *size,unsigned flag);
path.c +16 -0
@@ -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
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
@@ -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
PATCH 3/7 dir.c: replace home_config_paths() with xdg_config_home()
In reply to Paul TanSince 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
@@ -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
PATCH 4/7 credential-store.c: replace home_config_paths() with xdg_config_home()
In reply to Paul TanSince 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
@@ -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
PATCH 5/7 git-commit: replace use of home_config_paths()
In reply to Paul TanSince 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
@@ -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
PATCH 6/7 git-config: replace use of home_config_paths()
In reply to Paul TanSince 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
@@ -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
@@ -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
PATCH 7/7 path.c: remove home_config_paths()
In reply to Paul Tanhome_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
@@ -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
@@ -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
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
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
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/7mail 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 viagit branch --edit-descriptionand to letgit format-patchuse that via the--cover-letteroption.
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@@ -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_tin 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
@@ -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 1extern void *read_sha1_file_extended (const unsigned char *sha1,enum object_type *type,unsigned long *size,unsigned flag);
path.c +16 -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 ; +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
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 withexpand_user_path(), can replace all uses ofhome_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/
Stefan Beller
Reviewed-byQuoted 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 withexpand_user_path(), can replace all uses ofhome_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
Reviewed-by Matthieu Moy <[email protected]>
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
Quoted 9 lines · Johannes Schindelin
Quoted 9 lines
cache.h +0
-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);
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 nameconfig_fn_tin 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.
Quoted 23 lines · Junio C Hamano
Quoted 9 lines · Johannes Schindelin
Quoted 9 lines
cache.h +0
-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);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 nameconfig_fn_tin 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.
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 ;-)
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 ;-)
Quoted 2 lines · Paul Tan
Below is the fixed patch. I also decided to return NULL if
filenameis 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 withexpand_user_path(). Hence, it can be seen thathome_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 withexpand_user_path(), can replace all uses ofhome_config_paths().[1] http://standards.freedesktop.org/basedir-spec/basedir-spec-0.7.html
Signed-off-by Paul Tan <[email protected]>---
cache.h +0
-0
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 1extern void *read_sha1_file_extended (const unsigned char *sha1,enum object_type *type,unsigned long *size,unsigned flag);path.c +0
-0
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
Hi,
Quoted 36 lines · Eric Sunshine
Quoted 2 lines · Paul Tan
Below is the fixed patch. I also decided to return NULL if
filenameis 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_HOMEand HOME being undefined). If there is indeed no valid interpretation for filename==NULL, then it may be better todie()orassert()here to flag the programmer error as early as possible, rather than returning NULL.More below.
Quoted 6 lines
---
cache.h +0
-0cache.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 1extern void *read_sha1_file_extended (const unsigned char *sha1,enum object_type *type,unsigned long *size,unsigned flag);path.c +0
-0path.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_homeis 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
Hi,
Quoted 10 lines · Eric Sunshine
Quoted 2 lines · Paul Tan
Below is the fixed patch. I also decided to return NULL if
filenameis 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_HOMEand HOME being undefined). If there is indeed no valid interpretation for filename==NULL, then it may be better todie()orassert()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
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.
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
@@ -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 1extern void *read_sha1_file_extended (const unsigned char *sha1,enum object_type *type,unsigned long *size,unsigned flag);
path.c +15 -0
@@ -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
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
@@ -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
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
@@ -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
PATCH v3 4/7 credential-store.c: replace home_config_paths() with xdg_config_home()
In reply to Paul TanSince 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
@@ -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
PATCH v3 5/7 git-commit: replace use of home_config_paths()
In reply to Paul TanSince 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
@@ -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
PATCH v3 6/7 git-config: replace use of home_config_paths()
In reply to Paul TanSince 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
@@ -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
@@ -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
PATCH v3 7/7 path.c: remove home_config_paths()
In reply to Paul Tanhome_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
@@ -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
@@ -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