Files
SIA/claude.sh
2024-11-06 16:12:50 +01:00

69 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# Parse command line arguments
target_dir="." # Default to current directory
while [[ $# -gt 0 ]]; do
case $1 in
-d|--directory)
if [ -n "$2" ] && [ -d "$2" ]; then
target_dir="$2"
shift 2
else
echo "Error: Directory '$2' does not exist or is not specified" >&2
exit 1
fi
;;
*)
echo "Usage: $0 [-d|--directory <path>]" >&2
exit 1
;;
esac
done
# Change to target directory while storing original directory
original_dir=$(pwd)
cd "$target_dir" || exit 1
# Clear/create output file
output_file="$original_dir/claude.txt"
> "$output_file"
# Generate and add directory tree
echo "Directory Tree:" > "$output_file"
echo "=============" >> "$output_file"
# Use tree with gitignore patterns
tree -I "$(git check-ignore * .*)" >> "$output_file"
echo -e "\nFile Contents:" >> "$output_file"
echo "=============" >> "$output_file"
# Use git ls-files to get tracked files and untracked files that aren't ignored
# The --exclude-standard flag makes git ls-files respect .gitignore
# --others includes untracked files
# --cached includes tracked files
# -z uses null byte as separator for safer handling of filenames with spaces
(git ls-files -z --cached --others --exclude-standard) | while IFS= read -r -d '' file; do
# Skip the output file itself
if [ "$file" = "claude.txt" ]; then
continue
fi
# Skip non-existent files
if [ ! -f "$file" ]; then
continue
fi
# Skip binary files
if file "$file" | grep -q "binary"; then
echo "Skipping binary file: $file"
continue
fi
echo -e "\n=== File: $file ===" >> "$output_file"
echo -e "------------------------" >> "$output_file"
cat "$file" >> "$output_file"
done
# Return to original directory
cd "$original_dir" || exit 1
echo "Concatenation complete. Output written to claude.txt"