fix: correct binary file detection in git file viewer
- Remove useless git diff --numstat call that failed in bare repos - Use raw bytes instead of text decoding to avoid encoding issues - Properly check subprocess return codes Fixes false positive binary detection for text files like .env.sample
This commit is contained in:
@@ -190,18 +190,17 @@ def get_file_content(repo_path: str, branch: str, path: str) -> FileContent:
|
|||||||
|
|
||||||
|
|
||||||
def _is_binary_file(repo_path: str, branch: str, path: str) -> bool:
|
def _is_binary_file(repo_path: str, branch: str, path: str) -> bool:
|
||||||
"""Check if a file is binary."""
|
"""Check if a file is binary using raw bytes to avoid encoding issues."""
|
||||||
try:
|
try:
|
||||||
_run_git_command(
|
result = subprocess.run(
|
||||||
repo_path,
|
["git", "show", f"{branch}:{path}"],
|
||||||
"diff",
|
cwd=repo_path,
|
||||||
"--numstat",
|
capture_output=True,
|
||||||
"--",
|
|
||||||
path,
|
|
||||||
)
|
)
|
||||||
# Alternative: use git show and check for null bytes
|
if result.returncode != 0:
|
||||||
content = _run_git_command(repo_path, "show", f"{branch}:{path}")
|
raise RuntimeError(f"Git command failed: {result.stderr.decode()}")
|
||||||
return b"\x00" in content.encode("utf-8", errors="replace")
|
# A file is binary if it contains null bytes
|
||||||
|
return b"\x00" in result.stdout
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user