Skip to content

Instantly share code, notes, and snippets.

@jwinder
Created June 17, 2025 17:52
Show Gist options
  • Save jwinder/7fa5ad2be6bf520e50a56725c85a84c6 to your computer and use it in GitHub Desktop.
Save jwinder/7fa5ad2be6bf520e50a56725c85a84c6 to your computer and use it in GitHub Desktop.
#!/bin/bash
function is_git_repo {
git rev-parse --is-inside-work-tree &>/dev/null
}
function is_ignored_by_git {
git check-ignore -q "$1"
}
function is_binary {
local file="$1"
if file --mime "$file" | grep -q "charset=binary"; then
return 0
else
return 1
fi
}
function process_file {
local file="$1"
local file_size=$(stat -c%s "$file")
if [ $file_size -gt 65536 ]; then
echo "File: $file (Size: $file_size bytes) - Ignored (larger than 64KB)"
elif ! is_binary "$file"; then
echo "File: $file"
cat "$file"
echo -e "\n---\n"
fi
}
function process_directory {
local dir="${1:-.}" # Use the provided argument, or "." if no argument given
local use_git=false
if is_git_repo; then
use_git=true
fi
if $use_git; then
while IFS= read -r -d '' file; do
if [ -f "$file" ]; then
process_file "$file"
fi
done < <(git ls-files -z "$dir")
else
for item in "$dir"/*; do
if [ -d "$item" ]; then
process_directory "$item"
elif [ -f "$item" ]; then
if ! $use_git || ! is_ignored_by_git "$item"; then
process_file "$item"
fi
fi
done
fi
}
# Check if an argument is provided, otherwise use "."
start_path="${1:-.}"
process_directory "$start_path"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment