Added info about filesystem, tools and git as procedures
This commit is contained in:
132
claude.sh
132
claude.sh
@@ -1,69 +1,117 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Initialize variables
|
||||
output_file="claude.txt"
|
||||
current_filter=""
|
||||
directories=()
|
||||
|
||||
# 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"
|
||||
-f|--filter)
|
||||
if [ -n "$2" ]; then
|
||||
current_filter="$2"
|
||||
shift 2
|
||||
else
|
||||
echo "Error: Directory '$2' does not exist or is not specified" >&2
|
||||
echo "Error: No filter pattern specified after -f/--filter" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 [-d|--directory <path>]" >&2
|
||||
exit 1
|
||||
# Check if the argument is a valid directory
|
||||
if [ -d "$1" ]; then
|
||||
# Store directory with its associated filter
|
||||
directories+=("$1")
|
||||
directories+=("$current_filter")
|
||||
shift 1
|
||||
else
|
||||
echo "Error: Directory '$1' does not exist" >&2
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
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
|
||||
# Add header
|
||||
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
|
||||
# Process each directory
|
||||
for ((i=0; i<${#directories[@]}; i+=2)); do
|
||||
dir="${directories[i]}"
|
||||
filter="${directories[i+1]}"
|
||||
|
||||
echo "Processing directory: $dir (filter: ${filter:-none})"
|
||||
|
||||
# Add directory to output
|
||||
echo -e "\n=== Directory: $dir ===" >> "$output_file"
|
||||
|
||||
# Change to target directory
|
||||
original_dir=$(pwd)
|
||||
cd "$dir" || continue
|
||||
|
||||
# Generate directory tree for this directory
|
||||
if command -v tree &> /dev/null; then
|
||||
if [ -f ".gitignore" ]; then
|
||||
tree -I "$(git check-ignore * .*)" >> "$output_file"
|
||||
else
|
||||
tree >> "$output_file"
|
||||
fi
|
||||
else
|
||||
find . -type f -name "*" | sort >> "$output_file"
|
||||
fi
|
||||
|
||||
# Skip binary files
|
||||
if file "$file" | grep -q "binary"; then
|
||||
echo "Skipping binary file: $file"
|
||||
continue
|
||||
# List files based on filter
|
||||
echo -e "\nFile Contents for $dir:" >> "$output_file"
|
||||
echo "=======================" >> "$output_file"
|
||||
|
||||
# Get list of files to process
|
||||
files_to_process=()
|
||||
|
||||
if [ -z "$filter" ]; then
|
||||
# No filter, use git ls-files if git repo, otherwise use find
|
||||
if git rev-parse --is-inside-work-tree &> /dev/null; then
|
||||
mapfile -d $'\0' files_to_process < <(git ls-files -z --cached --others --exclude-standard)
|
||||
else
|
||||
mapfile -d $'\0' files_to_process < <(find . -type f -not -path "*/\.*" -print0)
|
||||
fi
|
||||
else
|
||||
# Use filter
|
||||
if git rev-parse --is-inside-work-tree &> /dev/null; then
|
||||
mapfile -d $'\0' files_to_process < <(git ls-files -z --cached --others --exclude-standard | grep -z "$filter$")
|
||||
else
|
||||
mapfile -d $'\0' files_to_process < <(find . -type f -name "*$filter" -print0)
|
||||
fi
|
||||
fi
|
||||
|
||||
echo -e "\n=== File: $file ===" >> "$output_file"
|
||||
echo -e "------------------------" >> "$output_file"
|
||||
cat "$file" >> "$output_file"
|
||||
# Process each file
|
||||
for file in "${files_to_process[@]}"; do
|
||||
# Skip the output file itself
|
||||
if [ "$file" = "$output_file" ] || [ "$file" = "./$output_file" ]; 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" >> "$output_file"
|
||||
continue
|
||||
fi
|
||||
|
||||
echo -e "\n=== File: $dir/$file ===" >> "$output_file"
|
||||
echo -e "------------------------" >> "$output_file"
|
||||
cat "$file" >> "$output_file"
|
||||
done
|
||||
|
||||
# Return to original directory
|
||||
cd "$original_dir" || exit 1
|
||||
done
|
||||
|
||||
# Return to original directory
|
||||
cd "$original_dir" || exit 1
|
||||
|
||||
echo "Concatenation complete. Output written to claude.txt"
|
||||
echo "Concatenation complete. Output written to $output_file"
|
||||
Reference in New Issue
Block a user