Skip to content

Instantly share code, notes, and snippets.

@wesleybliss
Created March 25, 2015 18:11
Show Gist options
  • Save wesleybliss/78d4c92a0d067161b63d to your computer and use it in GitHub Desktop.
Save wesleybliss/78d4c92a0d067161b63d to your computer and use it in GitHub Desktop.
Shows Git branches only for the current sprint, if detected, and allows for a grep parameter for filtering results.
#!/bin/bash
#
# A more intelligent branch listing.
#
# [ Sprint Detection ]
# If a current sprint is detected, only corresponding branches
# will be listed. The pattern is "sprint/X/" where X could be any string.
#
# [ Automatic Pattern Matching ]
# If an argument is passed, the final result will be grep'd.
#
echo
# Get the current branch
cb=$(git rev-parse --abbrev-ref HEAD)
if [ -z "$cb" ]; then
echo "It appears you're not in a Git repository."
exit 1
fi
# The trigger string we look to match on
trigger="sprint/"
triggerLen=$((${#trigger}))
# Check if the current branch looks like a sprint
if [[ "$cb" != *"sprint/"* ]]; then
# Do a normal, full branch listing (maybe with grep, if present)
if [ -z "$1" ]; then
git branch
else
git branch | grep "$1"
fi
fi
# Get the index of the trigger
idx=$(expr index "$cb" 1$trigger)
# Find the starting point for substring extraction
offset=$((idx + triggerLen))
# Trim unused prefix of current branch, before sprint trigger
#sprint="${cb:offset}" # Not ZSH compatible
sprint="$(echo $cb | cut -c$offset-)"
# Find the slash that comes after the current sprint name
edx=$(expr index "$sprint" C/)
edx=$((edx - 1))
# Trim the unused suffix of the current branch, after the sprint name
#sprint="${sprint:0:edx}" # Not ZSH compatible
sprint="$(echo $sprint | cut -c1-$edx)"
echo "Listing branches for sprint \"$sprint\":"
echo
if [ -z "$1" ]; then
git branch | grep "$sprint"
else
git branch | grep "$sprint" | grep "$1"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment