Skip to content

Instantly share code, notes, and snippets.

@justinjc
Last active September 9, 2022 21:27
Show Gist options
  • Save justinjc/cfd83078be80ca6f14ffa44ee22d73cf to your computer and use it in GitHub Desktop.
Save justinjc/cfd83078be80ca6f14ffa44ee22d73cf to your computer and use it in GitHub Desktop.
Merging m3db/* repos into m3db/m3
#!/bin/bash
set -euo pipefail
usage() {
echo "usage: $0 <repo> <name>"
echo
echo "<repo>"
echo " The repo link, e.g. https://github.com/m3db/m3coordinator.git"
echo
echo "<name>"
echo " The name of the directory within 'src/', conventionally the repo name with the 'm3' prefix stripped, e.g. coordinator"
}
if [[ $# -ne 2 ]]; then
usage
exit 1
fi
# git clone repo, e.g. https://github.com/m3db/m3coordinator.git
repo="$1"
# Name of the directory within src/, conventionally the repo name with the
# m3 prefix stripped, e.g. coordinator
name="$2"
oldname="$(echo "$repo" | sed 's#^.*/\([a-zA-Z0-9]*\).*#\1#')"
folder="src/$name"
monobranch="monorepo-merge-$name"
mkdir -p "$folder"
git remote add "$name" "$repo"
echo "Fetching '$name/master'..."
# Complains about no common commits, which is expected
git fetch -q "$name" master 2> /dev/null
# Hack to rewrite history to prepend $folder/ to all files
echo "Rewriting history to change directory structure..."
# NOTE for debugging: there is a tab character in the sed. Not all versions
# of sed interpret \t as a tab character.
git filter-branch -f --index-filter \
'git ls-files -s | sed "s% \"*%&'"$folder"'/%" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' "$name/master"
echo "Checking out branch '$monobranch'"
# Complains about non-empty .ci/ directory (submodule), but it's okay because
# we don't do modifications to it.
git checkout -q --no-track -b "$monobranch" "$name/master" 2> /dev/null
echo "Rebasing on 'master'..."
# Complains about non-empty .ci/ directory (submodule), but it's okay because
# we don't do modifications to it.
git rebase -q --committer-date-is-author-date master 2> /dev/null
# Clean up remote
git remote remove "$name"
echo "Cleaning up submodules..."
rm -rf "$folder/.ci" "$folder/.gitmodules" ".git/modules/$folder/.ci"
git rm -q --cached "$folder/.ci"
echo "Replacing 'github.com/m3db/$oldname' with 'github.com/m3db/m3/src/$name'.."
find . -name "*.go" -exec sed -i '' -e "s#github.com/m3db/$oldname#github.com/m3db/m3/src/$name#g" '{}' \;
echo "Done!"
@prateek
Copy link

prateek commented Oct 14, 2018

please add a find -exec with go fmt -w after the sed too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment