Skip to content

Commit 60759ba

Browse files
lehmacdjgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e7f136b commit 60759ba

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

Documentation/git-credential-cache.txt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ OPTIONS
3333
--socket <path>::
3434

3535
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.
4043

4144
CONTROLLING THE DAEMON
4245
----------------------

credential-cache.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,19 @@ static void do_cache(const char *socket, const char *action, int timeout,
8383
strbuf_release(&buf);
8484
}
8585

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+
8699
int cmd_main(int argc, const char **argv)
87100
{
88101
char *socket_path = NULL;
@@ -106,7 +119,7 @@ int cmd_main(int argc, const char **argv)
106119
op = argv[0];
107120

108121
if (!socket_path)
109-
socket_path = expand_user_path("~/.git-credential-cache/socket");
122+
socket_path = get_socket_path();
110123
if (!socket_path)
111124
die("unable to find a suitable socket path; use --socket");
112125

0 commit comments

Comments
 (0)