Skip to content

Instantly share code, notes, and snippets.

@TripleDogDare
Last active September 26, 2015 00:44
Show Gist options
  • Save TripleDogDare/fbf49c2818cdf81dce1b to your computer and use it in GitHub Desktop.
Save TripleDogDare/fbf49c2818cdf81dce1b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Check that we are in a git repository. Will help avoid reckless recursion through files.
git_dir=$(git rev-parse --show-toplevel) && is_git_dir=1
[ -z $is_git_dir ] && {
echo "Must execute inside a git repository"
exit
}
echo "Git repository: $git_dir"
# Don't run this if there are unsaved modifications to the repository.
# Just trying to avoid doing something bad on accident
git_status=$(git status --porcelain --untracked-files=no)
[ "$git_status" ] && {
echo "There are unsaved modifications to the repository"
echo "Please run git commit or git stash so we can continue with our business"
exit
}
# Let's remove any existing submodules or git links (aka bad submodules)
result=$(git ls-files --stage | grep 160000 | awk '{print $4}')
for folder in $result; do
echo "Remove: $folder"
git rm --cached $folder
done
# Let's find all our submodules and add them
sub_gits=$(find $git_dir -name .git -type d -prune)
for unparsed_git_module in $sub_gits; do
git_module=$(echo $unparsed_git_module | sed "s#^$git_dir/##" | sed "s/.git$//")
[ -z $git_module ] && {
echo "SKIP: $unparsed_git_module"
continue
}
cd $unparsed_git_module
origin=$(git remote show -n origin | grep "Fetch URL:" | awk '{print $3}')
cd $git_dir
echo "ADD"
echo " ORIGIN: $origin"
echo " MODULE: $git_module"
git submodule add $origin $git_module
done
# Check to see if anything changed
git_status=$(git status --porcelain --untracked-files=no)
[ "$git_status" ] && {
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " !!! HUMAN !!! "
echo " Commit your changes "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
exit
}
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo " No changes were made "
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment