Created
October 21, 2020 02:40
-
-
Save conr2d/89bcc49db3e69bd1a57baf59f4a840e0 to your computer and use it in GitHub Desktop.
A shell script for merging git submodules to the main repository
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# This script merges all submodules into the main repository. | |
# You need to put this file in the parent directory or home directory, | |
# but run in the repository directory. | |
# Otherwise, this file will be included in your repository. (or, add merge.sh to .gitignore) | |
# | |
# $ ../merge.sh | |
if [[ ! -z $(git submodule status --recursive | grep "^[+\-]") ]]; then | |
echo 'error: not clean or up-to-date' | |
exit 1 | |
fi | |
cat .gitmodules | while read i | |
do | |
if [[ $i == \[submodule* ]]; then | |
echo converting $i | |
read i | |
# extract the module's prefix | |
mpath=$(echo $i | grep -E "(\S+)$" -o) | |
echo path: $mpath | |
read i | |
# extract the url of the submodule | |
murl=$(echo $i|cut -d\= -f2|xargs) | |
echo url: $murl | |
# extract the module name | |
mname=$(basename $mpath) | |
echo name: $mname | |
# extract the referenced commit | |
mcommit=$(git submodule status $mpath | grep -E "\S+" -o | head -1) | |
mcommit=${mcommit//-/} | |
echo commit: $mcommit | |
# remove submodule and keep files | |
mv $mpath $mpath-$mcommit | |
git submodule deinit $mpath | |
git rm -rf $mpath | |
mv $mpath-$mcommit $mpath | |
find $mpath -name '.git' -prune -exec rm -rf {} \; | |
# add commit for merged submodule | |
git add -f $mpath | |
git commit -m "merge $mname-$mcommit" | |
echo | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment