Repository: pre-commit/pre-commit-hooks

Issue:
Add check for illegal Windows filenames
There are file and directory names which are legal in Git and most operating systems but illegal in Windows.  They should be avoided in case a developer happens to use Windows.

The following names are illegal as bare names as well as with extensions regardless of case for files or directories.  For instance `CoM6`, `cOm6.txt`, `com6.foo.jpg`, etc are all illegal.  Windows will refuse to create those files if you try to check them out.

From: https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file

> Do not use the following reserved names for the name of a file:
> 
> CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see [Namespaces](https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file).

Comments:

Patch:
diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml
index 4b4d0cf..116392d 100644
--- a/.pre-commit-hooks.yaml
+++ b/.pre-commit-hooks.yaml
@@ -40,6 +40,11 @@
     language: python
     types: [text, executable]
     stages: [commit, push, manual]
+-   id: check-illegal-windows-names
+    name: check illegal windows names
+    entry: Illegal windows filenames detected
+    language: fail
+    files: '(?i)(^|/)(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|/|$)'
 -   id: check-json
     name: check json
     description: checks json files for parseable syntax.
diff --git a/README.md b/README.md
index 4992baf..97bfba6 100644
--- a/README.md
+++ b/README.md
@@ -51,6 +51,9 @@ Checks for a common error of placing code before the docstring.
 #### `check-executables-have-shebangs`
 Checks that non-binary executables have a proper shebang.
 
+#### `check-illegal-windows-names`
+Check for files that cannot be created on Windows.
+
 #### `check-json`
 Attempts to load all json files to verify syntax.
 


Test patch:
diff --git a/tests/check_illegal_windows_names_test.py b/tests/check_illegal_windows_names_test.py
new file mode 100644
index 0000000..4dce4f6
--- /dev/null
+++ b/tests/check_illegal_windows_names_test.py
@@ -0,0 +1,46 @@
+from __future__ import annotations
+
+import os.path
+import re
+
+import pytest
+
+from pre_commit_hooks.check_yaml import yaml
+
+
+@pytest.fixture(scope='module')
+def hook_re():
+    here = os.path.dirname(__file__)
+    with open(os.path.join(here, '..', '.pre-commit-hooks.yaml')) as f:
+        hook_defs = yaml.load(f)
+    hook, = (
+        hook
+        for hook in hook_defs
+        if hook['id'] == 'check-illegal-windows-names'
+    )
+    yield re.compile(hook['files'])
+
+
+@pytest.mark.parametrize(
+    's',
+    (
+        pytest.param('aux.txt', id='with ext'),
+        pytest.param('aux', id='without ext'),
+        pytest.param('AuX.tXt', id='capitals'),
+        pytest.param('com7.dat', id='com with digit'),
+    ),
+)
+def test_check_illegal_windows_names_matches(hook_re, s):
+    assert hook_re.search(s)
+
+
+@pytest.mark.parametrize(
+    's',
+    (
+        pytest.param('README.md', id='standard file'),
+        pytest.param('foo.aux', id='as ext'),
+        pytest.param('com.dat', id='com without digit'),
+    ),
+)
+def test_check_illegal_windows_names_does_not_match(hook_re, s):
+    assert hook_re.search(s) is None