Last active
July 10, 2024 16:36
-
-
Save cryppadotta/3ed2b542ee6607ea35161824cc8656cc to your computer and use it in GitHub Desktop.
concat-repo.sh
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
#!/bin/bash | |
# Check if the repository path is provided as an argument | |
if [ $# -eq 0 ]; then | |
echo "Please provide the path to the git repository as an argument." | |
exit 1 | |
fi | |
repo_root="$1" | |
shift | |
# Initialize ignore list | |
ignore_list=() | |
# Parse optional arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--ignore) | |
IFS=',' read -r -a ignore_list <<< "$2" | |
shift 2 | |
;; | |
*) | |
echo "Unknown option: $1" | |
exit 1 | |
;; | |
esac | |
done | |
# Check if the provided path is a valid git repository | |
if ! git -C "$repo_root" rev-parse --is-inside-work-tree > /dev/null 2>&1; then | |
echo "The provided path is not a valid git repository." | |
exit 1 | |
fi | |
# Set the output file name | |
output_file="concatenated_repo.txt" | |
# Delete the output file if it already exists | |
rm -f "$output_file" | |
# Function to process each file | |
process_file() { | |
local file="$1" | |
echo "// $file" >> "$output_file" | |
cat "$file" >> "$output_file" | |
echo "" >> "$output_file" # Add a blank line between files | |
} | |
# Get the total number of files to process using git ls-files | |
echo "Counting total files to process..." | |
total_files=$(git -C "$repo_root" ls-files | wc -l) | |
echo "Total files to process: $total_files" | |
# Initialize the progress bar | |
progress=0 | |
progress_bar_width=50 | |
# Function to update the progress bar | |
update_progress_bar() { | |
local file="$1" | |
local file_size=$(stat -f%z "$repo_root/$file") | |
progress=$((progress + 1)) | |
local percent=$((progress * 100 / total_files)) | |
local filled_width=$((progress * progress_bar_width / total_files)) | |
local empty_width=$((progress_bar_width - filled_width)) | |
# Determine color for file size | |
if [ "$file_size" -gt 10000 ]; then | |
file_size_color="\033[31m" # Red | |
else | |
file_size_color="\033[0m" # Default | |
fi | |
printf "\rProgress: [%.*s%*s] %d%% - Processing: %s (${file_size_color}%d bytes\033[0m)\n" "$filled_width" "$(printf '%*s' "$filled_width" | tr ' ' '#')" "$empty_width" "$(printf '%*s' "$empty_width")" "$percent" "$file" "$file_size" | |
printf "\033[K" # Clear the line to the right of the cursor | |
} | |
echo "Starting file concatenation..." | |
# Build the ignore pattern for git ls-files | |
ignore_pattern=() | |
for dir in "${ignore_list[@]}"; do | |
ignore_pattern+=(":!$dir") | |
ignore_pattern+=(":!$dir/*") | |
done | |
# Iterate over all files in the repository using git ls-files | |
git -C "$repo_root" ls-files -z -- ':!*.png' ':!*.gif' ':!*.svg' ':!*.jpg' ':!*.lock' ':!*.ico' ':!.env' ':!.env.*' ':!*.ttf' ':!*.webp' ':!*.otf' ':!*.pyc' ':!*.woff' ':!*.woff2' ':!*.csv' ':!*-lock.yaml' ':!*.ipynb' "${ignore_pattern[@]}" | while IFS= read -r -d $'\0' file; do | |
# echo "Processing file: $file" | |
update_progress_bar "$file" | |
process_file "$repo_root/$file" | |
done | |
echo -e "\nConcatenation complete. Output file: $output_file" | |
output_file_size=$(stat -f%z "$output_file") | |
echo "Output file size: $output_file_size bytes" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment