Skip to content

Instantly share code, notes, and snippets.

@sauntimo
Created May 15, 2017 19:25
Show Gist options
  • Save sauntimo/72d05aa390cd01d6d56991257e23814c to your computer and use it in GitHub Desktop.
Save sauntimo/72d05aa390cd01d6d56991257e23814c to your computer and use it in GitHub Desktop.
Move dirty edits in submodules into an actual submodule and optionally commit them, pull them back into the original repo and commit them.
submodule="$1"
msg="$2"
# quit immediately if no arguments specified
if [ $# -eq 0 ]
then
echo "==> No arguments supplied"
exit 1
fi
# quit if submodule argument is bad
if [ "$submodule" != "page" ] && [ "$submodule" != "core" ]
then
echo "==> Unknown submodule argument"
exit 1
fi
sm_upper=$(echo "$submodule" | tr '[:lower:]' '[:upper:]')
# navigate to submodule
cd /var/www/api/"$submodule"
# get current branch
sm_branch="$(git rev-parse --abbrev-ref HEAD)"
# exit if master
if [ "$sm_branch" == "master" ]
then
echo ""
echo "==> $sm_upper is on branch master. Please checkout another branch before making changes to it. You scumbag."
exit 1
else
echo ""
echo "==> $sm_upper is on branch $sm_branch, proceeding to copy dirty edits across."
echo ""
fi
# navigate to seed/submodule folder
cd "/var/www/vhosts/seed/httpdocs/$submodule/"
# loop over dirty changes in seed submodule and copy changed files to api/submodule
git diff --name-only | \
while read srcfile
do
cp "/var/www/vhosts/seed/httpdocs/$submodule/$srcfile" "/var/www/api/$submodule/$srcfile"
done
# if second arg (msg) specified
if [ ${#msg} -gt 0 ]
then
# navigate to submodule
cd /var/www/api/"$submodule"
# add files and commit with specified message
git add -A
git commit -m "$msg"
echo ""
echo "==> Dirty edits to Seed $sm_upper succesffully copied to $sm_upper @ $sm_branch and committed with msg: $msg."
echo ""
# go back to seed
cd "/var/www/vhosts/seed/"
# get branch
seed_branch="$(git rev-parse --abbrev-ref HEAD)"
# exit if master
if [ "$seed_branch" == "master" ]
then
echo ""
echo "==> Seed is on branch master. Please checkout another branch before committing changes to $sm_upper. You scumbag."
echo ""
echo "==> Note that dirty edits will still exist in Seed $sm_upper even though they have been committed to $sm_upper @ $sm_branch."
echo ""
exit 1
else
echo ""
echo "==> Seed is on branch $seed_branch, proceeding to pull from $sm_upper @ $sm_branch"
echo ""
fi
# navigate to submodule folder
cd "/var/www/vhosts/seed/httpdocs/$submodule/"
# checkout dirty changes to this submodule
git reset --hard
# pull in commited changes from current branch
git pull origin "$sm_branch"
# go to seed top level
cd "/var/www/vhosts/seed/"
# add and commit changes pulled in to submodule
git add httpdocs/"$submodule"
git commit -m "Updated $sm_upper"
echo ""
echo "==> Dirty edits to Seed $sm_upper committed to $sm_upper @ $sm_branch, checked out of Seed and pulled back into Seed @ $seed_branch and commited."
echo ""
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment