Skip to content

Commit

Permalink
Made .gitignore File Reader Detect Its Encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed May 13, 2021
1 parent b0100b5 commit 6c3a7ea
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/black/files.py
Original file line number Diff line number Diff line change
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(encoding="utf-8") as gf:
with open(gitignore, "rb") as buf:
srcbuf = io.BytesIO(buf.read())
encoding, _ = tokenize.detect_encoding(srcbuf.readline)
# 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

0 comments on commit 6c3a7ea

Please sign in to comment.