-
-
Save gianpaolof/ba4ba71d6c0298b0dd4a3970eeddb7ea to your computer and use it in GitHub Desktop.
A script to commit the git parent repo and any submodule changes too
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 | |
# | |
# Usage: ./commit_everything.sh "Commit message" | |
BOLD=$(tput bold) | |
BLACK=$(tput setaf 0) | |
WHITE=$(tput setaf 7) | |
BLUE=$(tput setaf 4) | |
GREEN=$(tput setaf 2) | |
NORMAL=$(tput sgr0) | |
RED=$(tput setaf 1) | |
commit () { | |
# Figure out if the parent repo has changed and make a commit with the | |
# commit message that was passed in | |
STATUS=$(git status --porcelain --ignore-submodules | wc -l) | |
if [[ $STATUS -ne 0 ]]; then | |
echo "🤓 ${GREEN}UPDATING PARENT REPO${NORMAL}" | |
git add . | |
git commit -am "$1" | |
if [[ $? -ne 0 ]]; then | |
exit 1; | |
fi | |
fi | |
# Now go to each submodule and update those with the same commit message | |
SUBMODULES=($(git submodule | awk '{print $2}')) | |
SUBMODULES_UPDATED=0 | |
for SUBMODULE in "${SUBMODULES[@]}"; do | |
pushd $SUBMODULE > /dev/null | |
STATUS=$(git status --porcelain | wc -l) | |
if [[ $STATUS -ne 0 ]]; then | |
echo "🤓 ${GREEN}UPDATING SUBMODULE:${WHITE} ${SUBMODULE}${NORMAL}" | |
git add . | |
git commit -am "$1" | |
if [[ $? -ne 0 ]]; then | |
exit 1; | |
fi | |
SUBMODULES_UPDATED=1 | |
#git pull --rebase && git status --short --branch && git push | |
git status --short --branch && git push | |
fi | |
popd > /dev/null | |
done | |
# When submodules are updated, we need to commit that as well | |
if [[ $SUBMODULES_UPDATED -eq 1 ]]; then | |
echo "🤓 ${GREEN}UPDATING SUBMODULES${NORMAL}" | |
git add . | |
git commit -am "Updated submodules" | |
fi | |
# Push everything | |
echo "🤓 ${GREEN}UPDATING EVERYTHING${NORMAL}" | |
#git pull --rebase && git status --short --branch && git push | |
git status --short --branch && git push | |
} | |
check_branch () { | |
#check on which branch we are | |
BRANCH=`git rev-parse --abbrev-ref HEAD` | |
if [[ "$BRANCH" == "master" || "$BRANCH" == "develop" ]]; then | |
echo "${RED}${BOLD}You are on branch $BRANCH. you can't commit here${NORMAL}" | |
false | |
else | |
true | |
fi | |
#check remote of parent | |
REMOTE="$(git config "BRANCH.${BRANCH}.remote")" | |
UPSTREAM=`git status -b --porcelain=v2 | grep -m 1 "^# branch.upstream " | cut -d " " -f 3-` | |
if [[ "$UPSTREAM" == "$REMOTE/$BRANCH" ]]; then | |
echo "🤓 ${GREEN}MAIN: ${BRANCH} is tracking ${UPSTREAM}${NORMAL}" | |
else | |
echo "🤓 ${RED} MAIN: CHECK UPSTREAM!!! ${NORMAL}" | |
exit 1; | |
fi | |
# Now go to each submodule and check remote | |
SUBMODULES=($(git submodule | awk '{print $2}')) | |
for SUBMODULE in "${SUBMODULES[@]}"; do | |
pushd $SUBMODULE > /dev/null | |
REMOTE="$(git config "BRANCH.${BRANCH}.remote")" | |
UPSTREAM=`git status -b --porcelain=v2 | grep -m 1 "^# branch.upstream " | cut -d " " -f 3-` | |
echo "REMOTE ${REMOTE} $REMOTE.$BRANCH" | |
echo "${BRANCH} is tracking ${UPSTREAM}" | |
if [[ "$UPSTREAM" == "$REMOTE/$BRANCH" ]]; then | |
echo "🤓 ${GREEN}in submodule $SUBMODULE: ${BRANCH} is tracking ${UPSTREAM}${NORMAL}" | |
else | |
echo "🤓 ${RED} CHECK UPSTREAM!!! ${NORMAL}" | |
exit 1; | |
fi | |
popd > /dev/null | |
done | |
} | |
check_changes(){ | |
GIT_CMD="git diff HEAD --name-only" | |
GIT_SUBMODULE_CMD="git submodule foreach git diff HEAD --name-only" | |
OUTPUT="$($GIT_CMD && $GIT_SUBMODULE_CMD | sed 's/.*Entering.*//')" | |
if [ -z "$OUTPUT" ] | |
then | |
false | |
else | |
echo "$OUTPUT" | |
read -p "Continue (yes/no)?" CONT | |
if [ "$CONT" = "yes" ]; then | |
true | |
else | |
false | |
fi | |
fi | |
} | |
main () { | |
if check_branch; then | |
if check_changes; then | |
commit "$1"; | |
else | |
echo "EXITING"; | |
fi | |
else | |
echo "EXITING"; | |
fi | |
} | |
main "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added a check to avoid committing on master and on develop and added a check to list changes before proceeding with commit