Skip to content

Instantly share code, notes, and snippets.

@jwnimmer-tri
Created March 2, 2020 20:08
Show Gist options
  • Save jwnimmer-tri/45c09a32b6da11230adecc44a3e7c1e3 to your computer and use it in GitHub Desktop.
Save jwnimmer-tri/45c09a32b6da11230adecc44a3e7c1e3 to your computer and use it in GitHub Desktop.
#!/bin/sh
set -ex
# Download buildozer if you need it
# https://github.com/bazelbuild/buildtools/releases
buildozer=${HOME}/Downloads/buildozer
test -x $buildozer
# The stems.txt should list of components to be processed one per line, e.g.:
#
# systems/primitives/sine
#
# Check that the contents are sane.
stems=$(cat stems.txt)
for x in $stems; do
test -f $x.h
done
package=$(dirname $(head -n1 stems.txt))
short_package=$(basename $package)
labels=$(cat stems.txt | xargs -n1 basename | xargs -n1 -I{} echo "//$package:{}")
# Move the existing cc files out of the way and update BUILD to match.
for x in $stems; do
git mv $x.cc $x.old.cc
done
$buildozer 'substitute srcs \.cc$ .old.cc' $labels
git add $package/BUILD.bazel
git commit -m"$short_package: Rename old cc files out of the way
This clears the way for the header's history to replace the cc file's
history in a future commit.
"
bazel test --config lint //$package/...
# Rename the existing h files into nascent uncompiled cc files.
# Then undo the rename (but leave the nascent file intact).
# A rename is the only way to give it the correct git history.
git checkout -b git_prepare_h_to_cc
for x in $stems; do
git mv $x.h $x.cc
done
git commit -m"$short_package: Copy h files to cc files to preserve history
These files are not yet part of the build, but due to the missing header files
this commit will not compile. This commit is technically a rename (move), but
once combined with a future merge commit will behave like a copy instead.
"
headers=$(cat stems.txt | xargs -n1 -I{} echo "{}.h")
git checkout HEAD~ $headers
git commit -m"$short_package: Restore h files to unmodified status
"
git checkout -
git merge --no-ff git_prepare_h_to_cc -m"$short_package: Copy h files to cc files to preserve history"
git branch -d git_prepare_h_to_cc
bazel test --config lint //$package/...
# Copy the stub files' contents into the the new cc files for rework by a
# human. Then remove the stubs and repair the BUILD file.
for x in $stems; do
head -n1 $x.old.cc > $x.new.cc
tail -n+2 $x.cc >> $x.new.cc
tail -n+2 $x.old.cc >> $x.new.cc
mv $x.new.cc $x.cc
git add $x.cc
git rm $x.old.cc
done
$buildozer 'substitute srcs \.old\.cc$ .cc' $labels
git add $package/BUILD.bazel
git commit -m"$short_package: Rework header code into cc files
WIP The author should build and test this.
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment