sudo apt-get install xclip
pip3 install pyperclip
chmod +x cc.py
sudo mv cc.py /usr/local/bin/cc
Last active
October 30, 2024 21:07
-
-
Save creotiv/6c8815199a826e163751f52701fe4230 to your computer and use it in GitHub Desktop.
Dir concatenation script for ChatGPT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import re | |
import argparse | |
import pyperclip | |
def concatenate_files(root_dir, exclude_patterns): | |
# Compile regex patterns for exclusion | |
exclude_regex = [re.compile(pattern) for pattern in exclude_patterns] | |
concatenated_content = "" | |
for dirpath, dirnames, filenames in os.walk(root_dir): | |
# Skip .git directory and any other excluded directories | |
dirnames[:] = [d for d in dirnames if not any(regex.search(os.path.join(dirpath, d)) for regex in exclude_regex)] | |
for filename in filenames: | |
filepath = os.path.join(dirpath, filename) | |
# Check if file matches any exclusion pattern | |
if any(regex.search(filepath) for regex in exclude_regex): | |
continue | |
# Append filepath and file content to the concatenated content | |
with open(filepath, 'r', errors='ignore') as infile: | |
content = infile.read() | |
concatenated_content += f"{filepath}:\n```\n{content}\n```\n\n" | |
return concatenated_content | |
def main(): | |
parser = argparse.ArgumentParser(description="Concatenate all files in a directory into one output with specified exclusions, and copy it to clipboard.") | |
parser.add_argument("-d", "--directory", required=True, help="Root directory to traverse") | |
parser.add_argument("-o", "--output", help="Output file path for concatenated content") | |
parser.add_argument("-e", "--exclude", nargs="*", default=[], help="List of regex patterns to exclude files or directories") | |
args = parser.parse_args() | |
concatenated_content = concatenate_files(args.directory, args.exclude) | |
# Save to output file if provided | |
if args.output: | |
with open(args.output, 'w') as outfile: | |
outfile.write(concatenated_content) | |
# Copy content to clipboard | |
pyperclip.copy(concatenated_content) | |
print("Concatenated content has been copied to the clipboard.") | |
if args.output: | |
print(f"Content also saved to {args.output}") | |
if __name__ == "__main__": | |
# Example cc.py -d ./ -e 'chart/' 'go\.mod$' 'go\.sum$' '\.gitignore$' 'cc\.py$' 'README' '\.git$' | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment