Merge branch 'dl/credential-cache-socket-in-xdg-cache'
authorJunio C Hamano <gitster@pobox.com>
Fri, 24 Mar 2017 20:07:34 +0000 (13:07 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 24 Mar 2017 20:07:34 +0000 (13:07 -0700)
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

Documentation/git-credential-cache.txt
cache.h
credential-cache.c
path.c
t/t0301-credential-cache.sh
index 96208f822e0995f97664423038abe1f406431986..2b8582639332acb4d3e81273c6a90163a3a8b378 100644 (file)
@@ -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
 ----------------------
diff --git a/cache.h b/cache.h
index 14f8a06c46b91ffabb8877fec1e2d41236a53ce8..2214d52f6127f20ad9788402b0e448977db615c5 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -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
index cc8a6ee19214b12758fc3d4b39ff4a07f4442d91..3cbd4200190dd9d8378f5a111883de026b06e47d 100644 (file)
@@ -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");
 
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")
index 82c8411210831fbfbd3151b8e19dba9ee004fab0..fd92533acf488fd7d1aedd8152b9edaafc9bd87d 100755 (executable)
@@ -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,