mirror of
https://github.com/kirill-markin/repo-to-text.git
synced 2025-12-06 03:22:23 -08:00
commit
07521f06ca
3 changed files with 81 additions and 43 deletions
28
README.md
28
README.md
|
|
@ -42,6 +42,12 @@ After installation, you can use the `repo-to-text` command in your terminal. Nav
|
|||
repo-to-text
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
flatten
|
||||
```
|
||||
|
||||
This will create a file named `repo-to-text_YYYY-MM-DD-HH-MM-SS-UTC.txt` in the current directory with the text representation of the repository. The contents of this file will also be copied to your clipboard for easy sharing.
|
||||
|
||||
### Options
|
||||
|
|
@ -56,12 +62,18 @@ You can customize the behavior of `repo-to-text` with the following options:
|
|||
|
||||
This will save the file in the specified output directory instead of the current directory.
|
||||
|
||||
- `--create-settings`: Create a default `.repo-to-text-settings.yaml` file with predefined settings. This is useful if you want to start with a template settings file and customize it according to your needs. To create the default settings file, run the following command in your terminal:
|
||||
- `--create-settings` or `--init`: Create a default `.repo-to-text-settings.yaml` file with predefined settings. This is useful if you want to start with a template settings file and customize it according to your needs. To create the default settings file, run the following command in your terminal:
|
||||
|
||||
```bash
|
||||
repo-to-text --create-settings
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```bash
|
||||
repo-to-text --init
|
||||
```
|
||||
|
||||
This will create a file named `.repo-to-text-settings.yaml` in the current directory. If the file already exists, an error will be raised to prevent overwriting.
|
||||
|
||||
- `--debug`: Enable DEBUG logging. By default, `repo-to-text` runs with INFO logging level. To enable DEBUG logging, use the `--debug` flag:
|
||||
|
|
@ -76,6 +88,20 @@ You can customize the behavior of `repo-to-text` with the following options:
|
|||
repo-to-text --debug > debug_log.txt 2>&1
|
||||
```
|
||||
|
||||
- `input_dir`: Specify the directory to process. If not provided, the current directory (`.`) will be used. For example:
|
||||
|
||||
```bash
|
||||
repo-to-text /path/to/input_dir
|
||||
```
|
||||
|
||||
- `--stdout`: Output the generated text to stdout instead of a file. This is useful for piping the output to another command or saving it to a file using shell redirection. For example:
|
||||
|
||||
```bash
|
||||
repo-to-text --stdout > myfile.txt
|
||||
```
|
||||
|
||||
This will write the output directly to `myfile.txt` instead of creating a timestamped file.
|
||||
|
||||
## Settings
|
||||
|
||||
`repo-to-text` also supports configuration via a `.repo-to-text-settings.yaml` file. By default, the tool works without this file, but you can use it to customize what gets included in the final text file.
|
||||
|
|
|
|||
|
|
@ -150,7 +150,7 @@ def remove_empty_dirs(tree_output: str, path='.') -> str:
|
|||
logging.debug('Empty directory removal complete')
|
||||
return '\n'.join(final_lines)
|
||||
|
||||
def save_repo_to_text(path='.', output_dir=None) -> str:
|
||||
def save_repo_to_text(path='.', output_dir=None, to_stdout=False) -> str:
|
||||
logging.debug(f'Starting to save repo structure to text for path: {path}')
|
||||
gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec = load_ignore_specs(path)
|
||||
tree_structure = get_tree_structure(path, gitignore_spec, tree_and_content_ignore_spec)
|
||||
|
|
@ -167,52 +167,57 @@ def save_repo_to_text(path='.', output_dir=None) -> str:
|
|||
os.makedirs(output_dir)
|
||||
output_file = os.path.join(output_dir, output_file)
|
||||
|
||||
output_content = []
|
||||
project_name = os.path.basename(os.path.abspath(path))
|
||||
output_content.append(f'Directory: {project_name}\n\n')
|
||||
output_content.append('Directory Structure:\n')
|
||||
output_content.append('```\n.\n')
|
||||
|
||||
# Insert .gitignore if it exists
|
||||
if os.path.exists(os.path.join(path, '.gitignore')):
|
||||
output_content.append('├── .gitignore\n')
|
||||
|
||||
output_content.append(tree_structure + '\n' + '```\n')
|
||||
logging.debug('Tree structure written to output content')
|
||||
|
||||
for root, _, files in os.walk(path):
|
||||
for filename in files:
|
||||
file_path = os.path.join(root, filename)
|
||||
relative_path = os.path.relpath(file_path, path)
|
||||
|
||||
if should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec):
|
||||
continue
|
||||
|
||||
relative_path = relative_path.replace('./', '', 1)
|
||||
|
||||
output_content.append(f'\nContents of {relative_path}:\n')
|
||||
output_content.append('```\n')
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
output_content.append(f.read())
|
||||
except UnicodeDecodeError:
|
||||
logging.debug(f'Could not decode file contents: {file_path}')
|
||||
output_content.append('[Could not decode file contents]\n')
|
||||
output_content.append('\n```\n')
|
||||
|
||||
output_content.append('\n')
|
||||
logging.debug('Repository contents written to output content')
|
||||
|
||||
output_text = ''.join(output_content)
|
||||
|
||||
if to_stdout:
|
||||
print(output_text)
|
||||
return output_text
|
||||
|
||||
with open(output_file, 'w') as file:
|
||||
project_name = os.path.basename(os.path.abspath(path))
|
||||
file.write(f'Directory: {project_name}\n\n')
|
||||
file.write('Directory Structure:\n')
|
||||
file.write('```\n.\n')
|
||||
|
||||
# Insert .gitignore if it exists
|
||||
if os.path.exists(os.path.join(path, '.gitignore')):
|
||||
file.write('├── .gitignore\n')
|
||||
|
||||
file.write(tree_structure + '\n' + '```\n')
|
||||
logging.debug('Tree structure written to file')
|
||||
|
||||
for root, _, files in os.walk(path):
|
||||
for filename in files:
|
||||
file_path = os.path.join(root, filename)
|
||||
relative_path = os.path.relpath(file_path, path)
|
||||
|
||||
if should_ignore_file(file_path, relative_path, gitignore_spec, content_ignore_spec, tree_and_content_ignore_spec):
|
||||
continue
|
||||
|
||||
relative_path = relative_path.replace('./', '', 1)
|
||||
|
||||
file.write(f'\nContents of {relative_path}:\n')
|
||||
file.write('```\n')
|
||||
try:
|
||||
with open(file_path, 'r', encoding='utf-8') as f:
|
||||
file.write(f.read())
|
||||
except UnicodeDecodeError:
|
||||
logging.debug(f'Could not decode file contents: {file_path}')
|
||||
file.write('[Could not decode file contents]\n')
|
||||
file.write('\n```\n')
|
||||
|
||||
file.write('\n')
|
||||
logging.debug('Repository contents written to file')
|
||||
|
||||
# Read the contents of the generated file
|
||||
with open(output_file, 'r') as file:
|
||||
repo_text = file.read()
|
||||
file.write(output_text)
|
||||
|
||||
# Try to copy to clipboard if pyperclip is installed
|
||||
try:
|
||||
import importlib.util
|
||||
if importlib.util.find_spec("pyperclip"):
|
||||
import pyperclip
|
||||
pyperclip.copy(repo_text)
|
||||
pyperclip.copy(output_text)
|
||||
logging.debug('Repository structure and contents copied to clipboard')
|
||||
else:
|
||||
print("Tip: Install 'pyperclip' package to enable automatic clipboard copying:")
|
||||
|
|
@ -254,9 +259,11 @@ def create_default_settings_file():
|
|||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Convert repository structure and contents to text')
|
||||
parser.add_argument('input_dir', nargs='?', default='.', help='Directory to process')
|
||||
parser.add_argument('--debug', action='store_true', help='Enable debug logging')
|
||||
parser.add_argument('--output-dir', type=str, help='Directory to save the output file')
|
||||
parser.add_argument('--create-settings', action='store_true', help='Create default .repo-to-text-settings.yaml file') # Новый аргумент
|
||||
parser.add_argument('--create-settings', '--init', action='store_true', help='Create default .repo-to-text-settings.yaml file')
|
||||
parser.add_argument('--stdout', action='store_true', help='Output to stdout instead of a file')
|
||||
args = parser.parse_args()
|
||||
|
||||
setup_logging(debug=args.debug)
|
||||
|
|
@ -266,7 +273,11 @@ def main():
|
|||
create_default_settings_file()
|
||||
logging.debug('.repo-to-text-settings.yaml file created')
|
||||
else:
|
||||
save_repo_to_text(output_dir=args.output_dir)
|
||||
save_repo_to_text(
|
||||
path=args.input_dir,
|
||||
output_dir=args.output_dir,
|
||||
to_stdout=args.stdout
|
||||
)
|
||||
|
||||
logging.debug('repo-to-text script finished')
|
||||
|
||||
|
|
|
|||
3
setup.py
3
setup.py
|
|
@ -5,7 +5,7 @@ with open('requirements.txt') as f:
|
|||
|
||||
setup(
|
||||
name='repo-to-text',
|
||||
version='0.4.3',
|
||||
version='0.4.4',
|
||||
author='Kirill Markin',
|
||||
author_email='markinkirill@gmail.com',
|
||||
description='Convert a directory structure and its contents into a single text file, including the tree output and file contents in markdown code blocks. It may be useful to chat with LLM about your code.',
|
||||
|
|
@ -19,6 +19,7 @@ setup(
|
|||
entry_points={
|
||||
'console_scripts': [
|
||||
'repo-to-text=repo_to_text.main:main',
|
||||
'flatten=repo_to_text.main:main',
|
||||
],
|
||||
},
|
||||
classifiers=[
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue