Skip to content

Instantly share code, notes, and snippets.

@tensojka
Created July 15, 2023 21:49
Show Gist options
  • Save tensojka/61abd5edd8999f45c5cc87cc5a07a7e6 to your computer and use it in GitHub Desktop.
Save tensojka/61abd5edd8999f45c5cc87cc5a07a7e6 to your computer and use it in GitHub Desktop.
This script concats all of a git repository into one .txt file, perfect for supplying as context to Claude or for integration with other LLMs.
import os
import sys
import pathspec
def load_gitignore(root_dir):
gitignore = os.path.join(root_dir, '.gitignore')
if os.path.exists(gitignore):
with open(gitignore, 'r') as file:
spec = pathspec.PathSpec.from_lines('gitwildmatch', file)
else:
spec = pathspec.PathSpec.from_lines('gitwildmatch', [])
return spec
def concatenate_files(root_dir, output_file_path):
ignore_spec = load_gitignore(root_dir)
with open(output_file_path, 'w') as output_file:
for root, dirs, files in os.walk(root_dir):
dirs[:] = [d for d in dirs if not d.startswith('.git')]
for file in files:
file_path = os.path.join(root, file)
if ignore_spec.match_file(file_path):
continue
relative_path = os.path.relpath(file_path, root_dir)
output_file.write(f'---\n{relative_path}\n')
with open(file_path, 'r', errors='ignore') as input_file:
output_file.write(input_file.read())
output_file.write('\n')
root_dir = sys.argv[1]
output_file_path = sys.argv[2]
concatenate_files(root_dir, output_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment