Skip to content

Instantly share code, notes, and snippets.

@senorihl
Last active March 19, 2025 11:00
Show Gist options
  • Save senorihl/f1cb9fc3b595402535536000af15db33 to your computer and use it in GitHub Desktop.
Save senorihl/f1cb9fc3b595402535536000af15db33 to your computer and use it in GitHub Desktop.
Append branch to staging branch

Bash script to append branch to staging branche

Installation

wget https://gist.githubusercontent.com/senorihl/f1cb9fc3b595402535536000af15db33/raw/60fa518da823235e31923f0ec6447c846f508c7f/stage-branch.sh -O $HOME/.stage-branch.sh 
chmod a+x $HOME/.stage-branch.sh 
git config --global alias.stage-branch '!bash $HOME/.stage-branch.sh '

Usage

git stage-branch <environment> [-b|--branch <branch>] [--push] [-v|--verbose] [--dry-run] [-h|--help]

Description

This command will append current branch or the one specified by --branch to the staging one resseted from recette-

This command will fetch your git remote (origin) staging branch (recette-), will merge specified branch, or current one if omitted, in recette-. If push option specified it will try to push updated staging branch. If conflict occured it is reverted, you will be warned and script will go on

You can configure :

  • the staging branch prefix by setting CLEAN_STAGING_PREFIX environment variable (defaults to recette-).
  • the source branch by setting CLEAN_STAGING_ROOT environment variable (defaults to master).
  • the remote name by setting CLEAN_STAGING_ORIGIN environment variable (defaults to origin).

Options

  • -h|--help Display help.
  • -b|--branch <branch> Specify name to be merge (if ommited current branch is used)
  • -push Push staging branch after successful merge
  • -v|--verbose Display executed commands.
  • --dry-run Don't execute anything, just simulate commands
#!/usr/bin/env bash
VERBOSE=false
DRY=false
BRANCH_PREFIX=${CLEAN_STAGING_PREFIX:=recette-}
MASTER_OR_MAIN=${CLEAN_STAGING_ROOT:=master}
ORIGIN_OR_OTHER=${CLEAN_STAGING_ORIGIN:=origin}
POSITIONAL=()
MERGE=$(git rev-parse --abbrev-ref HEAD)
command_help () {
echo ""
echo "Append branch to staging branch ($PREFIX<environment>)."
echo ""
echo -e "\e[33mUsage:\e[0m"
echo " stage-branch <environment> [-b|--branch <branch>] [--push] [-v|--verbose] [--dry-run] [-h|--help]"
echo ""
echo -e "\e[33mDescription:\e[0m"
echo " This command will append current branch or the one specified by --branch to the staging one resseted from $BRANCH_PREFIX<environment>"
echo ""
echo -e "\e[33mOptions:\e[0m"
echo -e " \e[32m-h, --help\e[0m Show this screen."
echo -e " \e[32m-v, --verbose\e[0m Display executed commands."
echo -e " \e[32m-b, --branch <branch>\e[0m Specify <branch> name to be merge (if ommited current branch is used)"
echo -e " \e[32m--push\e[0m Push staging branch after successful merge"
echo -e " \e[32m--dry-run\e[0m Don't execute anything, just simulate commands"
echo ""
echo -e "\e[33mHelp:\e[0m"
echo -e " This command will fetch your git remote (\e[33m$ORIGIN_OR_OTHER\e[0m) staging branch (\e[33m$BRANCH_PREFIX<environment>\e[0m),"
echo -e " will merge specified branch, or current one if omitted, in \e[33m$BRANCH_PREFIX<environment>\e[0m."
echo " If push option specified it will try to push updated staging branch."
echo " If conflict occured it is reverted, you will be warned and script will go on."
echo ""
echo " You can configure :"
echo -e " - the staging branch prefix by setting \e[33mCLEAN_STAGING_PREFIX\e[0m environment variable (currently \e[33m$BRANCH_PREFIX\e[0m)."
echo -e " - the source branch by setting \e[33mCLEAN_STAGING_ROOT\e[0m environment variable (currently \e[33m$MASTER_OR_MAIN\e[0m)."
echo -e " - the remote name by setting \e[33mCLEAN_STAGING_ORIGIN\e[0m environment variable (currently \e[33m$ORIGIN_OR_OTHER\e[0m)."
echo ""
exit 1
}
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-h|--help)
command_help
shift # past argument
;;
-v|--verbose)
VERBOSE=true
shift # past argument
;;
--dry-run)
DRY=true
shift # past argument
;;
--push)
PUSH=true
shift # past argument
;;
-b|--branch)
MERGE="$2"
shift # past argument
shift # past argument
;;
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
die () {
echo >&2 "$@"
exit 1
}
command () {
local __resultvar=$2
[[ "$VERBOSE" = "true" ]] && echo -e "\e[36m$1\e[0m"
if [[ "$DRY" = "false" ]]; then
if [[ "$__resultvar" ]]; then
eval "$__resultvar=\`$1\`"
else
eval $1
fi
fi
}
command_no_dry () {
local __resultvar=$2
[[ "$VERBOSE" = "true" ]] && echo -e "\e[36m$1\e[0m"
if [[ "$__resultvar" ]]; then
eval "$__resultvar=\`$1\`"
else
eval $1
fi
}
if [[ "$#" -ne 1 ]]; then
echo
echo -e "\e[1m\e[31m1 argument required, $# provided\e[0m"
command_help;
fi
command_no_dry "git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'" current_branch
staging_branch=`echo $BRANCH_PREFIX$1`;
echo -e "Current branch is \e[33m$current_branch\e[0m"
[[ "$current_branch" = "$staging_branch" ]] || echo -e "Switching to \e[33m$staging_branch\e[0m" && command "git checkout ${staging_branch} &> /dev/null"
echo -e "Refreshing repository"
command_no_dry "git fetch -p &> /dev/null"
echo -e "Resetting \e[33m$staging_branch\e[39m at latest \e[33m$ORIGIN_OR_OTHER/$staging_branch\e[0m"
command "git reset --hard $ORIGIN_OR_OTHER/$staging_branch &> /dev/null"
errored=()
if [[ "$MERGE" = "$staging_branch" ]]; then
echo -e "\e[36mSkipping same branch ${MERGE}\e[0m"
elif [[ "$MERGE" = "$MASTER_OR_MAIN" ]]; then
echo -e "\e[36mSkipping source branch ${MERGE}\e[0m"
else
echo -e "Merging \e[33m${MERGE}\e[0m"
TMP=$(mktemp)
command "git merge --no-ff ${MERGE} 1>$TMP 2>/dev/null"
if [ $? -ne 0 ]; then
echo -e "\033[0;31mAn error occured during merge of \e[33m${MERGE}\033[0;31m, reverting.\033[0m";
echo -e "\e[37m";
cat $TMP
echo -e "\033[0m";
command "git merge --abort"
errored+=("${MERGE}")
fi
fi
if [ ${#errored[@]} -ne 0 ]; then
echo -e "Script has ended, but got error during merge of \e[33m${errored[*]}\e[0m, try merging it manually and push the branch \e[33m$staging_branch\e[0m"
exit 1
fi
if [[ "$PUSH" = "true" ]]; then
command "git push 2> /dev/null"
fi
echo -e "Switching to \e[33m$current_branch\e[0m"
command "git checkout ${current_branch} &> /dev/null"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment