Skip to content

Instantly share code, notes, and snippets.

@mr-karan
Created June 28, 2024 11:04
Show Gist options
  • Save mr-karan/94ca810001c534ddabcfb98825d80864 to your computer and use it in GitHub Desktop.
Save mr-karan/94ca810001c534ddabcfb98825d80864 to your computer and use it in GitHub Desktop.
Script to output a git repo's content to a single text file. Useful for prompting with LLMs where large volume of input is supported
#!/bin/bash
set -euo pipefail # Exit on error, unset variables, and prevent errors in pipelines
# Usage information
usage() {
echo "Usage: $0 <path-to-git-repo> <output-file>"
exit 1
}
# Check for mandatory arguments
if [ "$#" -ne 2 ]; then
usage
fi
GIT_REPO_PATH="$1"
OUTPUT_FILE="$2"
# Define ignore patterns for file types
declare -a ignore_patterns=(
"*.bin"
"*.png"
"*.jpg"
"*.gif"
"*.mp4"
"*.jpeg"
)
# # Verify the provided path is a git repository
# if [ ! -d "$GIT_REPO_PATH/.git" ]; then
# echo "The provided path does not seem to be a git repository."
# exit 1
# fi
# Create or clear the output file
: > "$OUTPUT_FILE"
# Output the directory tree to the top of the file
{
echo "Directory tree of the repository:"
tree "$GIT_REPO_PATH"
echo -e "\n\n"
} >> "$OUTPUT_FILE"
# Function to output file contents with separators
output_file_contents() {
local repo_path="$1"
local output_file="$2"
# Start the find command
local find_command="find $repo_path -type f ! -path '*.git/*'"
# Exclude defined ignore patterns
for pattern in "${ignore_patterns[@]}"; do
find_command+=" ! -name '$pattern'"
done
# Execute find command and process files
eval "$find_command" | while read -r file; do
{
echo "---------- $file ----------"
cat "$file"
echo -e "\n"
} >> "$output_file"
done
}
# Call function to output file contents
output_file_contents "$GIT_REPO_PATH" "$OUTPUT_FILE"
echo "Script completed. Output is in $OUTPUT_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment