Skip to content

Instantly share code, notes, and snippets.

@nvahalik
Created October 3, 2019 21:33
Show Gist options
  • Save nvahalik/bebd60450c42531d0d08b7dbf9a6208e to your computer and use it in GitHub Desktop.
Save nvahalik/bebd60450c42531d0d08b7dbf9a6208e to your computer and use it in GitHub Desktop.
Git branch listing with JIRA integration

Overview

This shell script allows one to list all branches in the current repo and also do a lookup within JIRA to show the results. Example:

Ref      Name                                              Upstream  JIRA Status      Last update
689f9e7  PROJ-776-something-something-reconfigure          origin    Code Review      13 days ago
043ff1e  PROJ-827-some-particular-task                     origin    Failed Testing   13 days ago
3fa55b0  PROJ-849-yet-another-task.                        origin    Code Review      2 hours ago
818602e  dev                                               origin    -                2 hours ago
c0b69b8  master                                            origin    -                5 weeks ago
de7881c  staging                                           origin    -                2 weeks ago
c0b69b8  origin/HEAD                                       n/a       -                5 weeks ago
c9b609f  origin/PROJ-776-something-something-reconfigure   n/a       Code Review      3 hours ago
d1955fd  origin/PROJ-827-some-particular-task              n/a       Failed Testing   13 days ago
60bc724  origin/PROJ-849-yet-another-task                  n/a       Code Review      2 hours ago
818602e  origin/dev                                        n/a       -                2 hours ago
c0b69b8  origin/master                                     n/a       -                5 weeks ago
693efcc  origin/staging                                    n/a       -                5 weeks ago

Installation

Put git-branch-info.sh into your path somewhere.

Install JIRA CLI:

npm install -g jira-cl

You'll need to run the configuration first, of course, and make sure that the jira command is in your path.

Warning

This is really just a hack-job. It works, but I don't keep a lot of branches in my repos. It usually takes 4-5 seconds to run so roughly 1/2 second per ticket.

local_branches=$(git branch --format="%(objectname:short) %(refname:lstrip=2) %(upstream:remotename)")
remote_branches=$(git branch -r --format="%(objectname:short) %(refname:lstrip=2)")
output=''
while read -r local_branch; do
read -r -a array <<< "$local_branch"
last_commit=$(git show -s "${array[0]}" --format="%ar")
ticket=$(echo "${array[1]}" | sed -En 's/[^A-Z]*([a-zA-Z][a-zA-Z]{2,8}-[1-9][0-9]*).*/\1/p')
status=""
if [ "$ticket" != "" ]; then
status=$(jira i "$ticket" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | grep Status | sed -En 's/^.+Status[ \t]+([A-Za-z]*.*)$/\1/p')
else
status="-";
fi;
output="$output\n${array[0]}|${array[1]}|${array[2]}|$status|$last_commit"
done <<< "$local_branches"
while read -r local_branch; do
read -r -a array <<< "$local_branch"
last_commit=$(git show -s "${array[0]}" --format="%ar")
ticket=$(echo "${array[1]}" | sed -En 's/[^A-Z]*([a-zA-Z][a-zA-Z]{2,8}-[1-9][0-9]*).*/\1/p')
status=""
if [ "$ticket" != "" ]; then
status=$(jira i "$ticket" | sed $'s,\x1b\\[[0-9;]*[a-zA-Z],,g' | grep Status | sed -En 's/^.+Status[ \t]+([A-Za-z]*.*)$/\1/p')
else
status="-";
fi;
output="$output\n${array[0]}|${array[1]}|n/a|$status|$last_commit"
done <<< "$remote_branches"
(echo "Ref|Name|Upstream|JIRA Status|Last update"; echo $output ) | column -t -s "|";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment