#!/usr/bin/env bash
## Delete all local branches that have been merged into **HEAD**.
## Source: git-extra-commands

#
# Quick script to purge all the branches that have been merged to a target
# branch (defaults to master)

if [[ $# != 1 ]]; then
  mergedInto="master"
else
  mergedInto="$1"
fi

currentBranch=$(git symbolic-ref --short HEAD)
if [[ "${currentBranch}" != "${mergedInto}" ]]; then
  echo "You have to be on the same git branch you're using as the merge target"
  exit 1
fi

git branch --merged "${mergedInto}" | grep -v "^[*+]" | xargs -n 1 git branch -d
