Skip to content

Instantly share code, notes, and snippets.

@drumnickydrum
Last active July 13, 2024 17:34
Show Gist options
  • Save drumnickydrum/0bb9afd8daebc88b06de5fc6470c37c6 to your computer and use it in GitHub Desktop.
Save drumnickydrum/0bb9afd8daebc88b06de5fc6470c37c6 to your computer and use it in GitHub Desktop.
[dump-repo] Dump the contents of an entire git repo into a text file at the root #bash #git
# A really insane command that dumps all of the text content of a directory into
# a single pathname-delimited text file. I use this to share my repo with claude,
# in lieu of a co-pilot.
# ⚠️ WARNING: You should definitely add repo.txt to your .gitignore !!
alias dump-repo='
# 2>/dev/null suppresses error messages if not in a Git repo
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$git_root" ]; then
echo "Not in a git repository"
return 1
fi
cd "$git_root"
file_count=0
# Start a subshell to capture all output
{
# List all files tracked by Git, untracked files, but exclude ignored files
# Then remove pnpm-lock.yaml from this list
# Finally, loop through each file
git ls-files --cached --others --exclude-standard | grep -v "pnpm-lock.yaml" | while IFS= read -r file; do
((file_count++))
# Print a separator with the file name
echo "=== $file ==="
if file -b --mime-type "$file" | grep -q "^text/"; then
# If it is a text file, output its contents
cat "$file"
else
# If it is not a text file (i.e., binary), output a description instead
echo "[Binary file: $(file -b "$file")]"
fi
done
# Redirect all output from the subshell to repo.txt
} > repo.txt
echo "Repository contents dumped to $git_root/repo.txt"
echo "Total number of files processed: $file_count"
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment