Skip to content

Commit cb7db5b

Browse files
L3n41cgitster
authored andcommitted
cache.h: Introduce a generic "xdg_config_home_for(…)" function
Current implementation of `xdg_config_home(filename)` returns `$XDG_CONFIG_HOME/git/$filename`, with the `git` subdirectory inserted between the `XDG_CONFIG_HOME` environment variable and the parameter. This patch introduces a `xdg_config_home_for(subdir, filename)` function which is more generic. It only concatenates "$XDG_CONFIG_HOME", or "$HOME/.config" if the former isn’t defined, with the parameters, without adding `git` in between. `xdg_config_home(filename)` is now implemented by calling `xdg_config_home_for("git", filename)` but this new generic function can be used to compute the configuration directory of other programs. Signed-off-by: Lénaïc Huard <[email protected]> Acked-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ebf3c04 commit cb7db5b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,13 @@ int is_ntfs_dotmailmap(const char *name);
12861286
*/
12871287
int looks_like_command_line_option(const char *str);
12881288

1289+
/**
1290+
* Return a newly allocated string with the evaluation of
1291+
* "$XDG_CONFIG_HOME/$subdir/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise
1292+
* "$HOME/.config/$subdir/$filename". Return NULL upon error.
1293+
*/
1294+
char *xdg_config_home_for(const char *subdir, const char *filename);
1295+
12891296
/**
12901297
* Return a newly allocated string with the evaluation of
12911298
* "$XDG_CONFIG_HOME/git/$filename" if $XDG_CONFIG_HOME is non-empty, otherwise

path.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,21 +1503,28 @@ int looks_like_command_line_option(const char *str)
15031503
return str && str[0] == '-';
15041504
}
15051505

1506-
char *xdg_config_home(const char *filename)
1506+
char *xdg_config_home_for(const char *subdir, const char *filename)
15071507
{
15081508
const char *home, *config_home;
15091509

1510+
assert(subdir);
15101511
assert(filename);
15111512
config_home = getenv("XDG_CONFIG_HOME");
15121513
if (config_home && *config_home)
1513-
return mkpathdup("%s/git/%s", config_home, filename);
1514+
return mkpathdup("%s/%s/%s", config_home, subdir, filename);
15141515

15151516
home = getenv("HOME");
15161517
if (home)
1517-
return mkpathdup("%s/.config/git/%s", home, filename);
1518+
return mkpathdup("%s/.config/%s/%s", home, subdir, filename);
1519+
15181520
return NULL;
15191521
}
15201522

1523+
char *xdg_config_home(const char *filename)
1524+
{
1525+
return xdg_config_home_for("git", filename);
1526+
}
1527+
15211528
char *xdg_cache_home(const char *filename)
15221529
{
15231530
const char *home, *cache_home;

0 commit comments

Comments
 (0)