From 3de8a7a8ca8290ba14d2ff9a618b63a3c3aa702f Mon Sep 17 00:00:00 2001 From: Luke Craig Date: Mon, 27 Oct 2025 10:16:02 -0400 Subject: [PATCH] core: simplify generate_output_content --- repo_to_text/core/core.py | 51 +++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/repo_to_text/core/core.py b/repo_to_text/core/core.py index 555e343..1e565f0 100644 --- a/repo_to_text/core/core.py +++ b/repo_to_text/core/core.py @@ -343,6 +343,34 @@ def save_repo_to_text( return output_filepaths[0] 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}" + else: + raise e + def generate_output_content( path: str, @@ -417,27 +445,8 @@ def generate_output_content( cleaned_relative_path = relative_path.replace('./', '', 1) _add_chunk_to_output(f'\n\n') - - try: - 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 - 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 = '' - _add_chunk_to_output(f"[symlink] -> {target}") - else: - raise e - + file_content = _read_file_content(file_path) + _add_chunk_to_output(file_content) _add_chunk_to_output('\n\n') _add_chunk_to_output('\n\n')