Skip to content

Instantly share code, notes, and snippets.

@geoffjay
Created January 8, 2022 22:52
Show Gist options
  • Save geoffjay/2351a0b29e4ea7bc7807a8b81f07c1d4 to your computer and use it in GitHub Desktop.
Save geoffjay/2351a0b29e4ea7bc7807a8b81f07c1d4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Simple file refactoring for a c project, not intended for use, just
# preserving something I've used a couple of times.
function __refactor {
dir=$(echo $1 | sed 's/\/$//')
files=$(find $dir -type f -regex ".*\.[ch]")
for file in $files; do
new=$(basename $file | sed "s/$2/$3/")
echo "refactoring: \"$file\" -> \"$dir/$new\""
__fix_references $(basename $file) $new
git mv $file $dir/$new
done
}
function __fix_references {
references=$(git grep "['\"/]$1['\"]" | sed 's/:.*$//' | uniq)
for reference in $references; do
# this is specific to macos/bsd sed
sed -i "" "s/$1/$2/" $reference &>/dev/null
if [[ $? -ne 0 ]]; then
echo "failed to update references in " $reference
fi
done
echo -e
}
function __usage {
cat <<=EOF
usage: $0 <path> <from> <to>
=EOF
}
if [[ "$#" -ne 3 ]]; then
__usage "$@"
exit 1
fi
__refactor "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment