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:
|
||||
"""Check if a file is binary."""
|
||||
"""Check if a file is binary using raw bytes to avoid encoding issues."""
|
||||
try:
|
||||
_run_git_command(
|
||||
repo_path,
|
||||
"diff",
|
||||
"--numstat",
|
||||
"--",
|
||||
path,
|
||||
result = subprocess.run(
|
||||
["git", "show", f"{branch}:{path}"],
|
||||
cwd=repo_path,
|
||||
capture_output=True,
|
||||
)
|
||||
# Alternative: use git show and check for null bytes
|
||||
content = _run_git_command(repo_path, "show", f"{branch}:{path}")
|
||||
return b"\x00" in content.encode("utf-8", errors="replace")
|
||||
if result.returncode != 0:
|
||||
raise RuntimeError(f"Git command failed: {result.stderr.decode()}")
|
||||
# A file is binary if it contains null bytes
|
||||
return b"\x00" in result.stdout
|
||||
except RuntimeError:
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user