Last active
March 21, 2022 09:32
-
-
Save triple0t/cd2853f3d73c2ce3ea05de294ccea619 to your computer and use it in GitHub Desktop.
Create a new git branch name and print it out
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 | |
# Exit if any command fails. | |
set -e | |
# Create a new git branch name and print it out | |
# author: Oluwaseun Olorunsola | |
# url: https://github.com/triple0t | |
# gist: https://gist.github.com/triple0t/cd2853f3d73c2ce3ea05de294ccea619 | |
# version: 1.0.1 | |
# | |
# Installation: | |
# 1. Use `chmod u+x git-branch.sh` to set the file as executable. e.g. `chmod u+x git-branch.sh` | |
# 2. Add the script to your $PATH. One method of doing that: | |
# `cp git-branch.sh` /usr/local/bin/git-branch` e.g. `cp git-branch.sh /usr/local/bin/git-branch` | |
# 3. Restart terminal session e.g source ~ | |
# | |
# Usage: | |
# git-branch 'feat' 'this is my branch name' | |
# args: | |
# 1: branch prefix: this can be any of fix/feat/chore/refactor/test etc | |
# 2: branch name. Note remember to add the quote. e.g 'this is my branch name' | |
# 3: option -s to switch to the new branch or option -c to copy branch name to clipboard | |
# e.g git-branch 'feat' 'this is my branch name' -s | |
# or e.g git-branch 'feat' 'this is my branch name' -c | |
DEFAULT_BRANCH_PREFIX='fix' | |
OPTION_SWITCH_TO_BRANCH='-s' | |
OPTION_COPY_TO_CLIPBOARD='-c' | |
toLowerCase() { | |
local lower_case="$(echo $1 | tr '[A-Z]' '[a-z]')" | |
echo $lower_case | |
} | |
mailpoetCommands() { | |
# check if we are in mailpoet free git repo | |
local OUTPUT=$(git remote get-url origin | grep -E -iwo 'mailpoet\/mailpoet?(\.git)?$') | |
if [[ -n $OUTPUT ]]; then | |
if [[ -d "${PWD}/mailpoet" ]]; then | |
# using new structure | |
cd "${PWD}/mailpoet" | |
./do compile:all | |
cd .. | |
else | |
./do compile:all | |
fi | |
fi | |
} | |
AfterGitCheckoutCustomCommands() { | |
# run the mailpoet repo commands | |
mailpoetCommands | |
} | |
createGitBranch() { | |
echo "Creating $2" | |
local type=$(toLowerCase "$1") | |
local name=$(toLowerCase "$2") | |
local option="$3" | |
local without_special_characters="${name//[\":\'()\{\}%$]/}" # replace special characters with empty | |
local branch_name="$type/${without_special_characters//[ |\/]/-}" # replace empty space or / with - | |
echo $branch_name | |
if [[ $option = $OPTION_COPY_TO_CLIPBOARD ]]; then | |
echo $branch_name | pbcopy | |
echo "Copied to clipboard"; | |
fi | |
if [[ $option = $OPTION_SWITCH_TO_BRANCH ]] && [[ $(git rev-parse --is-inside-work-tree) = true ]]; then | |
git checkout -b $branch_name | |
AfterGitCheckoutCustomCommands | |
fi | |
} | |
if [[ $# -eq 0 ]] || [[ $# -gt 3 ]]; then | |
echo "Error: you need to supply a branch name" | |
echo "e.g. git-branch feat 'my JIRA ticket name' -s (This will create the branch and switch to it)" | |
echo "or" | |
echo "e.g. git-branch feat 'my JIRA ticket name' -c (This will copy the branch name to clipboard)" | |
exit 1 | |
elif [[ $# -eq 3 ]]; then | |
# full number of args supplied | |
createGitBranch "$1" "$2" $3 | |
elif [[ $# -eq 2 ]]; then | |
# a mini check for right params here | |
second=$(toLowerCase "$2") | |
if [[ $second = $OPTION_SWITCH_TO_BRANCH ]]; then | |
createGitBranch $DEFAULT_BRANCH_PREFIX "$1" $OPTION_SWITCH_TO_BRANCH | |
elif [[ $second = $OPTION_COPY_TO_CLIPBOARD ]]; then | |
createGitBranch $DEFAULT_BRANCH_PREFIX "$1" $OPTION_COPY_TO_CLIPBOARD | |
else | |
createGitBranch "$1" "$2" | |
fi | |
else | |
createGitBranch $DEFAULT_BRANCH_PREFIX "$1" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment