Skip to content

Instantly share code, notes, and snippets.

@t-lo
Created April 6, 2023 12:24
Show Gist options
  • Save t-lo/f403f2f1df5368a46a1454fe08e064f9 to your computer and use it in GitHub Desktop.
Save t-lo/f403f2f1df5368a46a1454fe08e064f9 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Merge coreos-overlay and portage-stable submodules into scripts.
# From: https://x3ro.de/integrating-a-submodule-into-the-parent-repository/
#
# This commit contains the patch for scripts, ci-automation etc. to remove submodules.
git_ref_for_cherrypicking="c5b00016a13e1d4f7a52caae94c137572e3c0498"
set -euo pipefail
workdir="$(pwd)"
if [ -z "${1:-}" ] ; then
echo "Need flatcar version / branch ('flatcar-[MAJOR]' or 'main') to work on"
exit
fi
version="$1"
dir="${workdir}/scripts-no-submodules-${version}"
mkdir "${dir}"
cd "${dir}"
function git_rewrite_to_subfolder() {
local path="$1"
# Escape input for SED, taken from http://stackoverflow.com/a/2705678/124257
local target_path=$(echo -n "$path" | sed -e 's/[\/&]/\\&/g')
local tab="$(printf '\t')"
git ls-files -s | sed "s/${tab}/${tab}$target_path\//"
local cmd="git ls-files -s | sed \"s/${tab}/${tab}$target_path\//\" | GIT_INDEX_FILE=\${GIT_INDEX_FILE}.new git update-index --index-info && mv \${GIT_INDEX_FILE}.new \${GIT_INDEX_FILE}"
git filter-branch \
--index-filter "$cmd" \
HEAD
}
for r in portage-stable coreos-overlay scripts; do
echo
echo "################################ Cloning ${r} version ${version} #####################################"
echo
git clone git@github.com:flatcar/${r}.git
git -C "${r}" checkout "${version}"
done
for sm in coreos-overlay portage-stable; do
echo
echo "################################ Rewriting history of ${sm}. This can take a long time. #####################################"
echo
cd "${sm}"
git_rewrite_to_subfolder "sdk_container/src/third_party/${sm}" 2>&1 >"../rewrite-history-${sm}.log"
cd "${dir}"
done
echo
echo "################################ Removing submodules #####################################"
echo
cd scripts
git checkout -b t-lo/merge-submodules-to-paths-"${version}"
rm -rf sdk_container/src/third_party/coreos-overlay/
rm -rf sdk_container/src/third_party/portage-stable/
rm -f .gitmodules
git add -A .
git commit -m "remove submodules"
for sm in coreos-overlay portage-stable; do
echo
echo "################################ Merging rewritten history of ${sm} #####################################"
echo
git remote add "${sm}" ../"${sm}"/
git fetch "${sm}"
git merge --allow-unrelated-histories -s ours --no-commit "${sm}/${version}"
echo
echo "################################ Getting contents of ${sm} ${version} #####################################"
echo
git clone git@github.com:flatcar/${sm}.git "sdk_container/src/third_party/${sm}/"
git -C "sdk_container/src/third_party/${sm}/" checkout "${version}"
rm -rf "sdk_container/src/third_party/${sm}/.git"
rm -rf "sdk_container/src/third_party/${sm}/.github"
git add "sdk_container/src/third_party/${sm}/"
git commit -m "sdk_container/src/third_party/${sm} : merge contents of version ${version}"
done
cd ..
echo
echo "################################ Validating contents against sumbodules check-out of ${version} #####################################"
echo
echo "Fetching..."
git clone git@github.com:flatcar/scripts.git scripts-with-submodules
cd scripts-with-submodules
./checkout "${version}"
cd ..
#
# Coreos-overlay contains symlinks that only work inside the SDK container.
# diff returns an error for these, so we temporarily remove them.
#
echo
echo "###### Removing broken symlinks so as to not confuse diff"
rm scripts/sdk_container/src/third_party/coreos-overlay/coreos/scripts/build_packages
rm scripts-with-submodules/sdk_container/src/third_party/coreos-overlay/coreos/scripts/build_packages
rm scripts/sdk_container/src/third_party/coreos-overlay/coreos/scripts/setup_board
rm scripts-with-submodules/sdk_container/src/third_party/coreos-overlay/coreos/scripts/setup_board
echo
echo "###### FILE DIFFERENCES:"
diff -urN --exclude='.git*' scripts scripts-with-submodules
echo
echo "###### Restoring symlinks"
cd scripts
git checkout sdk_container/src/third_party/coreos-overlay/coreos/scripts/build_packages
git checkout sdk_container/src/third_party/coreos-overlay/coreos/scripts/setup_board
echo
echo "################################ Cherry-picking '${git_ref_for_cherrypicking}' #####################################"
echo
echo "There will be a merge conflict in version.txt which we can auto-resolve."
echo "If there are other conflicts please resolve these manually, then run 'git commit'"
echo
set +e
git cherry-pick --no-commit "${git_ref_for_cherrypicking}"
set -e
git checkout HEAD -- sdk_container/.repo/manifests/version.txt
git commit -m "scripts, CI, workflows: remove submodule handling (${version})"
echo
echo "################################ Running final 'git status'. #####################################"
echo
echo "There should be no changes."
echo
git status
echo
echo "################################ All done. #####################################"
echo
echo "Now run 'git push t-lo/merge-submodules-to-paths-"${version}" --force"
echo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment