mirror of
https://github.com/kirill-markin/repo-to-text.git
synced 2025-12-06 03:22:23 -08:00
Compare commits
5 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
77209f30aa | ||
|
|
8a94182b3d | ||
|
|
bcb0d82191 | ||
|
|
2807344752 | ||
|
|
3721ed45f0 |
5 changed files with 47 additions and 14 deletions
|
|
@ -1,3 +1,7 @@
|
||||||
|
---
|
||||||
|
alwaysApply: true
|
||||||
|
---
|
||||||
|
|
||||||
# repo-to-text
|
# repo-to-text
|
||||||
|
|
||||||
## Project Overview
|
## Project Overview
|
||||||
1
AGENTS.md
Symbolic link
1
AGENTS.md
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
.cursor/index.mdc
|
||||||
1
CLAUDE.md
Symbolic link
1
CLAUDE.md
Symbolic link
|
|
@ -0,0 +1 @@
|
||||||
|
.cursor/index.mdc
|
||||||
|
|
@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "repo-to-text"
|
name = "repo-to-text"
|
||||||
version = "0.7.0"
|
version = "0.8.0"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "Kirill Markin", email = "markinkirill@gmail.com" },
|
{ name = "Kirill Markin", email = "markinkirill@gmail.com" },
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ Core functionality for repo-to-text
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
|
import platform
|
||||||
from typing import Tuple, Optional, List, Dict, Any, Set
|
from typing import Tuple, Optional, List, Dict, Any, Set
|
||||||
from datetime import datetime, timezone
|
from datetime import datetime, timezone
|
||||||
from importlib.machinery import ModuleSpec
|
from importlib.machinery import ModuleSpec
|
||||||
|
|
@ -36,12 +37,20 @@ def get_tree_structure(
|
||||||
|
|
||||||
def run_tree_command(path: str) -> str:
|
def run_tree_command(path: str) -> str:
|
||||||
"""Run the tree command and return its output."""
|
"""Run the tree command and return its output."""
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
cmd = ["cmd", "/c", "tree", "/a", "/f", path]
|
||||||
|
else:
|
||||||
|
cmd = ["tree", "-a", "-f", "--noreport", path]
|
||||||
|
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
['tree', '-a', '-f', '--noreport', path],
|
cmd,
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
|
stderr=subprocess.PIPE,
|
||||||
|
text=True,
|
||||||
|
encoding='utf-8',
|
||||||
check=True
|
check=True
|
||||||
)
|
)
|
||||||
return result.stdout.decode('utf-8')
|
return result.stdout
|
||||||
|
|
||||||
def filter_tree_output(
|
def filter_tree_output(
|
||||||
tree_output: str,
|
tree_output: str,
|
||||||
|
|
@ -343,6 +352,33 @@ def save_repo_to_text(
|
||||||
return output_filepaths[0]
|
return output_filepaths[0]
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
def _read_file_content(file_path: str) -> str:
|
||||||
|
"""Read file content, handling binary files and broken symlinks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
file_path: Path to the file to read
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: File content or appropriate message for special cases
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
with open(file_path, 'r', encoding='utf-8') as f:
|
||||||
|
return f.read()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
logging.debug('Handling binary file contents: %s', file_path)
|
||||||
|
with open(file_path, 'rb') as f_bin:
|
||||||
|
binary_content: bytes = f_bin.read()
|
||||||
|
return binary_content.decode('latin1')
|
||||||
|
except FileNotFoundError as e:
|
||||||
|
# Minimal handling for bad symlinks
|
||||||
|
if os.path.islink(file_path) and not os.path.exists(file_path):
|
||||||
|
try:
|
||||||
|
target = os.readlink(file_path)
|
||||||
|
except OSError:
|
||||||
|
target = ''
|
||||||
|
return f"[symlink] -> {target}"
|
||||||
|
raise e
|
||||||
|
|
||||||
|
|
||||||
def generate_output_content(
|
def generate_output_content(
|
||||||
path: str,
|
path: str,
|
||||||
|
|
@ -417,17 +453,8 @@ def generate_output_content(
|
||||||
cleaned_relative_path = relative_path.replace('./', '', 1)
|
cleaned_relative_path = relative_path.replace('./', '', 1)
|
||||||
|
|
||||||
_add_chunk_to_output(f'\n<content full_path="{cleaned_relative_path}">\n')
|
_add_chunk_to_output(f'\n<content full_path="{cleaned_relative_path}">\n')
|
||||||
|
file_content = _read_file_content(file_path)
|
||||||
try:
|
_add_chunk_to_output(file_content)
|
||||||
with open(file_path, 'r', encoding='utf-8') as f:
|
|
||||||
file_content = f.read()
|
|
||||||
_add_chunk_to_output(file_content)
|
|
||||||
except UnicodeDecodeError:
|
|
||||||
logging.debug('Handling binary file contents: %s', file_path)
|
|
||||||
with open(file_path, 'rb') as f_bin:
|
|
||||||
binary_content: bytes = f_bin.read()
|
|
||||||
_add_chunk_to_output(binary_content.decode('latin1')) # Add decoded binary
|
|
||||||
|
|
||||||
_add_chunk_to_output('\n</content>\n')
|
_add_chunk_to_output('\n</content>\n')
|
||||||
|
|
||||||
_add_chunk_to_output('\n</repo-to-text>\n')
|
_add_chunk_to_output('\n</repo-to-text>\n')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue