Repository: pre-commit/pre-commit-hooks (python)

Issue:
check-case-conflict doesn't notice directory name conflicts
```
$ git status
On branch master

Initial commit

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

    new file:   TEST/foo
    new file:   test/bar

$ git commit -m "Test"
Check for case conflicts...................................................................................Passed
[master (root-commit) 6931ba5] Test
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 TEST/foo
 create mode 100644 test/bar

$ cat .pre-commit-config.yaml
- repo: git://github.com/pre-commit/pre-commit-hooks
  sha: '0d88124ef6343fbbc6c9d2872853f73546c98a3f'
  hooks:
      - id: check-case-conflict
```

A conflict such as `Test/foo` and `test/foo` is caught though.


Pull request:
check also directories in check_case_conflict
fix #70

Patch:
diff --git a/pre_commit_hooks/check_case_conflict.py b/pre_commit_hooks/check_case_conflict.py
index 6b8ba82..024c1c3 100644
--- a/pre_commit_hooks/check_case_conflict.py
+++ b/pre_commit_hooks/check_case_conflict.py
@@ -1,5 +1,7 @@
 import argparse
+import os.path
 from typing import Iterable
+from typing import Iterator
 from typing import Optional
 from typing import Sequence
 from typing import Set
@@ -12,9 +14,22 @@ def lower_set(iterable: Iterable[str]) -> Set[str]:
     return {x.lower() for x in iterable}
 
 
+def parents(file: str) -> Iterator[str]:
+    file = os.path.dirname(file)
+    while file:
+        yield file
+        file = os.path.dirname(file)
+
+
+def directories_for(files: Set[str]) -> Set[str]:
+    return {parent for file in files for parent in parents(file)}
+
+
 def find_conflicting_filenames(filenames: Sequence[str]) -> int:
     repo_files = set(cmd_output('git', 'ls-files').splitlines())
+    repo_files |= directories_for(repo_files)
     relevant_files = set(filenames) | added_files()
+    relevant_files |= directories_for(relevant_files)
     repo_files -= relevant_files
     retv = 0
 


Test patch:
diff --git a/tests/check_case_conflict_test.py b/tests/check_case_conflict_test.py
index 53de852..c8c9d12 100644
--- a/tests/check_case_conflict_test.py
+++ b/tests/check_case_conflict_test.py
@@ -1,7 +1,24 @@
+import sys
+
+import pytest
+
 from pre_commit_hooks.check_case_conflict import find_conflicting_filenames
 from pre_commit_hooks.check_case_conflict import main
+from pre_commit_hooks.check_case_conflict import parents
 from pre_commit_hooks.util import cmd_output
 
+skip_win32 = pytest.mark.skipif(
+    sys.platform == 'win32',
+    reason='case conflicts between directories and files',
+)
+
+
+def test_parents():
+    assert set(parents('a')) == set()
+    assert set(parents('a/b')) == {'a'}
+    assert set(parents('a/b/c')) == {'a/b', 'a'}
+    assert set(parents('a/b/c/d')) == {'a/b/c', 'a/b', 'a'}
+
 
 def test_nothing_added(temp_git_dir):
     with temp_git_dir.as_cwd():
@@ -26,6 +43,36 @@ def test_adding_something_with_conflict(temp_git_dir):
         assert find_conflicting_filenames(['f.py', 'F.py']) == 1
 
 
+@skip_win32  # pragma: win32 no cover
+def test_adding_files_with_conflicting_directories(temp_git_dir):
+    with temp_git_dir.as_cwd():
+        temp_git_dir.mkdir('dir').join('x').write('foo')
+        temp_git_dir.mkdir('DIR').join('y').write('foo')
+        cmd_output('git', 'add', '-A')
+
+        assert find_conflicting_filenames([]) == 1
+
+
+@skip_win32  # pragma: win32 no cover
+def test_adding_files_with_conflicting_deep_directories(temp_git_dir):
+    with temp_git_dir.as_cwd():
+        temp_git_dir.mkdir('x').mkdir('y').join('z').write('foo')
+        temp_git_dir.join('X').write('foo')
+        cmd_output('git', 'add', '-A')
+
+        assert find_conflicting_filenames([]) == 1
+
+
+@skip_win32  # pragma: win32 no cover
+def test_adding_file_with_conflicting_directory(temp_git_dir):
+    with temp_git_dir.as_cwd():
+        temp_git_dir.mkdir('dir').join('x').write('foo')
+        temp_git_dir.join('DIR').write('foo')
+        cmd_output('git', 'add', '-A')
+
+        assert find_conflicting_filenames([]) == 1
+
+
 def test_added_file_not_in_pre_commits_list(temp_git_dir):
     with temp_git_dir.as_cwd():
         temp_git_dir.join('f.py').write("print('hello world')")
@@ -46,6 +93,19 @@ def test_file_conflicts_with_committed_file(temp_git_dir):
         assert find_conflicting_filenames(['F.py']) == 1
 
 
+@skip_win32  # pragma: win32 no cover
+def test_file_conflicts_with_committed_dir(temp_git_dir):
+    with temp_git_dir.as_cwd():
+        temp_git_dir.mkdir('dir').join('x').write('foo')
+        cmd_output('git', 'add', '-A')
+        cmd_output('git', 'commit', '--no-gpg-sign', '-n', '-m', 'Add f.py')
+
+        temp_git_dir.join('DIR').write('foo')
+        cmd_output('git', 'add', '-A')
+
+        assert find_conflicting_filenames([]) == 1
+
+
 def test_integration(temp_git_dir):
     with temp_git_dir.as_cwd():
         assert main(argv=[]) == 0