Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved Problem with Non-ASCII .gitignore Files #2229

Merged
merged 12 commits into from May 24, 2021
8 changes: 7 additions & 1 deletion src/black/files.py
Expand Up @@ -3,6 +3,7 @@
import os
from pathlib import Path
import sys
import tokenize
from typing import (
Any,
Dict,
Expand Down Expand Up @@ -118,8 +119,13 @@ def get_gitignore(root: Path) -> PathSpec:
gitignore = root / ".gitignore"
lines: List[str] = []
if gitignore.is_file():
with gitignore.open() as gf:
with open(gitignore, "rb") as buf:
srcbuf = io.BytesIO(buf.read())
encoding, _ = tokenize.detect_encoding(srcbuf.readline)
ichard26 marked this conversation as resolved.
Show resolved Hide resolved
# Detect Encoding
with gitignore.open(encoding=encoding) as gf:
lines = gf.readlines()
# Open .gitignore file with detected encoding
return PathSpec.from_lines("gitwildmatch", lines)


Expand Down