Created
January 3, 2019 13:49
-
-
Save bitmvr/35d46c142de37d63e5616730824f97ab to your computer and use it in GitHub Desktop.
chipr - The lightweight git branch cleaner upper.
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
#!/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