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
This commit is contained in:
Junio C Hamano 2017-03-24 13:07:34 -07:00
Родитель f364f02724 612c49e94d
Коммит 78cf8efec3
5 изменённых файлов: 136 добавлений и 5 удалений

Просмотреть файл

@ -33,10 +33,13 @@ OPTIONS
--socket <path>::
Use `<path>` to contact a running cache daemon (or start a new
cache daemon if one is not started). Defaults to
`~/.git-credential-cache/socket`. If your home directory is on a
network-mounted filesystem, you may need to change this to a
local filesystem. You must specify an absolute path.
cache daemon if one is not started).
Defaults to `$XDG_CACHE_HOME/git/credential/socket` unless
`~/.git-credential-cache/` exists in which case
`~/.git-credential-cache/socket` is used instead.
If your home directory is on a network-mounted filesystem, you
may need to change this to a local filesystem. You must specify
an absolute path.
CONTROLLING THE DAEMON
----------------------

Просмотреть файл

@ -1176,6 +1176,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

Просмотреть файл

@ -83,6 +83,19 @@ static void do_cache(const char *socket, const char *action, int timeout,
strbuf_release(&buf);
}
static char *get_socket_path(void)
{
struct stat sb;
char *old_dir, *socket;
old_dir = expand_user_path("~/.git-credential-cache");
if (old_dir && !stat(old_dir, &sb) && S_ISDIR(sb.st_mode))
socket = xstrfmt("%s/socket", old_dir);
else
socket = xdg_cache_home("credential/socket");
free(old_dir);
return socket;
}
int cmd_main(int argc, const char **argv)
{
char *socket_path = NULL;
@ -106,7 +119,7 @@ int cmd_main(int argc, const char **argv)
op = argv[0];
if (!socket_path)
socket_path = expand_user_path("~/.git-credential-cache/socket");
socket_path = get_socket_path();
if (!socket_path)
die("unable to find a suitable socket path; use --socket");

15
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")

Просмотреть файл

@ -12,7 +12,100 @@ test -z "$NO_UNIX_SOCKETS" || {
# don't leave a stale daemon running
trap 'code=$?; git credential-cache exit; (exit $code); die' EXIT
# test that the daemon works with no special setup
helper_test cache
test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
test_when_finished "
git credential-cache exit &&
rmdir -p .cache/git/credential/
" &&
test_path_is_missing "$HOME/.git-credential-cache" &&
test -S "$HOME/.cache/git/credential/socket"
'
XDG_CACHE_HOME="$HOME/xdg"
export XDG_CACHE_HOME
# test behavior when XDG_CACHE_HOME is set
helper_test cache
test_expect_success "use custom XDG_CACHE_HOME if set and default sockets are not created" '
test_when_finished "git credential-cache exit" &&
test -S "$XDG_CACHE_HOME/git/credential/socket" &&
test_path_is_missing "$HOME/.git-credential-cache/socket" &&
test_path_is_missing "$HOME/.cache/git/credential/socket"
'
unset XDG_CACHE_HOME
test_expect_success 'credential-cache --socket option overrides default location' '
test_when_finished "
git credential-cache exit --socket \"\$HOME/dir/socket\" &&
rmdir \"\$HOME/dir\"
" &&
check approve "cache --socket \"\$HOME/dir/socket\"" <<-\EOF &&
protocol=https
host=example.com
username=store-user
password=store-pass
EOF
test -S "$HOME/dir/socket"
'
test_expect_success "use custom XDG_CACHE_HOME even if xdg socket exists" '
test_when_finished "
git credential-cache exit &&
sane_unset XDG_CACHE_HOME
" &&
check approve cache <<-\EOF &&
protocol=https
host=example.com
username=store-user
password=store-pass
EOF
test -S "$HOME/.cache/git/credential/socket" &&
XDG_CACHE_HOME="$HOME/xdg" &&
export XDG_CACHE_HOME &&
check approve cache <<-\EOF &&
protocol=https
host=example.com
username=store-user
password=store-pass
EOF
test -S "$XDG_CACHE_HOME/git/credential/socket"
'
test_expect_success 'use user socket if user directory exists' '
test_when_finished "
git credential-cache exit &&
rmdir \"\$HOME/.git-credential-cache/\"
" &&
mkdir -p -m 700 "$HOME/.git-credential-cache/" &&
check approve cache <<-\EOF &&
protocol=https
host=example.com
username=store-user
password=store-pass
EOF
test -S "$HOME/.git-credential-cache/socket"
'
test_expect_success SYMLINKS 'use user socket if user directory is a symlink to a directory' '
test_when_finished "
git credential-cache exit &&
rmdir \"\$HOME/dir/\" &&
rm \"\$HOME/.git-credential-cache\"
" &&
mkdir -p -m 700 "$HOME/dir/" &&
ln -s "$HOME/dir" "$HOME/.git-credential-cache" &&
check approve cache <<-\EOF &&
protocol=https
host=example.com
username=store-user
password=store-pass
EOF
test -S "$HOME/.git-credential-cache/socket"
'
helper_test_timeout cache --timeout=1
# we can't rely on our "trap" above working after test_done,