Skip to content

Commit 78cf8ef

Browse files
committed
Merge branch 'dl/credential-cache-socket-in-xdg-cache'
The default location "~/.git-credential-cache/socket" for the socket used to communicate with the credential-cache daemon has been moved to "~/.cache/git/credential/socket". * dl/credential-cache-socket-in-xdg-cache: credential-cache: add tests for XDG functionality credential-cache: use XDG_CACHE_HOME for socket path.c: add xdg_cache_home
2 parents f364f02 + 612c49e commit 78cf8ef

5 files changed

Lines changed: 136 additions & 5 deletions

File tree

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
----------------------

cache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,13 @@ extern int is_ntfs_dotgit(const char *name);
11761176
*/
11771177
extern char *xdg_config_home(const char *filename);
11781178

1179+
/**
1180+
* Return a newly allocated string with the evaluation of
1181+
* "$XDG_CACHE_HOME/git/$filename" if $XDG_CACHE_HOME is non-empty, otherwise
1182+
* "$HOME/.cache/git/$filename". Return NULL upon error.
1183+
*/
1184+
extern char *xdg_cache_home(const char *filename);
1185+
11791186
/* object replacement */
11801187
#define LOOKUP_REPLACE_OBJECT 1
11811188
#define LOOKUP_UNKNOWN_OBJECT 2

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

path.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1272,6 +1272,21 @@ char *xdg_config_home(const char *filename)
12721272
return NULL;
12731273
}
12741274

1275+
char *xdg_cache_home(const char *filename)
1276+
{
1277+
const char *home, *cache_home;
1278+
1279+
assert(filename);
1280+
cache_home = getenv("XDG_CACHE_HOME");
1281+
if (cache_home && *cache_home)
1282+
return mkpathdup("%s/git/%s", cache_home, filename);
1283+
1284+
home = getenv("HOME");
1285+
if (home)
1286+
return mkpathdup("%s/.cache/git/%s", home, filename);
1287+
return NULL;
1288+
}
1289+
12751290
GIT_PATH_FUNC(git_path_cherry_pick_head, "CHERRY_PICK_HEAD")
12761291
GIT_PATH_FUNC(git_path_revert_head, "REVERT_HEAD")
12771292
GIT_PATH_FUNC(git_path_squash_msg, "SQUASH_MSG")

t/t0301-credential-cache.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,100 @@ test -z "$NO_UNIX_SOCKETS" || {
1212
# don't leave a stale daemon running
1313
trap 'code=$?; git credential-cache exit; (exit $code); die' EXIT
1414

15+
# test that the daemon works with no special setup
1516
helper_test cache
17+
18+
test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
19+
test_when_finished "
20+
git credential-cache exit &&
21+
rmdir -p .cache/git/credential/
22+
" &&
23+
test_path_is_missing "$HOME/.git-credential-cache" &&
24+
test -S "$HOME/.cache/git/credential/socket"
25+
'
26+
27+
XDG_CACHE_HOME="$HOME/xdg"
28+
export XDG_CACHE_HOME
29+
# test behavior when XDG_CACHE_HOME is set
30+
helper_test cache
31+
32+
test_expect_success "use custom XDG_CACHE_HOME if set and default sockets are not created" '
33+
test_when_finished "git credential-cache exit" &&
34+
test -S "$XDG_CACHE_HOME/git/credential/socket" &&
35+
test_path_is_missing "$HOME/.git-credential-cache/socket" &&
36+
test_path_is_missing "$HOME/.cache/git/credential/socket"
37+
'
38+
unset XDG_CACHE_HOME
39+
40+
test_expect_success 'credential-cache --socket option overrides default location' '
41+
test_when_finished "
42+
git credential-cache exit --socket \"\$HOME/dir/socket\" &&
43+
rmdir \"\$HOME/dir\"
44+
" &&
45+
check approve "cache --socket \"\$HOME/dir/socket\"" <<-\EOF &&
46+
protocol=https
47+
host=example.com
48+
username=store-user
49+
password=store-pass
50+
EOF
51+
test -S "$HOME/dir/socket"
52+
'
53+
54+
test_expect_success "use custom XDG_CACHE_HOME even if xdg socket exists" '
55+
test_when_finished "
56+
git credential-cache exit &&
57+
sane_unset XDG_CACHE_HOME
58+
" &&
59+
check approve cache <<-\EOF &&
60+
protocol=https
61+
host=example.com
62+
username=store-user
63+
password=store-pass
64+
EOF
65+
test -S "$HOME/.cache/git/credential/socket" &&
66+
XDG_CACHE_HOME="$HOME/xdg" &&
67+
export XDG_CACHE_HOME &&
68+
check approve cache <<-\EOF &&
69+
protocol=https
70+
host=example.com
71+
username=store-user
72+
password=store-pass
73+
EOF
74+
test -S "$XDG_CACHE_HOME/git/credential/socket"
75+
'
76+
77+
test_expect_success 'use user socket if user directory exists' '
78+
test_when_finished "
79+
git credential-cache exit &&
80+
rmdir \"\$HOME/.git-credential-cache/\"
81+
" &&
82+
mkdir -p -m 700 "$HOME/.git-credential-cache/" &&
83+
check approve cache <<-\EOF &&
84+
protocol=https
85+
host=example.com
86+
username=store-user
87+
password=store-pass
88+
EOF
89+
test -S "$HOME/.git-credential-cache/socket"
90+
'
91+
92+
test_expect_success SYMLINKS 'use user socket if user directory is a symlink to a directory' '
93+
test_when_finished "
94+
git credential-cache exit &&
95+
rmdir \"\$HOME/dir/\" &&
96+
rm \"\$HOME/.git-credential-cache\"
97+
" &&
98+
mkdir -p -m 700 "$HOME/dir/" &&
99+
ln -s "$HOME/dir" "$HOME/.git-credential-cache" &&
100+
check approve cache <<-\EOF &&
101+
protocol=https
102+
host=example.com
103+
username=store-user
104+
password=store-pass
105+
EOF
106+
test -S "$HOME/.git-credential-cache/socket"
107+
'
108+
16109
helper_test_timeout cache --timeout=1
17110

18111
# we can't rely on our "trap" above working after test_done,

0 commit comments

Comments
 (0)