Skip to content

Instantly share code, notes, and snippets.

@0xBigBoss
Created August 24, 2024 14:07
Show Gist options
  • Save 0xBigBoss/f80c09c34f7344c7c3eedac970792fee to your computer and use it in GitHub Desktop.
Save 0xBigBoss/f80c09c34f7344c7c3eedac970792fee to your computer and use it in GitHub Desktop.
Analyze the current directory c++ source files and copy them based on their layout into a new folder. Great for uploading as contexts for projects that do not have a great API for managing files.
#!/bin/bash
# create a prefix based on the parent git repo directory name
prefix=$(basename "$(git rev-parse --show-toplevel)")
# Create a directory to store the flattened files
outdir="/tmp/flattened"
mkdir -p "$outdir"
# Find c++ files, rename them, and copy to flattened directory
find . -name "*.cpp" -o -name "*.h" | while read -r file; do
# ignore if not in git tree
if ! git ls-files --error-unmatch "$file" &>/dev/null; then
continue
fi
# Remove leading './' and replace '/' with '-'
newname=$(echo "$file" | sed 's/^\.\///' | tr '/' '-')
echo "Processing $file to $outdir/$prefix-$newname"
# Copy the file to the flattened directory with the new name
cp "$file" "$outdir/$prefix-$newname"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment