Skip to content

Instantly share code, notes, and snippets.

@edwinwong90
Last active November 4, 2019 10:35
Show Gist options
  • Save edwinwong90/e14f1d767fd42338789ee4f437cb8eab to your computer and use it in GitHub Desktop.
Save edwinwong90/e14f1d767fd42338789ee4f437cb8eab to your computer and use it in GitHub Desktop.
git flow extend cli tool which support submodule.

Installation

cd to root project and run following command

curl https://gist.githubusercontent.com/edwinwong90/e14f1d767fd42338789ee4f437cb8eab/raw/5e28bdb779815e207fd07940573b3840ba3abfc6/gitflow-extend-cli.sh > gitflow-extend-cli.sh && chmod 755 gitflow-extend-cli.sh

Usage

./gitflow-extend-cli.sh
#!/bin/bash
repoRoot=$(pwd)
currentBranchName=$(git rev-parse --abbrev-ref HEAD)
submodule=$(git config --file .gitmodules --get-regexp path | awk '{ print $2 }')
submoduleArr=($(echo "$submodule" | tr ' ' '\n'))
show_help() {
echo "
Usage: ./gitflow.sh <subcommand>
Git flow extend cli tool is a tool to support git submodule. You must install git flow first https://github.com/nvie/gitflow.
Available subcommands are:
init Initialize a new git repo with support for the branching model.
feature Manage your feature branches.
bugfix Manage your bugfix branches.
release Manage your release branches.
hotfix Manage your hotfix branches.
support Manage your support branches.
version Shows version information.
config Manage your git-flow configuration.
log Show log deviating from base branch.
branch Shows submodule branch name.
"
}
runCommandInSubmodule() {
for i in "${submoduleArr[@]}"; do # access each element of array
# if Directory exists
if [ -d "./$i" ]
then
echoBoldText "In directory $repoRoot/$i and executing command \"$gitCommand\""
cd $i && $gitCommand && cd $repoRoot
fi
done
}
echoSuccesText() {
tput setaf 2; echo $1; tput sgr0;
}
echoBoldText() {
tput bold; echo $1; tput sgr0;
}
echoErrorText() {
tput setaf 1; echo $1; tput sgr0;
}
echoUnderlineText() {
tput smul; echo $1; tput rmul;
}
validateSubmoduleAreSameBranchName() {
if [[ ! $currentBranchName == $(git rev-parse --abbrev-ref HEAD) ]]; then
echoErrorText "$repoRoot branch name does not match $currentBranchName! You may use 'git flow' command to manual finish your branch (if you know what you'r doing)."
fi
for i in "${submoduleArr[@]}"; do # access each element of array
# if Directory exists
if [ -d "./$i" ]
then
cd $i && if [[ ! $currentBranchName == $(git rev-parse --abbrev-ref HEAD) ]]; then
echoErrorText "$repoRoot/$i branch name does not match $currentBranchName! You may use 'git flow' command to manual finish your branch (if you know what you'r doing)."
exit 1
fi && cd $repoRoot
fi
done
}
showSubmoduleBranchName() {
echoUnderlineText "$repoRoot > $(git rev-parse --abbrev-ref HEAD)"
for i in "${submoduleArr[@]}"; do # access each element of array
# if Directory exists
if [ -d "./$i" ]
then
cd $i && echoUnderlineText "$repoRoot/$i > $(git rev-parse --abbrev-ref HEAD)" && cd $repoRoot
fi
done
}
case $1 in
"init")
git flow init
;;
"feature"|"bugfix"|"release"|"hotfix")
gitCommand="git flow $1 $2 $3"
echoBoldText "In direactory $repoRoot and executing command \"$gitCommand\""
if [[ $2 == 'finish' ]]; then
validateSubmoduleAreSameBranchName
fi
$gitCommand && runCommandInSubmodule $gitCommand && echoSuccesText "All Set! Good to go!"
;;
"branch")
showSubmoduleBranchName
;;
"")
show_help
;;
*)
git flow $1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment