credential-cache: use XDG_CACHE_HOME for socket

Make git-credential-cache follow the XDG base path specification by default. This increases consistency with other applications and helps keep clutter out of users' home directories. Check the old socket location, ~/.git-credential-cache/, and use ~/.git-credential-cache/socket if that directory exists rather than forcing users who have used `git credential-cache` before to migrate to the new XDG compliant location. Otherwise use the socket $XDG_CACHE_HOME/git/credential/socket following XDG base path specification. Use the subdirectory credential/ in case other files are cached under $XDG_CACHE_HOME/git/ in the future and to make the socket's purpose clear. Signed-off-by: Devin Lehmacher <lehmacdj@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Devin Lehmacher committed Mar 17, 2017 at 08:36 UTC 60759baac1f063a6c3069ca4b909abb8a20fa08a
2 files changed +21 -5
Documentation/git-credential-cache.txt
+7 -4
@@ -33,10 +33,13 @@ OPTIONS
33 --socket <path>::
34
35 Use `<path>` to contact a running cache daemon (or start a new
36 - cache daemon if one is not started). Defaults to
37 - `~/.git-credential-cache/socket`. If your home directory is on a
38 - network-mounted filesystem, you may need to change this to a
39 - local filesystem. You must specify an absolute path.
36 + cache daemon if one is not started).
37 + Defaults to `$XDG_CACHE_HOME/git/credential/socket` unless
38 + `~/.git-credential-cache/` exists in which case
39 + `~/.git-credential-cache/socket` is used instead.
40 + If your home directory is on a network-mounted filesystem, you
41 + may need to change this to a local filesystem. You must specify
42 + an absolute path.
43
44 CONTROLLING THE DAEMON
45 ----------------------
credential-cache.c
+14 -1
@@ -83,6 +83,19 @@ static void do_cache(const char *socket, const char *action, int timeout,
83 strbuf_release(&buf);
84 }
85
86 +static char *get_socket_path(void)
87 +{
88 + struct stat sb;
89 + char *old_dir, *socket;
90 + old_dir = expand_user_path("~/.git-credential-cache");
91 + if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
92 + socket = xstrfmt("%s/socket", old_dir);
93 + else
94 + socket = xdg_cache_home("credential/socket");
95 + free(old_dir);
96 + return socket;
97 +}
98 +
99 int cmd_main(int argc, const char **argv)
100 {
101 char *socket_path = NULL;
@@ -106,7 +119,7 @@ int cmd_main(int argc, const char **argv)
119 op = argv[0];
120
121 if (!socket_path)
109 - socket_path = expand_user_path("~/.git-credential-cache/socket");
122 + socket_path = get_socket_path();
123 if (!socket_path)
124 die("unable to find a suitable socket path; use --socket");
125