Skip to content

Instantly share code, notes, and snippets.

@jwu910
Last active October 27, 2017 21:50
Show Gist options
  • Save jwu910/32cbf0e487b43a8fbcedb3d350501b68 to your computer and use it in GitHub Desktop.
Save jwu910/32cbf0e487b43a8fbcedb3d350501b68 to your computer and use it in GitHub Desktop.
Open all files associated with JIRA ticket in current branch
# Takes one optional argument [JIRA TICKET NUM] in the format XXXX-0000. e.g. LOOP-1234.
# Opens all files changed associated with JIRA ticket. If no argument is provided, JIRA ticket is attempted to be taken from branch name.
function openCommitsIn {
local FILE_EDITOR="subl"
local MIN_THRESH=5
filesChanged() {
git log --grep "$1" --stat --name-only | grep -E ".(tag|jsp.?|vm|ftl|js|(s)?css)$" | sort | uniq
}
openFiles() {
local cwd=`pwd`
local repoRoot=$(git rev-parse --show-toplevel 2>/dev/null)
echo "Opening files."
cd "$repoRoot"
filesChanged "$TICKET" | xargs "$FILE_EDITOR"
cd $cwd
return
}
filesNotFound() {
echo "Ticket not found, please use 'openCommitsIn <XXXXX-0000>' format and verify your ticket exists in this branch. e.g. 'openCommitsIn LOOP-1234'"
}
if [ -z "$1" ]; then
local TICKET=$(git rev-parse --abbrev-ref HEAD | grep -Eo '([A-Z]{3,}-)([0-9]+)' -m 1)
echo "No ticket entered, using branch name $TICKET."
else
if [[ $1 =~ ^([A-Z]{3,}-)([0-9]+)$ ]]; then
local TICKET="$1"
echo "Using $TICKET"
else
filesNotFound
return
fi
fi
local FILES=$(filesChanged $TICKET)
local FILES_COUNT=$(filesChanged $TICKET | wc -l)
if [ $FILES_COUNT -eq "0" ]; then
filesNotFound
return
elif [ $FILES_COUNT -ge $MIN_THRESH ]; then
while true; do
echo -e "Open $FILES_COUNT files? (Y/n)"
read yn
case $yn in
[Yy]* )
openFiles; break;;
[Nn]* )
return; break;;
* )
echo "Invalid response, please enter Y or N."; break;;
esac
done
else
openFiles
return
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment