path.c: add xdg_cache_home
authorDevin Lehmacher <lehmacdj@gmail.com>
Mon, 13 Mar 2017 20:43:54 +0000 (16:43 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 13 Mar 2017 21:39:36 +0000 (14:39 -0700)
We already have xdg_config_home to format paths relative to
XDG_CONFIG_HOME. Let's provide a similar function xdg_cache_home to do
the same for paths relative to XDG_CACHE_HOME.

Signed-off-by: Devin Lehmacher <lehmacdj@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
path.c
diff --git a/cache.h b/cache.h
index 8c0e6442076eb91571dc0c5cf55db478ab47d2ad..5db29a945381a1b2347fa675ca6e94d818855e2d 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1169,6 +1169,13 @@ extern int is_ntfs_dotgit(const char *name);
  */
 extern char *xdg_config_home(const char *filename);
 
+/**
+ * Return a newly allocated string with the evaluation of
+ * "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
+ * "$HOME/.cache/git/$filename". Return NULL upon error.
+ */
+extern char *xdg_cache_home(const char *filename);
+
 /* object replacement */
 #define LOOKUP_REPLACE_OBJECT 1
 #define LOOKUP_UNKNOWN_OBJECT 2
diff --git a/path.c b/path.c
index efcedafba6f2c0cae218f2d32425c13272df8dea..22248436bfd392369cb81e644d12a4559a554693 100644 (file)
--- a/path.c
+++ b/path.c
@@ -1272,6 +1272,21 @@ char *xdg_config_home(const char *filename)
        return NULL;
 }
 
+char *xdg_cache_home(const char *filename)
+{
+       const char *home, *cache_home;
+
+       assert(filename);
+       cache_home = getenv("XDG_CACHE_HOME");
+       if (cache_home && *cache_home)
+               return mkpathdup("%s/git/%s", cache_home, filename);
+
+       home = getenv("HOME");
+       if (home)
+               return mkpathdup("%s/.cache/git/%s", home, filename);
+       return NULL;
+}
+
 GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
 GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
 GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")