Skip to content

Instantly share code, notes, and snippets.

@bratorange
Created November 19, 2024 13:11
Show Gist options
  • Save bratorange/ed7542fb0f0fef27a7304ac01d8389db to your computer and use it in GitHub Desktop.
Save bratorange/ed7542fb0f0fef27a7304ac01d8389db to your computer and use it in GitHub Desktop.
Simple script to concatenate the state of a git repo into a single file. Usefull for uploading your source code to chatbots.
#!/bin/bash
# Usage:
# $cd $your_repo
# $state.sh
# Project state has been saved to project_state.txt
# Name of this script
script_name=$(basename "$0")
# Use the provided path or default to current directory
search_path="${1:-.}"
# Name of the output file
output_file="$search_path/project_state.txt"
# Run tree command and save the output (excluding hidden files and folders)
echo "Project Structure:" > "$output_file"
tree -L 3 -I '.*' "$search_path" >> "$output_file"
echo -e "\n\n" >> "$output_file"
# Find all non-hidden files tracked by git (excluding this script and the output file)
files=$(git -C "$search_path" ls-files | grep -v -E "$script_name|$output_file")
# Loop through each file
for file in $files
do
file="$search_path/$file"
# Check if the file is text-based using file command
if file "$file" | grep -qE ' (ASCII|UTF-8|Unicode|ISO-8859|text|empty)'; then
echo "File: $file" >> "$output_file"
echo "----------------------------------------" >> "$output_file"
cat "$file" >> "$output_file"
echo -e "\n\n" >> "$output_file"
fi
done
echo "Project state has been saved to $output_file"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment