Skip to content

Instantly share code, notes, and snippets.

@bvenkatr
Last active March 1, 2019 07:50
Show Gist options
  • Save bvenkatr/77e477ba86cd1e70004fb7bc8ebe1242 to your computer and use it in GitHub Desktop.
Save bvenkatr/77e477ba86cd1e70004fb7bc8ebe1242 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
######################################################
#
# https://wiki.bash-hackers.org/howto/getopts_tutorial
# https://sookocheff.com/post/bash/parsing-bash-script-arguments-with-shopts/
######################################################
function help() {
echo -e "Project Initial Setup....\n"
echo -e "Usage: $0 COMMAND [PARAMETERS]..."
echo -e "\nCommands:"
echo -e "\t add_all_required_submodules\t\tThis will add all(git-hooks, servicedefs) required submodules with specified branch"
echo -e "\t add_submodule"
echo -e "\t remove_submodule"
echo -e "\t setup_git_hooks"
echo -e "\t update_all_submodules_code\t\tThis will update all submodules code to their latest version for specified branch"
echo -e "\nParameters:"
echo -e "\t-n name of the submodule"
echo -e "\t-b name of the branch"
# echo "==============List of available submodules are...=============="
# echo $(git submodule status | cut -d" " -f2)
# echo "==============================================================="
exit 1;
}
function setup_git_hooks {
if [[ "$1" == "" ]]; then
echo "branch name is a required arguments"
exit 1
fi
gitHooksDir="git-hooks"
git submodule add -b $1 [email protected]:roomville/"$gitHooksDir".git
chmod -R +x "$gitHooksDir"
# Use absolute paths while creating symbolic links
ln -svFf "$PWD/$gitHooksDir"/post-merge "$PWD"/.git/hooks/post-merge
ln -svFf "$PWD/$gitHooksDir"/pre-commit "$PWD"/.git/hooks/pre-commit
ln -svFf "$PWD/$gitHooksDir"/post-commit "$PWD"/.git/hooks/post-commit
}
function add_submodule()
{
echo "Adding submodule named $1 with branch $2"
if [[ "$1" == "" ]] || [[ "$2" == "" ]]; then
echo "submodule name and branch name are required arguments"
exit 1
fi
git submodule add -b $2 [email protected]:roomville/$1.git
if [ $? -ne 0 ]; then
echo "Looks like submodule already there, remove using remove_submodule sub-command"
fi
}
function remove_submodule() {
if [ "$1" == "" ]; then
echo "submodule name is a required argument"
exit 1
fi
echo "removing submodule $1 ......"
git submodule deinit $1
# OR
#git submodule deinit -f $name
rm -rf $1
rm -rf .git/modules/$1/
# TODO could not find how to delete specific lines which belongs to given sub-module
#open vi .gitmodules and remove required submodule section
git rm --cached $1
echo "Done."
}
function update_all_submodules_code() {
if [ "$1" == "" ]; then
echo "branch name is a reqruied argument"
exit 1
fi
git submodule update --init --recursive
git submodule foreach git checkout $1
git submodule foreach git pull
echo "Done."
}
function add_all_required_submodules() {
add_submodule "git-hooks" $1
add_submodule "servicedefs" $1
}
#Check the shell
if [ -z "$BASH_VERSION" ]; then
echo -e "Error: this script requires the BASH shell!"
exit 1
fi
let argnum=$#-$OPTIND
if [[ $argnum -lt 2 ]]; then
help
fi
COMMAND="${*:$OPTIND:1}"
# remove the sub-command from the arguments list using shift 1 or shift
shift 1
name=""
branch=""
# In Silent Mode, that is disable the verbose error handling by preceding the whole option string with a colon (:)
while getopts ':hn:b:' opt
do
case $opt in
n)
name=$OPTARG
;;
b)
branch=$OPTARG
;;
h)
help
;;
\?) # In Silent Mode, opt is set to ? if invalid option passed and $OPTARG is set to the (invalid) option character
echo "Invalid option: -$OPTARG" >&2
;;
:) # In Silent Mode, opt is set to : if required argument not found and $OPTARG contains the option-character in question
echo "Option -$OPTARG requires an argument." >&2
;;
esac
done
#CHECKING PARAMS VALUES
case $COMMAND in
add_all_required_submodules)
add_all_required_submodules $branch
;;
add_submodule)
add_submodule $name $branch
;;
remove_submodule)
remove_submodule $name
;;
setup_git_hooks)
setup_git_hooks $branch
;;
update_all_submodules_code)
update_all_submodules_code $branch
;;
*)
if [[ $COMMAND != "" ]]; then
echo -e "Error: Unknown sub-command: $COMMAND\n"
else
echo -e "Missing sub-command\n"
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment