Repository: pre-commit/pre-commit

Issue:
pre-commit should meet the XDG Base Directory Specification
XDG Base Directory Specification is quite common now. Just `ls ~/.cache ~/.config ~/.local` to realize it.

I think `~/.pre-commit` should be moved to `$XDG_CACHE_HOME` or `$HOME/.cache`

https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html


Discussion:
Pull requests welcome, note that this also has to work well for windows which doesn't adhere to XDG.

I've been hesitant to move the directory as it'd be a bit awkward for current users (though perhaps `clean` can just be updated to remove both paths for some amount of time?)
> Pull requests welcome, note that this also has to work well for windows which doesn't adhere to XDG.

Is there any documentation for pre-commit on Windows?

Is it supposed to run on:
- the [official Python release for Windows](https://www.python.org/downloads/release/python-362/);
- Cygwin;
- [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux);
- all of the above?

On which Windows version is it supposed to run?

How do you proceed to test it on Windows? Do you use a VM or something?

> I've been hesitant to move the directory as it'd be a bit awkward for current users (though perhaps `clean` can just be updated to remove both paths for some amount of time?)

I understand. But it won't break anything since it would just re-cache all the needed stuff, right? We can imagine three scenarios if `~/.pre-commit` already exists:
1. Move the existing `~/.pre-commit` in `$XDG_CACHE_HOME` or `$HOME/.cache`, with or without warning (see `2.`);
1. Don't move the existing `~/.pre-commit`, but display a warning before re-caching;
3. Just re-cache the needed parts without displaying any message (and maybe delete the existing `~/.pre-commit`) ¯\\\_(ツ)\_/¯

Well, it has been done already with the transition from `hooks.yaml` to `.pre-commit-hooks.yaml` in some sort.
@nagromc If you ^F windows on http://pre-commit.com you can see the current support matrix

It is tested automatically through appveyor -- my home desktop at home also runs windows so I often do more-difficult debugging on that.

As for the three solutions above:

1. Isn't feasible -- virtualenvs are not relocatable (the shebangs contain full paths to the interpreter)
2. A possibility, though...
3. sounds best :)
OK I'll give it a try when I have time. Didn't know about AppVeyor.

And for the third solution, should we delete `~/.pre-commit`?
I think just leave it alone for the most part (unless `pre-commit clean` is run, we can include it in there for some amount of time and then after some amount of time pretend like it never existed)

Resolving patch:
diff --git a/pre_commit/commands/clean.py b/pre_commit/commands/clean.py
--- a/pre_commit/commands/clean.py
+++ b/pre_commit/commands/clean.py
@@ -8,7 +8,9 @@
 
 
 def clean(runner):
-    if os.path.exists(runner.store.directory):
-        rmtree(runner.store.directory)
-        output.write_line('Cleaned {}.'.format(runner.store.directory))
+    legacy_path = os.path.expanduser('~/.pre-commit')
+    for directory in (runner.store.directory, legacy_path):
+        if os.path.exists(directory):
+            rmtree(directory)
+            output.write_line('Cleaned {}.'.format(directory))
     return 0
diff --git a/pre_commit/error_handler.py b/pre_commit/error_handler.py
--- a/pre_commit/error_handler.py
+++ b/pre_commit/error_handler.py
@@ -28,10 +28,11 @@ def _log_and_exit(msg, exc, formatted):
         _to_bytes(exc), b'\n',
     ))
     output.write(error_msg)
-    output.write_line('Check the log at ~/.pre-commit/pre-commit.log')
     store = Store()
     store.require_created()
-    with open(os.path.join(store.directory, 'pre-commit.log'), 'wb') as log:
+    log_path = os.path.join(store.directory, 'pre-commit.log')
+    output.write_line('Check the log at {}'.format(log_path))
+    with open(log_path, 'wb') as log:
         output.write(error_msg, stream=log)
         output.write_line(formatted, stream=log)
     raise SystemExit(1)
diff --git a/pre_commit/store.py b/pre_commit/store.py
--- a/pre_commit/store.py
+++ b/pre_commit/store.py
@@ -29,9 +29,9 @@ def _get_default_directory():
     `Store.get_default_directory` can be mocked in tests and
     `_get_default_directory` can be tested.
     """
-    return os.environ.get(
-        'PRE_COMMIT_HOME',
-        os.path.join(os.path.expanduser('~'), '.pre-commit'),
+    return os.environ.get('PRE_COMMIT_HOME') or os.path.join(
+        os.environ.get('XDG_CACHE_HOME') or os.path.expanduser('~/.cache'),
+        'pre-commit',
     )
 
 


Test patch:
diff --git a/tests/commands/clean_test.py b/tests/commands/clean_test.py
--- a/tests/commands/clean_test.py
+++ b/tests/commands/clean_test.py
@@ -2,18 +2,35 @@
 
 import os.path
 
+import mock
+import pytest
+
 from pre_commit.commands.clean import clean
 from pre_commit.util import rmtree
 
 
-def test_clean(runner_with_mocked_store):
+@pytest.fixture(autouse=True)
+def fake_old_dir(tempdir_factory):
+    fake_old_dir = tempdir_factory.get()
+
+    def _expanduser(path, *args, **kwargs):
+        assert path == '~/.pre-commit'
+        return fake_old_dir
+
+    with mock.patch.object(os.path, 'expanduser', side_effect=_expanduser):
+        yield fake_old_dir
+
+
+def test_clean(runner_with_mocked_store, fake_old_dir):
+    assert os.path.exists(fake_old_dir)
     assert os.path.exists(runner_with_mocked_store.store.directory)
     clean(runner_with_mocked_store)
+    assert not os.path.exists(fake_old_dir)
     assert not os.path.exists(runner_with_mocked_store.store.directory)
 
 
 def test_clean_empty(runner_with_mocked_store):
-    """Make sure clean succeeds when we the directory doesn't exist."""
+    """Make sure clean succeeds when the directory doesn't exist."""
     rmtree(runner_with_mocked_store.store.directory)
     assert not os.path.exists(runner_with_mocked_store.store.directory)
     clean(runner_with_mocked_store)
diff --git a/tests/error_handler_test.py b/tests/error_handler_test.py
--- a/tests/error_handler_test.py
+++ b/tests/error_handler_test.py
@@ -81,12 +81,12 @@ def test_log_and_exit(cap_out, mock_out_store_directory):
         )
 
     printed = cap_out.get()
+    log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
     assert printed == (
         'msg: FatalError: hai\n'
-        'Check the log at ~/.pre-commit/pre-commit.log\n'
+        'Check the log at {}\n'.format(log_file)
     )
 
-    log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
     assert os.path.exists(log_file)
     contents = io.open(log_file).read()
     assert contents == (
@@ -102,6 +102,7 @@ def test_error_handler_non_ascii_exception(mock_out_store_directory):
 
 
 def test_error_handler_no_tty(tempdir_factory):
+    pre_commit_home = tempdir_factory.get()
     output = cmd_output_mocked_pre_commit_home(
         sys.executable, '-c',
         'from __future__ import unicode_literals\n'
@@ -110,8 +111,10 @@ def test_error_handler_no_tty(tempdir_factory):
         '    raise ValueError("\\u2603")\n',
         retcode=1,
         tempdir_factory=tempdir_factory,
+        pre_commit_home=pre_commit_home,
     )
+    log_file = os.path.join(pre_commit_home, 'pre-commit.log')
     assert output[1].replace('\r', '') == (
         'An unexpected error has occurred: ValueError: ☃\n'
-        'Check the log at ~/.pre-commit/pre-commit.log\n'
+        'Check the log at {}\n'.format(log_file)
     )
diff --git a/tests/main_test.py b/tests/main_test.py
--- a/tests/main_test.py
+++ b/tests/main_test.py
@@ -2,6 +2,7 @@
 from __future__ import unicode_literals
 
 import argparse
+import os.path
 
 import mock
 import pytest
@@ -121,10 +122,11 @@ def test_expected_fatal_error_no_git_repo(
     with cwd(tempdir_factory.get()):
         with pytest.raises(SystemExit):
             main.main([])
+    log_file = os.path.join(mock_out_store_directory, 'pre-commit.log')
     assert cap_out.get() == (
         'An error has occurred: FatalError: git failed. '
         'Is it installed, and are you in a Git repository directory?\n'
-        'Check the log at ~/.pre-commit/pre-commit.log\n'
+        'Check the log at {}\n'.format(log_file)
     )
 
 
diff --git a/tests/store_test.py b/tests/store_test.py
--- a/tests/store_test.py
+++ b/tests/store_test.py
@@ -29,7 +29,15 @@ def test_our_session_fixture_works():
 def test_get_default_directory_defaults_to_home():
     # Not we use the module level one which is not mocked
     ret = _get_default_directory()
-    assert ret == os.path.join(os.path.expanduser('~'), '.pre-commit')
+    assert ret == os.path.join(os.path.expanduser('~/.cache'), 'pre-commit')
+
+
+def test_adheres_to_xdg_specification():
+    with mock.patch.dict(
+        os.environ, {'XDG_CACHE_HOME': '/tmp/fakehome'},
+    ):
+        ret = _get_default_directory()
+        assert ret == os.path.join('/tmp/fakehome', 'pre-commit')
 
 
 def test_uses_environment_variable_when_present():