Skip to content

Instantly share code, notes, and snippets.

@bitmvr
Created January 3, 2019 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitmvr/35d46c142de37d63e5616730824f97ab to your computer and use it in GitHub Desktop.
Save bitmvr/35d46c142de37d63e5616730824f97ab to your computer and use it in GitHub Desktop.
chipr - The lightweight git branch cleaner upper.
#!/usr/bin/env bash
####################################################################################
# Chipr Globals
####################################################################################
GIT_REMOTE="$(git remote)"
####################################################################################
# Chipr Functions
####################################################################################
chipr::usage(){
echo ""
echo "chipr - The lightweight git branch cleaner upper."
echo ""
echo "Usage: chipr [option]"
echo ""
echo " -i | --in-master List local branches that are in the remote master repository"
echo " -n | --not-in-master List local branches that are not in remote master repository"
echo " -l | --delete-local Delete your local branches that are in the remote master repository"
echo " -r | --delete-remote Delete your remote branches that are in the remote master repository"
echo " -a | --all Delete both the local and remote branhces that are in the remote master repository"
echo " -h | --help Displays this text"
echo ""
}
chipr::getBranches(){
git branch --format='%(refname:short)' | grep -Ev "^master$"
}
chipr::isInMaster(){
local branch="$1"
if chipr::branchContains "${branch}" | grep -Eoq "^${GIT_REMOTE}/master$"; then
return 0
else
return 1
fi
}
chipr::branchContains(){
local branch="$1"
git branch -a --contains "${branch}" --format='%(refname:short)'
}
chipr::isNotInMaster(){
local branch="$1"
if ! chipr::branchContains "${branch}" | grep -Eoq "^${GIT_REMOTE}/master$"; then
return 0
else
return 1
fi
}
chipr::deleteLocal(){
local branch="$1"
git branch --delete --force "$branch"
}
chipr::deleteRemote(){
local branch="$1"
git push "$GIT_REMOTE" --delete "$branch"
}
chipr::freshStart(){
git fetch --quiet --all --prune
}
####################################################################################
# Chipr Logic
####################################################################################
# Validate we were provided at least 1 argument
if [ $# -eq 0 ]; then
chipr::usage
exit 1
fi
# We shall freshen up before we get started
chipr::freshStart
# Doing work
BRANCHES=( $(chipr::getBranches) )
OPTION="$1"
case "$OPTION" in
-i|--in-master )
for branch in ${BRANCHES[@]}; do
if chipr::isInMaster "$branch"; then
echo "IN MASTER: ${branch}"
fi
done
;;
-n|--not-in-master )
for branch in ${BRANCHES[@]}; do
if chipr::isNotInMaster "$branch"; then
echo "NOT IN MASTER: ${branch}"
fi
done
;;
-h|--help)
chipr::usage
;;
*)
echo "Invalid option provided."
echo ""
;;
esac
##########
# NOTES
#########
# git branch -a --contains "refs/remotes/origin/feature/update-version" --format='%(refname:short)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment